2 * Copyright (C) STRATO AG 2011. All rights reserved.
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.
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.
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.
20 * This module can be used to catch cases when the btrfs kernel
21 * code executes write requests to the disk that bring the file
22 * system in an inconsistent state. In such a state, a power-loss
23 * or kernel panic event would cause that the data on disk is
24 * lost or at least damaged.
26 * Code is added that examines all block write requests during
27 * runtime (including writes of the super block). Three rules
28 * are verified and an error is printed on violation of the
30 * 1. It is not allowed to write a disk block which is
31 * currently referenced by the super block (either directly
33 * 2. When a super block is written, it is verified that all
34 * referenced (directly or indirectly) blocks fulfill the
35 * following requirements:
36 * 2a. All referenced blocks have either been present when
37 * the file system was mounted, (i.e., they have been
38 * referenced by the super block) or they have been
39 * written since then and the write completion callback
40 * was called and no write error was indicated and a
41 * FLUSH request to the device where these blocks are
42 * located was received and completed.
43 * 2b. All referenced blocks need to have a generation
44 * number which is equal to the parent's number.
46 * One issue that was found using this module was that the log
47 * tree on disk became temporarily corrupted because disk blocks
48 * that had been in use for the log tree had been freed and
49 * reused too early, while being referenced by the written super
52 * The search term in the kernel log that can be used to filter
53 * on the existence of detected integrity issues is
56 * The integrity check is enabled via mount options. These
57 * mount options are only supported if the integrity check
58 * tool is compiled by defining BTRFS_FS_CHECK_INTEGRITY.
60 * Example #1, apply integrity checks to all metadata:
61 * mount /dev/sdb1 /mnt -o check_int
63 * Example #2, apply integrity checks to all metadata and
65 * mount /dev/sdb1 /mnt -o check_int_data
67 * Example #3, apply integrity checks to all metadata and dump
68 * the tree that the super block references to kernel messages
69 * each time after a super block was written:
70 * mount /dev/sdb1 /mnt -o check_int,check_int_print_mask=263
72 * If the integrity check tool is included and activated in
73 * the mount options, plenty of kernel memory is used, and
74 * plenty of additional CPU cycles are spent. Enabling this
75 * functionality is not intended for normal use. In most
76 * cases, unless you are a btrfs developer who needs to verify
77 * the integrity of (super)-block write requests, do not
78 * enable the config option BTRFS_FS_CHECK_INTEGRITY to
79 * include and compile the integrity check tool.
81 * Expect millions of lines of information in the kernel log with an
82 * enabled check_int_print_mask. Therefore set LOG_BUF_SHIFT in the
83 * kernel config to at least 26 (which is 64MB). Usually the value is
84 * limited to 21 (which is 2MB) in init/Kconfig. The file needs to be
85 * changed like this before LOG_BUF_SHIFT can be set to a high value:
86 * config LOG_BUF_SHIFT
87 * int "Kernel log buffer size (16 => 64KB, 17 => 128KB)"
91 #include <linux/sched.h>
92 #include <linux/slab.h>
93 #include <linux/buffer_head.h>
94 #include <linux/mutex.h>
95 #include <linux/genhd.h>
96 #include <linux/blkdev.h>
100 #include "transaction.h"
101 #include "extent_io.h"
103 #include "print-tree.h"
105 #include "check-integrity.h"
106 #include "rcu-string.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)
120 * The definition of the bitmask fields for the print_mask.
121 * They are specified with the mount option check_integrity_print_mask.
123 #define BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE 0x00000001
124 #define BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION 0x00000002
125 #define BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE 0x00000004
126 #define BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE 0x00000008
127 #define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH 0x00000010
128 #define BTRFSIC_PRINT_MASK_END_IO_BIO_BH 0x00000020
129 #define BTRFSIC_PRINT_MASK_VERBOSE 0x00000040
130 #define BTRFSIC_PRINT_MASK_VERY_VERBOSE 0x00000080
131 #define BTRFSIC_PRINT_MASK_INITIAL_TREE 0x00000100
132 #define BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES 0x00000200
133 #define BTRFSIC_PRINT_MASK_INITIAL_DATABASE 0x00000400
134 #define BTRFSIC_PRINT_MASK_NUM_COPIES 0x00000800
135 #define BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS 0x00001000
136 #define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE 0x00002000
138 struct btrfsic_dev_state;
139 struct btrfsic_state;
141 struct btrfsic_block {
142 u32 magic_num; /* only used for debug purposes */
143 unsigned int is_metadata:1; /* if it is meta-data, not data-data */
144 unsigned int is_superblock:1; /* if it is one of the superblocks */
145 unsigned int is_iodone:1; /* if is done by lower subsystem */
146 unsigned int iodone_w_error:1; /* error was indicated to endio */
147 unsigned int never_written:1; /* block was added because it was
148 * referenced, not because it was
150 unsigned int mirror_num; /* large enough to hold
151 * BTRFS_SUPER_MIRROR_MAX */
152 struct btrfsic_dev_state *dev_state;
153 u64 dev_bytenr; /* key, physical byte num on disk */
154 u64 logical_bytenr; /* logical byte num on disk */
156 struct btrfs_disk_key disk_key; /* extra info to print in case of
157 * issues, will not always be correct */
158 struct list_head collision_resolving_node; /* list node */
159 struct list_head all_blocks_node; /* list node */
161 /* the following two lists contain block_link items */
162 struct list_head ref_to_list; /* list */
163 struct list_head ref_from_list; /* list */
164 struct btrfsic_block *next_in_same_bio;
165 void *orig_bio_bh_private;
169 } orig_bio_bh_end_io;
170 int submit_bio_bh_rw;
171 u64 flush_gen; /* only valid if !never_written */
175 * Elements of this type are allocated dynamically and required because
176 * each block object can refer to and can be ref from multiple blocks.
177 * The key to lookup them in the hashtable is the dev_bytenr of
178 * the block ref to plus the one from the block refered from.
179 * The fact that they are searchable via a hashtable and that a
180 * ref_cnt is maintained is not required for the btrfs integrity
181 * check algorithm itself, it is only used to make the output more
182 * beautiful in case that an error is detected (an error is defined
183 * as a write operation to a block while that block is still referenced).
185 struct btrfsic_block_link {
186 u32 magic_num; /* only used for debug purposes */
188 struct list_head node_ref_to; /* list node */
189 struct list_head node_ref_from; /* list node */
190 struct list_head collision_resolving_node; /* list node */
191 struct btrfsic_block *block_ref_to;
192 struct btrfsic_block *block_ref_from;
193 u64 parent_generation;
196 struct btrfsic_dev_state {
197 u32 magic_num; /* only used for debug purposes */
198 struct block_device *bdev;
199 struct btrfsic_state *state;
200 struct list_head collision_resolving_node; /* list node */
201 struct btrfsic_block dummy_block_for_bio_bh_flush;
203 char name[BDEVNAME_SIZE];
206 struct btrfsic_block_hashtable {
207 struct list_head table[BTRFSIC_BLOCK_HASHTABLE_SIZE];
210 struct btrfsic_block_link_hashtable {
211 struct list_head table[BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE];
214 struct btrfsic_dev_state_hashtable {
215 struct list_head table[BTRFSIC_DEV2STATE_HASHTABLE_SIZE];
218 struct btrfsic_block_data_ctx {
219 u64 start; /* virtual bytenr */
220 u64 dev_bytenr; /* physical bytenr on device */
222 struct btrfsic_dev_state *dev;
228 /* This structure is used to implement recursion without occupying
229 * any stack space, refer to btrfsic_process_metablock() */
230 struct btrfsic_stack_frame {
238 struct btrfsic_block *block;
239 struct btrfsic_block_data_ctx *block_ctx;
240 struct btrfsic_block *next_block;
241 struct btrfsic_block_data_ctx next_block_ctx;
242 struct btrfs_header *hdr;
243 struct btrfsic_stack_frame *prev;
246 /* Some state per mounted filesystem */
247 struct btrfsic_state {
249 int include_extent_data;
251 struct list_head all_blocks_list;
252 struct btrfsic_block_hashtable block_hashtable;
253 struct btrfsic_block_link_hashtable block_link_hashtable;
254 struct btrfs_root *root;
255 u64 max_superblock_generation;
256 struct btrfsic_block *latest_superblock;
261 static void btrfsic_block_init(struct btrfsic_block *b);
262 static struct btrfsic_block *btrfsic_block_alloc(void);
263 static void btrfsic_block_free(struct btrfsic_block *b);
264 static void btrfsic_block_link_init(struct btrfsic_block_link *n);
265 static struct btrfsic_block_link *btrfsic_block_link_alloc(void);
266 static void btrfsic_block_link_free(struct btrfsic_block_link *n);
267 static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds);
268 static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void);
269 static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds);
270 static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h);
271 static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
272 struct btrfsic_block_hashtable *h);
273 static void btrfsic_block_hashtable_remove(struct btrfsic_block *b);
274 static struct btrfsic_block *btrfsic_block_hashtable_lookup(
275 struct block_device *bdev,
277 struct btrfsic_block_hashtable *h);
278 static void btrfsic_block_link_hashtable_init(
279 struct btrfsic_block_link_hashtable *h);
280 static void btrfsic_block_link_hashtable_add(
281 struct btrfsic_block_link *l,
282 struct btrfsic_block_link_hashtable *h);
283 static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l);
284 static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
285 struct block_device *bdev_ref_to,
286 u64 dev_bytenr_ref_to,
287 struct block_device *bdev_ref_from,
288 u64 dev_bytenr_ref_from,
289 struct btrfsic_block_link_hashtable *h);
290 static void btrfsic_dev_state_hashtable_init(
291 struct btrfsic_dev_state_hashtable *h);
292 static void btrfsic_dev_state_hashtable_add(
293 struct btrfsic_dev_state *ds,
294 struct btrfsic_dev_state_hashtable *h);
295 static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds);
296 static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(
297 struct block_device *bdev,
298 struct btrfsic_dev_state_hashtable *h);
299 static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void);
300 static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf);
301 static int btrfsic_process_superblock(struct btrfsic_state *state,
302 struct btrfs_fs_devices *fs_devices);
303 static int btrfsic_process_metablock(struct btrfsic_state *state,
304 struct btrfsic_block *block,
305 struct btrfsic_block_data_ctx *block_ctx,
306 int limit_nesting, int force_iodone_flag);
307 static void btrfsic_read_from_block_data(
308 struct btrfsic_block_data_ctx *block_ctx,
309 void *dst, u32 offset, size_t len);
310 static int btrfsic_create_link_to_next_block(
311 struct btrfsic_state *state,
312 struct btrfsic_block *block,
313 struct btrfsic_block_data_ctx
314 *block_ctx, u64 next_bytenr,
316 struct btrfsic_block_data_ctx *next_block_ctx,
317 struct btrfsic_block **next_blockp,
318 int force_iodone_flag,
319 int *num_copiesp, int *mirror_nump,
320 struct btrfs_disk_key *disk_key,
321 u64 parent_generation);
322 static int btrfsic_handle_extent_data(struct btrfsic_state *state,
323 struct btrfsic_block *block,
324 struct btrfsic_block_data_ctx *block_ctx,
325 u32 item_offset, int force_iodone_flag);
326 static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
327 struct btrfsic_block_data_ctx *block_ctx_out,
329 static int btrfsic_map_superblock(struct btrfsic_state *state, u64 bytenr,
330 u32 len, struct block_device *bdev,
331 struct btrfsic_block_data_ctx *block_ctx_out);
332 static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx);
333 static int btrfsic_read_block(struct btrfsic_state *state,
334 struct btrfsic_block_data_ctx *block_ctx);
335 static void btrfsic_dump_database(struct btrfsic_state *state);
336 static int btrfsic_test_for_metadata(struct btrfsic_state *state,
337 char **datav, unsigned int num_pages);
338 static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
339 u64 dev_bytenr, char **mapped_datav,
340 unsigned int num_pages,
341 struct bio *bio, int *bio_is_patched,
342 struct buffer_head *bh,
343 int submit_bio_bh_rw);
344 static int btrfsic_process_written_superblock(
345 struct btrfsic_state *state,
346 struct btrfsic_block *const block,
347 struct btrfs_super_block *const super_hdr);
348 static void btrfsic_bio_end_io(struct bio *bp, int bio_error_status);
349 static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate);
350 static int btrfsic_is_block_ref_by_superblock(const struct btrfsic_state *state,
351 const struct btrfsic_block *block,
352 int recursion_level);
353 static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
354 struct btrfsic_block *const block,
355 int recursion_level);
356 static void btrfsic_print_add_link(const struct btrfsic_state *state,
357 const struct btrfsic_block_link *l);
358 static void btrfsic_print_rem_link(const struct btrfsic_state *state,
359 const struct btrfsic_block_link *l);
360 static char btrfsic_get_block_type(const struct btrfsic_state *state,
361 const struct btrfsic_block *block);
362 static void btrfsic_dump_tree(const struct btrfsic_state *state);
363 static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
364 const struct btrfsic_block *block,
366 static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
367 struct btrfsic_state *state,
368 struct btrfsic_block_data_ctx *next_block_ctx,
369 struct btrfsic_block *next_block,
370 struct btrfsic_block *from_block,
371 u64 parent_generation);
372 static struct btrfsic_block *btrfsic_block_lookup_or_add(
373 struct btrfsic_state *state,
374 struct btrfsic_block_data_ctx *block_ctx,
375 const char *additional_string,
381 static int btrfsic_process_superblock_dev_mirror(
382 struct btrfsic_state *state,
383 struct btrfsic_dev_state *dev_state,
384 struct btrfs_device *device,
385 int superblock_mirror_num,
386 struct btrfsic_dev_state **selected_dev_state,
387 struct btrfs_super_block *selected_super);
388 static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
389 struct block_device *bdev);
390 static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
392 struct btrfsic_dev_state *dev_state,
395 static struct mutex btrfsic_mutex;
396 static int btrfsic_is_initialized;
397 static struct btrfsic_dev_state_hashtable btrfsic_dev_state_hashtable;
400 static void btrfsic_block_init(struct btrfsic_block *b)
402 b->magic_num = BTRFSIC_BLOCK_MAGIC_NUMBER;
405 b->logical_bytenr = 0;
406 b->generation = BTRFSIC_GENERATION_UNKNOWN;
407 b->disk_key.objectid = 0;
408 b->disk_key.type = 0;
409 b->disk_key.offset = 0;
411 b->is_superblock = 0;
413 b->iodone_w_error = 0;
414 b->never_written = 0;
416 b->next_in_same_bio = NULL;
417 b->orig_bio_bh_private = NULL;
418 b->orig_bio_bh_end_io.bio = NULL;
419 INIT_LIST_HEAD(&b->collision_resolving_node);
420 INIT_LIST_HEAD(&b->all_blocks_node);
421 INIT_LIST_HEAD(&b->ref_to_list);
422 INIT_LIST_HEAD(&b->ref_from_list);
423 b->submit_bio_bh_rw = 0;
427 static struct btrfsic_block *btrfsic_block_alloc(void)
429 struct btrfsic_block *b;
431 b = kzalloc(sizeof(*b), GFP_NOFS);
433 btrfsic_block_init(b);
438 static void btrfsic_block_free(struct btrfsic_block *b)
440 BUG_ON(!(NULL == b || BTRFSIC_BLOCK_MAGIC_NUMBER == b->magic_num));
444 static void btrfsic_block_link_init(struct btrfsic_block_link *l)
446 l->magic_num = BTRFSIC_BLOCK_LINK_MAGIC_NUMBER;
448 INIT_LIST_HEAD(&l->node_ref_to);
449 INIT_LIST_HEAD(&l->node_ref_from);
450 INIT_LIST_HEAD(&l->collision_resolving_node);
451 l->block_ref_to = NULL;
452 l->block_ref_from = NULL;
455 static struct btrfsic_block_link *btrfsic_block_link_alloc(void)
457 struct btrfsic_block_link *l;
459 l = kzalloc(sizeof(*l), GFP_NOFS);
461 btrfsic_block_link_init(l);
466 static void btrfsic_block_link_free(struct btrfsic_block_link *l)
468 BUG_ON(!(NULL == l || BTRFSIC_BLOCK_LINK_MAGIC_NUMBER == l->magic_num));
472 static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds)
474 ds->magic_num = BTRFSIC_DEV2STATE_MAGIC_NUMBER;
478 INIT_LIST_HEAD(&ds->collision_resolving_node);
479 ds->last_flush_gen = 0;
480 btrfsic_block_init(&ds->dummy_block_for_bio_bh_flush);
481 ds->dummy_block_for_bio_bh_flush.is_iodone = 1;
482 ds->dummy_block_for_bio_bh_flush.dev_state = ds;
485 static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void)
487 struct btrfsic_dev_state *ds;
489 ds = kzalloc(sizeof(*ds), GFP_NOFS);
491 btrfsic_dev_state_init(ds);
496 static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds)
498 BUG_ON(!(NULL == ds ||
499 BTRFSIC_DEV2STATE_MAGIC_NUMBER == ds->magic_num));
503 static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h)
507 for (i = 0; i < BTRFSIC_BLOCK_HASHTABLE_SIZE; i++)
508 INIT_LIST_HEAD(h->table + i);
511 static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
512 struct btrfsic_block_hashtable *h)
514 const unsigned int hashval =
515 (((unsigned int)(b->dev_bytenr >> 16)) ^
516 ((unsigned int)((uintptr_t)b->dev_state->bdev))) &
517 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
519 list_add(&b->collision_resolving_node, h->table + hashval);
522 static void btrfsic_block_hashtable_remove(struct btrfsic_block *b)
524 list_del(&b->collision_resolving_node);
527 static struct btrfsic_block *btrfsic_block_hashtable_lookup(
528 struct block_device *bdev,
530 struct btrfsic_block_hashtable *h)
532 const unsigned int hashval =
533 (((unsigned int)(dev_bytenr >> 16)) ^
534 ((unsigned int)((uintptr_t)bdev))) &
535 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
536 struct list_head *elem;
538 list_for_each(elem, h->table + hashval) {
539 struct btrfsic_block *const b =
540 list_entry(elem, struct btrfsic_block,
541 collision_resolving_node);
543 if (b->dev_state->bdev == bdev && b->dev_bytenr == dev_bytenr)
550 static void btrfsic_block_link_hashtable_init(
551 struct btrfsic_block_link_hashtable *h)
555 for (i = 0; i < BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE; i++)
556 INIT_LIST_HEAD(h->table + i);
559 static void btrfsic_block_link_hashtable_add(
560 struct btrfsic_block_link *l,
561 struct btrfsic_block_link_hashtable *h)
563 const unsigned int hashval =
564 (((unsigned int)(l->block_ref_to->dev_bytenr >> 16)) ^
565 ((unsigned int)(l->block_ref_from->dev_bytenr >> 16)) ^
566 ((unsigned int)((uintptr_t)l->block_ref_to->dev_state->bdev)) ^
567 ((unsigned int)((uintptr_t)l->block_ref_from->dev_state->bdev)))
568 & (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
570 BUG_ON(NULL == l->block_ref_to);
571 BUG_ON(NULL == l->block_ref_from);
572 list_add(&l->collision_resolving_node, h->table + hashval);
575 static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l)
577 list_del(&l->collision_resolving_node);
580 static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
581 struct block_device *bdev_ref_to,
582 u64 dev_bytenr_ref_to,
583 struct block_device *bdev_ref_from,
584 u64 dev_bytenr_ref_from,
585 struct btrfsic_block_link_hashtable *h)
587 const unsigned int hashval =
588 (((unsigned int)(dev_bytenr_ref_to >> 16)) ^
589 ((unsigned int)(dev_bytenr_ref_from >> 16)) ^
590 ((unsigned int)((uintptr_t)bdev_ref_to)) ^
591 ((unsigned int)((uintptr_t)bdev_ref_from))) &
592 (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
593 struct list_head *elem;
595 list_for_each(elem, h->table + hashval) {
596 struct btrfsic_block_link *const l =
597 list_entry(elem, struct btrfsic_block_link,
598 collision_resolving_node);
600 BUG_ON(NULL == l->block_ref_to);
601 BUG_ON(NULL == l->block_ref_from);
602 if (l->block_ref_to->dev_state->bdev == bdev_ref_to &&
603 l->block_ref_to->dev_bytenr == dev_bytenr_ref_to &&
604 l->block_ref_from->dev_state->bdev == bdev_ref_from &&
605 l->block_ref_from->dev_bytenr == dev_bytenr_ref_from)
612 static void btrfsic_dev_state_hashtable_init(
613 struct btrfsic_dev_state_hashtable *h)
617 for (i = 0; i < BTRFSIC_DEV2STATE_HASHTABLE_SIZE; i++)
618 INIT_LIST_HEAD(h->table + i);
621 static void btrfsic_dev_state_hashtable_add(
622 struct btrfsic_dev_state *ds,
623 struct btrfsic_dev_state_hashtable *h)
625 const unsigned int hashval =
626 (((unsigned int)((uintptr_t)ds->bdev)) &
627 (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
629 list_add(&ds->collision_resolving_node, h->table + hashval);
632 static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds)
634 list_del(&ds->collision_resolving_node);
637 static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(
638 struct block_device *bdev,
639 struct btrfsic_dev_state_hashtable *h)
641 const unsigned int hashval =
642 (((unsigned int)((uintptr_t)bdev)) &
643 (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
644 struct list_head *elem;
646 list_for_each(elem, h->table + hashval) {
647 struct btrfsic_dev_state *const ds =
648 list_entry(elem, struct btrfsic_dev_state,
649 collision_resolving_node);
651 if (ds->bdev == bdev)
658 static int btrfsic_process_superblock(struct btrfsic_state *state,
659 struct btrfs_fs_devices *fs_devices)
662 struct btrfs_super_block *selected_super;
663 struct list_head *dev_head = &fs_devices->devices;
664 struct btrfs_device *device;
665 struct btrfsic_dev_state *selected_dev_state = NULL;
668 BUG_ON(NULL == state);
669 selected_super = kzalloc(sizeof(*selected_super), GFP_NOFS);
670 if (NULL == selected_super) {
671 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
675 list_for_each_entry(device, dev_head, dev_list) {
677 struct btrfsic_dev_state *dev_state;
679 if (!device->bdev || !device->name)
682 dev_state = btrfsic_dev_state_lookup(device->bdev);
683 BUG_ON(NULL == dev_state);
684 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
685 ret = btrfsic_process_superblock_dev_mirror(
686 state, dev_state, device, i,
687 &selected_dev_state, selected_super);
688 if (0 != ret && 0 == i) {
689 kfree(selected_super);
695 if (NULL == state->latest_superblock) {
696 printk(KERN_INFO "btrfsic: no superblock found!\n");
697 kfree(selected_super);
701 state->csum_size = btrfs_super_csum_size(selected_super);
703 for (pass = 0; pass < 3; pass++) {
710 next_bytenr = btrfs_super_root(selected_super);
711 if (state->print_mask &
712 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
713 printk(KERN_INFO "root@%llu\n", next_bytenr);
716 next_bytenr = btrfs_super_chunk_root(selected_super);
717 if (state->print_mask &
718 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
719 printk(KERN_INFO "chunk@%llu\n", next_bytenr);
722 next_bytenr = btrfs_super_log_root(selected_super);
723 if (0 == next_bytenr)
725 if (state->print_mask &
726 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
727 printk(KERN_INFO "log@%llu\n", next_bytenr);
732 btrfs_num_copies(state->root->fs_info,
733 next_bytenr, state->metablock_size);
734 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
735 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
736 next_bytenr, num_copies);
738 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
739 struct btrfsic_block *next_block;
740 struct btrfsic_block_data_ctx tmp_next_block_ctx;
741 struct btrfsic_block_link *l;
743 ret = btrfsic_map_block(state, next_bytenr,
744 state->metablock_size,
748 printk(KERN_INFO "btrfsic:"
749 " btrfsic_map_block(root @%llu,"
750 " mirror %d) failed!\n",
751 next_bytenr, mirror_num);
752 kfree(selected_super);
756 next_block = btrfsic_block_hashtable_lookup(
757 tmp_next_block_ctx.dev->bdev,
758 tmp_next_block_ctx.dev_bytenr,
759 &state->block_hashtable);
760 BUG_ON(NULL == next_block);
762 l = btrfsic_block_link_hashtable_lookup(
763 tmp_next_block_ctx.dev->bdev,
764 tmp_next_block_ctx.dev_bytenr,
765 state->latest_superblock->dev_state->
767 state->latest_superblock->dev_bytenr,
768 &state->block_link_hashtable);
771 ret = btrfsic_read_block(state, &tmp_next_block_ctx);
772 if (ret < (int)PAGE_CACHE_SIZE) {
774 "btrfsic: read @logical %llu failed!\n",
775 tmp_next_block_ctx.start);
776 btrfsic_release_block_ctx(&tmp_next_block_ctx);
777 kfree(selected_super);
781 ret = btrfsic_process_metablock(state,
784 BTRFS_MAX_LEVEL + 3, 1);
785 btrfsic_release_block_ctx(&tmp_next_block_ctx);
789 kfree(selected_super);
793 static int btrfsic_process_superblock_dev_mirror(
794 struct btrfsic_state *state,
795 struct btrfsic_dev_state *dev_state,
796 struct btrfs_device *device,
797 int superblock_mirror_num,
798 struct btrfsic_dev_state **selected_dev_state,
799 struct btrfs_super_block *selected_super)
801 struct btrfs_super_block *super_tmp;
803 struct buffer_head *bh;
804 struct btrfsic_block *superblock_tmp;
806 struct block_device *const superblock_bdev = device->bdev;
808 /* super block bytenr is always the unmapped device bytenr */
809 dev_bytenr = btrfs_sb_offset(superblock_mirror_num);
810 if (dev_bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
812 bh = __bread(superblock_bdev, dev_bytenr / 4096,
813 BTRFS_SUPER_INFO_SIZE);
816 super_tmp = (struct btrfs_super_block *)
817 (bh->b_data + (dev_bytenr & 4095));
819 if (btrfs_super_bytenr(super_tmp) != dev_bytenr ||
820 btrfs_super_magic(super_tmp) != BTRFS_MAGIC ||
821 memcmp(device->uuid, super_tmp->dev_item.uuid, BTRFS_UUID_SIZE) ||
822 btrfs_super_nodesize(super_tmp) != state->metablock_size ||
823 btrfs_super_sectorsize(super_tmp) != state->datablock_size) {
829 btrfsic_block_hashtable_lookup(superblock_bdev,
831 &state->block_hashtable);
832 if (NULL == superblock_tmp) {
833 superblock_tmp = btrfsic_block_alloc();
834 if (NULL == superblock_tmp) {
835 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
839 /* for superblock, only the dev_bytenr makes sense */
840 superblock_tmp->dev_bytenr = dev_bytenr;
841 superblock_tmp->dev_state = dev_state;
842 superblock_tmp->logical_bytenr = dev_bytenr;
843 superblock_tmp->generation = btrfs_super_generation(super_tmp);
844 superblock_tmp->is_metadata = 1;
845 superblock_tmp->is_superblock = 1;
846 superblock_tmp->is_iodone = 1;
847 superblock_tmp->never_written = 0;
848 superblock_tmp->mirror_num = 1 + superblock_mirror_num;
849 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
850 printk_in_rcu(KERN_INFO "New initial S-block (bdev %p, %s)"
851 " @%llu (%s/%llu/%d)\n",
853 rcu_str_deref(device->name), dev_bytenr,
854 dev_state->name, dev_bytenr,
855 superblock_mirror_num);
856 list_add(&superblock_tmp->all_blocks_node,
857 &state->all_blocks_list);
858 btrfsic_block_hashtable_add(superblock_tmp,
859 &state->block_hashtable);
862 /* select the one with the highest generation field */
863 if (btrfs_super_generation(super_tmp) >
864 state->max_superblock_generation ||
865 0 == state->max_superblock_generation) {
866 memcpy(selected_super, super_tmp, sizeof(*selected_super));
867 *selected_dev_state = dev_state;
868 state->max_superblock_generation =
869 btrfs_super_generation(super_tmp);
870 state->latest_superblock = superblock_tmp;
873 for (pass = 0; pass < 3; pass++) {
877 const char *additional_string = NULL;
878 struct btrfs_disk_key tmp_disk_key;
880 tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
881 tmp_disk_key.offset = 0;
884 btrfs_set_disk_key_objectid(&tmp_disk_key,
885 BTRFS_ROOT_TREE_OBJECTID);
886 additional_string = "initial root ";
887 next_bytenr = btrfs_super_root(super_tmp);
890 btrfs_set_disk_key_objectid(&tmp_disk_key,
891 BTRFS_CHUNK_TREE_OBJECTID);
892 additional_string = "initial chunk ";
893 next_bytenr = btrfs_super_chunk_root(super_tmp);
896 btrfs_set_disk_key_objectid(&tmp_disk_key,
897 BTRFS_TREE_LOG_OBJECTID);
898 additional_string = "initial log ";
899 next_bytenr = btrfs_super_log_root(super_tmp);
900 if (0 == next_bytenr)
906 btrfs_num_copies(state->root->fs_info,
907 next_bytenr, state->metablock_size);
908 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
909 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
910 next_bytenr, num_copies);
911 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
912 struct btrfsic_block *next_block;
913 struct btrfsic_block_data_ctx tmp_next_block_ctx;
914 struct btrfsic_block_link *l;
916 if (btrfsic_map_block(state, next_bytenr,
917 state->metablock_size,
920 printk(KERN_INFO "btrfsic: btrfsic_map_block("
921 "bytenr @%llu, mirror %d) failed!\n",
922 next_bytenr, mirror_num);
927 next_block = btrfsic_block_lookup_or_add(
928 state, &tmp_next_block_ctx,
929 additional_string, 1, 1, 0,
931 if (NULL == next_block) {
932 btrfsic_release_block_ctx(&tmp_next_block_ctx);
937 next_block->disk_key = tmp_disk_key;
938 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
939 l = btrfsic_block_link_lookup_or_add(
940 state, &tmp_next_block_ctx,
941 next_block, superblock_tmp,
942 BTRFSIC_GENERATION_UNKNOWN);
943 btrfsic_release_block_ctx(&tmp_next_block_ctx);
950 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES)
951 btrfsic_dump_tree_sub(state, superblock_tmp, 0);
957 static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void)
959 struct btrfsic_stack_frame *sf;
961 sf = kzalloc(sizeof(*sf), GFP_NOFS);
963 printk(KERN_INFO "btrfsic: alloc memory failed!\n");
965 sf->magic = BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER;
969 static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf)
971 BUG_ON(!(NULL == sf ||
972 BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER == sf->magic));
976 static int btrfsic_process_metablock(
977 struct btrfsic_state *state,
978 struct btrfsic_block *const first_block,
979 struct btrfsic_block_data_ctx *const first_block_ctx,
980 int first_limit_nesting, int force_iodone_flag)
982 struct btrfsic_stack_frame initial_stack_frame = { 0 };
983 struct btrfsic_stack_frame *sf;
984 struct btrfsic_stack_frame *next_stack;
985 struct btrfs_header *const first_hdr =
986 (struct btrfs_header *)first_block_ctx->datav[0];
989 sf = &initial_stack_frame;
992 sf->limit_nesting = first_limit_nesting;
993 sf->block = first_block;
994 sf->block_ctx = first_block_ctx;
995 sf->next_block = NULL;
999 continue_with_new_stack_frame:
1000 sf->block->generation = le64_to_cpu(sf->hdr->generation);
1001 if (0 == sf->hdr->level) {
1002 struct btrfs_leaf *const leafhdr =
1003 (struct btrfs_leaf *)sf->hdr;
1006 sf->nr = btrfs_stack_header_nritems(&leafhdr->header);
1008 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1010 "leaf %llu items %d generation %llu"
1012 sf->block_ctx->start, sf->nr,
1013 btrfs_stack_header_generation(
1015 btrfs_stack_header_owner(
1019 continue_with_current_leaf_stack_frame:
1020 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1025 if (sf->i < sf->nr) {
1026 struct btrfs_item disk_item;
1027 u32 disk_item_offset =
1028 (uintptr_t)(leafhdr->items + sf->i) -
1030 struct btrfs_disk_key *disk_key;
1035 if (disk_item_offset + sizeof(struct btrfs_item) >
1036 sf->block_ctx->len) {
1037 leaf_item_out_of_bounce_error:
1039 "btrfsic: leaf item out of bounce at logical %llu, dev %s\n",
1040 sf->block_ctx->start,
1041 sf->block_ctx->dev->name);
1042 goto one_stack_frame_backwards;
1044 btrfsic_read_from_block_data(sf->block_ctx,
1047 sizeof(struct btrfs_item));
1048 item_offset = btrfs_stack_item_offset(&disk_item);
1049 item_size = btrfs_stack_item_size(&disk_item);
1050 disk_key = &disk_item.key;
1051 type = btrfs_disk_key_type(disk_key);
1053 if (BTRFS_ROOT_ITEM_KEY == type) {
1054 struct btrfs_root_item root_item;
1055 u32 root_item_offset;
1058 root_item_offset = item_offset +
1059 offsetof(struct btrfs_leaf, items);
1060 if (root_item_offset + item_size >
1062 goto leaf_item_out_of_bounce_error;
1063 btrfsic_read_from_block_data(
1064 sf->block_ctx, &root_item,
1067 next_bytenr = btrfs_root_bytenr(&root_item);
1070 btrfsic_create_link_to_next_block(
1076 &sf->next_block_ctx,
1082 btrfs_root_generation(
1085 goto one_stack_frame_backwards;
1087 if (NULL != sf->next_block) {
1088 struct btrfs_header *const next_hdr =
1089 (struct btrfs_header *)
1090 sf->next_block_ctx.datav[0];
1093 btrfsic_stack_frame_alloc();
1094 if (NULL == next_stack) {
1096 btrfsic_release_block_ctx(
1099 goto one_stack_frame_backwards;
1103 next_stack->block = sf->next_block;
1104 next_stack->block_ctx =
1105 &sf->next_block_ctx;
1106 next_stack->next_block = NULL;
1107 next_stack->hdr = next_hdr;
1108 next_stack->limit_nesting =
1109 sf->limit_nesting - 1;
1110 next_stack->prev = sf;
1112 goto continue_with_new_stack_frame;
1114 } else if (BTRFS_EXTENT_DATA_KEY == type &&
1115 state->include_extent_data) {
1116 sf->error = btrfsic_handle_extent_data(
1123 goto one_stack_frame_backwards;
1126 goto continue_with_current_leaf_stack_frame;
1129 struct btrfs_node *const nodehdr = (struct btrfs_node *)sf->hdr;
1132 sf->nr = btrfs_stack_header_nritems(&nodehdr->header);
1134 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1135 printk(KERN_INFO "node %llu level %d items %d"
1136 " generation %llu owner %llu\n",
1137 sf->block_ctx->start,
1138 nodehdr->header.level, sf->nr,
1139 btrfs_stack_header_generation(
1141 btrfs_stack_header_owner(
1145 continue_with_current_node_stack_frame:
1146 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1151 if (sf->i < sf->nr) {
1152 struct btrfs_key_ptr key_ptr;
1156 key_ptr_offset = (uintptr_t)(nodehdr->ptrs + sf->i) -
1158 if (key_ptr_offset + sizeof(struct btrfs_key_ptr) >
1159 sf->block_ctx->len) {
1161 "btrfsic: node item out of bounce at logical %llu, dev %s\n",
1162 sf->block_ctx->start,
1163 sf->block_ctx->dev->name);
1164 goto one_stack_frame_backwards;
1166 btrfsic_read_from_block_data(
1167 sf->block_ctx, &key_ptr, key_ptr_offset,
1168 sizeof(struct btrfs_key_ptr));
1169 next_bytenr = btrfs_stack_key_blockptr(&key_ptr);
1171 sf->error = btrfsic_create_link_to_next_block(
1177 &sf->next_block_ctx,
1183 btrfs_stack_key_generation(&key_ptr));
1185 goto one_stack_frame_backwards;
1187 if (NULL != sf->next_block) {
1188 struct btrfs_header *const next_hdr =
1189 (struct btrfs_header *)
1190 sf->next_block_ctx.datav[0];
1192 next_stack = btrfsic_stack_frame_alloc();
1193 if (NULL == next_stack) {
1195 goto one_stack_frame_backwards;
1199 next_stack->block = sf->next_block;
1200 next_stack->block_ctx = &sf->next_block_ctx;
1201 next_stack->next_block = NULL;
1202 next_stack->hdr = next_hdr;
1203 next_stack->limit_nesting =
1204 sf->limit_nesting - 1;
1205 next_stack->prev = sf;
1207 goto continue_with_new_stack_frame;
1210 goto continue_with_current_node_stack_frame;
1214 one_stack_frame_backwards:
1215 if (NULL != sf->prev) {
1216 struct btrfsic_stack_frame *const prev = sf->prev;
1218 /* the one for the initial block is freed in the caller */
1219 btrfsic_release_block_ctx(sf->block_ctx);
1222 prev->error = sf->error;
1223 btrfsic_stack_frame_free(sf);
1225 goto one_stack_frame_backwards;
1228 btrfsic_stack_frame_free(sf);
1230 goto continue_with_new_stack_frame;
1232 BUG_ON(&initial_stack_frame != sf);
1238 static void btrfsic_read_from_block_data(
1239 struct btrfsic_block_data_ctx *block_ctx,
1240 void *dstv, u32 offset, size_t len)
1243 size_t offset_in_page;
1245 char *dst = (char *)dstv;
1246 size_t start_offset = block_ctx->start & ((u64)PAGE_CACHE_SIZE - 1);
1247 unsigned long i = (start_offset + offset) >> PAGE_CACHE_SHIFT;
1249 WARN_ON(offset + len > block_ctx->len);
1250 offset_in_page = (start_offset + offset) & (PAGE_CACHE_SIZE - 1);
1253 cur = min(len, ((size_t)PAGE_CACHE_SIZE - offset_in_page));
1254 BUG_ON(i >= (block_ctx->len + PAGE_CACHE_SIZE - 1) >>
1256 kaddr = block_ctx->datav[i];
1257 memcpy(dst, kaddr + offset_in_page, cur);
1266 static int btrfsic_create_link_to_next_block(
1267 struct btrfsic_state *state,
1268 struct btrfsic_block *block,
1269 struct btrfsic_block_data_ctx *block_ctx,
1272 struct btrfsic_block_data_ctx *next_block_ctx,
1273 struct btrfsic_block **next_blockp,
1274 int force_iodone_flag,
1275 int *num_copiesp, int *mirror_nump,
1276 struct btrfs_disk_key *disk_key,
1277 u64 parent_generation)
1279 struct btrfsic_block *next_block = NULL;
1281 struct btrfsic_block_link *l;
1282 int did_alloc_block_link;
1283 int block_was_created;
1285 *next_blockp = NULL;
1286 if (0 == *num_copiesp) {
1288 btrfs_num_copies(state->root->fs_info,
1289 next_bytenr, state->metablock_size);
1290 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1291 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
1292 next_bytenr, *num_copiesp);
1296 if (*mirror_nump > *num_copiesp)
1299 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1301 "btrfsic_create_link_to_next_block(mirror_num=%d)\n",
1303 ret = btrfsic_map_block(state, next_bytenr,
1304 state->metablock_size,
1305 next_block_ctx, *mirror_nump);
1308 "btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
1309 next_bytenr, *mirror_nump);
1310 btrfsic_release_block_ctx(next_block_ctx);
1311 *next_blockp = NULL;
1315 next_block = btrfsic_block_lookup_or_add(state,
1316 next_block_ctx, "referenced ",
1317 1, force_iodone_flag,
1320 &block_was_created);
1321 if (NULL == next_block) {
1322 btrfsic_release_block_ctx(next_block_ctx);
1323 *next_blockp = NULL;
1326 if (block_was_created) {
1328 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
1330 if (next_block->logical_bytenr != next_bytenr &&
1331 !(!next_block->is_metadata &&
1332 0 == next_block->logical_bytenr)) {
1334 "Referenced block @%llu (%s/%llu/%d)"
1335 " found in hash table, %c,"
1336 " bytenr mismatch (!= stored %llu).\n",
1337 next_bytenr, next_block_ctx->dev->name,
1338 next_block_ctx->dev_bytenr, *mirror_nump,
1339 btrfsic_get_block_type(state, next_block),
1340 next_block->logical_bytenr);
1341 } else if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1343 "Referenced block @%llu (%s/%llu/%d)"
1344 " found in hash table, %c.\n",
1345 next_bytenr, next_block_ctx->dev->name,
1346 next_block_ctx->dev_bytenr, *mirror_nump,
1347 btrfsic_get_block_type(state, next_block));
1348 next_block->logical_bytenr = next_bytenr;
1350 next_block->mirror_num = *mirror_nump;
1351 l = btrfsic_block_link_hashtable_lookup(
1352 next_block_ctx->dev->bdev,
1353 next_block_ctx->dev_bytenr,
1354 block_ctx->dev->bdev,
1355 block_ctx->dev_bytenr,
1356 &state->block_link_hashtable);
1359 next_block->disk_key = *disk_key;
1361 l = btrfsic_block_link_alloc();
1363 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
1364 btrfsic_release_block_ctx(next_block_ctx);
1365 *next_blockp = NULL;
1369 did_alloc_block_link = 1;
1370 l->block_ref_to = next_block;
1371 l->block_ref_from = block;
1373 l->parent_generation = parent_generation;
1375 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1376 btrfsic_print_add_link(state, l);
1378 list_add(&l->node_ref_to, &block->ref_to_list);
1379 list_add(&l->node_ref_from, &next_block->ref_from_list);
1381 btrfsic_block_link_hashtable_add(l,
1382 &state->block_link_hashtable);
1384 did_alloc_block_link = 0;
1385 if (0 == limit_nesting) {
1387 l->parent_generation = parent_generation;
1388 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1389 btrfsic_print_add_link(state, l);
1393 if (limit_nesting > 0 && did_alloc_block_link) {
1394 ret = btrfsic_read_block(state, next_block_ctx);
1395 if (ret < (int)next_block_ctx->len) {
1397 "btrfsic: read block @logical %llu failed!\n",
1399 btrfsic_release_block_ctx(next_block_ctx);
1400 *next_blockp = NULL;
1404 *next_blockp = next_block;
1406 *next_blockp = NULL;
1413 static int btrfsic_handle_extent_data(
1414 struct btrfsic_state *state,
1415 struct btrfsic_block *block,
1416 struct btrfsic_block_data_ctx *block_ctx,
1417 u32 item_offset, int force_iodone_flag)
1420 struct btrfs_file_extent_item file_extent_item;
1421 u64 file_extent_item_offset;
1425 struct btrfsic_block_link *l;
1427 file_extent_item_offset = offsetof(struct btrfs_leaf, items) +
1429 if (file_extent_item_offset +
1430 offsetof(struct btrfs_file_extent_item, disk_num_bytes) >
1433 "btrfsic: file item out of bounce at logical %llu, dev %s\n",
1434 block_ctx->start, block_ctx->dev->name);
1438 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1439 file_extent_item_offset,
1440 offsetof(struct btrfs_file_extent_item, disk_num_bytes));
1441 if (BTRFS_FILE_EXTENT_REG != file_extent_item.type ||
1442 btrfs_stack_file_extent_disk_bytenr(&file_extent_item) == 0) {
1443 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1444 printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu\n",
1445 file_extent_item.type,
1446 btrfs_stack_file_extent_disk_bytenr(
1447 &file_extent_item));
1451 if (file_extent_item_offset + sizeof(struct btrfs_file_extent_item) >
1454 "btrfsic: file item out of bounce at logical %llu, dev %s\n",
1455 block_ctx->start, block_ctx->dev->name);
1458 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1459 file_extent_item_offset,
1460 sizeof(struct btrfs_file_extent_item));
1461 next_bytenr = btrfs_stack_file_extent_disk_bytenr(&file_extent_item);
1462 if (btrfs_stack_file_extent_compression(&file_extent_item) ==
1463 BTRFS_COMPRESS_NONE) {
1464 next_bytenr += btrfs_stack_file_extent_offset(&file_extent_item);
1465 num_bytes = btrfs_stack_file_extent_num_bytes(&file_extent_item);
1467 num_bytes = btrfs_stack_file_extent_disk_num_bytes(&file_extent_item);
1469 generation = btrfs_stack_file_extent_generation(&file_extent_item);
1471 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1472 printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu,"
1473 " offset = %llu, num_bytes = %llu\n",
1474 file_extent_item.type,
1475 btrfs_stack_file_extent_disk_bytenr(&file_extent_item),
1476 btrfs_stack_file_extent_offset(&file_extent_item),
1478 while (num_bytes > 0) {
1483 if (num_bytes > state->datablock_size)
1484 chunk_len = state->datablock_size;
1486 chunk_len = num_bytes;
1489 btrfs_num_copies(state->root->fs_info,
1490 next_bytenr, state->datablock_size);
1491 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1492 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
1493 next_bytenr, num_copies);
1494 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
1495 struct btrfsic_block_data_ctx next_block_ctx;
1496 struct btrfsic_block *next_block;
1497 int block_was_created;
1499 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1500 printk(KERN_INFO "btrfsic_handle_extent_data("
1501 "mirror_num=%d)\n", mirror_num);
1502 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1504 "\tdisk_bytenr = %llu, num_bytes %u\n",
1505 next_bytenr, chunk_len);
1506 ret = btrfsic_map_block(state, next_bytenr,
1507 chunk_len, &next_block_ctx,
1511 "btrfsic: btrfsic_map_block(@%llu,"
1512 " mirror=%d) failed!\n",
1513 next_bytenr, mirror_num);
1517 next_block = btrfsic_block_lookup_or_add(
1525 &block_was_created);
1526 if (NULL == next_block) {
1528 "btrfsic: error, kmalloc failed!\n");
1529 btrfsic_release_block_ctx(&next_block_ctx);
1532 if (!block_was_created) {
1533 if (next_block->logical_bytenr != next_bytenr &&
1534 !(!next_block->is_metadata &&
1535 0 == next_block->logical_bytenr)) {
1538 " @%llu (%s/%llu/%d)"
1539 " found in hash table, D,"
1541 " (!= stored %llu).\n",
1543 next_block_ctx.dev->name,
1544 next_block_ctx.dev_bytenr,
1546 next_block->logical_bytenr);
1548 next_block->logical_bytenr = next_bytenr;
1549 next_block->mirror_num = mirror_num;
1552 l = btrfsic_block_link_lookup_or_add(state,
1556 btrfsic_release_block_ctx(&next_block_ctx);
1561 next_bytenr += chunk_len;
1562 num_bytes -= chunk_len;
1568 static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
1569 struct btrfsic_block_data_ctx *block_ctx_out,
1574 struct btrfs_bio *multi = NULL;
1575 struct btrfs_device *device;
1578 ret = btrfs_map_block(state->root->fs_info, READ,
1579 bytenr, &length, &multi, mirror_num);
1582 block_ctx_out->start = 0;
1583 block_ctx_out->dev_bytenr = 0;
1584 block_ctx_out->len = 0;
1585 block_ctx_out->dev = NULL;
1586 block_ctx_out->datav = NULL;
1587 block_ctx_out->pagev = NULL;
1588 block_ctx_out->mem_to_free = NULL;
1593 device = multi->stripes[0].dev;
1594 block_ctx_out->dev = btrfsic_dev_state_lookup(device->bdev);
1595 block_ctx_out->dev_bytenr = multi->stripes[0].physical;
1596 block_ctx_out->start = bytenr;
1597 block_ctx_out->len = len;
1598 block_ctx_out->datav = NULL;
1599 block_ctx_out->pagev = NULL;
1600 block_ctx_out->mem_to_free = NULL;
1603 if (NULL == block_ctx_out->dev) {
1605 printk(KERN_INFO "btrfsic: error, cannot lookup dev (#1)!\n");
1611 static int btrfsic_map_superblock(struct btrfsic_state *state, u64 bytenr,
1612 u32 len, struct block_device *bdev,
1613 struct btrfsic_block_data_ctx *block_ctx_out)
1615 block_ctx_out->dev = btrfsic_dev_state_lookup(bdev);
1616 block_ctx_out->dev_bytenr = bytenr;
1617 block_ctx_out->start = bytenr;
1618 block_ctx_out->len = len;
1619 block_ctx_out->datav = NULL;
1620 block_ctx_out->pagev = NULL;
1621 block_ctx_out->mem_to_free = NULL;
1622 if (NULL != block_ctx_out->dev) {
1625 printk(KERN_INFO "btrfsic: error, cannot lookup dev (#2)!\n");
1630 static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx)
1632 if (block_ctx->mem_to_free) {
1633 unsigned int num_pages;
1635 BUG_ON(!block_ctx->datav);
1636 BUG_ON(!block_ctx->pagev);
1637 num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
1639 while (num_pages > 0) {
1641 if (block_ctx->datav[num_pages]) {
1642 kunmap(block_ctx->pagev[num_pages]);
1643 block_ctx->datav[num_pages] = NULL;
1645 if (block_ctx->pagev[num_pages]) {
1646 __free_page(block_ctx->pagev[num_pages]);
1647 block_ctx->pagev[num_pages] = NULL;
1651 kfree(block_ctx->mem_to_free);
1652 block_ctx->mem_to_free = NULL;
1653 block_ctx->pagev = NULL;
1654 block_ctx->datav = NULL;
1658 static int btrfsic_read_block(struct btrfsic_state *state,
1659 struct btrfsic_block_data_ctx *block_ctx)
1661 unsigned int num_pages;
1666 BUG_ON(block_ctx->datav);
1667 BUG_ON(block_ctx->pagev);
1668 BUG_ON(block_ctx->mem_to_free);
1669 if (block_ctx->dev_bytenr & ((u64)PAGE_CACHE_SIZE - 1)) {
1671 "btrfsic: read_block() with unaligned bytenr %llu\n",
1672 block_ctx->dev_bytenr);
1676 num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
1678 block_ctx->mem_to_free = kzalloc((sizeof(*block_ctx->datav) +
1679 sizeof(*block_ctx->pagev)) *
1680 num_pages, GFP_NOFS);
1681 if (!block_ctx->mem_to_free)
1683 block_ctx->datav = block_ctx->mem_to_free;
1684 block_ctx->pagev = (struct page **)(block_ctx->datav + num_pages);
1685 for (i = 0; i < num_pages; i++) {
1686 block_ctx->pagev[i] = alloc_page(GFP_NOFS);
1687 if (!block_ctx->pagev[i])
1691 dev_bytenr = block_ctx->dev_bytenr;
1692 for (i = 0; i < num_pages;) {
1696 bio = btrfs_io_bio_alloc(GFP_NOFS, num_pages - i);
1699 "btrfsic: bio_alloc() for %u pages failed!\n",
1703 bio->bi_bdev = block_ctx->dev->bdev;
1704 bio->bi_iter.bi_sector = dev_bytenr >> 9;
1706 for (j = i; j < num_pages; j++) {
1707 ret = bio_add_page(bio, block_ctx->pagev[j],
1708 PAGE_CACHE_SIZE, 0);
1709 if (PAGE_CACHE_SIZE != ret)
1714 "btrfsic: error, failed to add a single page!\n");
1717 if (submit_bio_wait(READ, bio)) {
1719 "btrfsic: read error at logical %llu dev %s!\n",
1720 block_ctx->start, block_ctx->dev->name);
1725 dev_bytenr += (j - i) * PAGE_CACHE_SIZE;
1728 for (i = 0; i < num_pages; i++) {
1729 block_ctx->datav[i] = kmap(block_ctx->pagev[i]);
1730 if (!block_ctx->datav[i]) {
1731 printk(KERN_INFO "btrfsic: kmap() failed (dev %s)!\n",
1732 block_ctx->dev->name);
1737 return block_ctx->len;
1740 static void btrfsic_dump_database(struct btrfsic_state *state)
1742 struct list_head *elem_all;
1744 BUG_ON(NULL == state);
1746 printk(KERN_INFO "all_blocks_list:\n");
1747 list_for_each(elem_all, &state->all_blocks_list) {
1748 const struct btrfsic_block *const b_all =
1749 list_entry(elem_all, struct btrfsic_block,
1751 struct list_head *elem_ref_to;
1752 struct list_head *elem_ref_from;
1754 printk(KERN_INFO "%c-block @%llu (%s/%llu/%d)\n",
1755 btrfsic_get_block_type(state, b_all),
1756 b_all->logical_bytenr, b_all->dev_state->name,
1757 b_all->dev_bytenr, b_all->mirror_num);
1759 list_for_each(elem_ref_to, &b_all->ref_to_list) {
1760 const struct btrfsic_block_link *const l =
1761 list_entry(elem_ref_to,
1762 struct btrfsic_block_link,
1765 printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
1767 " %c @%llu (%s/%llu/%d)\n",
1768 btrfsic_get_block_type(state, b_all),
1769 b_all->logical_bytenr, b_all->dev_state->name,
1770 b_all->dev_bytenr, b_all->mirror_num,
1772 btrfsic_get_block_type(state, l->block_ref_to),
1773 l->block_ref_to->logical_bytenr,
1774 l->block_ref_to->dev_state->name,
1775 l->block_ref_to->dev_bytenr,
1776 l->block_ref_to->mirror_num);
1779 list_for_each(elem_ref_from, &b_all->ref_from_list) {
1780 const struct btrfsic_block_link *const l =
1781 list_entry(elem_ref_from,
1782 struct btrfsic_block_link,
1785 printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
1787 " %c @%llu (%s/%llu/%d)\n",
1788 btrfsic_get_block_type(state, b_all),
1789 b_all->logical_bytenr, b_all->dev_state->name,
1790 b_all->dev_bytenr, b_all->mirror_num,
1792 btrfsic_get_block_type(state, l->block_ref_from),
1793 l->block_ref_from->logical_bytenr,
1794 l->block_ref_from->dev_state->name,
1795 l->block_ref_from->dev_bytenr,
1796 l->block_ref_from->mirror_num);
1799 printk(KERN_INFO "\n");
1804 * Test whether the disk block contains a tree block (leaf or node)
1805 * (note that this test fails for the super block)
1807 static int btrfsic_test_for_metadata(struct btrfsic_state *state,
1808 char **datav, unsigned int num_pages)
1810 struct btrfs_header *h;
1811 u8 csum[BTRFS_CSUM_SIZE];
1815 if (num_pages * PAGE_CACHE_SIZE < state->metablock_size)
1816 return 1; /* not metadata */
1817 num_pages = state->metablock_size >> PAGE_CACHE_SHIFT;
1818 h = (struct btrfs_header *)datav[0];
1820 if (memcmp(h->fsid, state->root->fs_info->fsid, BTRFS_UUID_SIZE))
1823 for (i = 0; i < num_pages; i++) {
1824 u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE);
1825 size_t sublen = i ? PAGE_CACHE_SIZE :
1826 (PAGE_CACHE_SIZE - BTRFS_CSUM_SIZE);
1828 crc = btrfs_crc32c(crc, data, sublen);
1830 btrfs_csum_final(crc, csum);
1831 if (memcmp(csum, h->csum, state->csum_size))
1834 return 0; /* is metadata */
1837 static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
1838 u64 dev_bytenr, char **mapped_datav,
1839 unsigned int num_pages,
1840 struct bio *bio, int *bio_is_patched,
1841 struct buffer_head *bh,
1842 int submit_bio_bh_rw)
1845 struct btrfsic_block *block;
1846 struct btrfsic_block_data_ctx block_ctx;
1848 struct btrfsic_state *state = dev_state->state;
1849 struct block_device *bdev = dev_state->bdev;
1850 unsigned int processed_len;
1852 if (NULL != bio_is_patched)
1853 *bio_is_patched = 0;
1860 is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
1863 block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
1864 &state->block_hashtable);
1865 if (NULL != block) {
1867 struct list_head *elem_ref_to;
1868 struct list_head *tmp_ref_to;
1870 if (block->is_superblock) {
1871 bytenr = btrfs_super_bytenr((struct btrfs_super_block *)
1873 if (num_pages * PAGE_CACHE_SIZE <
1874 BTRFS_SUPER_INFO_SIZE) {
1876 "btrfsic: cannot work with too short bios!\n");
1880 BUG_ON(BTRFS_SUPER_INFO_SIZE & (PAGE_CACHE_SIZE - 1));
1881 processed_len = BTRFS_SUPER_INFO_SIZE;
1882 if (state->print_mask &
1883 BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) {
1885 "[before new superblock is written]:\n");
1886 btrfsic_dump_tree_sub(state, block, 0);
1890 if (!block->is_superblock) {
1891 if (num_pages * PAGE_CACHE_SIZE <
1892 state->metablock_size) {
1894 "btrfsic: cannot work with too short bios!\n");
1897 processed_len = state->metablock_size;
1898 bytenr = btrfs_stack_header_bytenr(
1899 (struct btrfs_header *)
1901 btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
1905 if (block->logical_bytenr != bytenr &&
1906 !(!block->is_metadata &&
1907 block->logical_bytenr == 0))
1909 "Written block @%llu (%s/%llu/%d)"
1910 " found in hash table, %c,"
1912 " (!= stored %llu).\n",
1913 bytenr, dev_state->name, dev_bytenr,
1915 btrfsic_get_block_type(state, block),
1916 block->logical_bytenr);
1917 else if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1919 "Written block @%llu (%s/%llu/%d)"
1920 " found in hash table, %c.\n",
1921 bytenr, dev_state->name, dev_bytenr,
1923 btrfsic_get_block_type(state, block));
1924 block->logical_bytenr = bytenr;
1926 if (num_pages * PAGE_CACHE_SIZE <
1927 state->datablock_size) {
1929 "btrfsic: cannot work with too short bios!\n");
1932 processed_len = state->datablock_size;
1933 bytenr = block->logical_bytenr;
1934 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1936 "Written block @%llu (%s/%llu/%d)"
1937 " found in hash table, %c.\n",
1938 bytenr, dev_state->name, dev_bytenr,
1940 btrfsic_get_block_type(state, block));
1943 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1945 "ref_to_list: %cE, ref_from_list: %cE\n",
1946 list_empty(&block->ref_to_list) ? ' ' : '!',
1947 list_empty(&block->ref_from_list) ? ' ' : '!');
1948 if (btrfsic_is_block_ref_by_superblock(state, block, 0)) {
1949 printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
1950 " @%llu (%s/%llu/%d), old(gen=%llu,"
1951 " objectid=%llu, type=%d, offset=%llu),"
1953 " which is referenced by most recent superblock"
1954 " (superblockgen=%llu)!\n",
1955 btrfsic_get_block_type(state, block), bytenr,
1956 dev_state->name, dev_bytenr, block->mirror_num,
1958 btrfs_disk_key_objectid(&block->disk_key),
1959 block->disk_key.type,
1960 btrfs_disk_key_offset(&block->disk_key),
1961 btrfs_stack_header_generation(
1962 (struct btrfs_header *) mapped_datav[0]),
1963 state->max_superblock_generation);
1964 btrfsic_dump_tree(state);
1967 if (!block->is_iodone && !block->never_written) {
1968 printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
1969 " @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu,"
1970 " which is not yet iodone!\n",
1971 btrfsic_get_block_type(state, block), bytenr,
1972 dev_state->name, dev_bytenr, block->mirror_num,
1974 btrfs_stack_header_generation(
1975 (struct btrfs_header *)
1977 /* it would not be safe to go on */
1978 btrfsic_dump_tree(state);
1983 * Clear all references of this block. Do not free
1984 * the block itself even if is not referenced anymore
1985 * because it still carries valueable information
1986 * like whether it was ever written and IO completed.
1988 list_for_each_safe(elem_ref_to, tmp_ref_to,
1989 &block->ref_to_list) {
1990 struct btrfsic_block_link *const l =
1991 list_entry(elem_ref_to,
1992 struct btrfsic_block_link,
1995 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1996 btrfsic_print_rem_link(state, l);
1998 if (0 == l->ref_cnt) {
1999 list_del(&l->node_ref_to);
2000 list_del(&l->node_ref_from);
2001 btrfsic_block_link_hashtable_remove(l);
2002 btrfsic_block_link_free(l);
2006 if (block->is_superblock)
2007 ret = btrfsic_map_superblock(state, bytenr,
2011 ret = btrfsic_map_block(state, bytenr, processed_len,
2015 "btrfsic: btrfsic_map_block(root @%llu)"
2016 " failed!\n", bytenr);
2019 block_ctx.datav = mapped_datav;
2020 /* the following is required in case of writes to mirrors,
2021 * use the same that was used for the lookup */
2022 block_ctx.dev = dev_state;
2023 block_ctx.dev_bytenr = dev_bytenr;
2025 if (is_metadata || state->include_extent_data) {
2026 block->never_written = 0;
2027 block->iodone_w_error = 0;
2029 block->is_iodone = 0;
2030 BUG_ON(NULL == bio_is_patched);
2031 if (!*bio_is_patched) {
2032 block->orig_bio_bh_private =
2034 block->orig_bio_bh_end_io.bio =
2036 block->next_in_same_bio = NULL;
2037 bio->bi_private = block;
2038 bio->bi_end_io = btrfsic_bio_end_io;
2039 *bio_is_patched = 1;
2041 struct btrfsic_block *chained_block =
2042 (struct btrfsic_block *)
2045 BUG_ON(NULL == chained_block);
2046 block->orig_bio_bh_private =
2047 chained_block->orig_bio_bh_private;
2048 block->orig_bio_bh_end_io.bio =
2049 chained_block->orig_bio_bh_end_io.
2051 block->next_in_same_bio = chained_block;
2052 bio->bi_private = block;
2054 } else if (NULL != bh) {
2055 block->is_iodone = 0;
2056 block->orig_bio_bh_private = bh->b_private;
2057 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2058 block->next_in_same_bio = NULL;
2059 bh->b_private = block;
2060 bh->b_end_io = btrfsic_bh_end_io;
2062 block->is_iodone = 1;
2063 block->orig_bio_bh_private = NULL;
2064 block->orig_bio_bh_end_io.bio = NULL;
2065 block->next_in_same_bio = NULL;
2069 block->flush_gen = dev_state->last_flush_gen + 1;
2070 block->submit_bio_bh_rw = submit_bio_bh_rw;
2072 block->logical_bytenr = bytenr;
2073 block->is_metadata = 1;
2074 if (block->is_superblock) {
2075 BUG_ON(PAGE_CACHE_SIZE !=
2076 BTRFS_SUPER_INFO_SIZE);
2077 ret = btrfsic_process_written_superblock(
2080 (struct btrfs_super_block *)
2082 if (state->print_mask &
2083 BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) {
2085 "[after new superblock is written]:\n");
2086 btrfsic_dump_tree_sub(state, block, 0);
2089 block->mirror_num = 0; /* unknown */
2090 ret = btrfsic_process_metablock(
2098 "btrfsic: btrfsic_process_metablock"
2099 "(root @%llu) failed!\n",
2102 block->is_metadata = 0;
2103 block->mirror_num = 0; /* unknown */
2104 block->generation = BTRFSIC_GENERATION_UNKNOWN;
2105 if (!state->include_extent_data
2106 && list_empty(&block->ref_from_list)) {
2108 * disk block is overwritten with extent
2109 * data (not meta data) and we are configured
2110 * to not include extent data: take the
2111 * chance and free the block's memory
2113 btrfsic_block_hashtable_remove(block);
2114 list_del(&block->all_blocks_node);
2115 btrfsic_block_free(block);
2118 btrfsic_release_block_ctx(&block_ctx);
2120 /* block has not been found in hash table */
2124 processed_len = state->datablock_size;
2125 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2126 printk(KERN_INFO "Written block (%s/%llu/?)"
2127 " !found in hash table, D.\n",
2128 dev_state->name, dev_bytenr);
2129 if (!state->include_extent_data) {
2130 /* ignore that written D block */
2134 /* this is getting ugly for the
2135 * include_extent_data case... */
2136 bytenr = 0; /* unknown */
2137 block_ctx.start = bytenr;
2138 block_ctx.len = processed_len;
2139 block_ctx.mem_to_free = NULL;
2140 block_ctx.pagev = NULL;
2142 processed_len = state->metablock_size;
2143 bytenr = btrfs_stack_header_bytenr(
2144 (struct btrfs_header *)
2146 btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
2148 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2150 "Written block @%llu (%s/%llu/?)"
2151 " !found in hash table, M.\n",
2152 bytenr, dev_state->name, dev_bytenr);
2154 ret = btrfsic_map_block(state, bytenr, processed_len,
2158 "btrfsic: btrfsic_map_block(root @%llu)"
2164 block_ctx.datav = mapped_datav;
2165 /* the following is required in case of writes to mirrors,
2166 * use the same that was used for the lookup */
2167 block_ctx.dev = dev_state;
2168 block_ctx.dev_bytenr = dev_bytenr;
2170 block = btrfsic_block_alloc();
2171 if (NULL == block) {
2172 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
2173 btrfsic_release_block_ctx(&block_ctx);
2176 block->dev_state = dev_state;
2177 block->dev_bytenr = dev_bytenr;
2178 block->logical_bytenr = bytenr;
2179 block->is_metadata = is_metadata;
2180 block->never_written = 0;
2181 block->iodone_w_error = 0;
2182 block->mirror_num = 0; /* unknown */
2183 block->flush_gen = dev_state->last_flush_gen + 1;
2184 block->submit_bio_bh_rw = submit_bio_bh_rw;
2186 block->is_iodone = 0;
2187 BUG_ON(NULL == bio_is_patched);
2188 if (!*bio_is_patched) {
2189 block->orig_bio_bh_private = bio->bi_private;
2190 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
2191 block->next_in_same_bio = NULL;
2192 bio->bi_private = block;
2193 bio->bi_end_io = btrfsic_bio_end_io;
2194 *bio_is_patched = 1;
2196 struct btrfsic_block *chained_block =
2197 (struct btrfsic_block *)
2200 BUG_ON(NULL == chained_block);
2201 block->orig_bio_bh_private =
2202 chained_block->orig_bio_bh_private;
2203 block->orig_bio_bh_end_io.bio =
2204 chained_block->orig_bio_bh_end_io.bio;
2205 block->next_in_same_bio = chained_block;
2206 bio->bi_private = block;
2208 } else if (NULL != bh) {
2209 block->is_iodone = 0;
2210 block->orig_bio_bh_private = bh->b_private;
2211 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2212 block->next_in_same_bio = NULL;
2213 bh->b_private = block;
2214 bh->b_end_io = btrfsic_bh_end_io;
2216 block->is_iodone = 1;
2217 block->orig_bio_bh_private = NULL;
2218 block->orig_bio_bh_end_io.bio = NULL;
2219 block->next_in_same_bio = NULL;
2221 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2223 "New written %c-block @%llu (%s/%llu/%d)\n",
2224 is_metadata ? 'M' : 'D',
2225 block->logical_bytenr, block->dev_state->name,
2226 block->dev_bytenr, block->mirror_num);
2227 list_add(&block->all_blocks_node, &state->all_blocks_list);
2228 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2231 ret = btrfsic_process_metablock(state, block,
2235 "btrfsic: process_metablock(root @%llu)"
2239 btrfsic_release_block_ctx(&block_ctx);
2243 BUG_ON(!processed_len);
2244 dev_bytenr += processed_len;
2245 mapped_datav += processed_len >> PAGE_CACHE_SHIFT;
2246 num_pages -= processed_len >> PAGE_CACHE_SHIFT;
2250 static void btrfsic_bio_end_io(struct bio *bp, int bio_error_status)
2252 struct btrfsic_block *block = (struct btrfsic_block *)bp->bi_private;
2255 /* mutex is not held! This is not save if IO is not yet completed
2258 if (bio_error_status)
2261 BUG_ON(NULL == block);
2262 bp->bi_private = block->orig_bio_bh_private;
2263 bp->bi_end_io = block->orig_bio_bh_end_io.bio;
2266 struct btrfsic_block *next_block;
2267 struct btrfsic_dev_state *const dev_state = block->dev_state;
2269 if ((dev_state->state->print_mask &
2270 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2272 "bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n",
2274 btrfsic_get_block_type(dev_state->state, block),
2275 block->logical_bytenr, dev_state->name,
2276 block->dev_bytenr, block->mirror_num);
2277 next_block = block->next_in_same_bio;
2278 block->iodone_w_error = iodone_w_error;
2279 if (block->submit_bio_bh_rw & REQ_FLUSH) {
2280 dev_state->last_flush_gen++;
2281 if ((dev_state->state->print_mask &
2282 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2284 "bio_end_io() new %s flush_gen=%llu\n",
2286 dev_state->last_flush_gen);
2288 if (block->submit_bio_bh_rw & REQ_FUA)
2289 block->flush_gen = 0; /* FUA completed means block is
2291 block->is_iodone = 1; /* for FLUSH, this releases the block */
2293 } while (NULL != block);
2295 bp->bi_end_io(bp, bio_error_status);
2298 static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate)
2300 struct btrfsic_block *block = (struct btrfsic_block *)bh->b_private;
2301 int iodone_w_error = !uptodate;
2302 struct btrfsic_dev_state *dev_state;
2304 BUG_ON(NULL == block);
2305 dev_state = block->dev_state;
2306 if ((dev_state->state->print_mask & BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2308 "bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n",
2310 btrfsic_get_block_type(dev_state->state, block),
2311 block->logical_bytenr, block->dev_state->name,
2312 block->dev_bytenr, block->mirror_num);
2314 block->iodone_w_error = iodone_w_error;
2315 if (block->submit_bio_bh_rw & REQ_FLUSH) {
2316 dev_state->last_flush_gen++;
2317 if ((dev_state->state->print_mask &
2318 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2320 "bh_end_io() new %s flush_gen=%llu\n",
2321 dev_state->name, dev_state->last_flush_gen);
2323 if (block->submit_bio_bh_rw & REQ_FUA)
2324 block->flush_gen = 0; /* FUA completed means block is on disk */
2326 bh->b_private = block->orig_bio_bh_private;
2327 bh->b_end_io = block->orig_bio_bh_end_io.bh;
2328 block->is_iodone = 1; /* for FLUSH, this releases the block */
2329 bh->b_end_io(bh, uptodate);
2332 static int btrfsic_process_written_superblock(
2333 struct btrfsic_state *state,
2334 struct btrfsic_block *const superblock,
2335 struct btrfs_super_block *const super_hdr)
2339 superblock->generation = btrfs_super_generation(super_hdr);
2340 if (!(superblock->generation > state->max_superblock_generation ||
2341 0 == state->max_superblock_generation)) {
2342 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2344 "btrfsic: superblock @%llu (%s/%llu/%d)"
2345 " with old gen %llu <= %llu\n",
2346 superblock->logical_bytenr,
2347 superblock->dev_state->name,
2348 superblock->dev_bytenr, superblock->mirror_num,
2349 btrfs_super_generation(super_hdr),
2350 state->max_superblock_generation);
2352 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2354 "btrfsic: got new superblock @%llu (%s/%llu/%d)"
2355 " with new gen %llu > %llu\n",
2356 superblock->logical_bytenr,
2357 superblock->dev_state->name,
2358 superblock->dev_bytenr, superblock->mirror_num,
2359 btrfs_super_generation(super_hdr),
2360 state->max_superblock_generation);
2362 state->max_superblock_generation =
2363 btrfs_super_generation(super_hdr);
2364 state->latest_superblock = superblock;
2367 for (pass = 0; pass < 3; pass++) {
2370 struct btrfsic_block *next_block;
2371 struct btrfsic_block_data_ctx tmp_next_block_ctx;
2372 struct btrfsic_block_link *l;
2375 const char *additional_string = NULL;
2376 struct btrfs_disk_key tmp_disk_key = {0};
2378 btrfs_set_disk_key_objectid(&tmp_disk_key,
2379 BTRFS_ROOT_ITEM_KEY);
2380 btrfs_set_disk_key_objectid(&tmp_disk_key, 0);
2384 btrfs_set_disk_key_objectid(&tmp_disk_key,
2385 BTRFS_ROOT_TREE_OBJECTID);
2386 additional_string = "root ";
2387 next_bytenr = btrfs_super_root(super_hdr);
2388 if (state->print_mask &
2389 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2390 printk(KERN_INFO "root@%llu\n", next_bytenr);
2393 btrfs_set_disk_key_objectid(&tmp_disk_key,
2394 BTRFS_CHUNK_TREE_OBJECTID);
2395 additional_string = "chunk ";
2396 next_bytenr = btrfs_super_chunk_root(super_hdr);
2397 if (state->print_mask &
2398 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2399 printk(KERN_INFO "chunk@%llu\n", next_bytenr);
2402 btrfs_set_disk_key_objectid(&tmp_disk_key,
2403 BTRFS_TREE_LOG_OBJECTID);
2404 additional_string = "log ";
2405 next_bytenr = btrfs_super_log_root(super_hdr);
2406 if (0 == next_bytenr)
2408 if (state->print_mask &
2409 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2410 printk(KERN_INFO "log@%llu\n", next_bytenr);
2415 btrfs_num_copies(state->root->fs_info,
2416 next_bytenr, BTRFS_SUPER_INFO_SIZE);
2417 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
2418 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
2419 next_bytenr, num_copies);
2420 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2423 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2425 "btrfsic_process_written_superblock("
2426 "mirror_num=%d)\n", mirror_num);
2427 ret = btrfsic_map_block(state, next_bytenr,
2428 BTRFS_SUPER_INFO_SIZE,
2429 &tmp_next_block_ctx,
2433 "btrfsic: btrfsic_map_block(@%llu,"
2434 " mirror=%d) failed!\n",
2435 next_bytenr, mirror_num);
2439 next_block = btrfsic_block_lookup_or_add(
2441 &tmp_next_block_ctx,
2446 if (NULL == next_block) {
2448 "btrfsic: error, kmalloc failed!\n");
2449 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2453 next_block->disk_key = tmp_disk_key;
2455 next_block->generation =
2456 BTRFSIC_GENERATION_UNKNOWN;
2457 l = btrfsic_block_link_lookup_or_add(
2459 &tmp_next_block_ctx,
2462 BTRFSIC_GENERATION_UNKNOWN);
2463 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2469 if (WARN_ON(-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)))
2470 btrfsic_dump_tree(state);
2475 static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
2476 struct btrfsic_block *const block,
2477 int recursion_level)
2479 struct list_head *elem_ref_to;
2482 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2484 * Note that this situation can happen and does not
2485 * indicate an error in regular cases. It happens
2486 * when disk blocks are freed and later reused.
2487 * The check-integrity module is not aware of any
2488 * block free operations, it just recognizes block
2489 * write operations. Therefore it keeps the linkage
2490 * information for a block until a block is
2491 * rewritten. This can temporarily cause incorrect
2492 * and even circular linkage informations. This
2493 * causes no harm unless such blocks are referenced
2494 * by the most recent super block.
2496 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2498 "btrfsic: abort cyclic linkage (case 1).\n");
2504 * This algorithm is recursive because the amount of used stack
2505 * space is very small and the max recursion depth is limited.
2507 list_for_each(elem_ref_to, &block->ref_to_list) {
2508 const struct btrfsic_block_link *const l =
2509 list_entry(elem_ref_to, struct btrfsic_block_link,
2512 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2514 "rl=%d, %c @%llu (%s/%llu/%d)"
2515 " %u* refers to %c @%llu (%s/%llu/%d)\n",
2517 btrfsic_get_block_type(state, block),
2518 block->logical_bytenr, block->dev_state->name,
2519 block->dev_bytenr, block->mirror_num,
2521 btrfsic_get_block_type(state, l->block_ref_to),
2522 l->block_ref_to->logical_bytenr,
2523 l->block_ref_to->dev_state->name,
2524 l->block_ref_to->dev_bytenr,
2525 l->block_ref_to->mirror_num);
2526 if (l->block_ref_to->never_written) {
2527 printk(KERN_INFO "btrfs: attempt to write superblock"
2528 " which references block %c @%llu (%s/%llu/%d)"
2529 " which is never written!\n",
2530 btrfsic_get_block_type(state, l->block_ref_to),
2531 l->block_ref_to->logical_bytenr,
2532 l->block_ref_to->dev_state->name,
2533 l->block_ref_to->dev_bytenr,
2534 l->block_ref_to->mirror_num);
2536 } else if (!l->block_ref_to->is_iodone) {
2537 printk(KERN_INFO "btrfs: attempt to write superblock"
2538 " which references block %c @%llu (%s/%llu/%d)"
2539 " which is not yet iodone!\n",
2540 btrfsic_get_block_type(state, l->block_ref_to),
2541 l->block_ref_to->logical_bytenr,
2542 l->block_ref_to->dev_state->name,
2543 l->block_ref_to->dev_bytenr,
2544 l->block_ref_to->mirror_num);
2546 } else if (l->block_ref_to->iodone_w_error) {
2547 printk(KERN_INFO "btrfs: attempt to write superblock"
2548 " which references block %c @%llu (%s/%llu/%d)"
2549 " which has write error!\n",
2550 btrfsic_get_block_type(state, l->block_ref_to),
2551 l->block_ref_to->logical_bytenr,
2552 l->block_ref_to->dev_state->name,
2553 l->block_ref_to->dev_bytenr,
2554 l->block_ref_to->mirror_num);
2556 } else if (l->parent_generation !=
2557 l->block_ref_to->generation &&
2558 BTRFSIC_GENERATION_UNKNOWN !=
2559 l->parent_generation &&
2560 BTRFSIC_GENERATION_UNKNOWN !=
2561 l->block_ref_to->generation) {
2562 printk(KERN_INFO "btrfs: attempt to write superblock"
2563 " which references block %c @%llu (%s/%llu/%d)"
2564 " with generation %llu !="
2565 " parent generation %llu!\n",
2566 btrfsic_get_block_type(state, l->block_ref_to),
2567 l->block_ref_to->logical_bytenr,
2568 l->block_ref_to->dev_state->name,
2569 l->block_ref_to->dev_bytenr,
2570 l->block_ref_to->mirror_num,
2571 l->block_ref_to->generation,
2572 l->parent_generation);
2574 } else if (l->block_ref_to->flush_gen >
2575 l->block_ref_to->dev_state->last_flush_gen) {
2576 printk(KERN_INFO "btrfs: attempt to write superblock"
2577 " which references block %c @%llu (%s/%llu/%d)"
2578 " which is not flushed out of disk's write cache"
2579 " (block flush_gen=%llu,"
2580 " dev->flush_gen=%llu)!\n",
2581 btrfsic_get_block_type(state, l->block_ref_to),
2582 l->block_ref_to->logical_bytenr,
2583 l->block_ref_to->dev_state->name,
2584 l->block_ref_to->dev_bytenr,
2585 l->block_ref_to->mirror_num, block->flush_gen,
2586 l->block_ref_to->dev_state->last_flush_gen);
2588 } else if (-1 == btrfsic_check_all_ref_blocks(state,
2599 static int btrfsic_is_block_ref_by_superblock(
2600 const struct btrfsic_state *state,
2601 const struct btrfsic_block *block,
2602 int recursion_level)
2604 struct list_head *elem_ref_from;
2606 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2607 /* refer to comment at "abort cyclic linkage (case 1)" */
2608 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2610 "btrfsic: abort cyclic linkage (case 2).\n");
2616 * This algorithm is recursive because the amount of used stack space
2617 * is very small and the max recursion depth is limited.
2619 list_for_each(elem_ref_from, &block->ref_from_list) {
2620 const struct btrfsic_block_link *const l =
2621 list_entry(elem_ref_from, struct btrfsic_block_link,
2624 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2626 "rl=%d, %c @%llu (%s/%llu/%d)"
2627 " is ref %u* from %c @%llu (%s/%llu/%d)\n",
2629 btrfsic_get_block_type(state, block),
2630 block->logical_bytenr, block->dev_state->name,
2631 block->dev_bytenr, block->mirror_num,
2633 btrfsic_get_block_type(state, l->block_ref_from),
2634 l->block_ref_from->logical_bytenr,
2635 l->block_ref_from->dev_state->name,
2636 l->block_ref_from->dev_bytenr,
2637 l->block_ref_from->mirror_num);
2638 if (l->block_ref_from->is_superblock &&
2639 state->latest_superblock->dev_bytenr ==
2640 l->block_ref_from->dev_bytenr &&
2641 state->latest_superblock->dev_state->bdev ==
2642 l->block_ref_from->dev_state->bdev)
2644 else if (btrfsic_is_block_ref_by_superblock(state,
2654 static void btrfsic_print_add_link(const struct btrfsic_state *state,
2655 const struct btrfsic_block_link *l)
2658 "Add %u* link from %c @%llu (%s/%llu/%d)"
2659 " to %c @%llu (%s/%llu/%d).\n",
2661 btrfsic_get_block_type(state, l->block_ref_from),
2662 l->block_ref_from->logical_bytenr,
2663 l->block_ref_from->dev_state->name,
2664 l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
2665 btrfsic_get_block_type(state, l->block_ref_to),
2666 l->block_ref_to->logical_bytenr,
2667 l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
2668 l->block_ref_to->mirror_num);
2671 static void btrfsic_print_rem_link(const struct btrfsic_state *state,
2672 const struct btrfsic_block_link *l)
2675 "Rem %u* link from %c @%llu (%s/%llu/%d)"
2676 " to %c @%llu (%s/%llu/%d).\n",
2678 btrfsic_get_block_type(state, l->block_ref_from),
2679 l->block_ref_from->logical_bytenr,
2680 l->block_ref_from->dev_state->name,
2681 l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
2682 btrfsic_get_block_type(state, l->block_ref_to),
2683 l->block_ref_to->logical_bytenr,
2684 l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
2685 l->block_ref_to->mirror_num);
2688 static char btrfsic_get_block_type(const struct btrfsic_state *state,
2689 const struct btrfsic_block *block)
2691 if (block->is_superblock &&
2692 state->latest_superblock->dev_bytenr == block->dev_bytenr &&
2693 state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
2695 else if (block->is_superblock)
2697 else if (block->is_metadata)
2703 static void btrfsic_dump_tree(const struct btrfsic_state *state)
2705 btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
2708 static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
2709 const struct btrfsic_block *block,
2712 struct list_head *elem_ref_to;
2714 static char buf[80];
2715 int cursor_position;
2718 * Should better fill an on-stack buffer with a complete line and
2719 * dump it at once when it is time to print a newline character.
2723 * This algorithm is recursive because the amount of used stack space
2724 * is very small and the max recursion depth is limited.
2726 indent_add = sprintf(buf, "%c-%llu(%s/%llu/%d)",
2727 btrfsic_get_block_type(state, block),
2728 block->logical_bytenr, block->dev_state->name,
2729 block->dev_bytenr, block->mirror_num);
2730 if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2735 indent_level += indent_add;
2736 if (list_empty(&block->ref_to_list)) {
2740 if (block->mirror_num > 1 &&
2741 !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
2746 cursor_position = indent_level;
2747 list_for_each(elem_ref_to, &block->ref_to_list) {
2748 const struct btrfsic_block_link *const l =
2749 list_entry(elem_ref_to, struct btrfsic_block_link,
2752 while (cursor_position < indent_level) {
2757 indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
2759 indent_add = sprintf(buf, " --> ");
2760 if (indent_level + indent_add >
2761 BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2763 cursor_position = 0;
2769 btrfsic_dump_tree_sub(state, l->block_ref_to,
2770 indent_level + indent_add);
2771 cursor_position = 0;
2775 static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
2776 struct btrfsic_state *state,
2777 struct btrfsic_block_data_ctx *next_block_ctx,
2778 struct btrfsic_block *next_block,
2779 struct btrfsic_block *from_block,
2780 u64 parent_generation)
2782 struct btrfsic_block_link *l;
2784 l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
2785 next_block_ctx->dev_bytenr,
2786 from_block->dev_state->bdev,
2787 from_block->dev_bytenr,
2788 &state->block_link_hashtable);
2790 l = btrfsic_block_link_alloc();
2793 "btrfsic: error, kmalloc" " failed!\n");
2797 l->block_ref_to = next_block;
2798 l->block_ref_from = from_block;
2800 l->parent_generation = parent_generation;
2802 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2803 btrfsic_print_add_link(state, l);
2805 list_add(&l->node_ref_to, &from_block->ref_to_list);
2806 list_add(&l->node_ref_from, &next_block->ref_from_list);
2808 btrfsic_block_link_hashtable_add(l,
2809 &state->block_link_hashtable);
2812 l->parent_generation = parent_generation;
2813 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2814 btrfsic_print_add_link(state, l);
2820 static struct btrfsic_block *btrfsic_block_lookup_or_add(
2821 struct btrfsic_state *state,
2822 struct btrfsic_block_data_ctx *block_ctx,
2823 const char *additional_string,
2830 struct btrfsic_block *block;
2832 block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
2833 block_ctx->dev_bytenr,
2834 &state->block_hashtable);
2835 if (NULL == block) {
2836 struct btrfsic_dev_state *dev_state;
2838 block = btrfsic_block_alloc();
2839 if (NULL == block) {
2840 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
2843 dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev);
2844 if (NULL == dev_state) {
2846 "btrfsic: error, lookup dev_state failed!\n");
2847 btrfsic_block_free(block);
2850 block->dev_state = dev_state;
2851 block->dev_bytenr = block_ctx->dev_bytenr;
2852 block->logical_bytenr = block_ctx->start;
2853 block->is_metadata = is_metadata;
2854 block->is_iodone = is_iodone;
2855 block->never_written = never_written;
2856 block->mirror_num = mirror_num;
2857 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2859 "New %s%c-block @%llu (%s/%llu/%d)\n",
2861 btrfsic_get_block_type(state, block),
2862 block->logical_bytenr, dev_state->name,
2863 block->dev_bytenr, mirror_num);
2864 list_add(&block->all_blocks_node, &state->all_blocks_list);
2865 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2866 if (NULL != was_created)
2869 if (NULL != was_created)
2876 static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
2878 struct btrfsic_dev_state *dev_state,
2884 struct btrfsic_block_data_ctx block_ctx;
2887 num_copies = btrfs_num_copies(state->root->fs_info,
2888 bytenr, state->metablock_size);
2890 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2891 ret = btrfsic_map_block(state, bytenr, state->metablock_size,
2892 &block_ctx, mirror_num);
2894 printk(KERN_INFO "btrfsic:"
2895 " btrfsic_map_block(logical @%llu,"
2896 " mirror %d) failed!\n",
2897 bytenr, mirror_num);
2901 if (dev_state->bdev == block_ctx.dev->bdev &&
2902 dev_bytenr == block_ctx.dev_bytenr) {
2904 btrfsic_release_block_ctx(&block_ctx);
2907 btrfsic_release_block_ctx(&block_ctx);
2910 if (WARN_ON(!match)) {
2911 printk(KERN_INFO "btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio,"
2912 " buffer->log_bytenr=%llu, submit_bio(bdev=%s,"
2913 " phys_bytenr=%llu)!\n",
2914 bytenr, dev_state->name, dev_bytenr);
2915 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2916 ret = btrfsic_map_block(state, bytenr,
2917 state->metablock_size,
2918 &block_ctx, mirror_num);
2922 printk(KERN_INFO "Read logical bytenr @%llu maps to"
2924 bytenr, block_ctx.dev->name,
2925 block_ctx.dev_bytenr, mirror_num);
2930 static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
2931 struct block_device *bdev)
2933 struct btrfsic_dev_state *ds;
2935 ds = btrfsic_dev_state_hashtable_lookup(bdev,
2936 &btrfsic_dev_state_hashtable);
2940 int btrfsic_submit_bh(int rw, struct buffer_head *bh)
2942 struct btrfsic_dev_state *dev_state;
2944 if (!btrfsic_is_initialized)
2945 return submit_bh(rw, bh);
2947 mutex_lock(&btrfsic_mutex);
2948 /* since btrfsic_submit_bh() might also be called before
2949 * btrfsic_mount(), this might return NULL */
2950 dev_state = btrfsic_dev_state_lookup(bh->b_bdev);
2952 /* Only called to write the superblock (incl. FLUSH/FUA) */
2953 if (NULL != dev_state &&
2954 (rw & WRITE) && bh->b_size > 0) {
2957 dev_bytenr = 4096 * bh->b_blocknr;
2958 if (dev_state->state->print_mask &
2959 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2961 "submit_bh(rw=0x%x, blocknr=%llu (bytenr %llu),"
2962 " size=%zu, data=%p, bdev=%p)\n",
2963 rw, (unsigned long long)bh->b_blocknr,
2964 dev_bytenr, bh->b_size, bh->b_data, bh->b_bdev);
2965 btrfsic_process_written_block(dev_state, dev_bytenr,
2966 &bh->b_data, 1, NULL,
2968 } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
2969 if (dev_state->state->print_mask &
2970 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2972 "submit_bh(rw=0x%x FLUSH, bdev=%p)\n",
2974 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
2975 if ((dev_state->state->print_mask &
2976 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
2977 BTRFSIC_PRINT_MASK_VERBOSE)))
2979 "btrfsic_submit_bh(%s) with FLUSH"
2980 " but dummy block already in use"
2984 struct btrfsic_block *const block =
2985 &dev_state->dummy_block_for_bio_bh_flush;
2987 block->is_iodone = 0;
2988 block->never_written = 0;
2989 block->iodone_w_error = 0;
2990 block->flush_gen = dev_state->last_flush_gen + 1;
2991 block->submit_bio_bh_rw = rw;
2992 block->orig_bio_bh_private = bh->b_private;
2993 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2994 block->next_in_same_bio = NULL;
2995 bh->b_private = block;
2996 bh->b_end_io = btrfsic_bh_end_io;
2999 mutex_unlock(&btrfsic_mutex);
3000 return submit_bh(rw, bh);
3003 static void __btrfsic_submit_bio(int rw, struct bio *bio)
3005 struct btrfsic_dev_state *dev_state;
3007 if (!btrfsic_is_initialized)
3010 mutex_lock(&btrfsic_mutex);
3011 /* since btrfsic_submit_bio() is also called before
3012 * btrfsic_mount(), this might return NULL */
3013 dev_state = btrfsic_dev_state_lookup(bio->bi_bdev);
3014 if (NULL != dev_state &&
3015 (rw & WRITE) && NULL != bio->bi_io_vec) {
3020 char **mapped_datav;
3022 dev_bytenr = 512 * bio->bi_iter.bi_sector;
3024 if (dev_state->state->print_mask &
3025 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
3027 "submit_bio(rw=0x%x, bi_vcnt=%u,"
3028 " bi_sector=%llu (bytenr %llu), bi_bdev=%p)\n",
3030 (unsigned long long)bio->bi_iter.bi_sector,
3031 dev_bytenr, bio->bi_bdev);
3033 mapped_datav = kmalloc(sizeof(*mapped_datav) * bio->bi_vcnt,
3037 cur_bytenr = dev_bytenr;
3038 for (i = 0; i < bio->bi_vcnt; i++) {
3039 BUG_ON(bio->bi_io_vec[i].bv_len != PAGE_CACHE_SIZE);
3040 mapped_datav[i] = kmap(bio->bi_io_vec[i].bv_page);
3041 if (!mapped_datav[i]) {
3044 kunmap(bio->bi_io_vec[i].bv_page);
3046 kfree(mapped_datav);
3049 if (dev_state->state->print_mask &
3050 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE)
3052 "#%u: bytenr=%llu, len=%u, offset=%u\n",
3053 i, cur_bytenr, bio->bi_io_vec[i].bv_len,
3054 bio->bi_io_vec[i].bv_offset);
3055 cur_bytenr += bio->bi_io_vec[i].bv_len;
3057 btrfsic_process_written_block(dev_state, dev_bytenr,
3058 mapped_datav, bio->bi_vcnt,
3059 bio, &bio_is_patched,
3063 kunmap(bio->bi_io_vec[i].bv_page);
3065 kfree(mapped_datav);
3066 } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
3067 if (dev_state->state->print_mask &
3068 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
3070 "submit_bio(rw=0x%x FLUSH, bdev=%p)\n",
3072 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
3073 if ((dev_state->state->print_mask &
3074 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
3075 BTRFSIC_PRINT_MASK_VERBOSE)))
3077 "btrfsic_submit_bio(%s) with FLUSH"
3078 " but dummy block already in use"
3082 struct btrfsic_block *const block =
3083 &dev_state->dummy_block_for_bio_bh_flush;
3085 block->is_iodone = 0;
3086 block->never_written = 0;
3087 block->iodone_w_error = 0;
3088 block->flush_gen = dev_state->last_flush_gen + 1;
3089 block->submit_bio_bh_rw = rw;
3090 block->orig_bio_bh_private = bio->bi_private;
3091 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
3092 block->next_in_same_bio = NULL;
3093 bio->bi_private = block;
3094 bio->bi_end_io = btrfsic_bio_end_io;
3098 mutex_unlock(&btrfsic_mutex);
3101 void btrfsic_submit_bio(int rw, struct bio *bio)
3103 __btrfsic_submit_bio(rw, bio);
3104 submit_bio(rw, bio);
3107 int btrfsic_submit_bio_wait(int rw, struct bio *bio)
3109 __btrfsic_submit_bio(rw, bio);
3110 return submit_bio_wait(rw, bio);
3113 int btrfsic_mount(struct btrfs_root *root,
3114 struct btrfs_fs_devices *fs_devices,
3115 int including_extent_data, u32 print_mask)
3118 struct btrfsic_state *state;
3119 struct list_head *dev_head = &fs_devices->devices;
3120 struct btrfs_device *device;
3122 if (root->nodesize & ((u64)PAGE_CACHE_SIZE - 1)) {
3124 "btrfsic: cannot handle nodesize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3125 root->nodesize, PAGE_CACHE_SIZE);
3128 if (root->sectorsize & ((u64)PAGE_CACHE_SIZE - 1)) {
3130 "btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3131 root->sectorsize, PAGE_CACHE_SIZE);
3134 state = kzalloc(sizeof(*state), GFP_NOFS);
3135 if (NULL == state) {
3136 printk(KERN_INFO "btrfs check-integrity: kmalloc() failed!\n");
3140 if (!btrfsic_is_initialized) {
3141 mutex_init(&btrfsic_mutex);
3142 btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
3143 btrfsic_is_initialized = 1;
3145 mutex_lock(&btrfsic_mutex);
3147 state->print_mask = print_mask;
3148 state->include_extent_data = including_extent_data;
3149 state->csum_size = 0;
3150 state->metablock_size = root->nodesize;
3151 state->datablock_size = root->sectorsize;
3152 INIT_LIST_HEAD(&state->all_blocks_list);
3153 btrfsic_block_hashtable_init(&state->block_hashtable);
3154 btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
3155 state->max_superblock_generation = 0;
3156 state->latest_superblock = NULL;
3158 list_for_each_entry(device, dev_head, dev_list) {
3159 struct btrfsic_dev_state *ds;
3162 if (!device->bdev || !device->name)
3165 ds = btrfsic_dev_state_alloc();
3168 "btrfs check-integrity: kmalloc() failed!\n");
3169 mutex_unlock(&btrfsic_mutex);
3172 ds->bdev = device->bdev;
3174 bdevname(ds->bdev, ds->name);
3175 ds->name[BDEVNAME_SIZE - 1] = '\0';
3176 for (p = ds->name; *p != '\0'; p++);
3177 while (p > ds->name && *p != '/')
3181 strlcpy(ds->name, p, sizeof(ds->name));
3182 btrfsic_dev_state_hashtable_add(ds,
3183 &btrfsic_dev_state_hashtable);
3186 ret = btrfsic_process_superblock(state, fs_devices);
3188 mutex_unlock(&btrfsic_mutex);
3189 btrfsic_unmount(root, fs_devices);
3193 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
3194 btrfsic_dump_database(state);
3195 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
3196 btrfsic_dump_tree(state);
3198 mutex_unlock(&btrfsic_mutex);
3202 void btrfsic_unmount(struct btrfs_root *root,
3203 struct btrfs_fs_devices *fs_devices)
3205 struct list_head *elem_all;
3206 struct list_head *tmp_all;
3207 struct btrfsic_state *state;
3208 struct list_head *dev_head = &fs_devices->devices;
3209 struct btrfs_device *device;
3211 if (!btrfsic_is_initialized)
3214 mutex_lock(&btrfsic_mutex);
3217 list_for_each_entry(device, dev_head, dev_list) {
3218 struct btrfsic_dev_state *ds;
3220 if (!device->bdev || !device->name)
3223 ds = btrfsic_dev_state_hashtable_lookup(
3225 &btrfsic_dev_state_hashtable);
3228 btrfsic_dev_state_hashtable_remove(ds);
3229 btrfsic_dev_state_free(ds);
3233 if (NULL == state) {
3235 "btrfsic: error, cannot find state information"
3237 mutex_unlock(&btrfsic_mutex);
3242 * Don't care about keeping the lists' state up to date,
3243 * just free all memory that was allocated dynamically.
3244 * Free the blocks and the block_links.
3246 list_for_each_safe(elem_all, tmp_all, &state->all_blocks_list) {
3247 struct btrfsic_block *const b_all =
3248 list_entry(elem_all, struct btrfsic_block,
3250 struct list_head *elem_ref_to;
3251 struct list_head *tmp_ref_to;
3253 list_for_each_safe(elem_ref_to, tmp_ref_to,
3254 &b_all->ref_to_list) {
3255 struct btrfsic_block_link *const l =
3256 list_entry(elem_ref_to,
3257 struct btrfsic_block_link,
3260 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
3261 btrfsic_print_rem_link(state, l);
3264 if (0 == l->ref_cnt)
3265 btrfsic_block_link_free(l);
3268 if (b_all->is_iodone || b_all->never_written)
3269 btrfsic_block_free(b_all);
3271 printk(KERN_INFO "btrfs: attempt to free %c-block"
3272 " @%llu (%s/%llu/%d) on umount which is"
3273 " not yet iodone!\n",
3274 btrfsic_get_block_type(state, b_all),
3275 b_all->logical_bytenr, b_all->dev_state->name,
3276 b_all->dev_bytenr, b_all->mirror_num);
3279 mutex_unlock(&btrfsic_mutex);