check-integrity.c 96 KB

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