check-integrity.c 100 KB

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