blob: 60fc45f3a361cf91a883d7c584230935a8029b37 [file] [log] [blame]
Stefan Behrens5db02762011-11-01 17:04:16 +01001/*
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
Stefan Behrens62856a92012-07-31 11:09:44 -060040 * 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.
Stefan Behrens5db02762011-11-01 17:04:16 +010043 * 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.
Stefan Behrens56d140f2013-11-13 17:19:08 +010080 *
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
Stefan Behrens5db02762011-11-01 17:04:16 +010089 */
90
91#include <linux/sched.h>
92#include <linux/slab.h>
93#include <linux/buffer_head.h>
94#include <linux/mutex.h>
Stefan Behrens5db02762011-11-01 17:04:16 +010095#include <linux/genhd.h>
96#include <linux/blkdev.h>
Chris Mason8f608de62014-11-25 05:58:23 -080097#include <linux/vmalloc.h>
Rasmus Villemoes02def692015-11-27 09:42:11 +010098#include <linux/string.h>
Stefan Behrens5db02762011-11-01 17:04:16 +010099#include "ctree.h"
100#include "disk-io.h"
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +0000101#include "hash.h"
Stefan Behrens5db02762011-11-01 17:04:16 +0100102#include "transaction.h"
103#include "extent_io.h"
Stefan Behrens5db02762011-11-01 17:04:16 +0100104#include "volumes.h"
105#include "print-tree.h"
106#include "locking.h"
107#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -0400108#include "rcu-string.h"
Stefan Behrens5db02762011-11-01 17:04:16 +0100109
110#define BTRFSIC_BLOCK_HASHTABLE_SIZE 0x10000
111#define BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE 0x10000
112#define BTRFSIC_DEV2STATE_HASHTABLE_SIZE 0x100
113#define BTRFSIC_BLOCK_MAGIC_NUMBER 0x14491051
114#define BTRFSIC_BLOCK_LINK_MAGIC_NUMBER 0x11070807
115#define BTRFSIC_DEV2STATE_MAGIC_NUMBER 0x20111530
116#define BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER 20111300
117#define BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL (200 - 6) /* in characters,
118 * excluding " [...]" */
Stefan Behrens5db02762011-11-01 17:04:16 +0100119#define BTRFSIC_GENERATION_UNKNOWN ((u64)-1)
120
121/*
122 * The definition of the bitmask fields for the print_mask.
123 * They are specified with the mount option check_integrity_print_mask.
124 */
125#define BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE 0x00000001
126#define BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION 0x00000002
127#define BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE 0x00000004
128#define BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE 0x00000008
129#define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH 0x00000010
130#define BTRFSIC_PRINT_MASK_END_IO_BIO_BH 0x00000020
131#define BTRFSIC_PRINT_MASK_VERBOSE 0x00000040
132#define BTRFSIC_PRINT_MASK_VERY_VERBOSE 0x00000080
133#define BTRFSIC_PRINT_MASK_INITIAL_TREE 0x00000100
134#define BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES 0x00000200
135#define BTRFSIC_PRINT_MASK_INITIAL_DATABASE 0x00000400
136#define BTRFSIC_PRINT_MASK_NUM_COPIES 0x00000800
137#define BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS 0x00001000
Stefan Behrens56d140f2013-11-13 17:19:08 +0100138#define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE 0x00002000
Stefan Behrens5db02762011-11-01 17:04:16 +0100139
140struct btrfsic_dev_state;
141struct btrfsic_state;
142
143struct btrfsic_block {
144 u32 magic_num; /* only used for debug purposes */
145 unsigned int is_metadata:1; /* if it is meta-data, not data-data */
146 unsigned int is_superblock:1; /* if it is one of the superblocks */
147 unsigned int is_iodone:1; /* if is done by lower subsystem */
148 unsigned int iodone_w_error:1; /* error was indicated to endio */
149 unsigned int never_written:1; /* block was added because it was
150 * referenced, not because it was
151 * written */
Stefan Behrenscb3806e2012-11-27 16:10:21 +0000152 unsigned int mirror_num; /* large enough to hold
Stefan Behrens5db02762011-11-01 17:04:16 +0100153 * BTRFS_SUPER_MIRROR_MAX */
154 struct btrfsic_dev_state *dev_state;
155 u64 dev_bytenr; /* key, physical byte num on disk */
156 u64 logical_bytenr; /* logical byte num on disk */
157 u64 generation;
158 struct btrfs_disk_key disk_key; /* extra info to print in case of
159 * issues, will not always be correct */
160 struct list_head collision_resolving_node; /* list node */
161 struct list_head all_blocks_node; /* list node */
162
163 /* the following two lists contain block_link items */
164 struct list_head ref_to_list; /* list */
165 struct list_head ref_from_list; /* list */
166 struct btrfsic_block *next_in_same_bio;
167 void *orig_bio_bh_private;
168 union {
169 bio_end_io_t *bio;
170 bh_end_io_t *bh;
171 } orig_bio_bh_end_io;
172 int submit_bio_bh_rw;
173 u64 flush_gen; /* only valid if !never_written */
174};
175
176/*
177 * Elements of this type are allocated dynamically and required because
178 * each block object can refer to and can be ref from multiple blocks.
179 * The key to lookup them in the hashtable is the dev_bytenr of
180 * the block ref to plus the one from the block refered from.
181 * The fact that they are searchable via a hashtable and that a
182 * ref_cnt is maintained is not required for the btrfs integrity
183 * check algorithm itself, it is only used to make the output more
184 * beautiful in case that an error is detected (an error is defined
185 * as a write operation to a block while that block is still referenced).
186 */
187struct btrfsic_block_link {
188 u32 magic_num; /* only used for debug purposes */
189 u32 ref_cnt;
190 struct list_head node_ref_to; /* list node */
191 struct list_head node_ref_from; /* list node */
192 struct list_head collision_resolving_node; /* list node */
193 struct btrfsic_block *block_ref_to;
194 struct btrfsic_block *block_ref_from;
195 u64 parent_generation;
196};
197
198struct btrfsic_dev_state {
199 u32 magic_num; /* only used for debug purposes */
200 struct block_device *bdev;
201 struct btrfsic_state *state;
202 struct list_head collision_resolving_node; /* list node */
203 struct btrfsic_block dummy_block_for_bio_bh_flush;
204 u64 last_flush_gen;
205 char name[BDEVNAME_SIZE];
206};
207
208struct btrfsic_block_hashtable {
209 struct list_head table[BTRFSIC_BLOCK_HASHTABLE_SIZE];
210};
211
212struct btrfsic_block_link_hashtable {
213 struct list_head table[BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE];
214};
215
216struct btrfsic_dev_state_hashtable {
217 struct list_head table[BTRFSIC_DEV2STATE_HASHTABLE_SIZE];
218};
219
220struct btrfsic_block_data_ctx {
221 u64 start; /* virtual bytenr */
222 u64 dev_bytenr; /* physical bytenr on device */
223 u32 len;
224 struct btrfsic_dev_state *dev;
Stefan Behrense06baab2012-04-12 12:53:40 +0200225 char **datav;
226 struct page **pagev;
227 void *mem_to_free;
Stefan Behrens5db02762011-11-01 17:04:16 +0100228};
229
230/* This structure is used to implement recursion without occupying
231 * any stack space, refer to btrfsic_process_metablock() */
232struct btrfsic_stack_frame {
233 u32 magic;
234 u32 nr;
235 int error;
236 int i;
237 int limit_nesting;
238 int num_copies;
239 int mirror_num;
240 struct btrfsic_block *block;
241 struct btrfsic_block_data_ctx *block_ctx;
242 struct btrfsic_block *next_block;
243 struct btrfsic_block_data_ctx next_block_ctx;
244 struct btrfs_header *hdr;
245 struct btrfsic_stack_frame *prev;
246};
247
248/* Some state per mounted filesystem */
249struct btrfsic_state {
250 u32 print_mask;
251 int include_extent_data;
252 int csum_size;
253 struct list_head all_blocks_list;
254 struct btrfsic_block_hashtable block_hashtable;
255 struct btrfsic_block_link_hashtable block_link_hashtable;
256 struct btrfs_root *root;
257 u64 max_superblock_generation;
258 struct btrfsic_block *latest_superblock;
Stefan Behrense06baab2012-04-12 12:53:40 +0200259 u32 metablock_size;
260 u32 datablock_size;
Stefan Behrens5db02762011-11-01 17:04:16 +0100261};
262
263static void btrfsic_block_init(struct btrfsic_block *b);
264static struct btrfsic_block *btrfsic_block_alloc(void);
265static void btrfsic_block_free(struct btrfsic_block *b);
266static void btrfsic_block_link_init(struct btrfsic_block_link *n);
267static struct btrfsic_block_link *btrfsic_block_link_alloc(void);
268static void btrfsic_block_link_free(struct btrfsic_block_link *n);
269static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds);
270static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void);
271static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds);
272static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h);
273static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
274 struct btrfsic_block_hashtable *h);
275static void btrfsic_block_hashtable_remove(struct btrfsic_block *b);
276static struct btrfsic_block *btrfsic_block_hashtable_lookup(
277 struct block_device *bdev,
278 u64 dev_bytenr,
279 struct btrfsic_block_hashtable *h);
280static void btrfsic_block_link_hashtable_init(
281 struct btrfsic_block_link_hashtable *h);
282static void btrfsic_block_link_hashtable_add(
283 struct btrfsic_block_link *l,
284 struct btrfsic_block_link_hashtable *h);
285static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l);
286static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
287 struct block_device *bdev_ref_to,
288 u64 dev_bytenr_ref_to,
289 struct block_device *bdev_ref_from,
290 u64 dev_bytenr_ref_from,
291 struct btrfsic_block_link_hashtable *h);
292static void btrfsic_dev_state_hashtable_init(
293 struct btrfsic_dev_state_hashtable *h);
294static void btrfsic_dev_state_hashtable_add(
295 struct btrfsic_dev_state *ds,
296 struct btrfsic_dev_state_hashtable *h);
297static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds);
298static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(
299 struct block_device *bdev,
300 struct btrfsic_dev_state_hashtable *h);
301static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void);
302static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf);
303static int btrfsic_process_superblock(struct btrfsic_state *state,
304 struct btrfs_fs_devices *fs_devices);
305static int btrfsic_process_metablock(struct btrfsic_state *state,
306 struct btrfsic_block *block,
307 struct btrfsic_block_data_ctx *block_ctx,
Stefan Behrens5db02762011-11-01 17:04:16 +0100308 int limit_nesting, int force_iodone_flag);
Stefan Behrense06baab2012-04-12 12:53:40 +0200309static void btrfsic_read_from_block_data(
310 struct btrfsic_block_data_ctx *block_ctx,
311 void *dst, u32 offset, size_t len);
Stefan Behrens5db02762011-11-01 17:04:16 +0100312static 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);
324static 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);
328static 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);
Stefan Behrens5db02762011-11-01 17:04:16 +0100331static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx);
332static int btrfsic_read_block(struct btrfsic_state *state,
333 struct btrfsic_block_data_ctx *block_ctx);
334static void btrfsic_dump_database(struct btrfsic_state *state);
335static int btrfsic_test_for_metadata(struct btrfsic_state *state,
Stefan Behrense06baab2012-04-12 12:53:40 +0200336 char **datav, unsigned int num_pages);
Stefan Behrens5db02762011-11-01 17:04:16 +0100337static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
Stefan Behrense06baab2012-04-12 12:53:40 +0200338 u64 dev_bytenr, char **mapped_datav,
339 unsigned int num_pages,
340 struct bio *bio, int *bio_is_patched,
Stefan Behrens5db02762011-11-01 17:04:16 +0100341 struct buffer_head *bh,
342 int submit_bio_bh_rw);
343static int btrfsic_process_written_superblock(
344 struct btrfsic_state *state,
345 struct btrfsic_block *const block,
346 struct btrfs_super_block *const super_hdr);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200347static void btrfsic_bio_end_io(struct bio *bp);
Stefan Behrens5db02762011-11-01 17:04:16 +0100348static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate);
349static int btrfsic_is_block_ref_by_superblock(const struct btrfsic_state *state,
350 const struct btrfsic_block *block,
351 int recursion_level);
352static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
353 struct btrfsic_block *const block,
354 int recursion_level);
355static void btrfsic_print_add_link(const struct btrfsic_state *state,
356 const struct btrfsic_block_link *l);
357static void btrfsic_print_rem_link(const struct btrfsic_state *state,
358 const struct btrfsic_block_link *l);
359static char btrfsic_get_block_type(const struct btrfsic_state *state,
360 const struct btrfsic_block *block);
361static void btrfsic_dump_tree(const struct btrfsic_state *state);
362static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
363 const struct btrfsic_block *block,
364 int indent_level);
365static 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);
371static 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);
380static 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);
387static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
388 struct block_device *bdev);
389static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
390 u64 bytenr,
391 struct btrfsic_dev_state *dev_state,
Stefan Behrense06baab2012-04-12 12:53:40 +0200392 u64 dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +0100393
394static struct mutex btrfsic_mutex;
395static int btrfsic_is_initialized;
396static struct btrfsic_dev_state_hashtable btrfsic_dev_state_hashtable;
397
398
399static void btrfsic_block_init(struct btrfsic_block *b)
400{
401 b->magic_num = BTRFSIC_BLOCK_MAGIC_NUMBER;
402 b->dev_state = NULL;
403 b->dev_bytenr = 0;
404 b->logical_bytenr = 0;
405 b->generation = BTRFSIC_GENERATION_UNKNOWN;
406 b->disk_key.objectid = 0;
407 b->disk_key.type = 0;
408 b->disk_key.offset = 0;
409 b->is_metadata = 0;
410 b->is_superblock = 0;
411 b->is_iodone = 0;
412 b->iodone_w_error = 0;
413 b->never_written = 0;
414 b->mirror_num = 0;
415 b->next_in_same_bio = NULL;
416 b->orig_bio_bh_private = NULL;
417 b->orig_bio_bh_end_io.bio = NULL;
418 INIT_LIST_HEAD(&b->collision_resolving_node);
419 INIT_LIST_HEAD(&b->all_blocks_node);
420 INIT_LIST_HEAD(&b->ref_to_list);
421 INIT_LIST_HEAD(&b->ref_from_list);
422 b->submit_bio_bh_rw = 0;
423 b->flush_gen = 0;
424}
425
426static struct btrfsic_block *btrfsic_block_alloc(void)
427{
428 struct btrfsic_block *b;
429
430 b = kzalloc(sizeof(*b), GFP_NOFS);
431 if (NULL != b)
432 btrfsic_block_init(b);
433
434 return b;
435}
436
437static void btrfsic_block_free(struct btrfsic_block *b)
438{
439 BUG_ON(!(NULL == b || BTRFSIC_BLOCK_MAGIC_NUMBER == b->magic_num));
440 kfree(b);
441}
442
443static void btrfsic_block_link_init(struct btrfsic_block_link *l)
444{
445 l->magic_num = BTRFSIC_BLOCK_LINK_MAGIC_NUMBER;
446 l->ref_cnt = 1;
447 INIT_LIST_HEAD(&l->node_ref_to);
448 INIT_LIST_HEAD(&l->node_ref_from);
449 INIT_LIST_HEAD(&l->collision_resolving_node);
450 l->block_ref_to = NULL;
451 l->block_ref_from = NULL;
452}
453
454static struct btrfsic_block_link *btrfsic_block_link_alloc(void)
455{
456 struct btrfsic_block_link *l;
457
458 l = kzalloc(sizeof(*l), GFP_NOFS);
459 if (NULL != l)
460 btrfsic_block_link_init(l);
461
462 return l;
463}
464
465static void btrfsic_block_link_free(struct btrfsic_block_link *l)
466{
467 BUG_ON(!(NULL == l || BTRFSIC_BLOCK_LINK_MAGIC_NUMBER == l->magic_num));
468 kfree(l);
469}
470
471static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds)
472{
473 ds->magic_num = BTRFSIC_DEV2STATE_MAGIC_NUMBER;
474 ds->bdev = NULL;
475 ds->state = NULL;
476 ds->name[0] = '\0';
477 INIT_LIST_HEAD(&ds->collision_resolving_node);
478 ds->last_flush_gen = 0;
479 btrfsic_block_init(&ds->dummy_block_for_bio_bh_flush);
480 ds->dummy_block_for_bio_bh_flush.is_iodone = 1;
481 ds->dummy_block_for_bio_bh_flush.dev_state = ds;
482}
483
484static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void)
485{
486 struct btrfsic_dev_state *ds;
487
488 ds = kzalloc(sizeof(*ds), GFP_NOFS);
489 if (NULL != ds)
490 btrfsic_dev_state_init(ds);
491
492 return ds;
493}
494
495static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds)
496{
497 BUG_ON(!(NULL == ds ||
498 BTRFSIC_DEV2STATE_MAGIC_NUMBER == ds->magic_num));
499 kfree(ds);
500}
501
502static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h)
503{
504 int i;
505
506 for (i = 0; i < BTRFSIC_BLOCK_HASHTABLE_SIZE; i++)
507 INIT_LIST_HEAD(h->table + i);
508}
509
510static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
511 struct btrfsic_block_hashtable *h)
512{
513 const unsigned int hashval =
514 (((unsigned int)(b->dev_bytenr >> 16)) ^
515 ((unsigned int)((uintptr_t)b->dev_state->bdev))) &
516 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
517
518 list_add(&b->collision_resolving_node, h->table + hashval);
519}
520
521static void btrfsic_block_hashtable_remove(struct btrfsic_block *b)
522{
523 list_del(&b->collision_resolving_node);
524}
525
526static struct btrfsic_block *btrfsic_block_hashtable_lookup(
527 struct block_device *bdev,
528 u64 dev_bytenr,
529 struct btrfsic_block_hashtable *h)
530{
531 const unsigned int hashval =
532 (((unsigned int)(dev_bytenr >> 16)) ^
533 ((unsigned int)((uintptr_t)bdev))) &
534 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
Geliang Tangb69f2be2015-12-18 22:16:59 +0800535 struct btrfsic_block *b;
Stefan Behrens5db02762011-11-01 17:04:16 +0100536
Geliang Tangb69f2be2015-12-18 22:16:59 +0800537 list_for_each_entry(b, h->table + hashval, collision_resolving_node) {
Stefan Behrens5db02762011-11-01 17:04:16 +0100538 if (b->dev_state->bdev == bdev && b->dev_bytenr == dev_bytenr)
539 return b;
540 }
541
542 return NULL;
543}
544
545static void btrfsic_block_link_hashtable_init(
546 struct btrfsic_block_link_hashtable *h)
547{
548 int i;
549
550 for (i = 0; i < BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE; i++)
551 INIT_LIST_HEAD(h->table + i);
552}
553
554static void btrfsic_block_link_hashtable_add(
555 struct btrfsic_block_link *l,
556 struct btrfsic_block_link_hashtable *h)
557{
558 const unsigned int hashval =
559 (((unsigned int)(l->block_ref_to->dev_bytenr >> 16)) ^
560 ((unsigned int)(l->block_ref_from->dev_bytenr >> 16)) ^
561 ((unsigned int)((uintptr_t)l->block_ref_to->dev_state->bdev)) ^
562 ((unsigned int)((uintptr_t)l->block_ref_from->dev_state->bdev)))
563 & (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
564
565 BUG_ON(NULL == l->block_ref_to);
566 BUG_ON(NULL == l->block_ref_from);
567 list_add(&l->collision_resolving_node, h->table + hashval);
568}
569
570static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l)
571{
572 list_del(&l->collision_resolving_node);
573}
574
575static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
576 struct block_device *bdev_ref_to,
577 u64 dev_bytenr_ref_to,
578 struct block_device *bdev_ref_from,
579 u64 dev_bytenr_ref_from,
580 struct btrfsic_block_link_hashtable *h)
581{
582 const unsigned int hashval =
583 (((unsigned int)(dev_bytenr_ref_to >> 16)) ^
584 ((unsigned int)(dev_bytenr_ref_from >> 16)) ^
585 ((unsigned int)((uintptr_t)bdev_ref_to)) ^
586 ((unsigned int)((uintptr_t)bdev_ref_from))) &
587 (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
Geliang Tangb69f2be2015-12-18 22:16:59 +0800588 struct btrfsic_block_link *l;
Stefan Behrens5db02762011-11-01 17:04:16 +0100589
Geliang Tangb69f2be2015-12-18 22:16:59 +0800590 list_for_each_entry(l, h->table + hashval, collision_resolving_node) {
Stefan Behrens5db02762011-11-01 17:04:16 +0100591 BUG_ON(NULL == l->block_ref_to);
592 BUG_ON(NULL == l->block_ref_from);
593 if (l->block_ref_to->dev_state->bdev == bdev_ref_to &&
594 l->block_ref_to->dev_bytenr == dev_bytenr_ref_to &&
595 l->block_ref_from->dev_state->bdev == bdev_ref_from &&
596 l->block_ref_from->dev_bytenr == dev_bytenr_ref_from)
597 return l;
598 }
599
600 return NULL;
601}
602
603static void btrfsic_dev_state_hashtable_init(
604 struct btrfsic_dev_state_hashtable *h)
605{
606 int i;
607
608 for (i = 0; i < BTRFSIC_DEV2STATE_HASHTABLE_SIZE; i++)
609 INIT_LIST_HEAD(h->table + i);
610}
611
612static void btrfsic_dev_state_hashtable_add(
613 struct btrfsic_dev_state *ds,
614 struct btrfsic_dev_state_hashtable *h)
615{
616 const unsigned int hashval =
617 (((unsigned int)((uintptr_t)ds->bdev)) &
618 (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
619
620 list_add(&ds->collision_resolving_node, h->table + hashval);
621}
622
623static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds)
624{
625 list_del(&ds->collision_resolving_node);
626}
627
628static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(
629 struct block_device *bdev,
630 struct btrfsic_dev_state_hashtable *h)
631{
632 const unsigned int hashval =
633 (((unsigned int)((uintptr_t)bdev)) &
634 (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
Geliang Tangb69f2be2015-12-18 22:16:59 +0800635 struct btrfsic_dev_state *ds;
Stefan Behrens5db02762011-11-01 17:04:16 +0100636
Geliang Tangb69f2be2015-12-18 22:16:59 +0800637 list_for_each_entry(ds, h->table + hashval, collision_resolving_node) {
Stefan Behrens5db02762011-11-01 17:04:16 +0100638 if (ds->bdev == bdev)
639 return ds;
640 }
641
642 return NULL;
643}
644
645static int btrfsic_process_superblock(struct btrfsic_state *state,
646 struct btrfs_fs_devices *fs_devices)
647{
Chris Masone77266e2012-02-24 10:39:05 -0500648 int ret = 0;
Stefan Behrens5db02762011-11-01 17:04:16 +0100649 struct btrfs_super_block *selected_super;
650 struct list_head *dev_head = &fs_devices->devices;
651 struct btrfs_device *device;
652 struct btrfsic_dev_state *selected_dev_state = NULL;
653 int pass;
654
655 BUG_ON(NULL == state);
Stefan Behrense06baab2012-04-12 12:53:40 +0200656 selected_super = kzalloc(sizeof(*selected_super), GFP_NOFS);
Stefan Behrens5db02762011-11-01 17:04:16 +0100657 if (NULL == selected_super) {
658 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
Luis de Bethencourt0b8d8ce2015-10-20 14:56:22 +0100659 return -ENOMEM;
Stefan Behrens5db02762011-11-01 17:04:16 +0100660 }
661
662 list_for_each_entry(device, dev_head, dev_list) {
663 int i;
664 struct btrfsic_dev_state *dev_state;
665
666 if (!device->bdev || !device->name)
667 continue;
668
669 dev_state = btrfsic_dev_state_lookup(device->bdev);
670 BUG_ON(NULL == dev_state);
671 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
672 ret = btrfsic_process_superblock_dev_mirror(
673 state, dev_state, device, i,
674 &selected_dev_state, selected_super);
675 if (0 != ret && 0 == i) {
676 kfree(selected_super);
677 return ret;
678 }
679 }
680 }
681
682 if (NULL == state->latest_superblock) {
683 printk(KERN_INFO "btrfsic: no superblock found!\n");
684 kfree(selected_super);
685 return -1;
686 }
687
688 state->csum_size = btrfs_super_csum_size(selected_super);
689
690 for (pass = 0; pass < 3; pass++) {
691 int num_copies;
692 int mirror_num;
693 u64 next_bytenr;
694
695 switch (pass) {
696 case 0:
697 next_bytenr = btrfs_super_root(selected_super);
698 if (state->print_mask &
699 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200700 printk(KERN_INFO "root@%llu\n", next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +0100701 break;
702 case 1:
703 next_bytenr = btrfs_super_chunk_root(selected_super);
704 if (state->print_mask &
705 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200706 printk(KERN_INFO "chunk@%llu\n", next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +0100707 break;
708 case 2:
709 next_bytenr = btrfs_super_log_root(selected_super);
710 if (0 == next_bytenr)
711 continue;
712 if (state->print_mask &
713 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200714 printk(KERN_INFO "log@%llu\n", next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +0100715 break;
716 }
717
718 num_copies =
Stefan Behrens5d964052012-11-05 14:59:07 +0100719 btrfs_num_copies(state->root->fs_info,
Stefan Behrense06baab2012-04-12 12:53:40 +0200720 next_bytenr, state->metablock_size);
Stefan Behrens5db02762011-11-01 17:04:16 +0100721 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
722 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200723 next_bytenr, num_copies);
Stefan Behrens5db02762011-11-01 17:04:16 +0100724
725 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
726 struct btrfsic_block *next_block;
727 struct btrfsic_block_data_ctx tmp_next_block_ctx;
728 struct btrfsic_block_link *l;
Stefan Behrens5db02762011-11-01 17:04:16 +0100729
Stefan Behrense06baab2012-04-12 12:53:40 +0200730 ret = btrfsic_map_block(state, next_bytenr,
731 state->metablock_size,
Stefan Behrens5db02762011-11-01 17:04:16 +0100732 &tmp_next_block_ctx,
733 mirror_num);
734 if (ret) {
735 printk(KERN_INFO "btrfsic:"
736 " btrfsic_map_block(root @%llu,"
737 " mirror %d) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200738 next_bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +0100739 kfree(selected_super);
740 return -1;
741 }
742
743 next_block = btrfsic_block_hashtable_lookup(
744 tmp_next_block_ctx.dev->bdev,
745 tmp_next_block_ctx.dev_bytenr,
746 &state->block_hashtable);
747 BUG_ON(NULL == next_block);
748
749 l = btrfsic_block_link_hashtable_lookup(
750 tmp_next_block_ctx.dev->bdev,
751 tmp_next_block_ctx.dev_bytenr,
752 state->latest_superblock->dev_state->
753 bdev,
754 state->latest_superblock->dev_bytenr,
755 &state->block_link_hashtable);
756 BUG_ON(NULL == l);
757
758 ret = btrfsic_read_block(state, &tmp_next_block_ctx);
Stefan Behrense06baab2012-04-12 12:53:40 +0200759 if (ret < (int)PAGE_CACHE_SIZE) {
Stefan Behrens5db02762011-11-01 17:04:16 +0100760 printk(KERN_INFO
761 "btrfsic: read @logical %llu failed!\n",
Stefan Behrens5db02762011-11-01 17:04:16 +0100762 tmp_next_block_ctx.start);
763 btrfsic_release_block_ctx(&tmp_next_block_ctx);
764 kfree(selected_super);
765 return -1;
766 }
767
Stefan Behrens5db02762011-11-01 17:04:16 +0100768 ret = btrfsic_process_metablock(state,
769 next_block,
770 &tmp_next_block_ctx,
Stefan Behrens5db02762011-11-01 17:04:16 +0100771 BTRFS_MAX_LEVEL + 3, 1);
772 btrfsic_release_block_ctx(&tmp_next_block_ctx);
773 }
774 }
775
776 kfree(selected_super);
777 return ret;
778}
779
780static int btrfsic_process_superblock_dev_mirror(
781 struct btrfsic_state *state,
782 struct btrfsic_dev_state *dev_state,
783 struct btrfs_device *device,
784 int superblock_mirror_num,
785 struct btrfsic_dev_state **selected_dev_state,
786 struct btrfs_super_block *selected_super)
787{
788 struct btrfs_super_block *super_tmp;
789 u64 dev_bytenr;
790 struct buffer_head *bh;
791 struct btrfsic_block *superblock_tmp;
792 int pass;
793 struct block_device *const superblock_bdev = device->bdev;
794
795 /* super block bytenr is always the unmapped device bytenr */
796 dev_bytenr = btrfs_sb_offset(superblock_mirror_num);
Miao Xie935e5cc2014-09-03 21:35:33 +0800797 if (dev_bytenr + BTRFS_SUPER_INFO_SIZE > device->commit_total_bytes)
Stefan Behrense06baab2012-04-12 12:53:40 +0200798 return -1;
799 bh = __bread(superblock_bdev, dev_bytenr / 4096,
800 BTRFS_SUPER_INFO_SIZE);
Stefan Behrens5db02762011-11-01 17:04:16 +0100801 if (NULL == bh)
802 return -1;
803 super_tmp = (struct btrfs_super_block *)
804 (bh->b_data + (dev_bytenr & 4095));
805
806 if (btrfs_super_bytenr(super_tmp) != dev_bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800807 btrfs_super_magic(super_tmp) != BTRFS_MAGIC ||
Stefan Behrense06baab2012-04-12 12:53:40 +0200808 memcmp(device->uuid, super_tmp->dev_item.uuid, BTRFS_UUID_SIZE) ||
809 btrfs_super_nodesize(super_tmp) != state->metablock_size ||
Stefan Behrense06baab2012-04-12 12:53:40 +0200810 btrfs_super_sectorsize(super_tmp) != state->datablock_size) {
Stefan Behrens5db02762011-11-01 17:04:16 +0100811 brelse(bh);
812 return 0;
813 }
814
815 superblock_tmp =
816 btrfsic_block_hashtable_lookup(superblock_bdev,
817 dev_bytenr,
818 &state->block_hashtable);
819 if (NULL == superblock_tmp) {
820 superblock_tmp = btrfsic_block_alloc();
821 if (NULL == superblock_tmp) {
822 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
823 brelse(bh);
824 return -1;
825 }
826 /* for superblock, only the dev_bytenr makes sense */
827 superblock_tmp->dev_bytenr = dev_bytenr;
828 superblock_tmp->dev_state = dev_state;
829 superblock_tmp->logical_bytenr = dev_bytenr;
830 superblock_tmp->generation = btrfs_super_generation(super_tmp);
831 superblock_tmp->is_metadata = 1;
832 superblock_tmp->is_superblock = 1;
833 superblock_tmp->is_iodone = 1;
834 superblock_tmp->never_written = 0;
835 superblock_tmp->mirror_num = 1 + superblock_mirror_num;
836 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
David Sterbaecaeb142015-10-08 09:01:03 +0200837 btrfs_info_in_rcu(device->dev_root->fs_info,
838 "new initial S-block (bdev %p, %s) @%llu (%s/%llu/%d)",
Josef Bacik606686e2012-06-04 14:03:51 -0400839 superblock_bdev,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200840 rcu_str_deref(device->name), dev_bytenr,
841 dev_state->name, dev_bytenr,
Josef Bacik606686e2012-06-04 14:03:51 -0400842 superblock_mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +0100843 list_add(&superblock_tmp->all_blocks_node,
844 &state->all_blocks_list);
845 btrfsic_block_hashtable_add(superblock_tmp,
846 &state->block_hashtable);
847 }
848
849 /* select the one with the highest generation field */
850 if (btrfs_super_generation(super_tmp) >
851 state->max_superblock_generation ||
852 0 == state->max_superblock_generation) {
853 memcpy(selected_super, super_tmp, sizeof(*selected_super));
854 *selected_dev_state = dev_state;
855 state->max_superblock_generation =
856 btrfs_super_generation(super_tmp);
857 state->latest_superblock = superblock_tmp;
858 }
859
860 for (pass = 0; pass < 3; pass++) {
861 u64 next_bytenr;
862 int num_copies;
863 int mirror_num;
864 const char *additional_string = NULL;
865 struct btrfs_disk_key tmp_disk_key;
866
867 tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
868 tmp_disk_key.offset = 0;
869 switch (pass) {
870 case 0:
Qu Wenruo3cae2102013-07-16 11:19:18 +0800871 btrfs_set_disk_key_objectid(&tmp_disk_key,
872 BTRFS_ROOT_TREE_OBJECTID);
Stefan Behrens5db02762011-11-01 17:04:16 +0100873 additional_string = "initial root ";
874 next_bytenr = btrfs_super_root(super_tmp);
875 break;
876 case 1:
Qu Wenruo3cae2102013-07-16 11:19:18 +0800877 btrfs_set_disk_key_objectid(&tmp_disk_key,
878 BTRFS_CHUNK_TREE_OBJECTID);
Stefan Behrens5db02762011-11-01 17:04:16 +0100879 additional_string = "initial chunk ";
880 next_bytenr = btrfs_super_chunk_root(super_tmp);
881 break;
882 case 2:
Qu Wenruo3cae2102013-07-16 11:19:18 +0800883 btrfs_set_disk_key_objectid(&tmp_disk_key,
884 BTRFS_TREE_LOG_OBJECTID);
Stefan Behrens5db02762011-11-01 17:04:16 +0100885 additional_string = "initial log ";
886 next_bytenr = btrfs_super_log_root(super_tmp);
887 if (0 == next_bytenr)
888 continue;
889 break;
890 }
891
892 num_copies =
Stefan Behrens5d964052012-11-05 14:59:07 +0100893 btrfs_num_copies(state->root->fs_info,
Stefan Behrense06baab2012-04-12 12:53:40 +0200894 next_bytenr, state->metablock_size);
Stefan Behrens5db02762011-11-01 17:04:16 +0100895 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
896 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200897 next_bytenr, num_copies);
Stefan Behrens5db02762011-11-01 17:04:16 +0100898 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
899 struct btrfsic_block *next_block;
900 struct btrfsic_block_data_ctx tmp_next_block_ctx;
901 struct btrfsic_block_link *l;
902
Stefan Behrense06baab2012-04-12 12:53:40 +0200903 if (btrfsic_map_block(state, next_bytenr,
904 state->metablock_size,
Stefan Behrens5db02762011-11-01 17:04:16 +0100905 &tmp_next_block_ctx,
906 mirror_num)) {
907 printk(KERN_INFO "btrfsic: btrfsic_map_block("
908 "bytenr @%llu, mirror %d) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200909 next_bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +0100910 brelse(bh);
911 return -1;
912 }
913
914 next_block = btrfsic_block_lookup_or_add(
915 state, &tmp_next_block_ctx,
916 additional_string, 1, 1, 0,
917 mirror_num, NULL);
918 if (NULL == next_block) {
919 btrfsic_release_block_ctx(&tmp_next_block_ctx);
920 brelse(bh);
921 return -1;
922 }
923
924 next_block->disk_key = tmp_disk_key;
925 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
926 l = btrfsic_block_link_lookup_or_add(
927 state, &tmp_next_block_ctx,
928 next_block, superblock_tmp,
929 BTRFSIC_GENERATION_UNKNOWN);
930 btrfsic_release_block_ctx(&tmp_next_block_ctx);
931 if (NULL == l) {
932 brelse(bh);
933 return -1;
934 }
935 }
936 }
937 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES)
938 btrfsic_dump_tree_sub(state, superblock_tmp, 0);
939
940 brelse(bh);
941 return 0;
942}
943
944static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void)
945{
946 struct btrfsic_stack_frame *sf;
947
948 sf = kzalloc(sizeof(*sf), GFP_NOFS);
949 if (NULL == sf)
950 printk(KERN_INFO "btrfsic: alloc memory failed!\n");
951 else
952 sf->magic = BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER;
953 return sf;
954}
955
956static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf)
957{
958 BUG_ON(!(NULL == sf ||
959 BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER == sf->magic));
960 kfree(sf);
961}
962
963static int btrfsic_process_metablock(
964 struct btrfsic_state *state,
965 struct btrfsic_block *const first_block,
966 struct btrfsic_block_data_ctx *const first_block_ctx,
Stefan Behrens5db02762011-11-01 17:04:16 +0100967 int first_limit_nesting, int force_iodone_flag)
968{
969 struct btrfsic_stack_frame initial_stack_frame = { 0 };
970 struct btrfsic_stack_frame *sf;
971 struct btrfsic_stack_frame *next_stack;
Stefan Behrense06baab2012-04-12 12:53:40 +0200972 struct btrfs_header *const first_hdr =
973 (struct btrfs_header *)first_block_ctx->datav[0];
Stefan Behrens5db02762011-11-01 17:04:16 +0100974
Stefan Behrense06baab2012-04-12 12:53:40 +0200975 BUG_ON(!first_hdr);
Stefan Behrens5db02762011-11-01 17:04:16 +0100976 sf = &initial_stack_frame;
977 sf->error = 0;
978 sf->i = -1;
979 sf->limit_nesting = first_limit_nesting;
980 sf->block = first_block;
981 sf->block_ctx = first_block_ctx;
982 sf->next_block = NULL;
983 sf->hdr = first_hdr;
984 sf->prev = NULL;
985
986continue_with_new_stack_frame:
987 sf->block->generation = le64_to_cpu(sf->hdr->generation);
988 if (0 == sf->hdr->level) {
989 struct btrfs_leaf *const leafhdr =
990 (struct btrfs_leaf *)sf->hdr;
991
992 if (-1 == sf->i) {
Qu Wenruo3cae2102013-07-16 11:19:18 +0800993 sf->nr = btrfs_stack_header_nritems(&leafhdr->header);
Stefan Behrens5db02762011-11-01 17:04:16 +0100994
995 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
996 printk(KERN_INFO
997 "leaf %llu items %d generation %llu"
998 " owner %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200999 sf->block_ctx->start, sf->nr,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001000 btrfs_stack_header_generation(
1001 &leafhdr->header),
Qu Wenruo3cae2102013-07-16 11:19:18 +08001002 btrfs_stack_header_owner(
1003 &leafhdr->header));
Stefan Behrens5db02762011-11-01 17:04:16 +01001004 }
1005
1006continue_with_current_leaf_stack_frame:
1007 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1008 sf->i++;
1009 sf->num_copies = 0;
1010 }
1011
1012 if (sf->i < sf->nr) {
Stefan Behrense06baab2012-04-12 12:53:40 +02001013 struct btrfs_item disk_item;
1014 u32 disk_item_offset =
1015 (uintptr_t)(leafhdr->items + sf->i) -
1016 (uintptr_t)leafhdr;
1017 struct btrfs_disk_key *disk_key;
Stefan Behrens5db02762011-11-01 17:04:16 +01001018 u8 type;
Stefan Behrense06baab2012-04-12 12:53:40 +02001019 u32 item_offset;
Alexander Block8ea05e32012-07-25 17:35:53 +02001020 u32 item_size;
Stefan Behrens5db02762011-11-01 17:04:16 +01001021
Stefan Behrense06baab2012-04-12 12:53:40 +02001022 if (disk_item_offset + sizeof(struct btrfs_item) >
1023 sf->block_ctx->len) {
1024leaf_item_out_of_bounce_error:
1025 printk(KERN_INFO
1026 "btrfsic: leaf item out of bounce at logical %llu, dev %s\n",
1027 sf->block_ctx->start,
1028 sf->block_ctx->dev->name);
1029 goto one_stack_frame_backwards;
1030 }
1031 btrfsic_read_from_block_data(sf->block_ctx,
1032 &disk_item,
1033 disk_item_offset,
1034 sizeof(struct btrfs_item));
Qu Wenruo3cae2102013-07-16 11:19:18 +08001035 item_offset = btrfs_stack_item_offset(&disk_item);
Stefan Behrensa5f519c2013-10-21 14:18:17 +02001036 item_size = btrfs_stack_item_size(&disk_item);
Stefan Behrense06baab2012-04-12 12:53:40 +02001037 disk_key = &disk_item.key;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001038 type = btrfs_disk_key_type(disk_key);
Stefan Behrens5db02762011-11-01 17:04:16 +01001039
1040 if (BTRFS_ROOT_ITEM_KEY == type) {
Stefan Behrense06baab2012-04-12 12:53:40 +02001041 struct btrfs_root_item root_item;
1042 u32 root_item_offset;
1043 u64 next_bytenr;
1044
1045 root_item_offset = item_offset +
1046 offsetof(struct btrfs_leaf, items);
Alexander Block8ea05e32012-07-25 17:35:53 +02001047 if (root_item_offset + item_size >
Stefan Behrense06baab2012-04-12 12:53:40 +02001048 sf->block_ctx->len)
1049 goto leaf_item_out_of_bounce_error;
1050 btrfsic_read_from_block_data(
1051 sf->block_ctx, &root_item,
1052 root_item_offset,
Alexander Block8ea05e32012-07-25 17:35:53 +02001053 item_size);
Qu Wenruo3cae2102013-07-16 11:19:18 +08001054 next_bytenr = btrfs_root_bytenr(&root_item);
Stefan Behrens5db02762011-11-01 17:04:16 +01001055
1056 sf->error =
1057 btrfsic_create_link_to_next_block(
1058 state,
1059 sf->block,
1060 sf->block_ctx,
1061 next_bytenr,
1062 sf->limit_nesting,
1063 &sf->next_block_ctx,
1064 &sf->next_block,
1065 force_iodone_flag,
1066 &sf->num_copies,
1067 &sf->mirror_num,
1068 disk_key,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001069 btrfs_root_generation(
1070 &root_item));
Stefan Behrens5db02762011-11-01 17:04:16 +01001071 if (sf->error)
1072 goto one_stack_frame_backwards;
1073
1074 if (NULL != sf->next_block) {
1075 struct btrfs_header *const next_hdr =
1076 (struct btrfs_header *)
Stefan Behrense06baab2012-04-12 12:53:40 +02001077 sf->next_block_ctx.datav[0];
Stefan Behrens5db02762011-11-01 17:04:16 +01001078
1079 next_stack =
1080 btrfsic_stack_frame_alloc();
1081 if (NULL == next_stack) {
Stefan Behrens98806b42014-05-09 15:28:07 +02001082 sf->error = -1;
Stefan Behrens5db02762011-11-01 17:04:16 +01001083 btrfsic_release_block_ctx(
1084 &sf->
1085 next_block_ctx);
1086 goto one_stack_frame_backwards;
1087 }
1088
1089 next_stack->i = -1;
1090 next_stack->block = sf->next_block;
1091 next_stack->block_ctx =
1092 &sf->next_block_ctx;
1093 next_stack->next_block = NULL;
1094 next_stack->hdr = next_hdr;
1095 next_stack->limit_nesting =
1096 sf->limit_nesting - 1;
1097 next_stack->prev = sf;
1098 sf = next_stack;
1099 goto continue_with_new_stack_frame;
1100 }
1101 } else if (BTRFS_EXTENT_DATA_KEY == type &&
1102 state->include_extent_data) {
1103 sf->error = btrfsic_handle_extent_data(
1104 state,
1105 sf->block,
1106 sf->block_ctx,
1107 item_offset,
1108 force_iodone_flag);
1109 if (sf->error)
1110 goto one_stack_frame_backwards;
1111 }
1112
1113 goto continue_with_current_leaf_stack_frame;
1114 }
1115 } else {
1116 struct btrfs_node *const nodehdr = (struct btrfs_node *)sf->hdr;
1117
1118 if (-1 == sf->i) {
Qu Wenruo3cae2102013-07-16 11:19:18 +08001119 sf->nr = btrfs_stack_header_nritems(&nodehdr->header);
Stefan Behrens5db02762011-11-01 17:04:16 +01001120
1121 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1122 printk(KERN_INFO "node %llu level %d items %d"
1123 " generation %llu owner %llu\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01001124 sf->block_ctx->start,
1125 nodehdr->header.level, sf->nr,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001126 btrfs_stack_header_generation(
1127 &nodehdr->header),
Qu Wenruo3cae2102013-07-16 11:19:18 +08001128 btrfs_stack_header_owner(
1129 &nodehdr->header));
Stefan Behrens5db02762011-11-01 17:04:16 +01001130 }
1131
1132continue_with_current_node_stack_frame:
1133 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1134 sf->i++;
1135 sf->num_copies = 0;
1136 }
1137
1138 if (sf->i < sf->nr) {
Stefan Behrense06baab2012-04-12 12:53:40 +02001139 struct btrfs_key_ptr key_ptr;
1140 u32 key_ptr_offset;
1141 u64 next_bytenr;
1142
1143 key_ptr_offset = (uintptr_t)(nodehdr->ptrs + sf->i) -
1144 (uintptr_t)nodehdr;
1145 if (key_ptr_offset + sizeof(struct btrfs_key_ptr) >
1146 sf->block_ctx->len) {
1147 printk(KERN_INFO
1148 "btrfsic: node item out of bounce at logical %llu, dev %s\n",
1149 sf->block_ctx->start,
1150 sf->block_ctx->dev->name);
1151 goto one_stack_frame_backwards;
1152 }
1153 btrfsic_read_from_block_data(
1154 sf->block_ctx, &key_ptr, key_ptr_offset,
1155 sizeof(struct btrfs_key_ptr));
Qu Wenruo3cae2102013-07-16 11:19:18 +08001156 next_bytenr = btrfs_stack_key_blockptr(&key_ptr);
Stefan Behrens5db02762011-11-01 17:04:16 +01001157
1158 sf->error = btrfsic_create_link_to_next_block(
1159 state,
1160 sf->block,
1161 sf->block_ctx,
1162 next_bytenr,
1163 sf->limit_nesting,
1164 &sf->next_block_ctx,
1165 &sf->next_block,
1166 force_iodone_flag,
1167 &sf->num_copies,
1168 &sf->mirror_num,
Stefan Behrense06baab2012-04-12 12:53:40 +02001169 &key_ptr.key,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001170 btrfs_stack_key_generation(&key_ptr));
Stefan Behrens5db02762011-11-01 17:04:16 +01001171 if (sf->error)
1172 goto one_stack_frame_backwards;
1173
1174 if (NULL != sf->next_block) {
1175 struct btrfs_header *const next_hdr =
1176 (struct btrfs_header *)
Stefan Behrense06baab2012-04-12 12:53:40 +02001177 sf->next_block_ctx.datav[0];
Stefan Behrens5db02762011-11-01 17:04:16 +01001178
1179 next_stack = btrfsic_stack_frame_alloc();
Stefan Behrens98806b42014-05-09 15:28:07 +02001180 if (NULL == next_stack) {
1181 sf->error = -1;
Stefan Behrens5db02762011-11-01 17:04:16 +01001182 goto one_stack_frame_backwards;
Stefan Behrens98806b42014-05-09 15:28:07 +02001183 }
Stefan Behrens5db02762011-11-01 17:04:16 +01001184
1185 next_stack->i = -1;
1186 next_stack->block = sf->next_block;
1187 next_stack->block_ctx = &sf->next_block_ctx;
1188 next_stack->next_block = NULL;
1189 next_stack->hdr = next_hdr;
1190 next_stack->limit_nesting =
1191 sf->limit_nesting - 1;
1192 next_stack->prev = sf;
1193 sf = next_stack;
1194 goto continue_with_new_stack_frame;
1195 }
1196
1197 goto continue_with_current_node_stack_frame;
1198 }
1199 }
1200
1201one_stack_frame_backwards:
1202 if (NULL != sf->prev) {
1203 struct btrfsic_stack_frame *const prev = sf->prev;
1204
1205 /* the one for the initial block is freed in the caller */
1206 btrfsic_release_block_ctx(sf->block_ctx);
1207
1208 if (sf->error) {
1209 prev->error = sf->error;
1210 btrfsic_stack_frame_free(sf);
1211 sf = prev;
1212 goto one_stack_frame_backwards;
1213 }
1214
1215 btrfsic_stack_frame_free(sf);
1216 sf = prev;
1217 goto continue_with_new_stack_frame;
1218 } else {
1219 BUG_ON(&initial_stack_frame != sf);
1220 }
1221
1222 return sf->error;
1223}
1224
Stefan Behrense06baab2012-04-12 12:53:40 +02001225static void btrfsic_read_from_block_data(
1226 struct btrfsic_block_data_ctx *block_ctx,
1227 void *dstv, u32 offset, size_t len)
1228{
1229 size_t cur;
1230 size_t offset_in_page;
1231 char *kaddr;
1232 char *dst = (char *)dstv;
1233 size_t start_offset = block_ctx->start & ((u64)PAGE_CACHE_SIZE - 1);
1234 unsigned long i = (start_offset + offset) >> PAGE_CACHE_SHIFT;
1235
1236 WARN_ON(offset + len > block_ctx->len);
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02001237 offset_in_page = (start_offset + offset) & (PAGE_CACHE_SIZE - 1);
Stefan Behrense06baab2012-04-12 12:53:40 +02001238
1239 while (len > 0) {
1240 cur = min(len, ((size_t)PAGE_CACHE_SIZE - offset_in_page));
David Sterbaed6078f2014-06-05 01:59:57 +02001241 BUG_ON(i >= DIV_ROUND_UP(block_ctx->len, PAGE_CACHE_SIZE));
Stefan Behrense06baab2012-04-12 12:53:40 +02001242 kaddr = block_ctx->datav[i];
1243 memcpy(dst, kaddr + offset_in_page, cur);
1244
1245 dst += cur;
1246 len -= cur;
1247 offset_in_page = 0;
1248 i++;
1249 }
1250}
1251
Stefan Behrens5db02762011-11-01 17:04:16 +01001252static int btrfsic_create_link_to_next_block(
1253 struct btrfsic_state *state,
1254 struct btrfsic_block *block,
1255 struct btrfsic_block_data_ctx *block_ctx,
1256 u64 next_bytenr,
1257 int limit_nesting,
1258 struct btrfsic_block_data_ctx *next_block_ctx,
1259 struct btrfsic_block **next_blockp,
1260 int force_iodone_flag,
1261 int *num_copiesp, int *mirror_nump,
1262 struct btrfs_disk_key *disk_key,
1263 u64 parent_generation)
1264{
1265 struct btrfsic_block *next_block = NULL;
1266 int ret;
1267 struct btrfsic_block_link *l;
1268 int did_alloc_block_link;
1269 int block_was_created;
1270
1271 *next_blockp = NULL;
1272 if (0 == *num_copiesp) {
1273 *num_copiesp =
Stefan Behrens5d964052012-11-05 14:59:07 +01001274 btrfs_num_copies(state->root->fs_info,
Stefan Behrense06baab2012-04-12 12:53:40 +02001275 next_bytenr, state->metablock_size);
Stefan Behrens5db02762011-11-01 17:04:16 +01001276 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1277 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001278 next_bytenr, *num_copiesp);
Stefan Behrens5db02762011-11-01 17:04:16 +01001279 *mirror_nump = 1;
1280 }
1281
1282 if (*mirror_nump > *num_copiesp)
1283 return 0;
1284
1285 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1286 printk(KERN_INFO
1287 "btrfsic_create_link_to_next_block(mirror_num=%d)\n",
1288 *mirror_nump);
1289 ret = btrfsic_map_block(state, next_bytenr,
Stefan Behrense06baab2012-04-12 12:53:40 +02001290 state->metablock_size,
Stefan Behrens5db02762011-11-01 17:04:16 +01001291 next_block_ctx, *mirror_nump);
1292 if (ret) {
1293 printk(KERN_INFO
1294 "btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001295 next_bytenr, *mirror_nump);
Stefan Behrens5db02762011-11-01 17:04:16 +01001296 btrfsic_release_block_ctx(next_block_ctx);
1297 *next_blockp = NULL;
1298 return -1;
1299 }
1300
1301 next_block = btrfsic_block_lookup_or_add(state,
1302 next_block_ctx, "referenced ",
1303 1, force_iodone_flag,
1304 !force_iodone_flag,
1305 *mirror_nump,
1306 &block_was_created);
1307 if (NULL == next_block) {
1308 btrfsic_release_block_ctx(next_block_ctx);
1309 *next_blockp = NULL;
1310 return -1;
1311 }
1312 if (block_was_created) {
1313 l = NULL;
1314 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
1315 } else {
Stefan Behrenscf90c592014-10-17 14:10:10 +02001316 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) {
1317 if (next_block->logical_bytenr != next_bytenr &&
1318 !(!next_block->is_metadata &&
1319 0 == next_block->logical_bytenr))
1320 printk(KERN_INFO
1321 "Referenced block @%llu (%s/%llu/%d) found in hash table, %c, bytenr mismatch (!= stored %llu).\n",
1322 next_bytenr, next_block_ctx->dev->name,
1323 next_block_ctx->dev_bytenr, *mirror_nump,
1324 btrfsic_get_block_type(state,
1325 next_block),
1326 next_block->logical_bytenr);
1327 else
1328 printk(KERN_INFO
1329 "Referenced block @%llu (%s/%llu/%d) found in hash table, %c.\n",
1330 next_bytenr, next_block_ctx->dev->name,
1331 next_block_ctx->dev_bytenr, *mirror_nump,
1332 btrfsic_get_block_type(state,
1333 next_block));
1334 }
Stefan Behrens5db02762011-11-01 17:04:16 +01001335 next_block->logical_bytenr = next_bytenr;
1336
1337 next_block->mirror_num = *mirror_nump;
1338 l = btrfsic_block_link_hashtable_lookup(
1339 next_block_ctx->dev->bdev,
1340 next_block_ctx->dev_bytenr,
1341 block_ctx->dev->bdev,
1342 block_ctx->dev_bytenr,
1343 &state->block_link_hashtable);
1344 }
1345
1346 next_block->disk_key = *disk_key;
1347 if (NULL == l) {
1348 l = btrfsic_block_link_alloc();
1349 if (NULL == l) {
1350 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
1351 btrfsic_release_block_ctx(next_block_ctx);
1352 *next_blockp = NULL;
1353 return -1;
1354 }
1355
1356 did_alloc_block_link = 1;
1357 l->block_ref_to = next_block;
1358 l->block_ref_from = block;
1359 l->ref_cnt = 1;
1360 l->parent_generation = parent_generation;
1361
1362 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1363 btrfsic_print_add_link(state, l);
1364
1365 list_add(&l->node_ref_to, &block->ref_to_list);
1366 list_add(&l->node_ref_from, &next_block->ref_from_list);
1367
1368 btrfsic_block_link_hashtable_add(l,
1369 &state->block_link_hashtable);
1370 } else {
1371 did_alloc_block_link = 0;
1372 if (0 == limit_nesting) {
1373 l->ref_cnt++;
1374 l->parent_generation = parent_generation;
1375 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1376 btrfsic_print_add_link(state, l);
1377 }
1378 }
1379
1380 if (limit_nesting > 0 && did_alloc_block_link) {
1381 ret = btrfsic_read_block(state, next_block_ctx);
Stefan Behrense06baab2012-04-12 12:53:40 +02001382 if (ret < (int)next_block_ctx->len) {
Stefan Behrens5db02762011-11-01 17:04:16 +01001383 printk(KERN_INFO
1384 "btrfsic: read block @logical %llu failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001385 next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01001386 btrfsic_release_block_ctx(next_block_ctx);
1387 *next_blockp = NULL;
1388 return -1;
1389 }
1390
1391 *next_blockp = next_block;
1392 } else {
1393 *next_blockp = NULL;
1394 }
1395 (*mirror_nump)++;
1396
1397 return 0;
1398}
1399
1400static int btrfsic_handle_extent_data(
1401 struct btrfsic_state *state,
1402 struct btrfsic_block *block,
1403 struct btrfsic_block_data_ctx *block_ctx,
1404 u32 item_offset, int force_iodone_flag)
1405{
1406 int ret;
Stefan Behrense06baab2012-04-12 12:53:40 +02001407 struct btrfs_file_extent_item file_extent_item;
1408 u64 file_extent_item_offset;
1409 u64 next_bytenr;
1410 u64 num_bytes;
1411 u64 generation;
Stefan Behrens5db02762011-11-01 17:04:16 +01001412 struct btrfsic_block_link *l;
1413
Stefan Behrense06baab2012-04-12 12:53:40 +02001414 file_extent_item_offset = offsetof(struct btrfs_leaf, items) +
1415 item_offset;
Stefan Behrens86ff7ff2012-04-24 18:10:16 +02001416 if (file_extent_item_offset +
1417 offsetof(struct btrfs_file_extent_item, disk_num_bytes) >
1418 block_ctx->len) {
1419 printk(KERN_INFO
1420 "btrfsic: file item out of bounce at logical %llu, dev %s\n",
1421 block_ctx->start, block_ctx->dev->name);
1422 return -1;
1423 }
1424
1425 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1426 file_extent_item_offset,
1427 offsetof(struct btrfs_file_extent_item, disk_num_bytes));
1428 if (BTRFS_FILE_EXTENT_REG != file_extent_item.type ||
Qu Wenruo3cae2102013-07-16 11:19:18 +08001429 btrfs_stack_file_extent_disk_bytenr(&file_extent_item) == 0) {
Stefan Behrens86ff7ff2012-04-24 18:10:16 +02001430 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1431 printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu\n",
1432 file_extent_item.type,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001433 btrfs_stack_file_extent_disk_bytenr(
1434 &file_extent_item));
Stefan Behrens86ff7ff2012-04-24 18:10:16 +02001435 return 0;
1436 }
1437
Stefan Behrense06baab2012-04-12 12:53:40 +02001438 if (file_extent_item_offset + sizeof(struct btrfs_file_extent_item) >
1439 block_ctx->len) {
1440 printk(KERN_INFO
1441 "btrfsic: file item out of bounce at logical %llu, dev %s\n",
1442 block_ctx->start, block_ctx->dev->name);
1443 return -1;
1444 }
1445 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1446 file_extent_item_offset,
1447 sizeof(struct btrfs_file_extent_item));
Josef Bacike20d6c52013-11-13 21:11:49 -05001448 next_bytenr = btrfs_stack_file_extent_disk_bytenr(&file_extent_item);
1449 if (btrfs_stack_file_extent_compression(&file_extent_item) ==
1450 BTRFS_COMPRESS_NONE) {
1451 next_bytenr += btrfs_stack_file_extent_offset(&file_extent_item);
1452 num_bytes = btrfs_stack_file_extent_num_bytes(&file_extent_item);
1453 } else {
1454 num_bytes = btrfs_stack_file_extent_disk_num_bytes(&file_extent_item);
1455 }
Qu Wenruo3cae2102013-07-16 11:19:18 +08001456 generation = btrfs_stack_file_extent_generation(&file_extent_item);
Stefan Behrense06baab2012-04-12 12:53:40 +02001457
Stefan Behrens5db02762011-11-01 17:04:16 +01001458 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1459 printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu,"
1460 " offset = %llu, num_bytes = %llu\n",
Stefan Behrense06baab2012-04-12 12:53:40 +02001461 file_extent_item.type,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001462 btrfs_stack_file_extent_disk_bytenr(&file_extent_item),
Qu Wenruo3cae2102013-07-16 11:19:18 +08001463 btrfs_stack_file_extent_offset(&file_extent_item),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001464 num_bytes);
Stefan Behrens5db02762011-11-01 17:04:16 +01001465 while (num_bytes > 0) {
1466 u32 chunk_len;
1467 int num_copies;
1468 int mirror_num;
1469
Stefan Behrense06baab2012-04-12 12:53:40 +02001470 if (num_bytes > state->datablock_size)
1471 chunk_len = state->datablock_size;
Stefan Behrens5db02762011-11-01 17:04:16 +01001472 else
1473 chunk_len = num_bytes;
1474
1475 num_copies =
Stefan Behrens5d964052012-11-05 14:59:07 +01001476 btrfs_num_copies(state->root->fs_info,
Stefan Behrense06baab2012-04-12 12:53:40 +02001477 next_bytenr, state->datablock_size);
Stefan Behrens5db02762011-11-01 17:04:16 +01001478 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1479 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001480 next_bytenr, num_copies);
Stefan Behrens5db02762011-11-01 17:04:16 +01001481 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
1482 struct btrfsic_block_data_ctx next_block_ctx;
1483 struct btrfsic_block *next_block;
1484 int block_was_created;
1485
1486 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1487 printk(KERN_INFO "btrfsic_handle_extent_data("
1488 "mirror_num=%d)\n", mirror_num);
1489 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1490 printk(KERN_INFO
1491 "\tdisk_bytenr = %llu, num_bytes %u\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001492 next_bytenr, chunk_len);
Stefan Behrens5db02762011-11-01 17:04:16 +01001493 ret = btrfsic_map_block(state, next_bytenr,
1494 chunk_len, &next_block_ctx,
1495 mirror_num);
1496 if (ret) {
1497 printk(KERN_INFO
1498 "btrfsic: btrfsic_map_block(@%llu,"
1499 " mirror=%d) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001500 next_bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01001501 return -1;
1502 }
1503
1504 next_block = btrfsic_block_lookup_or_add(
1505 state,
1506 &next_block_ctx,
1507 "referenced ",
1508 0,
1509 force_iodone_flag,
1510 !force_iodone_flag,
1511 mirror_num,
1512 &block_was_created);
1513 if (NULL == next_block) {
1514 printk(KERN_INFO
1515 "btrfsic: error, kmalloc failed!\n");
1516 btrfsic_release_block_ctx(&next_block_ctx);
1517 return -1;
1518 }
1519 if (!block_was_created) {
Stefan Behrenscf90c592014-10-17 14:10:10 +02001520 if ((state->print_mask &
1521 BTRFSIC_PRINT_MASK_VERBOSE) &&
1522 next_block->logical_bytenr != next_bytenr &&
Stefan Behrens5db02762011-11-01 17:04:16 +01001523 !(!next_block->is_metadata &&
1524 0 == next_block->logical_bytenr)) {
1525 printk(KERN_INFO
1526 "Referenced block"
1527 " @%llu (%s/%llu/%d)"
1528 " found in hash table, D,"
1529 " bytenr mismatch"
1530 " (!= stored %llu).\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001531 next_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01001532 next_block_ctx.dev->name,
Stefan Behrens5db02762011-11-01 17:04:16 +01001533 next_block_ctx.dev_bytenr,
1534 mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01001535 next_block->logical_bytenr);
1536 }
1537 next_block->logical_bytenr = next_bytenr;
1538 next_block->mirror_num = mirror_num;
1539 }
1540
1541 l = btrfsic_block_link_lookup_or_add(state,
1542 &next_block_ctx,
1543 next_block, block,
1544 generation);
1545 btrfsic_release_block_ctx(&next_block_ctx);
1546 if (NULL == l)
1547 return -1;
1548 }
1549
1550 next_bytenr += chunk_len;
1551 num_bytes -= chunk_len;
1552 }
1553
1554 return 0;
1555}
1556
1557static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
1558 struct btrfsic_block_data_ctx *block_ctx_out,
1559 int mirror_num)
1560{
1561 int ret;
1562 u64 length;
1563 struct btrfs_bio *multi = NULL;
1564 struct btrfs_device *device;
1565
1566 length = len;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001567 ret = btrfs_map_block(state->root->fs_info, READ,
Stefan Behrens5db02762011-11-01 17:04:16 +01001568 bytenr, &length, &multi, mirror_num);
1569
Stefan Behrens61891922012-11-05 18:51:52 +01001570 if (ret) {
1571 block_ctx_out->start = 0;
1572 block_ctx_out->dev_bytenr = 0;
1573 block_ctx_out->len = 0;
1574 block_ctx_out->dev = NULL;
1575 block_ctx_out->datav = NULL;
1576 block_ctx_out->pagev = NULL;
1577 block_ctx_out->mem_to_free = NULL;
1578
1579 return ret;
1580 }
1581
Stefan Behrens5db02762011-11-01 17:04:16 +01001582 device = multi->stripes[0].dev;
1583 block_ctx_out->dev = btrfsic_dev_state_lookup(device->bdev);
1584 block_ctx_out->dev_bytenr = multi->stripes[0].physical;
1585 block_ctx_out->start = bytenr;
1586 block_ctx_out->len = len;
Stefan Behrense06baab2012-04-12 12:53:40 +02001587 block_ctx_out->datav = NULL;
1588 block_ctx_out->pagev = NULL;
1589 block_ctx_out->mem_to_free = NULL;
Stefan Behrens5db02762011-11-01 17:04:16 +01001590
Stefan Behrens61891922012-11-05 18:51:52 +01001591 kfree(multi);
Stefan Behrens5db02762011-11-01 17:04:16 +01001592 if (NULL == block_ctx_out->dev) {
1593 ret = -ENXIO;
1594 printk(KERN_INFO "btrfsic: error, cannot lookup dev (#1)!\n");
1595 }
1596
1597 return ret;
1598}
1599
Stefan Behrens5db02762011-11-01 17:04:16 +01001600static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx)
1601{
Stefan Behrense06baab2012-04-12 12:53:40 +02001602 if (block_ctx->mem_to_free) {
1603 unsigned int num_pages;
1604
1605 BUG_ON(!block_ctx->datav);
1606 BUG_ON(!block_ctx->pagev);
1607 num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
1608 PAGE_CACHE_SHIFT;
1609 while (num_pages > 0) {
1610 num_pages--;
1611 if (block_ctx->datav[num_pages]) {
1612 kunmap(block_ctx->pagev[num_pages]);
1613 block_ctx->datav[num_pages] = NULL;
1614 }
1615 if (block_ctx->pagev[num_pages]) {
1616 __free_page(block_ctx->pagev[num_pages]);
1617 block_ctx->pagev[num_pages] = NULL;
1618 }
1619 }
1620
1621 kfree(block_ctx->mem_to_free);
1622 block_ctx->mem_to_free = NULL;
1623 block_ctx->pagev = NULL;
1624 block_ctx->datav = NULL;
Stefan Behrens5db02762011-11-01 17:04:16 +01001625 }
1626}
1627
1628static int btrfsic_read_block(struct btrfsic_state *state,
1629 struct btrfsic_block_data_ctx *block_ctx)
1630{
Stefan Behrense06baab2012-04-12 12:53:40 +02001631 unsigned int num_pages;
1632 unsigned int i;
1633 u64 dev_bytenr;
1634 int ret;
1635
1636 BUG_ON(block_ctx->datav);
1637 BUG_ON(block_ctx->pagev);
1638 BUG_ON(block_ctx->mem_to_free);
1639 if (block_ctx->dev_bytenr & ((u64)PAGE_CACHE_SIZE - 1)) {
Stefan Behrens5db02762011-11-01 17:04:16 +01001640 printk(KERN_INFO
1641 "btrfsic: read_block() with unaligned bytenr %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001642 block_ctx->dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01001643 return -1;
1644 }
Stefan Behrense06baab2012-04-12 12:53:40 +02001645
1646 num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
1647 PAGE_CACHE_SHIFT;
1648 block_ctx->mem_to_free = kzalloc((sizeof(*block_ctx->datav) +
1649 sizeof(*block_ctx->pagev)) *
1650 num_pages, GFP_NOFS);
1651 if (!block_ctx->mem_to_free)
Luis de Bethencourt0b8d8ce2015-10-20 14:56:22 +01001652 return -ENOMEM;
Stefan Behrense06baab2012-04-12 12:53:40 +02001653 block_ctx->datav = block_ctx->mem_to_free;
1654 block_ctx->pagev = (struct page **)(block_ctx->datav + num_pages);
1655 for (i = 0; i < num_pages; i++) {
1656 block_ctx->pagev[i] = alloc_page(GFP_NOFS);
1657 if (!block_ctx->pagev[i])
1658 return -1;
Stefan Behrens5db02762011-11-01 17:04:16 +01001659 }
1660
Stefan Behrense06baab2012-04-12 12:53:40 +02001661 dev_bytenr = block_ctx->dev_bytenr;
1662 for (i = 0; i < num_pages;) {
1663 struct bio *bio;
1664 unsigned int j;
Stefan Behrense06baab2012-04-12 12:53:40 +02001665
Chris Mason9be33952013-05-17 18:30:14 -04001666 bio = btrfs_io_bio_alloc(GFP_NOFS, num_pages - i);
Stefan Behrense06baab2012-04-12 12:53:40 +02001667 if (!bio) {
1668 printk(KERN_INFO
1669 "btrfsic: bio_alloc() for %u pages failed!\n",
1670 num_pages - i);
1671 return -1;
1672 }
1673 bio->bi_bdev = block_ctx->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001674 bio->bi_iter.bi_sector = dev_bytenr >> 9;
Stefan Behrense06baab2012-04-12 12:53:40 +02001675
1676 for (j = i; j < num_pages; j++) {
1677 ret = bio_add_page(bio, block_ctx->pagev[j],
1678 PAGE_CACHE_SIZE, 0);
1679 if (PAGE_CACHE_SIZE != ret)
1680 break;
1681 }
1682 if (j == i) {
1683 printk(KERN_INFO
1684 "btrfsic: error, failed to add a single page!\n");
1685 return -1;
1686 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001687 if (submit_bio_wait(READ, bio)) {
Stefan Behrense06baab2012-04-12 12:53:40 +02001688 printk(KERN_INFO
1689 "btrfsic: read error at logical %llu dev %s!\n",
1690 block_ctx->start, block_ctx->dev->name);
1691 bio_put(bio);
1692 return -1;
1693 }
1694 bio_put(bio);
1695 dev_bytenr += (j - i) * PAGE_CACHE_SIZE;
1696 i = j;
1697 }
1698 for (i = 0; i < num_pages; i++) {
1699 block_ctx->datav[i] = kmap(block_ctx->pagev[i]);
1700 if (!block_ctx->datav[i]) {
1701 printk(KERN_INFO "btrfsic: kmap() failed (dev %s)!\n",
1702 block_ctx->dev->name);
1703 return -1;
1704 }
1705 }
Stefan Behrens5db02762011-11-01 17:04:16 +01001706
1707 return block_ctx->len;
1708}
1709
1710static void btrfsic_dump_database(struct btrfsic_state *state)
1711{
Geliang Tangb69f2be2015-12-18 22:16:59 +08001712 const struct btrfsic_block *b_all;
Stefan Behrens5db02762011-11-01 17:04:16 +01001713
1714 BUG_ON(NULL == state);
1715
1716 printk(KERN_INFO "all_blocks_list:\n");
Geliang Tangb69f2be2015-12-18 22:16:59 +08001717 list_for_each_entry(b_all, &state->all_blocks_list, all_blocks_node) {
1718 const struct btrfsic_block_link *l;
Stefan Behrens5db02762011-11-01 17:04:16 +01001719
1720 printk(KERN_INFO "%c-block @%llu (%s/%llu/%d)\n",
1721 btrfsic_get_block_type(state, b_all),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001722 b_all->logical_bytenr, b_all->dev_state->name,
1723 b_all->dev_bytenr, b_all->mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01001724
Geliang Tangb69f2be2015-12-18 22:16:59 +08001725 list_for_each_entry(l, &b_all->ref_to_list, node_ref_to) {
Stefan Behrens5db02762011-11-01 17:04:16 +01001726 printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
1727 " refers %u* to"
1728 " %c @%llu (%s/%llu/%d)\n",
1729 btrfsic_get_block_type(state, b_all),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001730 b_all->logical_bytenr, b_all->dev_state->name,
1731 b_all->dev_bytenr, b_all->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01001732 l->ref_cnt,
1733 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens5db02762011-11-01 17:04:16 +01001734 l->block_ref_to->logical_bytenr,
1735 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001736 l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01001737 l->block_ref_to->mirror_num);
1738 }
1739
Geliang Tangb69f2be2015-12-18 22:16:59 +08001740 list_for_each_entry(l, &b_all->ref_from_list, node_ref_from) {
Stefan Behrens5db02762011-11-01 17:04:16 +01001741 printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
1742 " is ref %u* from"
1743 " %c @%llu (%s/%llu/%d)\n",
1744 btrfsic_get_block_type(state, b_all),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001745 b_all->logical_bytenr, b_all->dev_state->name,
1746 b_all->dev_bytenr, b_all->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01001747 l->ref_cnt,
1748 btrfsic_get_block_type(state, l->block_ref_from),
Stefan Behrens5db02762011-11-01 17:04:16 +01001749 l->block_ref_from->logical_bytenr,
1750 l->block_ref_from->dev_state->name,
Stefan Behrens5db02762011-11-01 17:04:16 +01001751 l->block_ref_from->dev_bytenr,
1752 l->block_ref_from->mirror_num);
1753 }
1754
1755 printk(KERN_INFO "\n");
1756 }
1757}
1758
1759/*
1760 * Test whether the disk block contains a tree block (leaf or node)
1761 * (note that this test fails for the super block)
1762 */
1763static int btrfsic_test_for_metadata(struct btrfsic_state *state,
Stefan Behrense06baab2012-04-12 12:53:40 +02001764 char **datav, unsigned int num_pages)
Stefan Behrens5db02762011-11-01 17:04:16 +01001765{
1766 struct btrfs_header *h;
1767 u8 csum[BTRFS_CSUM_SIZE];
1768 u32 crc = ~(u32)0;
Stefan Behrense06baab2012-04-12 12:53:40 +02001769 unsigned int i;
Stefan Behrens5db02762011-11-01 17:04:16 +01001770
Stefan Behrense06baab2012-04-12 12:53:40 +02001771 if (num_pages * PAGE_CACHE_SIZE < state->metablock_size)
1772 return 1; /* not metadata */
1773 num_pages = state->metablock_size >> PAGE_CACHE_SHIFT;
1774 h = (struct btrfs_header *)datav[0];
Stefan Behrens5db02762011-11-01 17:04:16 +01001775
1776 if (memcmp(h->fsid, state->root->fs_info->fsid, BTRFS_UUID_SIZE))
Stefan Behrense06baab2012-04-12 12:53:40 +02001777 return 1;
Stefan Behrens5db02762011-11-01 17:04:16 +01001778
Stefan Behrense06baab2012-04-12 12:53:40 +02001779 for (i = 0; i < num_pages; i++) {
1780 u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE);
1781 size_t sublen = i ? PAGE_CACHE_SIZE :
1782 (PAGE_CACHE_SIZE - BTRFS_CSUM_SIZE);
1783
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +00001784 crc = btrfs_crc32c(crc, data, sublen);
Stefan Behrense06baab2012-04-12 12:53:40 +02001785 }
Stefan Behrens5db02762011-11-01 17:04:16 +01001786 btrfs_csum_final(crc, csum);
1787 if (memcmp(csum, h->csum, state->csum_size))
Stefan Behrense06baab2012-04-12 12:53:40 +02001788 return 1;
Stefan Behrens5db02762011-11-01 17:04:16 +01001789
Stefan Behrense06baab2012-04-12 12:53:40 +02001790 return 0; /* is metadata */
Stefan Behrens5db02762011-11-01 17:04:16 +01001791}
1792
1793static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
Stefan Behrense06baab2012-04-12 12:53:40 +02001794 u64 dev_bytenr, char **mapped_datav,
1795 unsigned int num_pages,
1796 struct bio *bio, int *bio_is_patched,
Stefan Behrens5db02762011-11-01 17:04:16 +01001797 struct buffer_head *bh,
1798 int submit_bio_bh_rw)
1799{
1800 int is_metadata;
1801 struct btrfsic_block *block;
1802 struct btrfsic_block_data_ctx block_ctx;
1803 int ret;
1804 struct btrfsic_state *state = dev_state->state;
1805 struct block_device *bdev = dev_state->bdev;
Stefan Behrense06baab2012-04-12 12:53:40 +02001806 unsigned int processed_len;
Stefan Behrens5db02762011-11-01 17:04:16 +01001807
Stefan Behrens5db02762011-11-01 17:04:16 +01001808 if (NULL != bio_is_patched)
1809 *bio_is_patched = 0;
1810
Stefan Behrense06baab2012-04-12 12:53:40 +02001811again:
1812 if (num_pages == 0)
1813 return;
1814
1815 processed_len = 0;
1816 is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
1817 num_pages));
1818
Stefan Behrens5db02762011-11-01 17:04:16 +01001819 block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
1820 &state->block_hashtable);
1821 if (NULL != block) {
Stefan Behrens0b485142012-01-26 15:01:11 -05001822 u64 bytenr = 0;
Geliang Tangb69f2be2015-12-18 22:16:59 +08001823 struct btrfsic_block_link *l, *tmp;
Stefan Behrens5db02762011-11-01 17:04:16 +01001824
1825 if (block->is_superblock) {
Qu Wenruo3cae2102013-07-16 11:19:18 +08001826 bytenr = btrfs_super_bytenr((struct btrfs_super_block *)
1827 mapped_datav[0]);
Stefan Behrense06baab2012-04-12 12:53:40 +02001828 if (num_pages * PAGE_CACHE_SIZE <
1829 BTRFS_SUPER_INFO_SIZE) {
1830 printk(KERN_INFO
1831 "btrfsic: cannot work with too short bios!\n");
1832 return;
1833 }
Stefan Behrens5db02762011-11-01 17:04:16 +01001834 is_metadata = 1;
Stefan Behrense06baab2012-04-12 12:53:40 +02001835 BUG_ON(BTRFS_SUPER_INFO_SIZE & (PAGE_CACHE_SIZE - 1));
1836 processed_len = BTRFS_SUPER_INFO_SIZE;
Stefan Behrens5db02762011-11-01 17:04:16 +01001837 if (state->print_mask &
1838 BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) {
1839 printk(KERN_INFO
1840 "[before new superblock is written]:\n");
1841 btrfsic_dump_tree_sub(state, block, 0);
1842 }
1843 }
1844 if (is_metadata) {
1845 if (!block->is_superblock) {
Stefan Behrense06baab2012-04-12 12:53:40 +02001846 if (num_pages * PAGE_CACHE_SIZE <
1847 state->metablock_size) {
1848 printk(KERN_INFO
1849 "btrfsic: cannot work with too short bios!\n");
1850 return;
1851 }
1852 processed_len = state->metablock_size;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001853 bytenr = btrfs_stack_header_bytenr(
1854 (struct btrfs_header *)
1855 mapped_datav[0]);
Stefan Behrens5db02762011-11-01 17:04:16 +01001856 btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
1857 dev_state,
Stefan Behrense06baab2012-04-12 12:53:40 +02001858 dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01001859 }
Stefan Behrenscf90c592014-10-17 14:10:10 +02001860 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) {
1861 if (block->logical_bytenr != bytenr &&
1862 !(!block->is_metadata &&
1863 block->logical_bytenr == 0))
1864 printk(KERN_INFO
1865 "Written block @%llu (%s/%llu/%d) found in hash table, %c, bytenr mismatch (!= stored %llu).\n",
1866 bytenr, dev_state->name,
1867 dev_bytenr,
1868 block->mirror_num,
1869 btrfsic_get_block_type(state,
1870 block),
1871 block->logical_bytenr);
1872 else
1873 printk(KERN_INFO
1874 "Written block @%llu (%s/%llu/%d) found in hash table, %c.\n",
1875 bytenr, dev_state->name,
1876 dev_bytenr, block->mirror_num,
1877 btrfsic_get_block_type(state,
1878 block));
1879 }
Stefan Behrens301993a2013-10-21 18:46:58 +02001880 block->logical_bytenr = bytenr;
Stefan Behrens5db02762011-11-01 17:04:16 +01001881 } else {
Stefan Behrense06baab2012-04-12 12:53:40 +02001882 if (num_pages * PAGE_CACHE_SIZE <
1883 state->datablock_size) {
1884 printk(KERN_INFO
1885 "btrfsic: cannot work with too short bios!\n");
1886 return;
1887 }
1888 processed_len = state->datablock_size;
Stefan Behrens5db02762011-11-01 17:04:16 +01001889 bytenr = block->logical_bytenr;
1890 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1891 printk(KERN_INFO
1892 "Written block @%llu (%s/%llu/%d)"
1893 " found in hash table, %c.\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001894 bytenr, dev_state->name, dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01001895 block->mirror_num,
1896 btrfsic_get_block_type(state, block));
1897 }
1898
1899 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1900 printk(KERN_INFO
1901 "ref_to_list: %cE, ref_from_list: %cE\n",
1902 list_empty(&block->ref_to_list) ? ' ' : '!',
1903 list_empty(&block->ref_from_list) ? ' ' : '!');
1904 if (btrfsic_is_block_ref_by_superblock(state, block, 0)) {
1905 printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
1906 " @%llu (%s/%llu/%d), old(gen=%llu,"
1907 " objectid=%llu, type=%d, offset=%llu),"
1908 " new(gen=%llu),"
1909 " which is referenced by most recent superblock"
1910 " (superblockgen=%llu)!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001911 btrfsic_get_block_type(state, block), bytenr,
1912 dev_state->name, dev_bytenr, block->mirror_num,
1913 block->generation,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001914 btrfs_disk_key_objectid(&block->disk_key),
Stefan Behrens5db02762011-11-01 17:04:16 +01001915 block->disk_key.type,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001916 btrfs_disk_key_offset(&block->disk_key),
Qu Wenruo3cae2102013-07-16 11:19:18 +08001917 btrfs_stack_header_generation(
1918 (struct btrfs_header *) mapped_datav[0]),
Stefan Behrens5db02762011-11-01 17:04:16 +01001919 state->max_superblock_generation);
1920 btrfsic_dump_tree(state);
1921 }
1922
1923 if (!block->is_iodone && !block->never_written) {
1924 printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
1925 " @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu,"
1926 " which is not yet iodone!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001927 btrfsic_get_block_type(state, block), bytenr,
1928 dev_state->name, dev_bytenr, block->mirror_num,
1929 block->generation,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001930 btrfs_stack_header_generation(
1931 (struct btrfs_header *)
1932 mapped_datav[0]));
Stefan Behrens5db02762011-11-01 17:04:16 +01001933 /* it would not be safe to go on */
1934 btrfsic_dump_tree(state);
Stefan Behrense06baab2012-04-12 12:53:40 +02001935 goto continue_loop;
Stefan Behrens5db02762011-11-01 17:04:16 +01001936 }
1937
1938 /*
1939 * Clear all references of this block. Do not free
1940 * the block itself even if is not referenced anymore
1941 * because it still carries valueable information
1942 * like whether it was ever written and IO completed.
1943 */
Geliang Tangb69f2be2015-12-18 22:16:59 +08001944 list_for_each_entry_safe(l, tmp, &block->ref_to_list,
1945 node_ref_to) {
Stefan Behrens5db02762011-11-01 17:04:16 +01001946 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1947 btrfsic_print_rem_link(state, l);
1948 l->ref_cnt--;
1949 if (0 == l->ref_cnt) {
1950 list_del(&l->node_ref_to);
1951 list_del(&l->node_ref_from);
1952 btrfsic_block_link_hashtable_remove(l);
1953 btrfsic_block_link_free(l);
1954 }
1955 }
1956
Stefan Behrens5db02762011-11-01 17:04:16 +01001957 block_ctx.dev = dev_state;
1958 block_ctx.dev_bytenr = dev_bytenr;
Stefan Behrensf382e462014-10-16 17:48:48 +02001959 block_ctx.start = bytenr;
1960 block_ctx.len = processed_len;
1961 block_ctx.pagev = NULL;
1962 block_ctx.mem_to_free = NULL;
1963 block_ctx.datav = mapped_datav;
Stefan Behrens5db02762011-11-01 17:04:16 +01001964
1965 if (is_metadata || state->include_extent_data) {
1966 block->never_written = 0;
1967 block->iodone_w_error = 0;
1968 if (NULL != bio) {
1969 block->is_iodone = 0;
1970 BUG_ON(NULL == bio_is_patched);
1971 if (!*bio_is_patched) {
1972 block->orig_bio_bh_private =
1973 bio->bi_private;
1974 block->orig_bio_bh_end_io.bio =
1975 bio->bi_end_io;
1976 block->next_in_same_bio = NULL;
1977 bio->bi_private = block;
1978 bio->bi_end_io = btrfsic_bio_end_io;
1979 *bio_is_patched = 1;
1980 } else {
1981 struct btrfsic_block *chained_block =
1982 (struct btrfsic_block *)
1983 bio->bi_private;
1984
1985 BUG_ON(NULL == chained_block);
1986 block->orig_bio_bh_private =
1987 chained_block->orig_bio_bh_private;
1988 block->orig_bio_bh_end_io.bio =
1989 chained_block->orig_bio_bh_end_io.
1990 bio;
1991 block->next_in_same_bio = chained_block;
1992 bio->bi_private = block;
1993 }
1994 } else if (NULL != bh) {
1995 block->is_iodone = 0;
1996 block->orig_bio_bh_private = bh->b_private;
1997 block->orig_bio_bh_end_io.bh = bh->b_end_io;
1998 block->next_in_same_bio = NULL;
1999 bh->b_private = block;
2000 bh->b_end_io = btrfsic_bh_end_io;
2001 } else {
2002 block->is_iodone = 1;
2003 block->orig_bio_bh_private = NULL;
2004 block->orig_bio_bh_end_io.bio = NULL;
2005 block->next_in_same_bio = NULL;
2006 }
2007 }
2008
2009 block->flush_gen = dev_state->last_flush_gen + 1;
2010 block->submit_bio_bh_rw = submit_bio_bh_rw;
2011 if (is_metadata) {
2012 block->logical_bytenr = bytenr;
2013 block->is_metadata = 1;
2014 if (block->is_superblock) {
Stefan Behrense06baab2012-04-12 12:53:40 +02002015 BUG_ON(PAGE_CACHE_SIZE !=
2016 BTRFS_SUPER_INFO_SIZE);
Stefan Behrens5db02762011-11-01 17:04:16 +01002017 ret = btrfsic_process_written_superblock(
2018 state,
2019 block,
2020 (struct btrfs_super_block *)
Stefan Behrense06baab2012-04-12 12:53:40 +02002021 mapped_datav[0]);
Stefan Behrens5db02762011-11-01 17:04:16 +01002022 if (state->print_mask &
2023 BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) {
2024 printk(KERN_INFO
2025 "[after new superblock is written]:\n");
2026 btrfsic_dump_tree_sub(state, block, 0);
2027 }
2028 } else {
2029 block->mirror_num = 0; /* unknown */
2030 ret = btrfsic_process_metablock(
2031 state,
2032 block,
2033 &block_ctx,
Stefan Behrens5db02762011-11-01 17:04:16 +01002034 0, 0);
2035 }
2036 if (ret)
2037 printk(KERN_INFO
2038 "btrfsic: btrfsic_process_metablock"
2039 "(root @%llu) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002040 dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002041 } else {
2042 block->is_metadata = 0;
2043 block->mirror_num = 0; /* unknown */
2044 block->generation = BTRFSIC_GENERATION_UNKNOWN;
2045 if (!state->include_extent_data
2046 && list_empty(&block->ref_from_list)) {
2047 /*
2048 * disk block is overwritten with extent
2049 * data (not meta data) and we are configured
2050 * to not include extent data: take the
2051 * chance and free the block's memory
2052 */
2053 btrfsic_block_hashtable_remove(block);
2054 list_del(&block->all_blocks_node);
2055 btrfsic_block_free(block);
2056 }
2057 }
2058 btrfsic_release_block_ctx(&block_ctx);
2059 } else {
2060 /* block has not been found in hash table */
2061 u64 bytenr;
2062
2063 if (!is_metadata) {
Stefan Behrense06baab2012-04-12 12:53:40 +02002064 processed_len = state->datablock_size;
Stefan Behrens5db02762011-11-01 17:04:16 +01002065 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2066 printk(KERN_INFO "Written block (%s/%llu/?)"
2067 " !found in hash table, D.\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002068 dev_state->name, dev_bytenr);
Stefan Behrense06baab2012-04-12 12:53:40 +02002069 if (!state->include_extent_data) {
2070 /* ignore that written D block */
2071 goto continue_loop;
2072 }
Stefan Behrens5db02762011-11-01 17:04:16 +01002073
2074 /* this is getting ugly for the
2075 * include_extent_data case... */
2076 bytenr = 0; /* unknown */
Stefan Behrens5db02762011-11-01 17:04:16 +01002077 } else {
Stefan Behrense06baab2012-04-12 12:53:40 +02002078 processed_len = state->metablock_size;
Qu Wenruo3cae2102013-07-16 11:19:18 +08002079 bytenr = btrfs_stack_header_bytenr(
2080 (struct btrfs_header *)
2081 mapped_datav[0]);
Stefan Behrens5db02762011-11-01 17:04:16 +01002082 btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
Stefan Behrense06baab2012-04-12 12:53:40 +02002083 dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002084 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2085 printk(KERN_INFO
2086 "Written block @%llu (%s/%llu/?)"
2087 " !found in hash table, M.\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002088 bytenr, dev_state->name, dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002089 }
Stefan Behrensf382e462014-10-16 17:48:48 +02002090
Stefan Behrens5db02762011-11-01 17:04:16 +01002091 block_ctx.dev = dev_state;
2092 block_ctx.dev_bytenr = dev_bytenr;
Stefan Behrensf382e462014-10-16 17:48:48 +02002093 block_ctx.start = bytenr;
2094 block_ctx.len = processed_len;
2095 block_ctx.pagev = NULL;
2096 block_ctx.mem_to_free = NULL;
2097 block_ctx.datav = mapped_datav;
Stefan Behrens5db02762011-11-01 17:04:16 +01002098
2099 block = btrfsic_block_alloc();
2100 if (NULL == block) {
2101 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
2102 btrfsic_release_block_ctx(&block_ctx);
Stefan Behrense06baab2012-04-12 12:53:40 +02002103 goto continue_loop;
Stefan Behrens5db02762011-11-01 17:04:16 +01002104 }
2105 block->dev_state = dev_state;
2106 block->dev_bytenr = dev_bytenr;
2107 block->logical_bytenr = bytenr;
2108 block->is_metadata = is_metadata;
2109 block->never_written = 0;
2110 block->iodone_w_error = 0;
2111 block->mirror_num = 0; /* unknown */
2112 block->flush_gen = dev_state->last_flush_gen + 1;
2113 block->submit_bio_bh_rw = submit_bio_bh_rw;
2114 if (NULL != bio) {
2115 block->is_iodone = 0;
2116 BUG_ON(NULL == bio_is_patched);
2117 if (!*bio_is_patched) {
2118 block->orig_bio_bh_private = bio->bi_private;
2119 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
2120 block->next_in_same_bio = NULL;
2121 bio->bi_private = block;
2122 bio->bi_end_io = btrfsic_bio_end_io;
2123 *bio_is_patched = 1;
2124 } else {
2125 struct btrfsic_block *chained_block =
2126 (struct btrfsic_block *)
2127 bio->bi_private;
2128
2129 BUG_ON(NULL == chained_block);
2130 block->orig_bio_bh_private =
2131 chained_block->orig_bio_bh_private;
2132 block->orig_bio_bh_end_io.bio =
2133 chained_block->orig_bio_bh_end_io.bio;
2134 block->next_in_same_bio = chained_block;
2135 bio->bi_private = block;
2136 }
2137 } else if (NULL != bh) {
2138 block->is_iodone = 0;
2139 block->orig_bio_bh_private = bh->b_private;
2140 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2141 block->next_in_same_bio = NULL;
2142 bh->b_private = block;
2143 bh->b_end_io = btrfsic_bh_end_io;
2144 } else {
2145 block->is_iodone = 1;
2146 block->orig_bio_bh_private = NULL;
2147 block->orig_bio_bh_end_io.bio = NULL;
2148 block->next_in_same_bio = NULL;
2149 }
2150 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2151 printk(KERN_INFO
2152 "New written %c-block @%llu (%s/%llu/%d)\n",
2153 is_metadata ? 'M' : 'D',
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002154 block->logical_bytenr, block->dev_state->name,
2155 block->dev_bytenr, block->mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002156 list_add(&block->all_blocks_node, &state->all_blocks_list);
2157 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2158
2159 if (is_metadata) {
2160 ret = btrfsic_process_metablock(state, block,
Stefan Behrense06baab2012-04-12 12:53:40 +02002161 &block_ctx, 0, 0);
Stefan Behrens5db02762011-11-01 17:04:16 +01002162 if (ret)
2163 printk(KERN_INFO
2164 "btrfsic: process_metablock(root @%llu)"
2165 " failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002166 dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002167 }
2168 btrfsic_release_block_ctx(&block_ctx);
2169 }
Stefan Behrense06baab2012-04-12 12:53:40 +02002170
2171continue_loop:
2172 BUG_ON(!processed_len);
2173 dev_bytenr += processed_len;
2174 mapped_datav += processed_len >> PAGE_CACHE_SHIFT;
2175 num_pages -= processed_len >> PAGE_CACHE_SHIFT;
2176 goto again;
Stefan Behrens5db02762011-11-01 17:04:16 +01002177}
2178
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002179static void btrfsic_bio_end_io(struct bio *bp)
Stefan Behrens5db02762011-11-01 17:04:16 +01002180{
2181 struct btrfsic_block *block = (struct btrfsic_block *)bp->bi_private;
2182 int iodone_w_error;
2183
2184 /* mutex is not held! This is not save if IO is not yet completed
2185 * on umount */
2186 iodone_w_error = 0;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002187 if (bp->bi_error)
Stefan Behrens5db02762011-11-01 17:04:16 +01002188 iodone_w_error = 1;
2189
2190 BUG_ON(NULL == block);
2191 bp->bi_private = block->orig_bio_bh_private;
2192 bp->bi_end_io = block->orig_bio_bh_end_io.bio;
2193
2194 do {
2195 struct btrfsic_block *next_block;
2196 struct btrfsic_dev_state *const dev_state = block->dev_state;
2197
2198 if ((dev_state->state->print_mask &
2199 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2200 printk(KERN_INFO
2201 "bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n",
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002202 bp->bi_error,
Stefan Behrens5db02762011-11-01 17:04:16 +01002203 btrfsic_get_block_type(dev_state->state, block),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002204 block->logical_bytenr, dev_state->name,
2205 block->dev_bytenr, block->mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002206 next_block = block->next_in_same_bio;
2207 block->iodone_w_error = iodone_w_error;
2208 if (block->submit_bio_bh_rw & REQ_FLUSH) {
2209 dev_state->last_flush_gen++;
2210 if ((dev_state->state->print_mask &
2211 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2212 printk(KERN_INFO
2213 "bio_end_io() new %s flush_gen=%llu\n",
2214 dev_state->name,
Stefan Behrens5db02762011-11-01 17:04:16 +01002215 dev_state->last_flush_gen);
2216 }
2217 if (block->submit_bio_bh_rw & REQ_FUA)
2218 block->flush_gen = 0; /* FUA completed means block is
2219 * on disk */
2220 block->is_iodone = 1; /* for FLUSH, this releases the block */
2221 block = next_block;
2222 } while (NULL != block);
2223
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002224 bp->bi_end_io(bp);
Stefan Behrens5db02762011-11-01 17:04:16 +01002225}
2226
2227static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate)
2228{
2229 struct btrfsic_block *block = (struct btrfsic_block *)bh->b_private;
2230 int iodone_w_error = !uptodate;
2231 struct btrfsic_dev_state *dev_state;
2232
2233 BUG_ON(NULL == block);
2234 dev_state = block->dev_state;
2235 if ((dev_state->state->print_mask & BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2236 printk(KERN_INFO
2237 "bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n",
2238 iodone_w_error,
2239 btrfsic_get_block_type(dev_state->state, block),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002240 block->logical_bytenr, block->dev_state->name,
2241 block->dev_bytenr, block->mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002242
2243 block->iodone_w_error = iodone_w_error;
2244 if (block->submit_bio_bh_rw & REQ_FLUSH) {
2245 dev_state->last_flush_gen++;
2246 if ((dev_state->state->print_mask &
2247 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2248 printk(KERN_INFO
2249 "bh_end_io() new %s flush_gen=%llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002250 dev_state->name, dev_state->last_flush_gen);
Stefan Behrens5db02762011-11-01 17:04:16 +01002251 }
2252 if (block->submit_bio_bh_rw & REQ_FUA)
2253 block->flush_gen = 0; /* FUA completed means block is on disk */
2254
2255 bh->b_private = block->orig_bio_bh_private;
2256 bh->b_end_io = block->orig_bio_bh_end_io.bh;
2257 block->is_iodone = 1; /* for FLUSH, this releases the block */
2258 bh->b_end_io(bh, uptodate);
2259}
2260
2261static int btrfsic_process_written_superblock(
2262 struct btrfsic_state *state,
2263 struct btrfsic_block *const superblock,
2264 struct btrfs_super_block *const super_hdr)
2265{
2266 int pass;
2267
2268 superblock->generation = btrfs_super_generation(super_hdr);
2269 if (!(superblock->generation > state->max_superblock_generation ||
2270 0 == state->max_superblock_generation)) {
2271 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2272 printk(KERN_INFO
2273 "btrfsic: superblock @%llu (%s/%llu/%d)"
2274 " with old gen %llu <= %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002275 superblock->logical_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002276 superblock->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002277 superblock->dev_bytenr, superblock->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01002278 btrfs_super_generation(super_hdr),
Stefan Behrens5db02762011-11-01 17:04:16 +01002279 state->max_superblock_generation);
2280 } else {
2281 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2282 printk(KERN_INFO
2283 "btrfsic: got new superblock @%llu (%s/%llu/%d)"
2284 " with new gen %llu > %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002285 superblock->logical_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002286 superblock->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002287 superblock->dev_bytenr, superblock->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01002288 btrfs_super_generation(super_hdr),
Stefan Behrens5db02762011-11-01 17:04:16 +01002289 state->max_superblock_generation);
2290
2291 state->max_superblock_generation =
2292 btrfs_super_generation(super_hdr);
2293 state->latest_superblock = superblock;
2294 }
2295
2296 for (pass = 0; pass < 3; pass++) {
2297 int ret;
2298 u64 next_bytenr;
2299 struct btrfsic_block *next_block;
2300 struct btrfsic_block_data_ctx tmp_next_block_ctx;
2301 struct btrfsic_block_link *l;
2302 int num_copies;
2303 int mirror_num;
2304 const char *additional_string = NULL;
Stefan Behrens35a36212013-08-14 18:12:25 +02002305 struct btrfs_disk_key tmp_disk_key = {0};
Stefan Behrens5db02762011-11-01 17:04:16 +01002306
Qu Wenruo3cae2102013-07-16 11:19:18 +08002307 btrfs_set_disk_key_objectid(&tmp_disk_key,
2308 BTRFS_ROOT_ITEM_KEY);
2309 btrfs_set_disk_key_objectid(&tmp_disk_key, 0);
Stefan Behrens5db02762011-11-01 17:04:16 +01002310
2311 switch (pass) {
2312 case 0:
Qu Wenruo3cae2102013-07-16 11:19:18 +08002313 btrfs_set_disk_key_objectid(&tmp_disk_key,
2314 BTRFS_ROOT_TREE_OBJECTID);
Stefan Behrens5db02762011-11-01 17:04:16 +01002315 additional_string = "root ";
2316 next_bytenr = btrfs_super_root(super_hdr);
2317 if (state->print_mask &
2318 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002319 printk(KERN_INFO "root@%llu\n", next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002320 break;
2321 case 1:
Qu Wenruo3cae2102013-07-16 11:19:18 +08002322 btrfs_set_disk_key_objectid(&tmp_disk_key,
2323 BTRFS_CHUNK_TREE_OBJECTID);
Stefan Behrens5db02762011-11-01 17:04:16 +01002324 additional_string = "chunk ";
2325 next_bytenr = btrfs_super_chunk_root(super_hdr);
2326 if (state->print_mask &
2327 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002328 printk(KERN_INFO "chunk@%llu\n", next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002329 break;
2330 case 2:
Qu Wenruo3cae2102013-07-16 11:19:18 +08002331 btrfs_set_disk_key_objectid(&tmp_disk_key,
2332 BTRFS_TREE_LOG_OBJECTID);
Stefan Behrens5db02762011-11-01 17:04:16 +01002333 additional_string = "log ";
2334 next_bytenr = btrfs_super_log_root(super_hdr);
2335 if (0 == next_bytenr)
2336 continue;
2337 if (state->print_mask &
2338 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002339 printk(KERN_INFO "log@%llu\n", next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002340 break;
2341 }
2342
2343 num_copies =
Stefan Behrens5d964052012-11-05 14:59:07 +01002344 btrfs_num_copies(state->root->fs_info,
Stefan Behrense06baab2012-04-12 12:53:40 +02002345 next_bytenr, BTRFS_SUPER_INFO_SIZE);
Stefan Behrens5db02762011-11-01 17:04:16 +01002346 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
2347 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002348 next_bytenr, num_copies);
Stefan Behrens5db02762011-11-01 17:04:16 +01002349 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2350 int was_created;
2351
2352 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2353 printk(KERN_INFO
2354 "btrfsic_process_written_superblock("
2355 "mirror_num=%d)\n", mirror_num);
Stefan Behrense06baab2012-04-12 12:53:40 +02002356 ret = btrfsic_map_block(state, next_bytenr,
2357 BTRFS_SUPER_INFO_SIZE,
Stefan Behrens5db02762011-11-01 17:04:16 +01002358 &tmp_next_block_ctx,
2359 mirror_num);
2360 if (ret) {
2361 printk(KERN_INFO
2362 "btrfsic: btrfsic_map_block(@%llu,"
2363 " mirror=%d) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002364 next_bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002365 return -1;
2366 }
2367
2368 next_block = btrfsic_block_lookup_or_add(
2369 state,
2370 &tmp_next_block_ctx,
2371 additional_string,
2372 1, 0, 1,
2373 mirror_num,
2374 &was_created);
2375 if (NULL == next_block) {
2376 printk(KERN_INFO
2377 "btrfsic: error, kmalloc failed!\n");
2378 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2379 return -1;
2380 }
2381
2382 next_block->disk_key = tmp_disk_key;
2383 if (was_created)
2384 next_block->generation =
2385 BTRFSIC_GENERATION_UNKNOWN;
2386 l = btrfsic_block_link_lookup_or_add(
2387 state,
2388 &tmp_next_block_ctx,
2389 next_block,
2390 superblock,
2391 BTRFSIC_GENERATION_UNKNOWN);
2392 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2393 if (NULL == l)
2394 return -1;
2395 }
2396 }
2397
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302398 if (WARN_ON(-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)))
Stefan Behrens5db02762011-11-01 17:04:16 +01002399 btrfsic_dump_tree(state);
Stefan Behrens5db02762011-11-01 17:04:16 +01002400
2401 return 0;
2402}
2403
2404static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
2405 struct btrfsic_block *const block,
2406 int recursion_level)
2407{
Geliang Tangb69f2be2015-12-18 22:16:59 +08002408 const struct btrfsic_block_link *l;
Stefan Behrens5db02762011-11-01 17:04:16 +01002409 int ret = 0;
2410
2411 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2412 /*
2413 * Note that this situation can happen and does not
2414 * indicate an error in regular cases. It happens
2415 * when disk blocks are freed and later reused.
2416 * The check-integrity module is not aware of any
2417 * block free operations, it just recognizes block
2418 * write operations. Therefore it keeps the linkage
2419 * information for a block until a block is
2420 * rewritten. This can temporarily cause incorrect
2421 * and even circular linkage informations. This
2422 * causes no harm unless such blocks are referenced
2423 * by the most recent super block.
2424 */
2425 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2426 printk(KERN_INFO
2427 "btrfsic: abort cyclic linkage (case 1).\n");
2428
2429 return ret;
2430 }
2431
2432 /*
2433 * This algorithm is recursive because the amount of used stack
2434 * space is very small and the max recursion depth is limited.
2435 */
Geliang Tangb69f2be2015-12-18 22:16:59 +08002436 list_for_each_entry(l, &block->ref_to_list, node_ref_to) {
Stefan Behrens5db02762011-11-01 17:04:16 +01002437 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2438 printk(KERN_INFO
2439 "rl=%d, %c @%llu (%s/%llu/%d)"
2440 " %u* refers to %c @%llu (%s/%llu/%d)\n",
2441 recursion_level,
2442 btrfsic_get_block_type(state, block),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002443 block->logical_bytenr, block->dev_state->name,
2444 block->dev_bytenr, block->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01002445 l->ref_cnt,
2446 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens5db02762011-11-01 17:04:16 +01002447 l->block_ref_to->logical_bytenr,
2448 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002449 l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002450 l->block_ref_to->mirror_num);
2451 if (l->block_ref_to->never_written) {
2452 printk(KERN_INFO "btrfs: attempt to write superblock"
2453 " which references block %c @%llu (%s/%llu/%d)"
2454 " which is never written!\n",
2455 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens5db02762011-11-01 17:04:16 +01002456 l->block_ref_to->logical_bytenr,
2457 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002458 l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002459 l->block_ref_to->mirror_num);
2460 ret = -1;
2461 } else if (!l->block_ref_to->is_iodone) {
2462 printk(KERN_INFO "btrfs: attempt to write superblock"
2463 " which references block %c @%llu (%s/%llu/%d)"
2464 " which is not yet iodone!\n",
2465 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens5db02762011-11-01 17:04:16 +01002466 l->block_ref_to->logical_bytenr,
2467 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002468 l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002469 l->block_ref_to->mirror_num);
2470 ret = -1;
Stefan Behrens62856a92012-07-31 11:09:44 -06002471 } else if (l->block_ref_to->iodone_w_error) {
2472 printk(KERN_INFO "btrfs: attempt to write superblock"
2473 " which references block %c @%llu (%s/%llu/%d)"
2474 " which has write error!\n",
2475 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens62856a92012-07-31 11:09:44 -06002476 l->block_ref_to->logical_bytenr,
2477 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002478 l->block_ref_to->dev_bytenr,
Stefan Behrens62856a92012-07-31 11:09:44 -06002479 l->block_ref_to->mirror_num);
2480 ret = -1;
Stefan Behrens5db02762011-11-01 17:04:16 +01002481 } else if (l->parent_generation !=
2482 l->block_ref_to->generation &&
2483 BTRFSIC_GENERATION_UNKNOWN !=
2484 l->parent_generation &&
2485 BTRFSIC_GENERATION_UNKNOWN !=
2486 l->block_ref_to->generation) {
2487 printk(KERN_INFO "btrfs: attempt to write superblock"
2488 " which references block %c @%llu (%s/%llu/%d)"
2489 " with generation %llu !="
2490 " parent generation %llu!\n",
2491 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens5db02762011-11-01 17:04:16 +01002492 l->block_ref_to->logical_bytenr,
2493 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002494 l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002495 l->block_ref_to->mirror_num,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002496 l->block_ref_to->generation,
2497 l->parent_generation);
Stefan Behrens5db02762011-11-01 17:04:16 +01002498 ret = -1;
2499 } else if (l->block_ref_to->flush_gen >
2500 l->block_ref_to->dev_state->last_flush_gen) {
2501 printk(KERN_INFO "btrfs: attempt to write superblock"
2502 " which references block %c @%llu (%s/%llu/%d)"
2503 " which is not flushed out of disk's write cache"
2504 " (block flush_gen=%llu,"
2505 " dev->flush_gen=%llu)!\n",
2506 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens5db02762011-11-01 17:04:16 +01002507 l->block_ref_to->logical_bytenr,
2508 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002509 l->block_ref_to->dev_bytenr,
2510 l->block_ref_to->mirror_num, block->flush_gen,
Stefan Behrens5db02762011-11-01 17:04:16 +01002511 l->block_ref_to->dev_state->last_flush_gen);
2512 ret = -1;
2513 } else if (-1 == btrfsic_check_all_ref_blocks(state,
2514 l->block_ref_to,
2515 recursion_level +
2516 1)) {
2517 ret = -1;
2518 }
2519 }
2520
2521 return ret;
2522}
2523
2524static int btrfsic_is_block_ref_by_superblock(
2525 const struct btrfsic_state *state,
2526 const struct btrfsic_block *block,
2527 int recursion_level)
2528{
Geliang Tangb69f2be2015-12-18 22:16:59 +08002529 const struct btrfsic_block_link *l;
Stefan Behrens5db02762011-11-01 17:04:16 +01002530
2531 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2532 /* refer to comment at "abort cyclic linkage (case 1)" */
2533 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2534 printk(KERN_INFO
2535 "btrfsic: abort cyclic linkage (case 2).\n");
2536
2537 return 0;
2538 }
2539
2540 /*
2541 * This algorithm is recursive because the amount of used stack space
2542 * is very small and the max recursion depth is limited.
2543 */
Geliang Tangb69f2be2015-12-18 22:16:59 +08002544 list_for_each_entry(l, &block->ref_from_list, node_ref_from) {
Stefan Behrens5db02762011-11-01 17:04:16 +01002545 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2546 printk(KERN_INFO
2547 "rl=%d, %c @%llu (%s/%llu/%d)"
2548 " is ref %u* from %c @%llu (%s/%llu/%d)\n",
2549 recursion_level,
2550 btrfsic_get_block_type(state, block),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002551 block->logical_bytenr, block->dev_state->name,
2552 block->dev_bytenr, block->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01002553 l->ref_cnt,
2554 btrfsic_get_block_type(state, l->block_ref_from),
Stefan Behrens5db02762011-11-01 17:04:16 +01002555 l->block_ref_from->logical_bytenr,
2556 l->block_ref_from->dev_state->name,
Stefan Behrens5db02762011-11-01 17:04:16 +01002557 l->block_ref_from->dev_bytenr,
2558 l->block_ref_from->mirror_num);
2559 if (l->block_ref_from->is_superblock &&
2560 state->latest_superblock->dev_bytenr ==
2561 l->block_ref_from->dev_bytenr &&
2562 state->latest_superblock->dev_state->bdev ==
2563 l->block_ref_from->dev_state->bdev)
2564 return 1;
2565 else if (btrfsic_is_block_ref_by_superblock(state,
2566 l->block_ref_from,
2567 recursion_level +
2568 1))
2569 return 1;
2570 }
2571
2572 return 0;
2573}
2574
2575static void btrfsic_print_add_link(const struct btrfsic_state *state,
2576 const struct btrfsic_block_link *l)
2577{
2578 printk(KERN_INFO
2579 "Add %u* link from %c @%llu (%s/%llu/%d)"
2580 " to %c @%llu (%s/%llu/%d).\n",
2581 l->ref_cnt,
2582 btrfsic_get_block_type(state, l->block_ref_from),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002583 l->block_ref_from->logical_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002584 l->block_ref_from->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002585 l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01002586 btrfsic_get_block_type(state, l->block_ref_to),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002587 l->block_ref_to->logical_bytenr,
2588 l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002589 l->block_ref_to->mirror_num);
2590}
2591
2592static void btrfsic_print_rem_link(const struct btrfsic_state *state,
2593 const struct btrfsic_block_link *l)
2594{
2595 printk(KERN_INFO
2596 "Rem %u* link from %c @%llu (%s/%llu/%d)"
2597 " to %c @%llu (%s/%llu/%d).\n",
2598 l->ref_cnt,
2599 btrfsic_get_block_type(state, l->block_ref_from),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002600 l->block_ref_from->logical_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002601 l->block_ref_from->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002602 l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01002603 btrfsic_get_block_type(state, l->block_ref_to),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002604 l->block_ref_to->logical_bytenr,
2605 l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002606 l->block_ref_to->mirror_num);
2607}
2608
2609static char btrfsic_get_block_type(const struct btrfsic_state *state,
2610 const struct btrfsic_block *block)
2611{
2612 if (block->is_superblock &&
2613 state->latest_superblock->dev_bytenr == block->dev_bytenr &&
2614 state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
2615 return 'S';
2616 else if (block->is_superblock)
2617 return 's';
2618 else if (block->is_metadata)
2619 return 'M';
2620 else
2621 return 'D';
2622}
2623
2624static void btrfsic_dump_tree(const struct btrfsic_state *state)
2625{
2626 btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
2627}
2628
2629static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
2630 const struct btrfsic_block *block,
2631 int indent_level)
2632{
Geliang Tangb69f2be2015-12-18 22:16:59 +08002633 const struct btrfsic_block_link *l;
Stefan Behrens5db02762011-11-01 17:04:16 +01002634 int indent_add;
2635 static char buf[80];
2636 int cursor_position;
2637
2638 /*
2639 * Should better fill an on-stack buffer with a complete line and
2640 * dump it at once when it is time to print a newline character.
2641 */
2642
2643 /*
2644 * This algorithm is recursive because the amount of used stack space
2645 * is very small and the max recursion depth is limited.
2646 */
2647 indent_add = sprintf(buf, "%c-%llu(%s/%llu/%d)",
2648 btrfsic_get_block_type(state, block),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002649 block->logical_bytenr, block->dev_state->name,
2650 block->dev_bytenr, block->mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002651 if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2652 printk("[...]\n");
2653 return;
2654 }
2655 printk(buf);
2656 indent_level += indent_add;
2657 if (list_empty(&block->ref_to_list)) {
2658 printk("\n");
2659 return;
2660 }
2661 if (block->mirror_num > 1 &&
2662 !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
2663 printk(" [...]\n");
2664 return;
2665 }
2666
2667 cursor_position = indent_level;
Geliang Tangb69f2be2015-12-18 22:16:59 +08002668 list_for_each_entry(l, &block->ref_to_list, node_ref_to) {
Stefan Behrens5db02762011-11-01 17:04:16 +01002669 while (cursor_position < indent_level) {
2670 printk(" ");
2671 cursor_position++;
2672 }
2673 if (l->ref_cnt > 1)
2674 indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
2675 else
2676 indent_add = sprintf(buf, " --> ");
2677 if (indent_level + indent_add >
2678 BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2679 printk("[...]\n");
2680 cursor_position = 0;
2681 continue;
2682 }
2683
2684 printk(buf);
2685
2686 btrfsic_dump_tree_sub(state, l->block_ref_to,
2687 indent_level + indent_add);
2688 cursor_position = 0;
2689 }
2690}
2691
2692static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
2693 struct btrfsic_state *state,
2694 struct btrfsic_block_data_ctx *next_block_ctx,
2695 struct btrfsic_block *next_block,
2696 struct btrfsic_block *from_block,
2697 u64 parent_generation)
2698{
2699 struct btrfsic_block_link *l;
2700
2701 l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
2702 next_block_ctx->dev_bytenr,
2703 from_block->dev_state->bdev,
2704 from_block->dev_bytenr,
2705 &state->block_link_hashtable);
2706 if (NULL == l) {
2707 l = btrfsic_block_link_alloc();
2708 if (NULL == l) {
2709 printk(KERN_INFO
2710 "btrfsic: error, kmalloc" " failed!\n");
2711 return NULL;
2712 }
2713
2714 l->block_ref_to = next_block;
2715 l->block_ref_from = from_block;
2716 l->ref_cnt = 1;
2717 l->parent_generation = parent_generation;
2718
2719 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2720 btrfsic_print_add_link(state, l);
2721
2722 list_add(&l->node_ref_to, &from_block->ref_to_list);
2723 list_add(&l->node_ref_from, &next_block->ref_from_list);
2724
2725 btrfsic_block_link_hashtable_add(l,
2726 &state->block_link_hashtable);
2727 } else {
2728 l->ref_cnt++;
2729 l->parent_generation = parent_generation;
2730 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2731 btrfsic_print_add_link(state, l);
2732 }
2733
2734 return l;
2735}
2736
2737static struct btrfsic_block *btrfsic_block_lookup_or_add(
2738 struct btrfsic_state *state,
2739 struct btrfsic_block_data_ctx *block_ctx,
2740 const char *additional_string,
2741 int is_metadata,
2742 int is_iodone,
2743 int never_written,
2744 int mirror_num,
2745 int *was_created)
2746{
2747 struct btrfsic_block *block;
2748
2749 block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
2750 block_ctx->dev_bytenr,
2751 &state->block_hashtable);
2752 if (NULL == block) {
2753 struct btrfsic_dev_state *dev_state;
2754
2755 block = btrfsic_block_alloc();
2756 if (NULL == block) {
2757 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
2758 return NULL;
2759 }
2760 dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev);
2761 if (NULL == dev_state) {
2762 printk(KERN_INFO
2763 "btrfsic: error, lookup dev_state failed!\n");
2764 btrfsic_block_free(block);
2765 return NULL;
2766 }
2767 block->dev_state = dev_state;
2768 block->dev_bytenr = block_ctx->dev_bytenr;
2769 block->logical_bytenr = block_ctx->start;
2770 block->is_metadata = is_metadata;
2771 block->is_iodone = is_iodone;
2772 block->never_written = never_written;
2773 block->mirror_num = mirror_num;
2774 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2775 printk(KERN_INFO
2776 "New %s%c-block @%llu (%s/%llu/%d)\n",
2777 additional_string,
2778 btrfsic_get_block_type(state, block),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002779 block->logical_bytenr, dev_state->name,
2780 block->dev_bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002781 list_add(&block->all_blocks_node, &state->all_blocks_list);
2782 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2783 if (NULL != was_created)
2784 *was_created = 1;
2785 } else {
2786 if (NULL != was_created)
2787 *was_created = 0;
2788 }
2789
2790 return block;
2791}
2792
2793static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
2794 u64 bytenr,
2795 struct btrfsic_dev_state *dev_state,
Stefan Behrense06baab2012-04-12 12:53:40 +02002796 u64 dev_bytenr)
Stefan Behrens5db02762011-11-01 17:04:16 +01002797{
2798 int num_copies;
2799 int mirror_num;
2800 int ret;
2801 struct btrfsic_block_data_ctx block_ctx;
2802 int match = 0;
2803
Stefan Behrens5d964052012-11-05 14:59:07 +01002804 num_copies = btrfs_num_copies(state->root->fs_info,
Stefan Behrense06baab2012-04-12 12:53:40 +02002805 bytenr, state->metablock_size);
Stefan Behrens5db02762011-11-01 17:04:16 +01002806
2807 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
Stefan Behrense06baab2012-04-12 12:53:40 +02002808 ret = btrfsic_map_block(state, bytenr, state->metablock_size,
Stefan Behrens5db02762011-11-01 17:04:16 +01002809 &block_ctx, mirror_num);
2810 if (ret) {
2811 printk(KERN_INFO "btrfsic:"
2812 " btrfsic_map_block(logical @%llu,"
2813 " mirror %d) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002814 bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002815 continue;
2816 }
2817
2818 if (dev_state->bdev == block_ctx.dev->bdev &&
2819 dev_bytenr == block_ctx.dev_bytenr) {
2820 match++;
2821 btrfsic_release_block_ctx(&block_ctx);
2822 break;
2823 }
2824 btrfsic_release_block_ctx(&block_ctx);
2825 }
2826
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302827 if (WARN_ON(!match)) {
Stefan Behrens5db02762011-11-01 17:04:16 +01002828 printk(KERN_INFO "btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio,"
2829 " buffer->log_bytenr=%llu, submit_bio(bdev=%s,"
2830 " phys_bytenr=%llu)!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002831 bytenr, dev_state->name, dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002832 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
Stefan Behrense06baab2012-04-12 12:53:40 +02002833 ret = btrfsic_map_block(state, bytenr,
2834 state->metablock_size,
Stefan Behrens5db02762011-11-01 17:04:16 +01002835 &block_ctx, mirror_num);
2836 if (ret)
2837 continue;
2838
2839 printk(KERN_INFO "Read logical bytenr @%llu maps to"
2840 " (%s/%llu/%d)\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002841 bytenr, block_ctx.dev->name,
2842 block_ctx.dev_bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002843 }
Stefan Behrens5db02762011-11-01 17:04:16 +01002844 }
2845}
2846
2847static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
2848 struct block_device *bdev)
2849{
2850 struct btrfsic_dev_state *ds;
2851
2852 ds = btrfsic_dev_state_hashtable_lookup(bdev,
2853 &btrfsic_dev_state_hashtable);
2854 return ds;
2855}
2856
2857int btrfsic_submit_bh(int rw, struct buffer_head *bh)
2858{
2859 struct btrfsic_dev_state *dev_state;
2860
2861 if (!btrfsic_is_initialized)
2862 return submit_bh(rw, bh);
2863
2864 mutex_lock(&btrfsic_mutex);
2865 /* since btrfsic_submit_bh() might also be called before
2866 * btrfsic_mount(), this might return NULL */
2867 dev_state = btrfsic_dev_state_lookup(bh->b_bdev);
2868
2869 /* Only called to write the superblock (incl. FLUSH/FUA) */
2870 if (NULL != dev_state &&
2871 (rw & WRITE) && bh->b_size > 0) {
2872 u64 dev_bytenr;
2873
2874 dev_bytenr = 4096 * bh->b_blocknr;
2875 if (dev_state->state->print_mask &
2876 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2877 printk(KERN_INFO
Geert Uytterhoevenfce29362013-08-20 13:20:17 +02002878 "submit_bh(rw=0x%x, blocknr=%llu (bytenr %llu),"
Geert Uytterhoeven8d78eb12013-08-20 13:20:18 +02002879 " size=%zu, data=%p, bdev=%p)\n",
Geert Uytterhoevenfce29362013-08-20 13:20:17 +02002880 rw, (unsigned long long)bh->b_blocknr,
Geert Uytterhoeven8d78eb12013-08-20 13:20:18 +02002881 dev_bytenr, bh->b_size, bh->b_data, bh->b_bdev);
Stefan Behrens5db02762011-11-01 17:04:16 +01002882 btrfsic_process_written_block(dev_state, dev_bytenr,
Stefan Behrense06baab2012-04-12 12:53:40 +02002883 &bh->b_data, 1, NULL,
Stefan Behrens5db02762011-11-01 17:04:16 +01002884 NULL, bh, rw);
2885 } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
2886 if (dev_state->state->print_mask &
2887 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2888 printk(KERN_INFO
Stefan Behrense06baab2012-04-12 12:53:40 +02002889 "submit_bh(rw=0x%x FLUSH, bdev=%p)\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002890 rw, bh->b_bdev);
2891 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
2892 if ((dev_state->state->print_mask &
2893 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
2894 BTRFSIC_PRINT_MASK_VERBOSE)))
2895 printk(KERN_INFO
2896 "btrfsic_submit_bh(%s) with FLUSH"
2897 " but dummy block already in use"
2898 " (ignored)!\n",
2899 dev_state->name);
2900 } else {
2901 struct btrfsic_block *const block =
2902 &dev_state->dummy_block_for_bio_bh_flush;
2903
2904 block->is_iodone = 0;
2905 block->never_written = 0;
2906 block->iodone_w_error = 0;
2907 block->flush_gen = dev_state->last_flush_gen + 1;
2908 block->submit_bio_bh_rw = rw;
2909 block->orig_bio_bh_private = bh->b_private;
2910 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2911 block->next_in_same_bio = NULL;
2912 bh->b_private = block;
2913 bh->b_end_io = btrfsic_bh_end_io;
2914 }
2915 }
2916 mutex_unlock(&btrfsic_mutex);
2917 return submit_bh(rw, bh);
2918}
2919
Kent Overstreet33879d42013-11-23 22:33:32 -08002920static void __btrfsic_submit_bio(int rw, struct bio *bio)
Stefan Behrens5db02762011-11-01 17:04:16 +01002921{
2922 struct btrfsic_dev_state *dev_state;
2923
Kent Overstreet33879d42013-11-23 22:33:32 -08002924 if (!btrfsic_is_initialized)
Stefan Behrens5db02762011-11-01 17:04:16 +01002925 return;
Stefan Behrens5db02762011-11-01 17:04:16 +01002926
2927 mutex_lock(&btrfsic_mutex);
2928 /* since btrfsic_submit_bio() is also called before
2929 * btrfsic_mount(), this might return NULL */
2930 dev_state = btrfsic_dev_state_lookup(bio->bi_bdev);
2931 if (NULL != dev_state &&
2932 (rw & WRITE) && NULL != bio->bi_io_vec) {
2933 unsigned int i;
2934 u64 dev_bytenr;
Stefan Behrens56d140f2013-11-13 17:19:08 +01002935 u64 cur_bytenr;
Stefan Behrens5db02762011-11-01 17:04:16 +01002936 int bio_is_patched;
Stefan Behrense06baab2012-04-12 12:53:40 +02002937 char **mapped_datav;
Stefan Behrens5db02762011-11-01 17:04:16 +01002938
Kent Overstreet4f024f32013-10-11 15:44:27 -07002939 dev_bytenr = 512 * bio->bi_iter.bi_sector;
Stefan Behrens5db02762011-11-01 17:04:16 +01002940 bio_is_patched = 0;
2941 if (dev_state->state->print_mask &
2942 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2943 printk(KERN_INFO
2944 "submit_bio(rw=0x%x, bi_vcnt=%u,"
Geert Uytterhoevenfce29362013-08-20 13:20:17 +02002945 " bi_sector=%llu (bytenr %llu), bi_bdev=%p)\n",
2946 rw, bio->bi_vcnt,
Kent Overstreet4f024f32013-10-11 15:44:27 -07002947 (unsigned long long)bio->bi_iter.bi_sector,
2948 dev_bytenr, bio->bi_bdev);
Stefan Behrens5db02762011-11-01 17:04:16 +01002949
David Sterba31e818f2015-02-20 18:00:26 +01002950 mapped_datav = kmalloc_array(bio->bi_vcnt,
2951 sizeof(*mapped_datav), GFP_NOFS);
Stefan Behrense06baab2012-04-12 12:53:40 +02002952 if (!mapped_datav)
2953 goto leave;
Stefan Behrens56d140f2013-11-13 17:19:08 +01002954 cur_bytenr = dev_bytenr;
Stefan Behrens5db02762011-11-01 17:04:16 +01002955 for (i = 0; i < bio->bi_vcnt; i++) {
Stefan Behrense06baab2012-04-12 12:53:40 +02002956 BUG_ON(bio->bi_io_vec[i].bv_len != PAGE_CACHE_SIZE);
2957 mapped_datav[i] = kmap(bio->bi_io_vec[i].bv_page);
2958 if (!mapped_datav[i]) {
2959 while (i > 0) {
2960 i--;
2961 kunmap(bio->bi_io_vec[i].bv_page);
2962 }
2963 kfree(mapped_datav);
2964 goto leave;
2965 }
Stefan Behrens56d140f2013-11-13 17:19:08 +01002966 if (dev_state->state->print_mask &
2967 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE)
Stefan Behrens5db02762011-11-01 17:04:16 +01002968 printk(KERN_INFO
Stefan Behrens56d140f2013-11-13 17:19:08 +01002969 "#%u: bytenr=%llu, len=%u, offset=%u\n",
2970 i, cur_bytenr, bio->bi_io_vec[i].bv_len,
Stefan Behrens5db02762011-11-01 17:04:16 +01002971 bio->bi_io_vec[i].bv_offset);
Stefan Behrens56d140f2013-11-13 17:19:08 +01002972 cur_bytenr += bio->bi_io_vec[i].bv_len;
Stefan Behrens5db02762011-11-01 17:04:16 +01002973 }
Stefan Behrense06baab2012-04-12 12:53:40 +02002974 btrfsic_process_written_block(dev_state, dev_bytenr,
2975 mapped_datav, bio->bi_vcnt,
2976 bio, &bio_is_patched,
2977 NULL, rw);
2978 while (i > 0) {
2979 i--;
2980 kunmap(bio->bi_io_vec[i].bv_page);
2981 }
2982 kfree(mapped_datav);
Stefan Behrens5db02762011-11-01 17:04:16 +01002983 } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
2984 if (dev_state->state->print_mask &
2985 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2986 printk(KERN_INFO
Stefan Behrense06baab2012-04-12 12:53:40 +02002987 "submit_bio(rw=0x%x FLUSH, bdev=%p)\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002988 rw, bio->bi_bdev);
2989 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
2990 if ((dev_state->state->print_mask &
2991 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
2992 BTRFSIC_PRINT_MASK_VERBOSE)))
2993 printk(KERN_INFO
2994 "btrfsic_submit_bio(%s) with FLUSH"
2995 " but dummy block already in use"
2996 " (ignored)!\n",
2997 dev_state->name);
2998 } else {
2999 struct btrfsic_block *const block =
3000 &dev_state->dummy_block_for_bio_bh_flush;
3001
3002 block->is_iodone = 0;
3003 block->never_written = 0;
3004 block->iodone_w_error = 0;
3005 block->flush_gen = dev_state->last_flush_gen + 1;
3006 block->submit_bio_bh_rw = rw;
3007 block->orig_bio_bh_private = bio->bi_private;
3008 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
3009 block->next_in_same_bio = NULL;
3010 bio->bi_private = block;
3011 bio->bi_end_io = btrfsic_bio_end_io;
3012 }
3013 }
Stefan Behrense06baab2012-04-12 12:53:40 +02003014leave:
Stefan Behrens5db02762011-11-01 17:04:16 +01003015 mutex_unlock(&btrfsic_mutex);
Kent Overstreet33879d42013-11-23 22:33:32 -08003016}
Stefan Behrens5db02762011-11-01 17:04:16 +01003017
Kent Overstreet33879d42013-11-23 22:33:32 -08003018void btrfsic_submit_bio(int rw, struct bio *bio)
3019{
3020 __btrfsic_submit_bio(rw, bio);
Stefan Behrens5db02762011-11-01 17:04:16 +01003021 submit_bio(rw, bio);
3022}
3023
Kent Overstreet33879d42013-11-23 22:33:32 -08003024int btrfsic_submit_bio_wait(int rw, struct bio *bio)
3025{
3026 __btrfsic_submit_bio(rw, bio);
3027 return submit_bio_wait(rw, bio);
3028}
3029
Stefan Behrens5db02762011-11-01 17:04:16 +01003030int btrfsic_mount(struct btrfs_root *root,
3031 struct btrfs_fs_devices *fs_devices,
3032 int including_extent_data, u32 print_mask)
3033{
3034 int ret;
3035 struct btrfsic_state *state;
3036 struct list_head *dev_head = &fs_devices->devices;
3037 struct btrfs_device *device;
3038
Stefan Behrense06baab2012-04-12 12:53:40 +02003039 if (root->nodesize & ((u64)PAGE_CACHE_SIZE - 1)) {
3040 printk(KERN_INFO
3041 "btrfsic: cannot handle nodesize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02003042 root->nodesize, PAGE_CACHE_SIZE);
Stefan Behrense06baab2012-04-12 12:53:40 +02003043 return -1;
3044 }
Stefan Behrense06baab2012-04-12 12:53:40 +02003045 if (root->sectorsize & ((u64)PAGE_CACHE_SIZE - 1)) {
3046 printk(KERN_INFO
3047 "btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02003048 root->sectorsize, PAGE_CACHE_SIZE);
Stefan Behrense06baab2012-04-12 12:53:40 +02003049 return -1;
3050 }
Shilong Wang6b3a4d62014-10-10 17:35:59 -04003051 state = kzalloc(sizeof(*state), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
3052 if (!state) {
3053 state = vzalloc(sizeof(*state));
3054 if (!state) {
3055 printk(KERN_INFO "btrfs check-integrity: vzalloc() failed!\n");
3056 return -1;
3057 }
Stefan Behrens5db02762011-11-01 17:04:16 +01003058 }
3059
3060 if (!btrfsic_is_initialized) {
3061 mutex_init(&btrfsic_mutex);
3062 btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
3063 btrfsic_is_initialized = 1;
3064 }
3065 mutex_lock(&btrfsic_mutex);
3066 state->root = root;
3067 state->print_mask = print_mask;
3068 state->include_extent_data = including_extent_data;
3069 state->csum_size = 0;
Stefan Behrense06baab2012-04-12 12:53:40 +02003070 state->metablock_size = root->nodesize;
3071 state->datablock_size = root->sectorsize;
Stefan Behrens5db02762011-11-01 17:04:16 +01003072 INIT_LIST_HEAD(&state->all_blocks_list);
3073 btrfsic_block_hashtable_init(&state->block_hashtable);
3074 btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
3075 state->max_superblock_generation = 0;
3076 state->latest_superblock = NULL;
3077
3078 list_for_each_entry(device, dev_head, dev_list) {
3079 struct btrfsic_dev_state *ds;
Rasmus Villemoes02def692015-11-27 09:42:11 +01003080 const char *p;
Stefan Behrens5db02762011-11-01 17:04:16 +01003081
3082 if (!device->bdev || !device->name)
3083 continue;
3084
3085 ds = btrfsic_dev_state_alloc();
3086 if (NULL == ds) {
3087 printk(KERN_INFO
3088 "btrfs check-integrity: kmalloc() failed!\n");
3089 mutex_unlock(&btrfsic_mutex);
3090 return -1;
3091 }
3092 ds->bdev = device->bdev;
3093 ds->state = state;
3094 bdevname(ds->bdev, ds->name);
3095 ds->name[BDEVNAME_SIZE - 1] = '\0';
Rasmus Villemoes02def692015-11-27 09:42:11 +01003096 p = kbasename(ds->name);
Stefan Behrens5db02762011-11-01 17:04:16 +01003097 strlcpy(ds->name, p, sizeof(ds->name));
3098 btrfsic_dev_state_hashtable_add(ds,
3099 &btrfsic_dev_state_hashtable);
3100 }
3101
3102 ret = btrfsic_process_superblock(state, fs_devices);
3103 if (0 != ret) {
3104 mutex_unlock(&btrfsic_mutex);
3105 btrfsic_unmount(root, fs_devices);
3106 return ret;
3107 }
3108
3109 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
3110 btrfsic_dump_database(state);
3111 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
3112 btrfsic_dump_tree(state);
3113
3114 mutex_unlock(&btrfsic_mutex);
3115 return 0;
3116}
3117
3118void btrfsic_unmount(struct btrfs_root *root,
3119 struct btrfs_fs_devices *fs_devices)
3120{
Geliang Tangb69f2be2015-12-18 22:16:59 +08003121 struct btrfsic_block *b_all, *tmp_all;
Stefan Behrens5db02762011-11-01 17:04:16 +01003122 struct btrfsic_state *state;
3123 struct list_head *dev_head = &fs_devices->devices;
3124 struct btrfs_device *device;
3125
3126 if (!btrfsic_is_initialized)
3127 return;
3128
3129 mutex_lock(&btrfsic_mutex);
3130
3131 state = NULL;
3132 list_for_each_entry(device, dev_head, dev_list) {
3133 struct btrfsic_dev_state *ds;
3134
3135 if (!device->bdev || !device->name)
3136 continue;
3137
3138 ds = btrfsic_dev_state_hashtable_lookup(
3139 device->bdev,
3140 &btrfsic_dev_state_hashtable);
3141 if (NULL != ds) {
3142 state = ds->state;
3143 btrfsic_dev_state_hashtable_remove(ds);
3144 btrfsic_dev_state_free(ds);
3145 }
3146 }
3147
3148 if (NULL == state) {
3149 printk(KERN_INFO
3150 "btrfsic: error, cannot find state information"
3151 " on umount!\n");
3152 mutex_unlock(&btrfsic_mutex);
3153 return;
3154 }
3155
3156 /*
3157 * Don't care about keeping the lists' state up to date,
3158 * just free all memory that was allocated dynamically.
3159 * Free the blocks and the block_links.
3160 */
Geliang Tangb69f2be2015-12-18 22:16:59 +08003161 list_for_each_entry_safe(b_all, tmp_all, &state->all_blocks_list,
3162 all_blocks_node) {
3163 struct btrfsic_block_link *l, *tmp;
Stefan Behrens5db02762011-11-01 17:04:16 +01003164
Geliang Tangb69f2be2015-12-18 22:16:59 +08003165 list_for_each_entry_safe(l, tmp, &b_all->ref_to_list,
3166 node_ref_to) {
Stefan Behrens5db02762011-11-01 17:04:16 +01003167 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
3168 btrfsic_print_rem_link(state, l);
3169
3170 l->ref_cnt--;
3171 if (0 == l->ref_cnt)
3172 btrfsic_block_link_free(l);
3173 }
3174
Stefan Behrens48235a62012-05-23 17:57:49 +02003175 if (b_all->is_iodone || b_all->never_written)
Stefan Behrens5db02762011-11-01 17:04:16 +01003176 btrfsic_block_free(b_all);
3177 else
3178 printk(KERN_INFO "btrfs: attempt to free %c-block"
3179 " @%llu (%s/%llu/%d) on umount which is"
3180 " not yet iodone!\n",
3181 btrfsic_get_block_type(state, b_all),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003182 b_all->logical_bytenr, b_all->dev_state->name,
3183 b_all->dev_bytenr, b_all->mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01003184 }
3185
3186 mutex_unlock(&btrfsic_mutex);
3187
Wang Shilongf7493032014-11-22 21:13:10 +08003188 kvfree(state);
Stefan Behrens5db02762011-11-01 17:04:16 +01003189}