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