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