check-integrity.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288
  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_offset(&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) &
  1116. ((unsigned long)PAGE_CACHE_SIZE - 1);
  1117. while (len > 0) {
  1118. cur = min(len, ((size_t)PAGE_CACHE_SIZE - offset_in_page));
  1119. BUG_ON(i >= (block_ctx->len + PAGE_CACHE_SIZE - 1) >>
  1120. PAGE_CACHE_SHIFT);
  1121. kaddr = block_ctx->datav[i];
  1122. memcpy(dst, kaddr + offset_in_page, cur);
  1123. dst += cur;
  1124. len -= cur;
  1125. offset_in_page = 0;
  1126. i++;
  1127. }
  1128. }
  1129. static int btrfsic_create_link_to_next_block(
  1130. struct btrfsic_state *state,
  1131. struct btrfsic_block *block,
  1132. struct btrfsic_block_data_ctx *block_ctx,
  1133. u64 next_bytenr,
  1134. int limit_nesting,
  1135. struct btrfsic_block_data_ctx *next_block_ctx,
  1136. struct btrfsic_block **next_blockp,
  1137. int force_iodone_flag,
  1138. int *num_copiesp, int *mirror_nump,
  1139. struct btrfs_disk_key *disk_key,
  1140. u64 parent_generation)
  1141. {
  1142. struct btrfsic_block *next_block = NULL;
  1143. int ret;
  1144. struct btrfsic_block_link *l;
  1145. int did_alloc_block_link;
  1146. int block_was_created;
  1147. *next_blockp = NULL;
  1148. if (0 == *num_copiesp) {
  1149. *num_copiesp =
  1150. btrfs_num_copies(state->root->fs_info,
  1151. next_bytenr, state->metablock_size);
  1152. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  1153. printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
  1154. next_bytenr, *num_copiesp);
  1155. *mirror_nump = 1;
  1156. }
  1157. if (*mirror_nump > *num_copiesp)
  1158. return 0;
  1159. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1160. printk(KERN_INFO
  1161. "btrfsic_create_link_to_next_block(mirror_num=%d)\n",
  1162. *mirror_nump);
  1163. ret = btrfsic_map_block(state, next_bytenr,
  1164. state->metablock_size,
  1165. next_block_ctx, *mirror_nump);
  1166. if (ret) {
  1167. printk(KERN_INFO
  1168. "btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
  1169. next_bytenr, *mirror_nump);
  1170. btrfsic_release_block_ctx(next_block_ctx);
  1171. *next_blockp = NULL;
  1172. return -1;
  1173. }
  1174. next_block = btrfsic_block_lookup_or_add(state,
  1175. next_block_ctx, "referenced ",
  1176. 1, force_iodone_flag,
  1177. !force_iodone_flag,
  1178. *mirror_nump,
  1179. &block_was_created);
  1180. if (NULL == next_block) {
  1181. btrfsic_release_block_ctx(next_block_ctx);
  1182. *next_blockp = NULL;
  1183. return -1;
  1184. }
  1185. if (block_was_created) {
  1186. l = NULL;
  1187. next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
  1188. } else {
  1189. if (next_block->logical_bytenr != next_bytenr &&
  1190. !(!next_block->is_metadata &&
  1191. 0 == next_block->logical_bytenr)) {
  1192. printk(KERN_INFO
  1193. "Referenced block @%llu (%s/%llu/%d)"
  1194. " found in hash table, %c,"
  1195. " bytenr mismatch (!= stored %llu).\n",
  1196. next_bytenr, next_block_ctx->dev->name,
  1197. next_block_ctx->dev_bytenr, *mirror_nump,
  1198. btrfsic_get_block_type(state, next_block),
  1199. next_block->logical_bytenr);
  1200. } else if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1201. printk(KERN_INFO
  1202. "Referenced block @%llu (%s/%llu/%d)"
  1203. " found in hash table, %c.\n",
  1204. next_bytenr, next_block_ctx->dev->name,
  1205. next_block_ctx->dev_bytenr, *mirror_nump,
  1206. btrfsic_get_block_type(state, next_block));
  1207. next_block->logical_bytenr = next_bytenr;
  1208. next_block->mirror_num = *mirror_nump;
  1209. l = btrfsic_block_link_hashtable_lookup(
  1210. next_block_ctx->dev->bdev,
  1211. next_block_ctx->dev_bytenr,
  1212. block_ctx->dev->bdev,
  1213. block_ctx->dev_bytenr,
  1214. &state->block_link_hashtable);
  1215. }
  1216. next_block->disk_key = *disk_key;
  1217. if (NULL == l) {
  1218. l = btrfsic_block_link_alloc();
  1219. if (NULL == l) {
  1220. printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
  1221. btrfsic_release_block_ctx(next_block_ctx);
  1222. *next_blockp = NULL;
  1223. return -1;
  1224. }
  1225. did_alloc_block_link = 1;
  1226. l->block_ref_to = next_block;
  1227. l->block_ref_from = block;
  1228. l->ref_cnt = 1;
  1229. l->parent_generation = parent_generation;
  1230. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1231. btrfsic_print_add_link(state, l);
  1232. list_add(&l->node_ref_to, &block->ref_to_list);
  1233. list_add(&l->node_ref_from, &next_block->ref_from_list);
  1234. btrfsic_block_link_hashtable_add(l,
  1235. &state->block_link_hashtable);
  1236. } else {
  1237. did_alloc_block_link = 0;
  1238. if (0 == limit_nesting) {
  1239. l->ref_cnt++;
  1240. l->parent_generation = parent_generation;
  1241. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1242. btrfsic_print_add_link(state, l);
  1243. }
  1244. }
  1245. if (limit_nesting > 0 && did_alloc_block_link) {
  1246. ret = btrfsic_read_block(state, next_block_ctx);
  1247. if (ret < (int)next_block_ctx->len) {
  1248. printk(KERN_INFO
  1249. "btrfsic: read block @logical %llu failed!\n",
  1250. next_bytenr);
  1251. btrfsic_release_block_ctx(next_block_ctx);
  1252. *next_blockp = NULL;
  1253. return -1;
  1254. }
  1255. *next_blockp = next_block;
  1256. } else {
  1257. *next_blockp = NULL;
  1258. }
  1259. (*mirror_nump)++;
  1260. return 0;
  1261. }
  1262. static int btrfsic_handle_extent_data(
  1263. struct btrfsic_state *state,
  1264. struct btrfsic_block *block,
  1265. struct btrfsic_block_data_ctx *block_ctx,
  1266. u32 item_offset, int force_iodone_flag)
  1267. {
  1268. int ret;
  1269. struct btrfs_file_extent_item file_extent_item;
  1270. u64 file_extent_item_offset;
  1271. u64 next_bytenr;
  1272. u64 num_bytes;
  1273. u64 generation;
  1274. struct btrfsic_block_link *l;
  1275. file_extent_item_offset = offsetof(struct btrfs_leaf, items) +
  1276. item_offset;
  1277. if (file_extent_item_offset +
  1278. offsetof(struct btrfs_file_extent_item, disk_num_bytes) >
  1279. block_ctx->len) {
  1280. printk(KERN_INFO
  1281. "btrfsic: file item out of bounce at logical %llu, dev %s\n",
  1282. block_ctx->start, block_ctx->dev->name);
  1283. return -1;
  1284. }
  1285. btrfsic_read_from_block_data(block_ctx, &file_extent_item,
  1286. file_extent_item_offset,
  1287. offsetof(struct btrfs_file_extent_item, disk_num_bytes));
  1288. if (BTRFS_FILE_EXTENT_REG != file_extent_item.type ||
  1289. btrfs_stack_file_extent_disk_bytenr(&file_extent_item) == 0) {
  1290. if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
  1291. printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu\n",
  1292. file_extent_item.type,
  1293. btrfs_stack_file_extent_disk_bytenr(
  1294. &file_extent_item));
  1295. return 0;
  1296. }
  1297. if (file_extent_item_offset + sizeof(struct btrfs_file_extent_item) >
  1298. block_ctx->len) {
  1299. printk(KERN_INFO
  1300. "btrfsic: file item out of bounce at logical %llu, dev %s\n",
  1301. block_ctx->start, block_ctx->dev->name);
  1302. return -1;
  1303. }
  1304. btrfsic_read_from_block_data(block_ctx, &file_extent_item,
  1305. file_extent_item_offset,
  1306. sizeof(struct btrfs_file_extent_item));
  1307. next_bytenr = btrfs_stack_file_extent_disk_bytenr(&file_extent_item) +
  1308. btrfs_stack_file_extent_offset(&file_extent_item);
  1309. generation = btrfs_stack_file_extent_generation(&file_extent_item);
  1310. num_bytes = btrfs_stack_file_extent_num_bytes(&file_extent_item);
  1311. generation = btrfs_stack_file_extent_generation(&file_extent_item);
  1312. if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
  1313. printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu,"
  1314. " offset = %llu, num_bytes = %llu\n",
  1315. file_extent_item.type,
  1316. btrfs_stack_file_extent_disk_bytenr(&file_extent_item),
  1317. btrfs_stack_file_extent_offset(&file_extent_item),
  1318. num_bytes);
  1319. while (num_bytes > 0) {
  1320. u32 chunk_len;
  1321. int num_copies;
  1322. int mirror_num;
  1323. if (num_bytes > state->datablock_size)
  1324. chunk_len = state->datablock_size;
  1325. else
  1326. chunk_len = num_bytes;
  1327. num_copies =
  1328. btrfs_num_copies(state->root->fs_info,
  1329. next_bytenr, state->datablock_size);
  1330. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  1331. printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
  1332. next_bytenr, num_copies);
  1333. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  1334. struct btrfsic_block_data_ctx next_block_ctx;
  1335. struct btrfsic_block *next_block;
  1336. int block_was_created;
  1337. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1338. printk(KERN_INFO "btrfsic_handle_extent_data("
  1339. "mirror_num=%d)\n", mirror_num);
  1340. if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
  1341. printk(KERN_INFO
  1342. "\tdisk_bytenr = %llu, num_bytes %u\n",
  1343. next_bytenr, chunk_len);
  1344. ret = btrfsic_map_block(state, next_bytenr,
  1345. chunk_len, &next_block_ctx,
  1346. mirror_num);
  1347. if (ret) {
  1348. printk(KERN_INFO
  1349. "btrfsic: btrfsic_map_block(@%llu,"
  1350. " mirror=%d) failed!\n",
  1351. next_bytenr, mirror_num);
  1352. return -1;
  1353. }
  1354. next_block = btrfsic_block_lookup_or_add(
  1355. state,
  1356. &next_block_ctx,
  1357. "referenced ",
  1358. 0,
  1359. force_iodone_flag,
  1360. !force_iodone_flag,
  1361. mirror_num,
  1362. &block_was_created);
  1363. if (NULL == next_block) {
  1364. printk(KERN_INFO
  1365. "btrfsic: error, kmalloc failed!\n");
  1366. btrfsic_release_block_ctx(&next_block_ctx);
  1367. return -1;
  1368. }
  1369. if (!block_was_created) {
  1370. if (next_block->logical_bytenr != next_bytenr &&
  1371. !(!next_block->is_metadata &&
  1372. 0 == next_block->logical_bytenr)) {
  1373. printk(KERN_INFO
  1374. "Referenced block"
  1375. " @%llu (%s/%llu/%d)"
  1376. " found in hash table, D,"
  1377. " bytenr mismatch"
  1378. " (!= stored %llu).\n",
  1379. next_bytenr,
  1380. next_block_ctx.dev->name,
  1381. next_block_ctx.dev_bytenr,
  1382. mirror_num,
  1383. next_block->logical_bytenr);
  1384. }
  1385. next_block->logical_bytenr = next_bytenr;
  1386. next_block->mirror_num = mirror_num;
  1387. }
  1388. l = btrfsic_block_link_lookup_or_add(state,
  1389. &next_block_ctx,
  1390. next_block, block,
  1391. generation);
  1392. btrfsic_release_block_ctx(&next_block_ctx);
  1393. if (NULL == l)
  1394. return -1;
  1395. }
  1396. next_bytenr += chunk_len;
  1397. num_bytes -= chunk_len;
  1398. }
  1399. return 0;
  1400. }
  1401. static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
  1402. struct btrfsic_block_data_ctx *block_ctx_out,
  1403. int mirror_num)
  1404. {
  1405. int ret;
  1406. u64 length;
  1407. struct btrfs_bio *multi = NULL;
  1408. struct btrfs_device *device;
  1409. length = len;
  1410. ret = btrfs_map_block(state->root->fs_info, READ,
  1411. bytenr, &length, &multi, mirror_num);
  1412. if (ret) {
  1413. block_ctx_out->start = 0;
  1414. block_ctx_out->dev_bytenr = 0;
  1415. block_ctx_out->len = 0;
  1416. block_ctx_out->dev = NULL;
  1417. block_ctx_out->datav = NULL;
  1418. block_ctx_out->pagev = NULL;
  1419. block_ctx_out->mem_to_free = NULL;
  1420. return ret;
  1421. }
  1422. device = multi->stripes[0].dev;
  1423. block_ctx_out->dev = btrfsic_dev_state_lookup(device->bdev);
  1424. block_ctx_out->dev_bytenr = multi->stripes[0].physical;
  1425. block_ctx_out->start = bytenr;
  1426. block_ctx_out->len = len;
  1427. block_ctx_out->datav = NULL;
  1428. block_ctx_out->pagev = NULL;
  1429. block_ctx_out->mem_to_free = NULL;
  1430. kfree(multi);
  1431. if (NULL == block_ctx_out->dev) {
  1432. ret = -ENXIO;
  1433. printk(KERN_INFO "btrfsic: error, cannot lookup dev (#1)!\n");
  1434. }
  1435. return ret;
  1436. }
  1437. static int btrfsic_map_superblock(struct btrfsic_state *state, u64 bytenr,
  1438. u32 len, struct block_device *bdev,
  1439. struct btrfsic_block_data_ctx *block_ctx_out)
  1440. {
  1441. block_ctx_out->dev = btrfsic_dev_state_lookup(bdev);
  1442. block_ctx_out->dev_bytenr = bytenr;
  1443. block_ctx_out->start = bytenr;
  1444. block_ctx_out->len = len;
  1445. block_ctx_out->datav = NULL;
  1446. block_ctx_out->pagev = NULL;
  1447. block_ctx_out->mem_to_free = NULL;
  1448. if (NULL != block_ctx_out->dev) {
  1449. return 0;
  1450. } else {
  1451. printk(KERN_INFO "btrfsic: error, cannot lookup dev (#2)!\n");
  1452. return -ENXIO;
  1453. }
  1454. }
  1455. static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx)
  1456. {
  1457. if (block_ctx->mem_to_free) {
  1458. unsigned int num_pages;
  1459. BUG_ON(!block_ctx->datav);
  1460. BUG_ON(!block_ctx->pagev);
  1461. num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
  1462. PAGE_CACHE_SHIFT;
  1463. while (num_pages > 0) {
  1464. num_pages--;
  1465. if (block_ctx->datav[num_pages]) {
  1466. kunmap(block_ctx->pagev[num_pages]);
  1467. block_ctx->datav[num_pages] = NULL;
  1468. }
  1469. if (block_ctx->pagev[num_pages]) {
  1470. __free_page(block_ctx->pagev[num_pages]);
  1471. block_ctx->pagev[num_pages] = NULL;
  1472. }
  1473. }
  1474. kfree(block_ctx->mem_to_free);
  1475. block_ctx->mem_to_free = NULL;
  1476. block_ctx->pagev = NULL;
  1477. block_ctx->datav = NULL;
  1478. }
  1479. }
  1480. static int btrfsic_read_block(struct btrfsic_state *state,
  1481. struct btrfsic_block_data_ctx *block_ctx)
  1482. {
  1483. unsigned int num_pages;
  1484. unsigned int i;
  1485. u64 dev_bytenr;
  1486. int ret;
  1487. BUG_ON(block_ctx->datav);
  1488. BUG_ON(block_ctx->pagev);
  1489. BUG_ON(block_ctx->mem_to_free);
  1490. if (block_ctx->dev_bytenr & ((u64)PAGE_CACHE_SIZE - 1)) {
  1491. printk(KERN_INFO
  1492. "btrfsic: read_block() with unaligned bytenr %llu\n",
  1493. block_ctx->dev_bytenr);
  1494. return -1;
  1495. }
  1496. num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
  1497. PAGE_CACHE_SHIFT;
  1498. block_ctx->mem_to_free = kzalloc((sizeof(*block_ctx->datav) +
  1499. sizeof(*block_ctx->pagev)) *
  1500. num_pages, GFP_NOFS);
  1501. if (!block_ctx->mem_to_free)
  1502. return -1;
  1503. block_ctx->datav = block_ctx->mem_to_free;
  1504. block_ctx->pagev = (struct page **)(block_ctx->datav + num_pages);
  1505. for (i = 0; i < num_pages; i++) {
  1506. block_ctx->pagev[i] = alloc_page(GFP_NOFS);
  1507. if (!block_ctx->pagev[i])
  1508. return -1;
  1509. }
  1510. dev_bytenr = block_ctx->dev_bytenr;
  1511. for (i = 0; i < num_pages;) {
  1512. struct bio *bio;
  1513. unsigned int j;
  1514. DECLARE_COMPLETION_ONSTACK(complete);
  1515. bio = btrfs_io_bio_alloc(GFP_NOFS, num_pages - i);
  1516. if (!bio) {
  1517. printk(KERN_INFO
  1518. "btrfsic: bio_alloc() for %u pages failed!\n",
  1519. num_pages - i);
  1520. return -1;
  1521. }
  1522. bio->bi_bdev = block_ctx->dev->bdev;
  1523. bio->bi_sector = dev_bytenr >> 9;
  1524. bio->bi_end_io = btrfsic_complete_bio_end_io;
  1525. bio->bi_private = &complete;
  1526. for (j = i; j < num_pages; j++) {
  1527. ret = bio_add_page(bio, block_ctx->pagev[j],
  1528. PAGE_CACHE_SIZE, 0);
  1529. if (PAGE_CACHE_SIZE != ret)
  1530. break;
  1531. }
  1532. if (j == i) {
  1533. printk(KERN_INFO
  1534. "btrfsic: error, failed to add a single page!\n");
  1535. return -1;
  1536. }
  1537. submit_bio(READ, bio);
  1538. /* this will also unplug the queue */
  1539. wait_for_completion(&complete);
  1540. if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  1541. printk(KERN_INFO
  1542. "btrfsic: read error at logical %llu dev %s!\n",
  1543. block_ctx->start, block_ctx->dev->name);
  1544. bio_put(bio);
  1545. return -1;
  1546. }
  1547. bio_put(bio);
  1548. dev_bytenr += (j - i) * PAGE_CACHE_SIZE;
  1549. i = j;
  1550. }
  1551. for (i = 0; i < num_pages; i++) {
  1552. block_ctx->datav[i] = kmap(block_ctx->pagev[i]);
  1553. if (!block_ctx->datav[i]) {
  1554. printk(KERN_INFO "btrfsic: kmap() failed (dev %s)!\n",
  1555. block_ctx->dev->name);
  1556. return -1;
  1557. }
  1558. }
  1559. return block_ctx->len;
  1560. }
  1561. static void btrfsic_complete_bio_end_io(struct bio *bio, int err)
  1562. {
  1563. complete((struct completion *)bio->bi_private);
  1564. }
  1565. static void btrfsic_dump_database(struct btrfsic_state *state)
  1566. {
  1567. struct list_head *elem_all;
  1568. BUG_ON(NULL == state);
  1569. printk(KERN_INFO "all_blocks_list:\n");
  1570. list_for_each(elem_all, &state->all_blocks_list) {
  1571. const struct btrfsic_block *const b_all =
  1572. list_entry(elem_all, struct btrfsic_block,
  1573. all_blocks_node);
  1574. struct list_head *elem_ref_to;
  1575. struct list_head *elem_ref_from;
  1576. printk(KERN_INFO "%c-block @%llu (%s/%llu/%d)\n",
  1577. btrfsic_get_block_type(state, b_all),
  1578. b_all->logical_bytenr, b_all->dev_state->name,
  1579. b_all->dev_bytenr, b_all->mirror_num);
  1580. list_for_each(elem_ref_to, &b_all->ref_to_list) {
  1581. const struct btrfsic_block_link *const l =
  1582. list_entry(elem_ref_to,
  1583. struct btrfsic_block_link,
  1584. node_ref_to);
  1585. printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
  1586. " refers %u* to"
  1587. " %c @%llu (%s/%llu/%d)\n",
  1588. btrfsic_get_block_type(state, b_all),
  1589. b_all->logical_bytenr, b_all->dev_state->name,
  1590. b_all->dev_bytenr, b_all->mirror_num,
  1591. l->ref_cnt,
  1592. btrfsic_get_block_type(state, l->block_ref_to),
  1593. l->block_ref_to->logical_bytenr,
  1594. l->block_ref_to->dev_state->name,
  1595. l->block_ref_to->dev_bytenr,
  1596. l->block_ref_to->mirror_num);
  1597. }
  1598. list_for_each(elem_ref_from, &b_all->ref_from_list) {
  1599. const struct btrfsic_block_link *const l =
  1600. list_entry(elem_ref_from,
  1601. struct btrfsic_block_link,
  1602. node_ref_from);
  1603. printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
  1604. " is ref %u* from"
  1605. " %c @%llu (%s/%llu/%d)\n",
  1606. btrfsic_get_block_type(state, b_all),
  1607. b_all->logical_bytenr, b_all->dev_state->name,
  1608. b_all->dev_bytenr, b_all->mirror_num,
  1609. l->ref_cnt,
  1610. btrfsic_get_block_type(state, l->block_ref_from),
  1611. l->block_ref_from->logical_bytenr,
  1612. l->block_ref_from->dev_state->name,
  1613. l->block_ref_from->dev_bytenr,
  1614. l->block_ref_from->mirror_num);
  1615. }
  1616. printk(KERN_INFO "\n");
  1617. }
  1618. }
  1619. /*
  1620. * Test whether the disk block contains a tree block (leaf or node)
  1621. * (note that this test fails for the super block)
  1622. */
  1623. static int btrfsic_test_for_metadata(struct btrfsic_state *state,
  1624. char **datav, unsigned int num_pages)
  1625. {
  1626. struct btrfs_header *h;
  1627. u8 csum[BTRFS_CSUM_SIZE];
  1628. u32 crc = ~(u32)0;
  1629. unsigned int i;
  1630. if (num_pages * PAGE_CACHE_SIZE < state->metablock_size)
  1631. return 1; /* not metadata */
  1632. num_pages = state->metablock_size >> PAGE_CACHE_SHIFT;
  1633. h = (struct btrfs_header *)datav[0];
  1634. if (memcmp(h->fsid, state->root->fs_info->fsid, BTRFS_UUID_SIZE))
  1635. return 1;
  1636. for (i = 0; i < num_pages; i++) {
  1637. u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE);
  1638. size_t sublen = i ? PAGE_CACHE_SIZE :
  1639. (PAGE_CACHE_SIZE - BTRFS_CSUM_SIZE);
  1640. crc = crc32c(crc, data, sublen);
  1641. }
  1642. btrfs_csum_final(crc, csum);
  1643. if (memcmp(csum, h->csum, state->csum_size))
  1644. return 1;
  1645. return 0; /* is metadata */
  1646. }
  1647. static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
  1648. u64 dev_bytenr, char **mapped_datav,
  1649. unsigned int num_pages,
  1650. struct bio *bio, int *bio_is_patched,
  1651. struct buffer_head *bh,
  1652. int submit_bio_bh_rw)
  1653. {
  1654. int is_metadata;
  1655. struct btrfsic_block *block;
  1656. struct btrfsic_block_data_ctx block_ctx;
  1657. int ret;
  1658. struct btrfsic_state *state = dev_state->state;
  1659. struct block_device *bdev = dev_state->bdev;
  1660. unsigned int processed_len;
  1661. if (NULL != bio_is_patched)
  1662. *bio_is_patched = 0;
  1663. again:
  1664. if (num_pages == 0)
  1665. return;
  1666. processed_len = 0;
  1667. is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
  1668. num_pages));
  1669. block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
  1670. &state->block_hashtable);
  1671. if (NULL != block) {
  1672. u64 bytenr = 0;
  1673. struct list_head *elem_ref_to;
  1674. struct list_head *tmp_ref_to;
  1675. if (block->is_superblock) {
  1676. bytenr = btrfs_super_bytenr((struct btrfs_super_block *)
  1677. mapped_datav[0]);
  1678. if (num_pages * PAGE_CACHE_SIZE <
  1679. BTRFS_SUPER_INFO_SIZE) {
  1680. printk(KERN_INFO
  1681. "btrfsic: cannot work with too short bios!\n");
  1682. return;
  1683. }
  1684. is_metadata = 1;
  1685. BUG_ON(BTRFS_SUPER_INFO_SIZE & (PAGE_CACHE_SIZE - 1));
  1686. processed_len = BTRFS_SUPER_INFO_SIZE;
  1687. if (state->print_mask &
  1688. BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) {
  1689. printk(KERN_INFO
  1690. "[before new superblock is written]:\n");
  1691. btrfsic_dump_tree_sub(state, block, 0);
  1692. }
  1693. }
  1694. if (is_metadata) {
  1695. if (!block->is_superblock) {
  1696. if (num_pages * PAGE_CACHE_SIZE <
  1697. state->metablock_size) {
  1698. printk(KERN_INFO
  1699. "btrfsic: cannot work with too short bios!\n");
  1700. return;
  1701. }
  1702. processed_len = state->metablock_size;
  1703. bytenr = btrfs_stack_header_bytenr(
  1704. (struct btrfs_header *)
  1705. mapped_datav[0]);
  1706. btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
  1707. dev_state,
  1708. dev_bytenr);
  1709. }
  1710. if (block->logical_bytenr != bytenr) {
  1711. printk(KERN_INFO
  1712. "Written block @%llu (%s/%llu/%d)"
  1713. " found in hash table, %c,"
  1714. " bytenr mismatch"
  1715. " (!= stored %llu).\n",
  1716. bytenr, dev_state->name, dev_bytenr,
  1717. block->mirror_num,
  1718. btrfsic_get_block_type(state, block),
  1719. block->logical_bytenr);
  1720. block->logical_bytenr = bytenr;
  1721. } else if (state->print_mask &
  1722. BTRFSIC_PRINT_MASK_VERBOSE)
  1723. printk(KERN_INFO
  1724. "Written block @%llu (%s/%llu/%d)"
  1725. " found in hash table, %c.\n",
  1726. bytenr, dev_state->name, dev_bytenr,
  1727. block->mirror_num,
  1728. btrfsic_get_block_type(state, block));
  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 (-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)) {
  2238. WARN_ON(1);
  2239. btrfsic_dump_tree(state);
  2240. }
  2241. return 0;
  2242. }
  2243. static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
  2244. struct btrfsic_block *const block,
  2245. int recursion_level)
  2246. {
  2247. struct list_head *elem_ref_to;
  2248. int ret = 0;
  2249. if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
  2250. /*
  2251. * Note that this situation can happen and does not
  2252. * indicate an error in regular cases. It happens
  2253. * when disk blocks are freed and later reused.
  2254. * The check-integrity module is not aware of any
  2255. * block free operations, it just recognizes block
  2256. * write operations. Therefore it keeps the linkage
  2257. * information for a block until a block is
  2258. * rewritten. This can temporarily cause incorrect
  2259. * and even circular linkage informations. This
  2260. * causes no harm unless such blocks are referenced
  2261. * by the most recent super block.
  2262. */
  2263. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2264. printk(KERN_INFO
  2265. "btrfsic: abort cyclic linkage (case 1).\n");
  2266. return ret;
  2267. }
  2268. /*
  2269. * This algorithm is recursive because the amount of used stack
  2270. * space is very small and the max recursion depth is limited.
  2271. */
  2272. list_for_each(elem_ref_to, &block->ref_to_list) {
  2273. const struct btrfsic_block_link *const l =
  2274. list_entry(elem_ref_to, struct btrfsic_block_link,
  2275. node_ref_to);
  2276. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2277. printk(KERN_INFO
  2278. "rl=%d, %c @%llu (%s/%llu/%d)"
  2279. " %u* refers to %c @%llu (%s/%llu/%d)\n",
  2280. recursion_level,
  2281. btrfsic_get_block_type(state, block),
  2282. block->logical_bytenr, block->dev_state->name,
  2283. block->dev_bytenr, block->mirror_num,
  2284. l->ref_cnt,
  2285. btrfsic_get_block_type(state, l->block_ref_to),
  2286. l->block_ref_to->logical_bytenr,
  2287. l->block_ref_to->dev_state->name,
  2288. l->block_ref_to->dev_bytenr,
  2289. l->block_ref_to->mirror_num);
  2290. if (l->block_ref_to->never_written) {
  2291. printk(KERN_INFO "btrfs: attempt to write superblock"
  2292. " which references block %c @%llu (%s/%llu/%d)"
  2293. " which is never written!\n",
  2294. btrfsic_get_block_type(state, l->block_ref_to),
  2295. l->block_ref_to->logical_bytenr,
  2296. l->block_ref_to->dev_state->name,
  2297. l->block_ref_to->dev_bytenr,
  2298. l->block_ref_to->mirror_num);
  2299. ret = -1;
  2300. } else if (!l->block_ref_to->is_iodone) {
  2301. printk(KERN_INFO "btrfs: attempt to write superblock"
  2302. " which references block %c @%llu (%s/%llu/%d)"
  2303. " which is not yet iodone!\n",
  2304. btrfsic_get_block_type(state, l->block_ref_to),
  2305. l->block_ref_to->logical_bytenr,
  2306. l->block_ref_to->dev_state->name,
  2307. l->block_ref_to->dev_bytenr,
  2308. l->block_ref_to->mirror_num);
  2309. ret = -1;
  2310. } else if (l->block_ref_to->iodone_w_error) {
  2311. printk(KERN_INFO "btrfs: attempt to write superblock"
  2312. " which references block %c @%llu (%s/%llu/%d)"
  2313. " which has write error!\n",
  2314. btrfsic_get_block_type(state, l->block_ref_to),
  2315. l->block_ref_to->logical_bytenr,
  2316. l->block_ref_to->dev_state->name,
  2317. l->block_ref_to->dev_bytenr,
  2318. l->block_ref_to->mirror_num);
  2319. ret = -1;
  2320. } else if (l->parent_generation !=
  2321. l->block_ref_to->generation &&
  2322. BTRFSIC_GENERATION_UNKNOWN !=
  2323. l->parent_generation &&
  2324. BTRFSIC_GENERATION_UNKNOWN !=
  2325. l->block_ref_to->generation) {
  2326. printk(KERN_INFO "btrfs: attempt to write superblock"
  2327. " which references block %c @%llu (%s/%llu/%d)"
  2328. " with generation %llu !="
  2329. " parent generation %llu!\n",
  2330. btrfsic_get_block_type(state, l->block_ref_to),
  2331. l->block_ref_to->logical_bytenr,
  2332. l->block_ref_to->dev_state->name,
  2333. l->block_ref_to->dev_bytenr,
  2334. l->block_ref_to->mirror_num,
  2335. l->block_ref_to->generation,
  2336. l->parent_generation);
  2337. ret = -1;
  2338. } else if (l->block_ref_to->flush_gen >
  2339. l->block_ref_to->dev_state->last_flush_gen) {
  2340. printk(KERN_INFO "btrfs: attempt to write superblock"
  2341. " which references block %c @%llu (%s/%llu/%d)"
  2342. " which is not flushed out of disk's write cache"
  2343. " (block flush_gen=%llu,"
  2344. " dev->flush_gen=%llu)!\n",
  2345. btrfsic_get_block_type(state, l->block_ref_to),
  2346. l->block_ref_to->logical_bytenr,
  2347. l->block_ref_to->dev_state->name,
  2348. l->block_ref_to->dev_bytenr,
  2349. l->block_ref_to->mirror_num, block->flush_gen,
  2350. l->block_ref_to->dev_state->last_flush_gen);
  2351. ret = -1;
  2352. } else if (-1 == btrfsic_check_all_ref_blocks(state,
  2353. l->block_ref_to,
  2354. recursion_level +
  2355. 1)) {
  2356. ret = -1;
  2357. }
  2358. }
  2359. return ret;
  2360. }
  2361. static int btrfsic_is_block_ref_by_superblock(
  2362. const struct btrfsic_state *state,
  2363. const struct btrfsic_block *block,
  2364. int recursion_level)
  2365. {
  2366. struct list_head *elem_ref_from;
  2367. if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
  2368. /* refer to comment at "abort cyclic linkage (case 1)" */
  2369. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2370. printk(KERN_INFO
  2371. "btrfsic: abort cyclic linkage (case 2).\n");
  2372. return 0;
  2373. }
  2374. /*
  2375. * This algorithm is recursive because the amount of used stack space
  2376. * is very small and the max recursion depth is limited.
  2377. */
  2378. list_for_each(elem_ref_from, &block->ref_from_list) {
  2379. const struct btrfsic_block_link *const l =
  2380. list_entry(elem_ref_from, struct btrfsic_block_link,
  2381. node_ref_from);
  2382. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2383. printk(KERN_INFO
  2384. "rl=%d, %c @%llu (%s/%llu/%d)"
  2385. " is ref %u* from %c @%llu (%s/%llu/%d)\n",
  2386. recursion_level,
  2387. btrfsic_get_block_type(state, block),
  2388. block->logical_bytenr, block->dev_state->name,
  2389. block->dev_bytenr, block->mirror_num,
  2390. l->ref_cnt,
  2391. btrfsic_get_block_type(state, l->block_ref_from),
  2392. l->block_ref_from->logical_bytenr,
  2393. l->block_ref_from->dev_state->name,
  2394. l->block_ref_from->dev_bytenr,
  2395. l->block_ref_from->mirror_num);
  2396. if (l->block_ref_from->is_superblock &&
  2397. state->latest_superblock->dev_bytenr ==
  2398. l->block_ref_from->dev_bytenr &&
  2399. state->latest_superblock->dev_state->bdev ==
  2400. l->block_ref_from->dev_state->bdev)
  2401. return 1;
  2402. else if (btrfsic_is_block_ref_by_superblock(state,
  2403. l->block_ref_from,
  2404. recursion_level +
  2405. 1))
  2406. return 1;
  2407. }
  2408. return 0;
  2409. }
  2410. static void btrfsic_print_add_link(const struct btrfsic_state *state,
  2411. const struct btrfsic_block_link *l)
  2412. {
  2413. printk(KERN_INFO
  2414. "Add %u* link from %c @%llu (%s/%llu/%d)"
  2415. " to %c @%llu (%s/%llu/%d).\n",
  2416. l->ref_cnt,
  2417. btrfsic_get_block_type(state, l->block_ref_from),
  2418. l->block_ref_from->logical_bytenr,
  2419. l->block_ref_from->dev_state->name,
  2420. l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
  2421. btrfsic_get_block_type(state, l->block_ref_to),
  2422. l->block_ref_to->logical_bytenr,
  2423. l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
  2424. l->block_ref_to->mirror_num);
  2425. }
  2426. static void btrfsic_print_rem_link(const struct btrfsic_state *state,
  2427. const struct btrfsic_block_link *l)
  2428. {
  2429. printk(KERN_INFO
  2430. "Rem %u* link from %c @%llu (%s/%llu/%d)"
  2431. " to %c @%llu (%s/%llu/%d).\n",
  2432. l->ref_cnt,
  2433. btrfsic_get_block_type(state, l->block_ref_from),
  2434. l->block_ref_from->logical_bytenr,
  2435. l->block_ref_from->dev_state->name,
  2436. l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
  2437. btrfsic_get_block_type(state, l->block_ref_to),
  2438. l->block_ref_to->logical_bytenr,
  2439. l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
  2440. l->block_ref_to->mirror_num);
  2441. }
  2442. static char btrfsic_get_block_type(const struct btrfsic_state *state,
  2443. const struct btrfsic_block *block)
  2444. {
  2445. if (block->is_superblock &&
  2446. state->latest_superblock->dev_bytenr == block->dev_bytenr &&
  2447. state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
  2448. return 'S';
  2449. else if (block->is_superblock)
  2450. return 's';
  2451. else if (block->is_metadata)
  2452. return 'M';
  2453. else
  2454. return 'D';
  2455. }
  2456. static void btrfsic_dump_tree(const struct btrfsic_state *state)
  2457. {
  2458. btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
  2459. }
  2460. static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
  2461. const struct btrfsic_block *block,
  2462. int indent_level)
  2463. {
  2464. struct list_head *elem_ref_to;
  2465. int indent_add;
  2466. static char buf[80];
  2467. int cursor_position;
  2468. /*
  2469. * Should better fill an on-stack buffer with a complete line and
  2470. * dump it at once when it is time to print a newline character.
  2471. */
  2472. /*
  2473. * This algorithm is recursive because the amount of used stack space
  2474. * is very small and the max recursion depth is limited.
  2475. */
  2476. indent_add = sprintf(buf, "%c-%llu(%s/%llu/%d)",
  2477. btrfsic_get_block_type(state, block),
  2478. block->logical_bytenr, block->dev_state->name,
  2479. block->dev_bytenr, block->mirror_num);
  2480. if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
  2481. printk("[...]\n");
  2482. return;
  2483. }
  2484. printk(buf);
  2485. indent_level += indent_add;
  2486. if (list_empty(&block->ref_to_list)) {
  2487. printk("\n");
  2488. return;
  2489. }
  2490. if (block->mirror_num > 1 &&
  2491. !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
  2492. printk(" [...]\n");
  2493. return;
  2494. }
  2495. cursor_position = indent_level;
  2496. list_for_each(elem_ref_to, &block->ref_to_list) {
  2497. const struct btrfsic_block_link *const l =
  2498. list_entry(elem_ref_to, struct btrfsic_block_link,
  2499. node_ref_to);
  2500. while (cursor_position < indent_level) {
  2501. printk(" ");
  2502. cursor_position++;
  2503. }
  2504. if (l->ref_cnt > 1)
  2505. indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
  2506. else
  2507. indent_add = sprintf(buf, " --> ");
  2508. if (indent_level + indent_add >
  2509. BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
  2510. printk("[...]\n");
  2511. cursor_position = 0;
  2512. continue;
  2513. }
  2514. printk(buf);
  2515. btrfsic_dump_tree_sub(state, l->block_ref_to,
  2516. indent_level + indent_add);
  2517. cursor_position = 0;
  2518. }
  2519. }
  2520. static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
  2521. struct btrfsic_state *state,
  2522. struct btrfsic_block_data_ctx *next_block_ctx,
  2523. struct btrfsic_block *next_block,
  2524. struct btrfsic_block *from_block,
  2525. u64 parent_generation)
  2526. {
  2527. struct btrfsic_block_link *l;
  2528. l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
  2529. next_block_ctx->dev_bytenr,
  2530. from_block->dev_state->bdev,
  2531. from_block->dev_bytenr,
  2532. &state->block_link_hashtable);
  2533. if (NULL == l) {
  2534. l = btrfsic_block_link_alloc();
  2535. if (NULL == l) {
  2536. printk(KERN_INFO
  2537. "btrfsic: error, kmalloc" " failed!\n");
  2538. return NULL;
  2539. }
  2540. l->block_ref_to = next_block;
  2541. l->block_ref_from = from_block;
  2542. l->ref_cnt = 1;
  2543. l->parent_generation = parent_generation;
  2544. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2545. btrfsic_print_add_link(state, l);
  2546. list_add(&l->node_ref_to, &from_block->ref_to_list);
  2547. list_add(&l->node_ref_from, &next_block->ref_from_list);
  2548. btrfsic_block_link_hashtable_add(l,
  2549. &state->block_link_hashtable);
  2550. } else {
  2551. l->ref_cnt++;
  2552. l->parent_generation = parent_generation;
  2553. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2554. btrfsic_print_add_link(state, l);
  2555. }
  2556. return l;
  2557. }
  2558. static struct btrfsic_block *btrfsic_block_lookup_or_add(
  2559. struct btrfsic_state *state,
  2560. struct btrfsic_block_data_ctx *block_ctx,
  2561. const char *additional_string,
  2562. int is_metadata,
  2563. int is_iodone,
  2564. int never_written,
  2565. int mirror_num,
  2566. int *was_created)
  2567. {
  2568. struct btrfsic_block *block;
  2569. block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
  2570. block_ctx->dev_bytenr,
  2571. &state->block_hashtable);
  2572. if (NULL == block) {
  2573. struct btrfsic_dev_state *dev_state;
  2574. block = btrfsic_block_alloc();
  2575. if (NULL == block) {
  2576. printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
  2577. return NULL;
  2578. }
  2579. dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev);
  2580. if (NULL == dev_state) {
  2581. printk(KERN_INFO
  2582. "btrfsic: error, lookup dev_state failed!\n");
  2583. btrfsic_block_free(block);
  2584. return NULL;
  2585. }
  2586. block->dev_state = dev_state;
  2587. block->dev_bytenr = block_ctx->dev_bytenr;
  2588. block->logical_bytenr = block_ctx->start;
  2589. block->is_metadata = is_metadata;
  2590. block->is_iodone = is_iodone;
  2591. block->never_written = never_written;
  2592. block->mirror_num = mirror_num;
  2593. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2594. printk(KERN_INFO
  2595. "New %s%c-block @%llu (%s/%llu/%d)\n",
  2596. additional_string,
  2597. btrfsic_get_block_type(state, block),
  2598. block->logical_bytenr, dev_state->name,
  2599. block->dev_bytenr, mirror_num);
  2600. list_add(&block->all_blocks_node, &state->all_blocks_list);
  2601. btrfsic_block_hashtable_add(block, &state->block_hashtable);
  2602. if (NULL != was_created)
  2603. *was_created = 1;
  2604. } else {
  2605. if (NULL != was_created)
  2606. *was_created = 0;
  2607. }
  2608. return block;
  2609. }
  2610. static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
  2611. u64 bytenr,
  2612. struct btrfsic_dev_state *dev_state,
  2613. u64 dev_bytenr)
  2614. {
  2615. int num_copies;
  2616. int mirror_num;
  2617. int ret;
  2618. struct btrfsic_block_data_ctx block_ctx;
  2619. int match = 0;
  2620. num_copies = btrfs_num_copies(state->root->fs_info,
  2621. bytenr, state->metablock_size);
  2622. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2623. ret = btrfsic_map_block(state, bytenr, state->metablock_size,
  2624. &block_ctx, mirror_num);
  2625. if (ret) {
  2626. printk(KERN_INFO "btrfsic:"
  2627. " btrfsic_map_block(logical @%llu,"
  2628. " mirror %d) failed!\n",
  2629. bytenr, mirror_num);
  2630. continue;
  2631. }
  2632. if (dev_state->bdev == block_ctx.dev->bdev &&
  2633. dev_bytenr == block_ctx.dev_bytenr) {
  2634. match++;
  2635. btrfsic_release_block_ctx(&block_ctx);
  2636. break;
  2637. }
  2638. btrfsic_release_block_ctx(&block_ctx);
  2639. }
  2640. if (!match) {
  2641. printk(KERN_INFO "btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio,"
  2642. " buffer->log_bytenr=%llu, submit_bio(bdev=%s,"
  2643. " phys_bytenr=%llu)!\n",
  2644. bytenr, dev_state->name, dev_bytenr);
  2645. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2646. ret = btrfsic_map_block(state, bytenr,
  2647. state->metablock_size,
  2648. &block_ctx, mirror_num);
  2649. if (ret)
  2650. continue;
  2651. printk(KERN_INFO "Read logical bytenr @%llu maps to"
  2652. " (%s/%llu/%d)\n",
  2653. bytenr, block_ctx.dev->name,
  2654. block_ctx.dev_bytenr, mirror_num);
  2655. }
  2656. WARN_ON(1);
  2657. }
  2658. }
  2659. static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
  2660. struct block_device *bdev)
  2661. {
  2662. struct btrfsic_dev_state *ds;
  2663. ds = btrfsic_dev_state_hashtable_lookup(bdev,
  2664. &btrfsic_dev_state_hashtable);
  2665. return ds;
  2666. }
  2667. int btrfsic_submit_bh(int rw, struct buffer_head *bh)
  2668. {
  2669. struct btrfsic_dev_state *dev_state;
  2670. if (!btrfsic_is_initialized)
  2671. return submit_bh(rw, bh);
  2672. mutex_lock(&btrfsic_mutex);
  2673. /* since btrfsic_submit_bh() might also be called before
  2674. * btrfsic_mount(), this might return NULL */
  2675. dev_state = btrfsic_dev_state_lookup(bh->b_bdev);
  2676. /* Only called to write the superblock (incl. FLUSH/FUA) */
  2677. if (NULL != dev_state &&
  2678. (rw & WRITE) && bh->b_size > 0) {
  2679. u64 dev_bytenr;
  2680. dev_bytenr = 4096 * bh->b_blocknr;
  2681. if (dev_state->state->print_mask &
  2682. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2683. printk(KERN_INFO
  2684. "submit_bh(rw=0x%x, blocknr=%lu (bytenr %llu),"
  2685. " size=%lu, data=%p, bdev=%p)\n",
  2686. rw, (unsigned long)bh->b_blocknr, dev_bytenr,
  2687. (unsigned long)bh->b_size, bh->b_data,
  2688. bh->b_bdev);
  2689. btrfsic_process_written_block(dev_state, dev_bytenr,
  2690. &bh->b_data, 1, NULL,
  2691. NULL, bh, rw);
  2692. } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
  2693. if (dev_state->state->print_mask &
  2694. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2695. printk(KERN_INFO
  2696. "submit_bh(rw=0x%x FLUSH, bdev=%p)\n",
  2697. rw, bh->b_bdev);
  2698. if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
  2699. if ((dev_state->state->print_mask &
  2700. (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2701. BTRFSIC_PRINT_MASK_VERBOSE)))
  2702. printk(KERN_INFO
  2703. "btrfsic_submit_bh(%s) with FLUSH"
  2704. " but dummy block already in use"
  2705. " (ignored)!\n",
  2706. dev_state->name);
  2707. } else {
  2708. struct btrfsic_block *const block =
  2709. &dev_state->dummy_block_for_bio_bh_flush;
  2710. block->is_iodone = 0;
  2711. block->never_written = 0;
  2712. block->iodone_w_error = 0;
  2713. block->flush_gen = dev_state->last_flush_gen + 1;
  2714. block->submit_bio_bh_rw = rw;
  2715. block->orig_bio_bh_private = bh->b_private;
  2716. block->orig_bio_bh_end_io.bh = bh->b_end_io;
  2717. block->next_in_same_bio = NULL;
  2718. bh->b_private = block;
  2719. bh->b_end_io = btrfsic_bh_end_io;
  2720. }
  2721. }
  2722. mutex_unlock(&btrfsic_mutex);
  2723. return submit_bh(rw, bh);
  2724. }
  2725. void btrfsic_submit_bio(int rw, struct bio *bio)
  2726. {
  2727. struct btrfsic_dev_state *dev_state;
  2728. if (!btrfsic_is_initialized) {
  2729. submit_bio(rw, bio);
  2730. return;
  2731. }
  2732. mutex_lock(&btrfsic_mutex);
  2733. /* since btrfsic_submit_bio() is also called before
  2734. * btrfsic_mount(), this might return NULL */
  2735. dev_state = btrfsic_dev_state_lookup(bio->bi_bdev);
  2736. if (NULL != dev_state &&
  2737. (rw & WRITE) && NULL != bio->bi_io_vec) {
  2738. unsigned int i;
  2739. u64 dev_bytenr;
  2740. int bio_is_patched;
  2741. char **mapped_datav;
  2742. dev_bytenr = 512 * bio->bi_sector;
  2743. bio_is_patched = 0;
  2744. if (dev_state->state->print_mask &
  2745. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2746. printk(KERN_INFO
  2747. "submit_bio(rw=0x%x, bi_vcnt=%u,"
  2748. " bi_sector=%lu (bytenr %llu), bi_bdev=%p)\n",
  2749. rw, bio->bi_vcnt, (unsigned long)bio->bi_sector,
  2750. dev_bytenr, bio->bi_bdev);
  2751. mapped_datav = kmalloc(sizeof(*mapped_datav) * bio->bi_vcnt,
  2752. GFP_NOFS);
  2753. if (!mapped_datav)
  2754. goto leave;
  2755. for (i = 0; i < bio->bi_vcnt; i++) {
  2756. BUG_ON(bio->bi_io_vec[i].bv_len != PAGE_CACHE_SIZE);
  2757. mapped_datav[i] = kmap(bio->bi_io_vec[i].bv_page);
  2758. if (!mapped_datav[i]) {
  2759. while (i > 0) {
  2760. i--;
  2761. kunmap(bio->bi_io_vec[i].bv_page);
  2762. }
  2763. kfree(mapped_datav);
  2764. goto leave;
  2765. }
  2766. if ((BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2767. BTRFSIC_PRINT_MASK_VERBOSE) ==
  2768. (dev_state->state->print_mask &
  2769. (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2770. BTRFSIC_PRINT_MASK_VERBOSE)))
  2771. printk(KERN_INFO
  2772. "#%u: page=%p, len=%u, offset=%u\n",
  2773. i, bio->bi_io_vec[i].bv_page,
  2774. bio->bi_io_vec[i].bv_len,
  2775. bio->bi_io_vec[i].bv_offset);
  2776. }
  2777. btrfsic_process_written_block(dev_state, dev_bytenr,
  2778. mapped_datav, bio->bi_vcnt,
  2779. bio, &bio_is_patched,
  2780. NULL, rw);
  2781. while (i > 0) {
  2782. i--;
  2783. kunmap(bio->bi_io_vec[i].bv_page);
  2784. }
  2785. kfree(mapped_datav);
  2786. } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
  2787. if (dev_state->state->print_mask &
  2788. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2789. printk(KERN_INFO
  2790. "submit_bio(rw=0x%x FLUSH, bdev=%p)\n",
  2791. rw, bio->bi_bdev);
  2792. if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
  2793. if ((dev_state->state->print_mask &
  2794. (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2795. BTRFSIC_PRINT_MASK_VERBOSE)))
  2796. printk(KERN_INFO
  2797. "btrfsic_submit_bio(%s) with FLUSH"
  2798. " but dummy block already in use"
  2799. " (ignored)!\n",
  2800. dev_state->name);
  2801. } else {
  2802. struct btrfsic_block *const block =
  2803. &dev_state->dummy_block_for_bio_bh_flush;
  2804. block->is_iodone = 0;
  2805. block->never_written = 0;
  2806. block->iodone_w_error = 0;
  2807. block->flush_gen = dev_state->last_flush_gen + 1;
  2808. block->submit_bio_bh_rw = rw;
  2809. block->orig_bio_bh_private = bio->bi_private;
  2810. block->orig_bio_bh_end_io.bio = bio->bi_end_io;
  2811. block->next_in_same_bio = NULL;
  2812. bio->bi_private = block;
  2813. bio->bi_end_io = btrfsic_bio_end_io;
  2814. }
  2815. }
  2816. leave:
  2817. mutex_unlock(&btrfsic_mutex);
  2818. submit_bio(rw, bio);
  2819. }
  2820. int btrfsic_mount(struct btrfs_root *root,
  2821. struct btrfs_fs_devices *fs_devices,
  2822. int including_extent_data, u32 print_mask)
  2823. {
  2824. int ret;
  2825. struct btrfsic_state *state;
  2826. struct list_head *dev_head = &fs_devices->devices;
  2827. struct btrfs_device *device;
  2828. if (root->nodesize != root->leafsize) {
  2829. printk(KERN_INFO
  2830. "btrfsic: cannot handle nodesize %d != leafsize %d!\n",
  2831. root->nodesize, root->leafsize);
  2832. return -1;
  2833. }
  2834. if (root->nodesize & ((u64)PAGE_CACHE_SIZE - 1)) {
  2835. printk(KERN_INFO
  2836. "btrfsic: cannot handle nodesize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
  2837. root->nodesize, (unsigned long)PAGE_CACHE_SIZE);
  2838. return -1;
  2839. }
  2840. if (root->leafsize & ((u64)PAGE_CACHE_SIZE - 1)) {
  2841. printk(KERN_INFO
  2842. "btrfsic: cannot handle leafsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
  2843. root->leafsize, (unsigned long)PAGE_CACHE_SIZE);
  2844. return -1;
  2845. }
  2846. if (root->sectorsize & ((u64)PAGE_CACHE_SIZE - 1)) {
  2847. printk(KERN_INFO
  2848. "btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
  2849. root->sectorsize, (unsigned long)PAGE_CACHE_SIZE);
  2850. return -1;
  2851. }
  2852. state = kzalloc(sizeof(*state), GFP_NOFS);
  2853. if (NULL == state) {
  2854. printk(KERN_INFO "btrfs check-integrity: kmalloc() failed!\n");
  2855. return -1;
  2856. }
  2857. if (!btrfsic_is_initialized) {
  2858. mutex_init(&btrfsic_mutex);
  2859. btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
  2860. btrfsic_is_initialized = 1;
  2861. }
  2862. mutex_lock(&btrfsic_mutex);
  2863. state->root = root;
  2864. state->print_mask = print_mask;
  2865. state->include_extent_data = including_extent_data;
  2866. state->csum_size = 0;
  2867. state->metablock_size = root->nodesize;
  2868. state->datablock_size = root->sectorsize;
  2869. INIT_LIST_HEAD(&state->all_blocks_list);
  2870. btrfsic_block_hashtable_init(&state->block_hashtable);
  2871. btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
  2872. state->max_superblock_generation = 0;
  2873. state->latest_superblock = NULL;
  2874. list_for_each_entry(device, dev_head, dev_list) {
  2875. struct btrfsic_dev_state *ds;
  2876. char *p;
  2877. if (!device->bdev || !device->name)
  2878. continue;
  2879. ds = btrfsic_dev_state_alloc();
  2880. if (NULL == ds) {
  2881. printk(KERN_INFO
  2882. "btrfs check-integrity: kmalloc() failed!\n");
  2883. mutex_unlock(&btrfsic_mutex);
  2884. return -1;
  2885. }
  2886. ds->bdev = device->bdev;
  2887. ds->state = state;
  2888. bdevname(ds->bdev, ds->name);
  2889. ds->name[BDEVNAME_SIZE - 1] = '\0';
  2890. for (p = ds->name; *p != '\0'; p++);
  2891. while (p > ds->name && *p != '/')
  2892. p--;
  2893. if (*p == '/')
  2894. p++;
  2895. strlcpy(ds->name, p, sizeof(ds->name));
  2896. btrfsic_dev_state_hashtable_add(ds,
  2897. &btrfsic_dev_state_hashtable);
  2898. }
  2899. ret = btrfsic_process_superblock(state, fs_devices);
  2900. if (0 != ret) {
  2901. mutex_unlock(&btrfsic_mutex);
  2902. btrfsic_unmount(root, fs_devices);
  2903. return ret;
  2904. }
  2905. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
  2906. btrfsic_dump_database(state);
  2907. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
  2908. btrfsic_dump_tree(state);
  2909. mutex_unlock(&btrfsic_mutex);
  2910. return 0;
  2911. }
  2912. void btrfsic_unmount(struct btrfs_root *root,
  2913. struct btrfs_fs_devices *fs_devices)
  2914. {
  2915. struct list_head *elem_all;
  2916. struct list_head *tmp_all;
  2917. struct btrfsic_state *state;
  2918. struct list_head *dev_head = &fs_devices->devices;
  2919. struct btrfs_device *device;
  2920. if (!btrfsic_is_initialized)
  2921. return;
  2922. mutex_lock(&btrfsic_mutex);
  2923. state = NULL;
  2924. list_for_each_entry(device, dev_head, dev_list) {
  2925. struct btrfsic_dev_state *ds;
  2926. if (!device->bdev || !device->name)
  2927. continue;
  2928. ds = btrfsic_dev_state_hashtable_lookup(
  2929. device->bdev,
  2930. &btrfsic_dev_state_hashtable);
  2931. if (NULL != ds) {
  2932. state = ds->state;
  2933. btrfsic_dev_state_hashtable_remove(ds);
  2934. btrfsic_dev_state_free(ds);
  2935. }
  2936. }
  2937. if (NULL == state) {
  2938. printk(KERN_INFO
  2939. "btrfsic: error, cannot find state information"
  2940. " on umount!\n");
  2941. mutex_unlock(&btrfsic_mutex);
  2942. return;
  2943. }
  2944. /*
  2945. * Don't care about keeping the lists' state up to date,
  2946. * just free all memory that was allocated dynamically.
  2947. * Free the blocks and the block_links.
  2948. */
  2949. list_for_each_safe(elem_all, tmp_all, &state->all_blocks_list) {
  2950. struct btrfsic_block *const b_all =
  2951. list_entry(elem_all, struct btrfsic_block,
  2952. all_blocks_node);
  2953. struct list_head *elem_ref_to;
  2954. struct list_head *tmp_ref_to;
  2955. list_for_each_safe(elem_ref_to, tmp_ref_to,
  2956. &b_all->ref_to_list) {
  2957. struct btrfsic_block_link *const l =
  2958. list_entry(elem_ref_to,
  2959. struct btrfsic_block_link,
  2960. node_ref_to);
  2961. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2962. btrfsic_print_rem_link(state, l);
  2963. l->ref_cnt--;
  2964. if (0 == l->ref_cnt)
  2965. btrfsic_block_link_free(l);
  2966. }
  2967. if (b_all->is_iodone || b_all->never_written)
  2968. btrfsic_block_free(b_all);
  2969. else
  2970. printk(KERN_INFO "btrfs: attempt to free %c-block"
  2971. " @%llu (%s/%llu/%d) on umount which is"
  2972. " not yet iodone!\n",
  2973. btrfsic_get_block_type(state, b_all),
  2974. b_all->logical_bytenr, b_all->dev_state->name,
  2975. b_all->dev_bytenr, b_all->mirror_num);
  2976. }
  2977. mutex_unlock(&btrfsic_mutex);
  2978. kfree(state);
  2979. }