blob: 1fee5fe93484e69a7497c3947630c85a0ee7a32d [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>
David Sterba818e0102017-05-31 18:40:02 +020097#include <linux/mm.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"
Anand Jainebb87652016-03-10 17:26:59 +0800109#include "compression.h"
Stefan Behrens5db02762011-11-01 17:04:16 +0100110
111#define BTRFSIC_BLOCK_HASHTABLE_SIZE 0x10000
112#define BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE 0x10000
113#define BTRFSIC_DEV2STATE_HASHTABLE_SIZE 0x100
114#define BTRFSIC_BLOCK_MAGIC_NUMBER 0x14491051
115#define BTRFSIC_BLOCK_LINK_MAGIC_NUMBER 0x11070807
116#define BTRFSIC_DEV2STATE_MAGIC_NUMBER 0x20111530
117#define BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER 20111300
118#define BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL (200 - 6) /* in characters,
119 * excluding " [...]" */
Stefan Behrens5db02762011-11-01 17:04:16 +0100120#define BTRFSIC_GENERATION_UNKNOWN ((u64)-1)
121
122/*
123 * The definition of the bitmask fields for the print_mask.
124 * They are specified with the mount option check_integrity_print_mask.
125 */
126#define BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE 0x00000001
127#define BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION 0x00000002
128#define BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE 0x00000004
129#define BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE 0x00000008
130#define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH 0x00000010
131#define BTRFSIC_PRINT_MASK_END_IO_BIO_BH 0x00000020
132#define BTRFSIC_PRINT_MASK_VERBOSE 0x00000040
133#define BTRFSIC_PRINT_MASK_VERY_VERBOSE 0x00000080
134#define BTRFSIC_PRINT_MASK_INITIAL_TREE 0x00000100
135#define BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES 0x00000200
136#define BTRFSIC_PRINT_MASK_INITIAL_DATABASE 0x00000400
137#define BTRFSIC_PRINT_MASK_NUM_COPIES 0x00000800
138#define BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS 0x00001000
Stefan Behrens56d140f2013-11-13 17:19:08 +0100139#define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE 0x00002000
Stefan Behrens5db02762011-11-01 17:04:16 +0100140
141struct btrfsic_dev_state;
142struct btrfsic_state;
143
144struct btrfsic_block {
145 u32 magic_num; /* only used for debug purposes */
146 unsigned int is_metadata:1; /* if it is meta-data, not data-data */
147 unsigned int is_superblock:1; /* if it is one of the superblocks */
148 unsigned int is_iodone:1; /* if is done by lower subsystem */
149 unsigned int iodone_w_error:1; /* error was indicated to endio */
150 unsigned int never_written:1; /* block was added because it was
151 * referenced, not because it was
152 * written */
Stefan Behrenscb3806e2012-11-27 16:10:21 +0000153 unsigned int mirror_num; /* large enough to hold
Stefan Behrens5db02762011-11-01 17:04:16 +0100154 * BTRFS_SUPER_MIRROR_MAX */
155 struct btrfsic_dev_state *dev_state;
156 u64 dev_bytenr; /* key, physical byte num on disk */
157 u64 logical_bytenr; /* logical byte num on disk */
158 u64 generation;
159 struct btrfs_disk_key disk_key; /* extra info to print in case of
160 * issues, will not always be correct */
161 struct list_head collision_resolving_node; /* list node */
162 struct list_head all_blocks_node; /* list node */
163
164 /* the following two lists contain block_link items */
165 struct list_head ref_to_list; /* list */
166 struct list_head ref_from_list; /* list */
167 struct btrfsic_block *next_in_same_bio;
168 void *orig_bio_bh_private;
169 union {
170 bio_end_io_t *bio;
171 bh_end_io_t *bh;
172 } orig_bio_bh_end_io;
173 int submit_bio_bh_rw;
174 u64 flush_gen; /* only valid if !never_written */
175};
176
177/*
178 * Elements of this type are allocated dynamically and required because
179 * each block object can refer to and can be ref from multiple blocks.
180 * The key to lookup them in the hashtable is the dev_bytenr of
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -0800181 * the block ref to plus the one from the block referred from.
Stefan Behrens5db02762011-11-01 17:04:16 +0100182 * The fact that they are searchable via a hashtable and that a
183 * ref_cnt is maintained is not required for the btrfs integrity
184 * check algorithm itself, it is only used to make the output more
185 * beautiful in case that an error is detected (an error is defined
186 * as a write operation to a block while that block is still referenced).
187 */
188struct btrfsic_block_link {
189 u32 magic_num; /* only used for debug purposes */
190 u32 ref_cnt;
191 struct list_head node_ref_to; /* list node */
192 struct list_head node_ref_from; /* list node */
193 struct list_head collision_resolving_node; /* list node */
194 struct btrfsic_block *block_ref_to;
195 struct btrfsic_block *block_ref_from;
196 u64 parent_generation;
197};
198
199struct btrfsic_dev_state {
200 u32 magic_num; /* only used for debug purposes */
201 struct block_device *bdev;
202 struct btrfsic_state *state;
203 struct list_head collision_resolving_node; /* list node */
204 struct btrfsic_block dummy_block_for_bio_bh_flush;
205 u64 last_flush_gen;
206 char name[BDEVNAME_SIZE];
207};
208
209struct btrfsic_block_hashtable {
210 struct list_head table[BTRFSIC_BLOCK_HASHTABLE_SIZE];
211};
212
213struct btrfsic_block_link_hashtable {
214 struct list_head table[BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE];
215};
216
217struct btrfsic_dev_state_hashtable {
218 struct list_head table[BTRFSIC_DEV2STATE_HASHTABLE_SIZE];
219};
220
221struct btrfsic_block_data_ctx {
222 u64 start; /* virtual bytenr */
223 u64 dev_bytenr; /* physical bytenr on device */
224 u32 len;
225 struct btrfsic_dev_state *dev;
Stefan Behrense06baab2012-04-12 12:53:40 +0200226 char **datav;
227 struct page **pagev;
228 void *mem_to_free;
Stefan Behrens5db02762011-11-01 17:04:16 +0100229};
230
231/* This structure is used to implement recursion without occupying
232 * any stack space, refer to btrfsic_process_metablock() */
233struct btrfsic_stack_frame {
234 u32 magic;
235 u32 nr;
236 int error;
237 int i;
238 int limit_nesting;
239 int num_copies;
240 int mirror_num;
241 struct btrfsic_block *block;
242 struct btrfsic_block_data_ctx *block_ctx;
243 struct btrfsic_block *next_block;
244 struct btrfsic_block_data_ctx next_block_ctx;
245 struct btrfs_header *hdr;
246 struct btrfsic_stack_frame *prev;
247};
248
249/* Some state per mounted filesystem */
250struct btrfsic_state {
251 u32 print_mask;
252 int include_extent_data;
253 int csum_size;
254 struct list_head all_blocks_list;
255 struct btrfsic_block_hashtable block_hashtable;
256 struct btrfsic_block_link_hashtable block_link_hashtable;
Jeff Mahoneyde143792016-06-22 18:54:36 -0400257 struct btrfs_fs_info *fs_info;
Stefan Behrens5db02762011-11-01 17:04:16 +0100258 u64 max_superblock_generation;
259 struct btrfsic_block *latest_superblock;
Stefan Behrense06baab2012-04-12 12:53:40 +0200260 u32 metablock_size;
261 u32 datablock_size;
Stefan Behrens5db02762011-11-01 17:04:16 +0100262};
263
264static void btrfsic_block_init(struct btrfsic_block *b);
265static struct btrfsic_block *btrfsic_block_alloc(void);
266static void btrfsic_block_free(struct btrfsic_block *b);
267static void btrfsic_block_link_init(struct btrfsic_block_link *n);
268static struct btrfsic_block_link *btrfsic_block_link_alloc(void);
269static void btrfsic_block_link_free(struct btrfsic_block_link *n);
270static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds);
271static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void);
272static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds);
273static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h);
274static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
275 struct btrfsic_block_hashtable *h);
276static void btrfsic_block_hashtable_remove(struct btrfsic_block *b);
277static struct btrfsic_block *btrfsic_block_hashtable_lookup(
278 struct block_device *bdev,
279 u64 dev_bytenr,
280 struct btrfsic_block_hashtable *h);
281static void btrfsic_block_link_hashtable_init(
282 struct btrfsic_block_link_hashtable *h);
283static void btrfsic_block_link_hashtable_add(
284 struct btrfsic_block_link *l,
285 struct btrfsic_block_link_hashtable *h);
286static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l);
287static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
288 struct block_device *bdev_ref_to,
289 u64 dev_bytenr_ref_to,
290 struct block_device *bdev_ref_from,
291 u64 dev_bytenr_ref_from,
292 struct btrfsic_block_link_hashtable *h);
293static void btrfsic_dev_state_hashtable_init(
294 struct btrfsic_dev_state_hashtable *h);
295static void btrfsic_dev_state_hashtable_add(
296 struct btrfsic_dev_state *ds,
297 struct btrfsic_dev_state_hashtable *h);
298static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds);
Christoph Hellwigf8f84b22017-08-23 19:10:27 +0200299static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(dev_t dev,
Stefan Behrens5db02762011-11-01 17:04:16 +0100300 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);
Christoph Hellwigf8f84b22017-08-23 19:10:27 +0200387static struct btrfsic_dev_state *btrfsic_dev_state_lookup(dev_t dev);
Stefan Behrens5db02762011-11-01 17:04:16 +0100388static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
389 u64 bytenr,
390 struct btrfsic_dev_state *dev_state,
Stefan Behrense06baab2012-04-12 12:53:40 +0200391 u64 dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +0100392
393static struct mutex btrfsic_mutex;
394static int btrfsic_is_initialized;
395static struct btrfsic_dev_state_hashtable btrfsic_dev_state_hashtable;
396
397
398static void btrfsic_block_init(struct btrfsic_block *b)
399{
400 b->magic_num = BTRFSIC_BLOCK_MAGIC_NUMBER;
401 b->dev_state = NULL;
402 b->dev_bytenr = 0;
403 b->logical_bytenr = 0;
404 b->generation = BTRFSIC_GENERATION_UNKNOWN;
405 b->disk_key.objectid = 0;
406 b->disk_key.type = 0;
407 b->disk_key.offset = 0;
408 b->is_metadata = 0;
409 b->is_superblock = 0;
410 b->is_iodone = 0;
411 b->iodone_w_error = 0;
412 b->never_written = 0;
413 b->mirror_num = 0;
414 b->next_in_same_bio = NULL;
415 b->orig_bio_bh_private = NULL;
416 b->orig_bio_bh_end_io.bio = NULL;
417 INIT_LIST_HEAD(&b->collision_resolving_node);
418 INIT_LIST_HEAD(&b->all_blocks_node);
419 INIT_LIST_HEAD(&b->ref_to_list);
420 INIT_LIST_HEAD(&b->ref_from_list);
421 b->submit_bio_bh_rw = 0;
422 b->flush_gen = 0;
423}
424
425static struct btrfsic_block *btrfsic_block_alloc(void)
426{
427 struct btrfsic_block *b;
428
429 b = kzalloc(sizeof(*b), GFP_NOFS);
430 if (NULL != b)
431 btrfsic_block_init(b);
432
433 return b;
434}
435
436static void btrfsic_block_free(struct btrfsic_block *b)
437{
438 BUG_ON(!(NULL == b || BTRFSIC_BLOCK_MAGIC_NUMBER == b->magic_num));
439 kfree(b);
440}
441
442static void btrfsic_block_link_init(struct btrfsic_block_link *l)
443{
444 l->magic_num = BTRFSIC_BLOCK_LINK_MAGIC_NUMBER;
445 l->ref_cnt = 1;
446 INIT_LIST_HEAD(&l->node_ref_to);
447 INIT_LIST_HEAD(&l->node_ref_from);
448 INIT_LIST_HEAD(&l->collision_resolving_node);
449 l->block_ref_to = NULL;
450 l->block_ref_from = NULL;
451}
452
453static struct btrfsic_block_link *btrfsic_block_link_alloc(void)
454{
455 struct btrfsic_block_link *l;
456
457 l = kzalloc(sizeof(*l), GFP_NOFS);
458 if (NULL != l)
459 btrfsic_block_link_init(l);
460
461 return l;
462}
463
464static void btrfsic_block_link_free(struct btrfsic_block_link *l)
465{
466 BUG_ON(!(NULL == l || BTRFSIC_BLOCK_LINK_MAGIC_NUMBER == l->magic_num));
467 kfree(l);
468}
469
470static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds)
471{
472 ds->magic_num = BTRFSIC_DEV2STATE_MAGIC_NUMBER;
473 ds->bdev = NULL;
474 ds->state = NULL;
475 ds->name[0] = '\0';
476 INIT_LIST_HEAD(&ds->collision_resolving_node);
477 ds->last_flush_gen = 0;
478 btrfsic_block_init(&ds->dummy_block_for_bio_bh_flush);
479 ds->dummy_block_for_bio_bh_flush.is_iodone = 1;
480 ds->dummy_block_for_bio_bh_flush.dev_state = ds;
481}
482
483static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void)
484{
485 struct btrfsic_dev_state *ds;
486
487 ds = kzalloc(sizeof(*ds), GFP_NOFS);
488 if (NULL != ds)
489 btrfsic_dev_state_init(ds);
490
491 return ds;
492}
493
494static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds)
495{
496 BUG_ON(!(NULL == ds ||
497 BTRFSIC_DEV2STATE_MAGIC_NUMBER == ds->magic_num));
498 kfree(ds);
499}
500
501static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h)
502{
503 int i;
504
505 for (i = 0; i < BTRFSIC_BLOCK_HASHTABLE_SIZE; i++)
506 INIT_LIST_HEAD(h->table + i);
507}
508
509static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
510 struct btrfsic_block_hashtable *h)
511{
512 const unsigned int hashval =
513 (((unsigned int)(b->dev_bytenr >> 16)) ^
514 ((unsigned int)((uintptr_t)b->dev_state->bdev))) &
515 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
516
517 list_add(&b->collision_resolving_node, h->table + hashval);
518}
519
520static void btrfsic_block_hashtable_remove(struct btrfsic_block *b)
521{
522 list_del(&b->collision_resolving_node);
523}
524
525static struct btrfsic_block *btrfsic_block_hashtable_lookup(
526 struct block_device *bdev,
527 u64 dev_bytenr,
528 struct btrfsic_block_hashtable *h)
529{
530 const unsigned int hashval =
531 (((unsigned int)(dev_bytenr >> 16)) ^
532 ((unsigned int)((uintptr_t)bdev))) &
533 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
Geliang Tangb69f2be2015-12-18 22:16:59 +0800534 struct btrfsic_block *b;
Stefan Behrens5db02762011-11-01 17:04:16 +0100535
Geliang Tangb69f2be2015-12-18 22:16:59 +0800536 list_for_each_entry(b, h->table + hashval, collision_resolving_node) {
Stefan Behrens5db02762011-11-01 17:04:16 +0100537 if (b->dev_state->bdev == bdev && b->dev_bytenr == dev_bytenr)
538 return b;
539 }
540
541 return NULL;
542}
543
544static void btrfsic_block_link_hashtable_init(
545 struct btrfsic_block_link_hashtable *h)
546{
547 int i;
548
549 for (i = 0; i < BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE; i++)
550 INIT_LIST_HEAD(h->table + i);
551}
552
553static void btrfsic_block_link_hashtable_add(
554 struct btrfsic_block_link *l,
555 struct btrfsic_block_link_hashtable *h)
556{
557 const unsigned int hashval =
558 (((unsigned int)(l->block_ref_to->dev_bytenr >> 16)) ^
559 ((unsigned int)(l->block_ref_from->dev_bytenr >> 16)) ^
560 ((unsigned int)((uintptr_t)l->block_ref_to->dev_state->bdev)) ^
561 ((unsigned int)((uintptr_t)l->block_ref_from->dev_state->bdev)))
562 & (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
563
564 BUG_ON(NULL == l->block_ref_to);
565 BUG_ON(NULL == l->block_ref_from);
566 list_add(&l->collision_resolving_node, h->table + hashval);
567}
568
569static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l)
570{
571 list_del(&l->collision_resolving_node);
572}
573
574static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
575 struct block_device *bdev_ref_to,
576 u64 dev_bytenr_ref_to,
577 struct block_device *bdev_ref_from,
578 u64 dev_bytenr_ref_from,
579 struct btrfsic_block_link_hashtable *h)
580{
581 const unsigned int hashval =
582 (((unsigned int)(dev_bytenr_ref_to >> 16)) ^
583 ((unsigned int)(dev_bytenr_ref_from >> 16)) ^
584 ((unsigned int)((uintptr_t)bdev_ref_to)) ^
585 ((unsigned int)((uintptr_t)bdev_ref_from))) &
586 (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
Geliang Tangb69f2be2015-12-18 22:16:59 +0800587 struct btrfsic_block_link *l;
Stefan Behrens5db02762011-11-01 17:04:16 +0100588
Geliang Tangb69f2be2015-12-18 22:16:59 +0800589 list_for_each_entry(l, h->table + hashval, collision_resolving_node) {
Stefan Behrens5db02762011-11-01 17:04:16 +0100590 BUG_ON(NULL == l->block_ref_to);
591 BUG_ON(NULL == l->block_ref_from);
592 if (l->block_ref_to->dev_state->bdev == bdev_ref_to &&
593 l->block_ref_to->dev_bytenr == dev_bytenr_ref_to &&
594 l->block_ref_from->dev_state->bdev == bdev_ref_from &&
595 l->block_ref_from->dev_bytenr == dev_bytenr_ref_from)
596 return l;
597 }
598
599 return NULL;
600}
601
602static void btrfsic_dev_state_hashtable_init(
603 struct btrfsic_dev_state_hashtable *h)
604{
605 int i;
606
607 for (i = 0; i < BTRFSIC_DEV2STATE_HASHTABLE_SIZE; i++)
608 INIT_LIST_HEAD(h->table + i);
609}
610
611static void btrfsic_dev_state_hashtable_add(
612 struct btrfsic_dev_state *ds,
613 struct btrfsic_dev_state_hashtable *h)
614{
615 const unsigned int hashval =
Gu JinXiang859a58a2017-10-11 16:44:27 +0800616 (((unsigned int)((uintptr_t)ds->bdev->bd_dev)) &
Stefan Behrens5db02762011-11-01 17:04:16 +0100617 (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
618
619 list_add(&ds->collision_resolving_node, h->table + hashval);
620}
621
622static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds)
623{
624 list_del(&ds->collision_resolving_node);
625}
626
Christoph Hellwigf8f84b22017-08-23 19:10:27 +0200627static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(dev_t dev,
Stefan Behrens5db02762011-11-01 17:04:16 +0100628 struct btrfsic_dev_state_hashtable *h)
629{
630 const unsigned int hashval =
Christoph Hellwigf8f84b22017-08-23 19:10:27 +0200631 dev & (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1);
Geliang Tangb69f2be2015-12-18 22:16:59 +0800632 struct btrfsic_dev_state *ds;
Stefan Behrens5db02762011-11-01 17:04:16 +0100633
Geliang Tangb69f2be2015-12-18 22:16:59 +0800634 list_for_each_entry(ds, h->table + hashval, collision_resolving_node) {
Christoph Hellwigf8f84b22017-08-23 19:10:27 +0200635 if (ds->bdev->bd_dev == dev)
Stefan Behrens5db02762011-11-01 17:04:16 +0100636 return ds;
637 }
638
639 return NULL;
640}
641
642static int btrfsic_process_superblock(struct btrfsic_state *state,
643 struct btrfs_fs_devices *fs_devices)
644{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400645 struct btrfs_fs_info *fs_info = state->fs_info;
Stefan Behrens5db02762011-11-01 17:04:16 +0100646 struct btrfs_super_block *selected_super;
647 struct list_head *dev_head = &fs_devices->devices;
648 struct btrfs_device *device;
649 struct btrfsic_dev_state *selected_dev_state = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400650 int ret = 0;
Stefan Behrens5db02762011-11-01 17:04:16 +0100651 int pass;
652
653 BUG_ON(NULL == state);
Stefan Behrense06baab2012-04-12 12:53:40 +0200654 selected_super = kzalloc(sizeof(*selected_super), GFP_NOFS);
Stefan Behrens5db02762011-11-01 17:04:16 +0100655 if (NULL == selected_super) {
Jeff Mahoney62e85572016-09-20 10:05:01 -0400656 pr_info("btrfsic: error, kmalloc failed!\n");
Luis de Bethencourt0b8d8ce2015-10-20 14:56:22 +0100657 return -ENOMEM;
Stefan Behrens5db02762011-11-01 17:04:16 +0100658 }
659
660 list_for_each_entry(device, dev_head, dev_list) {
661 int i;
662 struct btrfsic_dev_state *dev_state;
663
664 if (!device->bdev || !device->name)
665 continue;
666
Christoph Hellwigf8f84b22017-08-23 19:10:27 +0200667 dev_state = btrfsic_dev_state_lookup(device->bdev->bd_dev);
Stefan Behrens5db02762011-11-01 17:04:16 +0100668 BUG_ON(NULL == dev_state);
669 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
670 ret = btrfsic_process_superblock_dev_mirror(
671 state, dev_state, device, i,
672 &selected_dev_state, selected_super);
673 if (0 != ret && 0 == i) {
674 kfree(selected_super);
675 return ret;
676 }
677 }
678 }
679
680 if (NULL == state->latest_superblock) {
Jeff Mahoney62e85572016-09-20 10:05:01 -0400681 pr_info("btrfsic: no superblock found!\n");
Stefan Behrens5db02762011-11-01 17:04:16 +0100682 kfree(selected_super);
683 return -1;
684 }
685
686 state->csum_size = btrfs_super_csum_size(selected_super);
687
688 for (pass = 0; pass < 3; pass++) {
689 int num_copies;
690 int mirror_num;
691 u64 next_bytenr;
692
693 switch (pass) {
694 case 0:
695 next_bytenr = btrfs_super_root(selected_super);
696 if (state->print_mask &
697 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
Jeff Mahoney62e85572016-09-20 10:05:01 -0400698 pr_info("root@%llu\n", next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +0100699 break;
700 case 1:
701 next_bytenr = btrfs_super_chunk_root(selected_super);
702 if (state->print_mask &
703 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
Jeff Mahoney62e85572016-09-20 10:05:01 -0400704 pr_info("chunk@%llu\n", next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +0100705 break;
706 case 2:
707 next_bytenr = btrfs_super_log_root(selected_super);
708 if (0 == next_bytenr)
709 continue;
710 if (state->print_mask &
711 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
Jeff Mahoney62e85572016-09-20 10:05:01 -0400712 pr_info("log@%llu\n", next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +0100713 break;
714 }
715
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400716 num_copies = btrfs_num_copies(fs_info, next_bytenr,
717 state->metablock_size);
Stefan Behrens5db02762011-11-01 17:04:16 +0100718 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
Jeff Mahoney62e85572016-09-20 10:05:01 -0400719 pr_info("num_copies(log_bytenr=%llu) = %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200720 next_bytenr, num_copies);
Stefan Behrens5db02762011-11-01 17:04:16 +0100721
722 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
723 struct btrfsic_block *next_block;
724 struct btrfsic_block_data_ctx tmp_next_block_ctx;
725 struct btrfsic_block_link *l;
Stefan Behrens5db02762011-11-01 17:04:16 +0100726
Stefan Behrense06baab2012-04-12 12:53:40 +0200727 ret = btrfsic_map_block(state, next_bytenr,
728 state->metablock_size,
Stefan Behrens5db02762011-11-01 17:04:16 +0100729 &tmp_next_block_ctx,
730 mirror_num);
731 if (ret) {
Jeff Mahoney62e85572016-09-20 10:05:01 -0400732 pr_info("btrfsic: btrfsic_map_block(root @%llu, mirror %d) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200733 next_bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +0100734 kfree(selected_super);
735 return -1;
736 }
737
738 next_block = btrfsic_block_hashtable_lookup(
739 tmp_next_block_ctx.dev->bdev,
740 tmp_next_block_ctx.dev_bytenr,
741 &state->block_hashtable);
742 BUG_ON(NULL == next_block);
743
744 l = btrfsic_block_link_hashtable_lookup(
745 tmp_next_block_ctx.dev->bdev,
746 tmp_next_block_ctx.dev_bytenr,
747 state->latest_superblock->dev_state->
748 bdev,
749 state->latest_superblock->dev_bytenr,
750 &state->block_link_hashtable);
751 BUG_ON(NULL == l);
752
753 ret = btrfsic_read_block(state, &tmp_next_block_ctx);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300754 if (ret < (int)PAGE_SIZE) {
Jeff Mahoney62e85572016-09-20 10:05:01 -0400755 pr_info("btrfsic: read @logical %llu failed!\n",
Stefan Behrens5db02762011-11-01 17:04:16 +0100756 tmp_next_block_ctx.start);
757 btrfsic_release_block_ctx(&tmp_next_block_ctx);
758 kfree(selected_super);
759 return -1;
760 }
761
Stefan Behrens5db02762011-11-01 17:04:16 +0100762 ret = btrfsic_process_metablock(state,
763 next_block,
764 &tmp_next_block_ctx,
Stefan Behrens5db02762011-11-01 17:04:16 +0100765 BTRFS_MAX_LEVEL + 3, 1);
766 btrfsic_release_block_ctx(&tmp_next_block_ctx);
767 }
768 }
769
770 kfree(selected_super);
771 return ret;
772}
773
774static int btrfsic_process_superblock_dev_mirror(
775 struct btrfsic_state *state,
776 struct btrfsic_dev_state *dev_state,
777 struct btrfs_device *device,
778 int superblock_mirror_num,
779 struct btrfsic_dev_state **selected_dev_state,
780 struct btrfs_super_block *selected_super)
781{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400782 struct btrfs_fs_info *fs_info = state->fs_info;
Stefan Behrens5db02762011-11-01 17:04:16 +0100783 struct btrfs_super_block *super_tmp;
784 u64 dev_bytenr;
785 struct buffer_head *bh;
786 struct btrfsic_block *superblock_tmp;
787 int pass;
788 struct block_device *const superblock_bdev = device->bdev;
789
790 /* super block bytenr is always the unmapped device bytenr */
791 dev_bytenr = btrfs_sb_offset(superblock_mirror_num);
Miao Xie935e5cc2014-09-03 21:35:33 +0800792 if (dev_bytenr + BTRFS_SUPER_INFO_SIZE > device->commit_total_bytes)
Stefan Behrense06baab2012-04-12 12:53:40 +0200793 return -1;
David Sterba9f6d2512017-06-16 01:48:05 +0200794 bh = __bread(superblock_bdev, dev_bytenr / BTRFS_BDEV_BLOCKSIZE,
Stefan Behrense06baab2012-04-12 12:53:40 +0200795 BTRFS_SUPER_INFO_SIZE);
Stefan Behrens5db02762011-11-01 17:04:16 +0100796 if (NULL == bh)
797 return -1;
798 super_tmp = (struct btrfs_super_block *)
David Sterba9f6d2512017-06-16 01:48:05 +0200799 (bh->b_data + (dev_bytenr & (BTRFS_BDEV_BLOCKSIZE - 1)));
Stefan Behrens5db02762011-11-01 17:04:16 +0100800
801 if (btrfs_super_bytenr(super_tmp) != dev_bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800802 btrfs_super_magic(super_tmp) != BTRFS_MAGIC ||
Stefan Behrense06baab2012-04-12 12:53:40 +0200803 memcmp(device->uuid, super_tmp->dev_item.uuid, BTRFS_UUID_SIZE) ||
804 btrfs_super_nodesize(super_tmp) != state->metablock_size ||
Stefan Behrense06baab2012-04-12 12:53:40 +0200805 btrfs_super_sectorsize(super_tmp) != state->datablock_size) {
Stefan Behrens5db02762011-11-01 17:04:16 +0100806 brelse(bh);
807 return 0;
808 }
809
810 superblock_tmp =
811 btrfsic_block_hashtable_lookup(superblock_bdev,
812 dev_bytenr,
813 &state->block_hashtable);
814 if (NULL == superblock_tmp) {
815 superblock_tmp = btrfsic_block_alloc();
816 if (NULL == superblock_tmp) {
Jeff Mahoney62e85572016-09-20 10:05:01 -0400817 pr_info("btrfsic: error, kmalloc failed!\n");
Stefan Behrens5db02762011-11-01 17:04:16 +0100818 brelse(bh);
819 return -1;
820 }
821 /* for superblock, only the dev_bytenr makes sense */
822 superblock_tmp->dev_bytenr = dev_bytenr;
823 superblock_tmp->dev_state = dev_state;
824 superblock_tmp->logical_bytenr = dev_bytenr;
825 superblock_tmp->generation = btrfs_super_generation(super_tmp);
826 superblock_tmp->is_metadata = 1;
827 superblock_tmp->is_superblock = 1;
828 superblock_tmp->is_iodone = 1;
829 superblock_tmp->never_written = 0;
830 superblock_tmp->mirror_num = 1 + superblock_mirror_num;
831 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400832 btrfs_info_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +0200833 "new initial S-block (bdev %p, %s) @%llu (%s/%llu/%d)",
Josef Bacik606686e2012-06-04 14:03:51 -0400834 superblock_bdev,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200835 rcu_str_deref(device->name), dev_bytenr,
836 dev_state->name, dev_bytenr,
Josef Bacik606686e2012-06-04 14:03:51 -0400837 superblock_mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +0100838 list_add(&superblock_tmp->all_blocks_node,
839 &state->all_blocks_list);
840 btrfsic_block_hashtable_add(superblock_tmp,
841 &state->block_hashtable);
842 }
843
844 /* select the one with the highest generation field */
845 if (btrfs_super_generation(super_tmp) >
846 state->max_superblock_generation ||
847 0 == state->max_superblock_generation) {
848 memcpy(selected_super, super_tmp, sizeof(*selected_super));
849 *selected_dev_state = dev_state;
850 state->max_superblock_generation =
851 btrfs_super_generation(super_tmp);
852 state->latest_superblock = superblock_tmp;
853 }
854
855 for (pass = 0; pass < 3; pass++) {
856 u64 next_bytenr;
857 int num_copies;
858 int mirror_num;
859 const char *additional_string = NULL;
860 struct btrfs_disk_key tmp_disk_key;
861
862 tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
863 tmp_disk_key.offset = 0;
864 switch (pass) {
865 case 0:
Qu Wenruo3cae2102013-07-16 11:19:18 +0800866 btrfs_set_disk_key_objectid(&tmp_disk_key,
867 BTRFS_ROOT_TREE_OBJECTID);
Stefan Behrens5db02762011-11-01 17:04:16 +0100868 additional_string = "initial root ";
869 next_bytenr = btrfs_super_root(super_tmp);
870 break;
871 case 1:
Qu Wenruo3cae2102013-07-16 11:19:18 +0800872 btrfs_set_disk_key_objectid(&tmp_disk_key,
873 BTRFS_CHUNK_TREE_OBJECTID);
Stefan Behrens5db02762011-11-01 17:04:16 +0100874 additional_string = "initial chunk ";
875 next_bytenr = btrfs_super_chunk_root(super_tmp);
876 break;
877 case 2:
Qu Wenruo3cae2102013-07-16 11:19:18 +0800878 btrfs_set_disk_key_objectid(&tmp_disk_key,
879 BTRFS_TREE_LOG_OBJECTID);
Stefan Behrens5db02762011-11-01 17:04:16 +0100880 additional_string = "initial log ";
881 next_bytenr = btrfs_super_log_root(super_tmp);
882 if (0 == next_bytenr)
883 continue;
884 break;
885 }
886
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400887 num_copies = btrfs_num_copies(fs_info, next_bytenr,
888 state->metablock_size);
Stefan Behrens5db02762011-11-01 17:04:16 +0100889 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
Jeff Mahoney62e85572016-09-20 10:05:01 -0400890 pr_info("num_copies(log_bytenr=%llu) = %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200891 next_bytenr, num_copies);
Stefan Behrens5db02762011-11-01 17:04:16 +0100892 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
893 struct btrfsic_block *next_block;
894 struct btrfsic_block_data_ctx tmp_next_block_ctx;
895 struct btrfsic_block_link *l;
896
Stefan Behrense06baab2012-04-12 12:53:40 +0200897 if (btrfsic_map_block(state, next_bytenr,
898 state->metablock_size,
Stefan Behrens5db02762011-11-01 17:04:16 +0100899 &tmp_next_block_ctx,
900 mirror_num)) {
Jeff Mahoney62e85572016-09-20 10:05:01 -0400901 pr_info("btrfsic: btrfsic_map_block(bytenr @%llu, mirror %d) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200902 next_bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +0100903 brelse(bh);
904 return -1;
905 }
906
907 next_block = btrfsic_block_lookup_or_add(
908 state, &tmp_next_block_ctx,
909 additional_string, 1, 1, 0,
910 mirror_num, NULL);
911 if (NULL == next_block) {
912 btrfsic_release_block_ctx(&tmp_next_block_ctx);
913 brelse(bh);
914 return -1;
915 }
916
917 next_block->disk_key = tmp_disk_key;
918 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
919 l = btrfsic_block_link_lookup_or_add(
920 state, &tmp_next_block_ctx,
921 next_block, superblock_tmp,
922 BTRFSIC_GENERATION_UNKNOWN);
923 btrfsic_release_block_ctx(&tmp_next_block_ctx);
924 if (NULL == l) {
925 brelse(bh);
926 return -1;
927 }
928 }
929 }
930 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES)
931 btrfsic_dump_tree_sub(state, superblock_tmp, 0);
932
933 brelse(bh);
934 return 0;
935}
936
937static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void)
938{
939 struct btrfsic_stack_frame *sf;
940
941 sf = kzalloc(sizeof(*sf), GFP_NOFS);
942 if (NULL == sf)
Jeff Mahoney62e85572016-09-20 10:05:01 -0400943 pr_info("btrfsic: alloc memory failed!\n");
Stefan Behrens5db02762011-11-01 17:04:16 +0100944 else
945 sf->magic = BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER;
946 return sf;
947}
948
949static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf)
950{
951 BUG_ON(!(NULL == sf ||
952 BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER == sf->magic));
953 kfree(sf);
954}
955
956static int btrfsic_process_metablock(
957 struct btrfsic_state *state,
958 struct btrfsic_block *const first_block,
959 struct btrfsic_block_data_ctx *const first_block_ctx,
Stefan Behrens5db02762011-11-01 17:04:16 +0100960 int first_limit_nesting, int force_iodone_flag)
961{
962 struct btrfsic_stack_frame initial_stack_frame = { 0 };
963 struct btrfsic_stack_frame *sf;
964 struct btrfsic_stack_frame *next_stack;
Stefan Behrense06baab2012-04-12 12:53:40 +0200965 struct btrfs_header *const first_hdr =
966 (struct btrfs_header *)first_block_ctx->datav[0];
Stefan Behrens5db02762011-11-01 17:04:16 +0100967
Stefan Behrense06baab2012-04-12 12:53:40 +0200968 BUG_ON(!first_hdr);
Stefan Behrens5db02762011-11-01 17:04:16 +0100969 sf = &initial_stack_frame;
970 sf->error = 0;
971 sf->i = -1;
972 sf->limit_nesting = first_limit_nesting;
973 sf->block = first_block;
974 sf->block_ctx = first_block_ctx;
975 sf->next_block = NULL;
976 sf->hdr = first_hdr;
977 sf->prev = NULL;
978
979continue_with_new_stack_frame:
980 sf->block->generation = le64_to_cpu(sf->hdr->generation);
981 if (0 == sf->hdr->level) {
982 struct btrfs_leaf *const leafhdr =
983 (struct btrfs_leaf *)sf->hdr;
984
985 if (-1 == sf->i) {
Qu Wenruo3cae2102013-07-16 11:19:18 +0800986 sf->nr = btrfs_stack_header_nritems(&leafhdr->header);
Stefan Behrens5db02762011-11-01 17:04:16 +0100987
988 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -0400989 pr_info("leaf %llu items %d generation %llu owner %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200990 sf->block_ctx->start, sf->nr,
Qu Wenruo3cae2102013-07-16 11:19:18 +0800991 btrfs_stack_header_generation(
992 &leafhdr->header),
Qu Wenruo3cae2102013-07-16 11:19:18 +0800993 btrfs_stack_header_owner(
994 &leafhdr->header));
Stefan Behrens5db02762011-11-01 17:04:16 +0100995 }
996
997continue_with_current_leaf_stack_frame:
998 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
999 sf->i++;
1000 sf->num_copies = 0;
1001 }
1002
1003 if (sf->i < sf->nr) {
Stefan Behrense06baab2012-04-12 12:53:40 +02001004 struct btrfs_item disk_item;
1005 u32 disk_item_offset =
1006 (uintptr_t)(leafhdr->items + sf->i) -
1007 (uintptr_t)leafhdr;
1008 struct btrfs_disk_key *disk_key;
Stefan Behrens5db02762011-11-01 17:04:16 +01001009 u8 type;
Stefan Behrense06baab2012-04-12 12:53:40 +02001010 u32 item_offset;
Alexander Block8ea05e32012-07-25 17:35:53 +02001011 u32 item_size;
Stefan Behrens5db02762011-11-01 17:04:16 +01001012
Stefan Behrense06baab2012-04-12 12:53:40 +02001013 if (disk_item_offset + sizeof(struct btrfs_item) >
1014 sf->block_ctx->len) {
1015leaf_item_out_of_bounce_error:
Jeff Mahoney62e85572016-09-20 10:05:01 -04001016 pr_info("btrfsic: leaf item out of bounce at logical %llu, dev %s\n",
Stefan Behrense06baab2012-04-12 12:53:40 +02001017 sf->block_ctx->start,
1018 sf->block_ctx->dev->name);
1019 goto one_stack_frame_backwards;
1020 }
1021 btrfsic_read_from_block_data(sf->block_ctx,
1022 &disk_item,
1023 disk_item_offset,
1024 sizeof(struct btrfs_item));
Qu Wenruo3cae2102013-07-16 11:19:18 +08001025 item_offset = btrfs_stack_item_offset(&disk_item);
Stefan Behrensa5f519c2013-10-21 14:18:17 +02001026 item_size = btrfs_stack_item_size(&disk_item);
Stefan Behrense06baab2012-04-12 12:53:40 +02001027 disk_key = &disk_item.key;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001028 type = btrfs_disk_key_type(disk_key);
Stefan Behrens5db02762011-11-01 17:04:16 +01001029
1030 if (BTRFS_ROOT_ITEM_KEY == type) {
Stefan Behrense06baab2012-04-12 12:53:40 +02001031 struct btrfs_root_item root_item;
1032 u32 root_item_offset;
1033 u64 next_bytenr;
1034
1035 root_item_offset = item_offset +
1036 offsetof(struct btrfs_leaf, items);
Alexander Block8ea05e32012-07-25 17:35:53 +02001037 if (root_item_offset + item_size >
Stefan Behrense06baab2012-04-12 12:53:40 +02001038 sf->block_ctx->len)
1039 goto leaf_item_out_of_bounce_error;
1040 btrfsic_read_from_block_data(
1041 sf->block_ctx, &root_item,
1042 root_item_offset,
Alexander Block8ea05e32012-07-25 17:35:53 +02001043 item_size);
Qu Wenruo3cae2102013-07-16 11:19:18 +08001044 next_bytenr = btrfs_root_bytenr(&root_item);
Stefan Behrens5db02762011-11-01 17:04:16 +01001045
1046 sf->error =
1047 btrfsic_create_link_to_next_block(
1048 state,
1049 sf->block,
1050 sf->block_ctx,
1051 next_bytenr,
1052 sf->limit_nesting,
1053 &sf->next_block_ctx,
1054 &sf->next_block,
1055 force_iodone_flag,
1056 &sf->num_copies,
1057 &sf->mirror_num,
1058 disk_key,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001059 btrfs_root_generation(
1060 &root_item));
Stefan Behrens5db02762011-11-01 17:04:16 +01001061 if (sf->error)
1062 goto one_stack_frame_backwards;
1063
1064 if (NULL != sf->next_block) {
1065 struct btrfs_header *const next_hdr =
1066 (struct btrfs_header *)
Stefan Behrense06baab2012-04-12 12:53:40 +02001067 sf->next_block_ctx.datav[0];
Stefan Behrens5db02762011-11-01 17:04:16 +01001068
1069 next_stack =
1070 btrfsic_stack_frame_alloc();
1071 if (NULL == next_stack) {
Stefan Behrens98806b42014-05-09 15:28:07 +02001072 sf->error = -1;
Stefan Behrens5db02762011-11-01 17:04:16 +01001073 btrfsic_release_block_ctx(
1074 &sf->
1075 next_block_ctx);
1076 goto one_stack_frame_backwards;
1077 }
1078
1079 next_stack->i = -1;
1080 next_stack->block = sf->next_block;
1081 next_stack->block_ctx =
1082 &sf->next_block_ctx;
1083 next_stack->next_block = NULL;
1084 next_stack->hdr = next_hdr;
1085 next_stack->limit_nesting =
1086 sf->limit_nesting - 1;
1087 next_stack->prev = sf;
1088 sf = next_stack;
1089 goto continue_with_new_stack_frame;
1090 }
1091 } else if (BTRFS_EXTENT_DATA_KEY == type &&
1092 state->include_extent_data) {
1093 sf->error = btrfsic_handle_extent_data(
1094 state,
1095 sf->block,
1096 sf->block_ctx,
1097 item_offset,
1098 force_iodone_flag);
1099 if (sf->error)
1100 goto one_stack_frame_backwards;
1101 }
1102
1103 goto continue_with_current_leaf_stack_frame;
1104 }
1105 } else {
1106 struct btrfs_node *const nodehdr = (struct btrfs_node *)sf->hdr;
1107
1108 if (-1 == sf->i) {
Qu Wenruo3cae2102013-07-16 11:19:18 +08001109 sf->nr = btrfs_stack_header_nritems(&nodehdr->header);
Stefan Behrens5db02762011-11-01 17:04:16 +01001110
1111 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04001112 pr_info("node %llu level %d items %d generation %llu owner %llu\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01001113 sf->block_ctx->start,
1114 nodehdr->header.level, sf->nr,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001115 btrfs_stack_header_generation(
1116 &nodehdr->header),
Qu Wenruo3cae2102013-07-16 11:19:18 +08001117 btrfs_stack_header_owner(
1118 &nodehdr->header));
Stefan Behrens5db02762011-11-01 17:04:16 +01001119 }
1120
1121continue_with_current_node_stack_frame:
1122 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1123 sf->i++;
1124 sf->num_copies = 0;
1125 }
1126
1127 if (sf->i < sf->nr) {
Stefan Behrense06baab2012-04-12 12:53:40 +02001128 struct btrfs_key_ptr key_ptr;
1129 u32 key_ptr_offset;
1130 u64 next_bytenr;
1131
1132 key_ptr_offset = (uintptr_t)(nodehdr->ptrs + sf->i) -
1133 (uintptr_t)nodehdr;
1134 if (key_ptr_offset + sizeof(struct btrfs_key_ptr) >
1135 sf->block_ctx->len) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001136 pr_info("btrfsic: node item out of bounce at logical %llu, dev %s\n",
Stefan Behrense06baab2012-04-12 12:53:40 +02001137 sf->block_ctx->start,
1138 sf->block_ctx->dev->name);
1139 goto one_stack_frame_backwards;
1140 }
1141 btrfsic_read_from_block_data(
1142 sf->block_ctx, &key_ptr, key_ptr_offset,
1143 sizeof(struct btrfs_key_ptr));
Qu Wenruo3cae2102013-07-16 11:19:18 +08001144 next_bytenr = btrfs_stack_key_blockptr(&key_ptr);
Stefan Behrens5db02762011-11-01 17:04:16 +01001145
1146 sf->error = btrfsic_create_link_to_next_block(
1147 state,
1148 sf->block,
1149 sf->block_ctx,
1150 next_bytenr,
1151 sf->limit_nesting,
1152 &sf->next_block_ctx,
1153 &sf->next_block,
1154 force_iodone_flag,
1155 &sf->num_copies,
1156 &sf->mirror_num,
Stefan Behrense06baab2012-04-12 12:53:40 +02001157 &key_ptr.key,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001158 btrfs_stack_key_generation(&key_ptr));
Stefan Behrens5db02762011-11-01 17:04:16 +01001159 if (sf->error)
1160 goto one_stack_frame_backwards;
1161
1162 if (NULL != sf->next_block) {
1163 struct btrfs_header *const next_hdr =
1164 (struct btrfs_header *)
Stefan Behrense06baab2012-04-12 12:53:40 +02001165 sf->next_block_ctx.datav[0];
Stefan Behrens5db02762011-11-01 17:04:16 +01001166
1167 next_stack = btrfsic_stack_frame_alloc();
Stefan Behrens98806b42014-05-09 15:28:07 +02001168 if (NULL == next_stack) {
1169 sf->error = -1;
Stefan Behrens5db02762011-11-01 17:04:16 +01001170 goto one_stack_frame_backwards;
Stefan Behrens98806b42014-05-09 15:28:07 +02001171 }
Stefan Behrens5db02762011-11-01 17:04:16 +01001172
1173 next_stack->i = -1;
1174 next_stack->block = sf->next_block;
1175 next_stack->block_ctx = &sf->next_block_ctx;
1176 next_stack->next_block = NULL;
1177 next_stack->hdr = next_hdr;
1178 next_stack->limit_nesting =
1179 sf->limit_nesting - 1;
1180 next_stack->prev = sf;
1181 sf = next_stack;
1182 goto continue_with_new_stack_frame;
1183 }
1184
1185 goto continue_with_current_node_stack_frame;
1186 }
1187 }
1188
1189one_stack_frame_backwards:
1190 if (NULL != sf->prev) {
1191 struct btrfsic_stack_frame *const prev = sf->prev;
1192
1193 /* the one for the initial block is freed in the caller */
1194 btrfsic_release_block_ctx(sf->block_ctx);
1195
1196 if (sf->error) {
1197 prev->error = sf->error;
1198 btrfsic_stack_frame_free(sf);
1199 sf = prev;
1200 goto one_stack_frame_backwards;
1201 }
1202
1203 btrfsic_stack_frame_free(sf);
1204 sf = prev;
1205 goto continue_with_new_stack_frame;
1206 } else {
1207 BUG_ON(&initial_stack_frame != sf);
1208 }
1209
1210 return sf->error;
1211}
1212
Stefan Behrense06baab2012-04-12 12:53:40 +02001213static void btrfsic_read_from_block_data(
1214 struct btrfsic_block_data_ctx *block_ctx,
1215 void *dstv, u32 offset, size_t len)
1216{
1217 size_t cur;
1218 size_t offset_in_page;
1219 char *kaddr;
1220 char *dst = (char *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001221 size_t start_offset = block_ctx->start & ((u64)PAGE_SIZE - 1);
1222 unsigned long i = (start_offset + offset) >> PAGE_SHIFT;
Stefan Behrense06baab2012-04-12 12:53:40 +02001223
1224 WARN_ON(offset + len > block_ctx->len);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001225 offset_in_page = (start_offset + offset) & (PAGE_SIZE - 1);
Stefan Behrense06baab2012-04-12 12:53:40 +02001226
1227 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001228 cur = min(len, ((size_t)PAGE_SIZE - offset_in_page));
1229 BUG_ON(i >= DIV_ROUND_UP(block_ctx->len, PAGE_SIZE));
Stefan Behrense06baab2012-04-12 12:53:40 +02001230 kaddr = block_ctx->datav[i];
1231 memcpy(dst, kaddr + offset_in_page, cur);
1232
1233 dst += cur;
1234 len -= cur;
1235 offset_in_page = 0;
1236 i++;
1237 }
1238}
1239
Stefan Behrens5db02762011-11-01 17:04:16 +01001240static int btrfsic_create_link_to_next_block(
1241 struct btrfsic_state *state,
1242 struct btrfsic_block *block,
1243 struct btrfsic_block_data_ctx *block_ctx,
1244 u64 next_bytenr,
1245 int limit_nesting,
1246 struct btrfsic_block_data_ctx *next_block_ctx,
1247 struct btrfsic_block **next_blockp,
1248 int force_iodone_flag,
1249 int *num_copiesp, int *mirror_nump,
1250 struct btrfs_disk_key *disk_key,
1251 u64 parent_generation)
1252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001253 struct btrfs_fs_info *fs_info = state->fs_info;
Stefan Behrens5db02762011-11-01 17:04:16 +01001254 struct btrfsic_block *next_block = NULL;
1255 int ret;
1256 struct btrfsic_block_link *l;
1257 int did_alloc_block_link;
1258 int block_was_created;
1259
1260 *next_blockp = NULL;
1261 if (0 == *num_copiesp) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001262 *num_copiesp = btrfs_num_copies(fs_info, next_bytenr,
1263 state->metablock_size);
Stefan Behrens5db02762011-11-01 17:04:16 +01001264 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
Jeff Mahoney62e85572016-09-20 10:05:01 -04001265 pr_info("num_copies(log_bytenr=%llu) = %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001266 next_bytenr, *num_copiesp);
Stefan Behrens5db02762011-11-01 17:04:16 +01001267 *mirror_nump = 1;
1268 }
1269
1270 if (*mirror_nump > *num_copiesp)
1271 return 0;
1272
1273 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04001274 pr_info("btrfsic_create_link_to_next_block(mirror_num=%d)\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01001275 *mirror_nump);
1276 ret = btrfsic_map_block(state, next_bytenr,
Stefan Behrense06baab2012-04-12 12:53:40 +02001277 state->metablock_size,
Stefan Behrens5db02762011-11-01 17:04:16 +01001278 next_block_ctx, *mirror_nump);
1279 if (ret) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001280 pr_info("btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001281 next_bytenr, *mirror_nump);
Stefan Behrens5db02762011-11-01 17:04:16 +01001282 btrfsic_release_block_ctx(next_block_ctx);
1283 *next_blockp = NULL;
1284 return -1;
1285 }
1286
1287 next_block = btrfsic_block_lookup_or_add(state,
1288 next_block_ctx, "referenced ",
1289 1, force_iodone_flag,
1290 !force_iodone_flag,
1291 *mirror_nump,
1292 &block_was_created);
1293 if (NULL == next_block) {
1294 btrfsic_release_block_ctx(next_block_ctx);
1295 *next_blockp = NULL;
1296 return -1;
1297 }
1298 if (block_was_created) {
1299 l = NULL;
1300 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
1301 } else {
Stefan Behrenscf90c592014-10-17 14:10:10 +02001302 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) {
1303 if (next_block->logical_bytenr != next_bytenr &&
1304 !(!next_block->is_metadata &&
1305 0 == next_block->logical_bytenr))
Jeff Mahoney62e85572016-09-20 10:05:01 -04001306 pr_info("Referenced block @%llu (%s/%llu/%d) found in hash table, %c, bytenr mismatch (!= stored %llu).\n",
Stefan Behrenscf90c592014-10-17 14:10:10 +02001307 next_bytenr, next_block_ctx->dev->name,
1308 next_block_ctx->dev_bytenr, *mirror_nump,
1309 btrfsic_get_block_type(state,
1310 next_block),
1311 next_block->logical_bytenr);
1312 else
Jeff Mahoney62e85572016-09-20 10:05:01 -04001313 pr_info("Referenced block @%llu (%s/%llu/%d) found in hash table, %c.\n",
Stefan Behrenscf90c592014-10-17 14:10:10 +02001314 next_bytenr, next_block_ctx->dev->name,
1315 next_block_ctx->dev_bytenr, *mirror_nump,
1316 btrfsic_get_block_type(state,
1317 next_block));
1318 }
Stefan Behrens5db02762011-11-01 17:04:16 +01001319 next_block->logical_bytenr = next_bytenr;
1320
1321 next_block->mirror_num = *mirror_nump;
1322 l = btrfsic_block_link_hashtable_lookup(
1323 next_block_ctx->dev->bdev,
1324 next_block_ctx->dev_bytenr,
1325 block_ctx->dev->bdev,
1326 block_ctx->dev_bytenr,
1327 &state->block_link_hashtable);
1328 }
1329
1330 next_block->disk_key = *disk_key;
1331 if (NULL == l) {
1332 l = btrfsic_block_link_alloc();
1333 if (NULL == l) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001334 pr_info("btrfsic: error, kmalloc failed!\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01001335 btrfsic_release_block_ctx(next_block_ctx);
1336 *next_blockp = NULL;
1337 return -1;
1338 }
1339
1340 did_alloc_block_link = 1;
1341 l->block_ref_to = next_block;
1342 l->block_ref_from = block;
1343 l->ref_cnt = 1;
1344 l->parent_generation = parent_generation;
1345
1346 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1347 btrfsic_print_add_link(state, l);
1348
1349 list_add(&l->node_ref_to, &block->ref_to_list);
1350 list_add(&l->node_ref_from, &next_block->ref_from_list);
1351
1352 btrfsic_block_link_hashtable_add(l,
1353 &state->block_link_hashtable);
1354 } else {
1355 did_alloc_block_link = 0;
1356 if (0 == limit_nesting) {
1357 l->ref_cnt++;
1358 l->parent_generation = parent_generation;
1359 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1360 btrfsic_print_add_link(state, l);
1361 }
1362 }
1363
1364 if (limit_nesting > 0 && did_alloc_block_link) {
1365 ret = btrfsic_read_block(state, next_block_ctx);
Stefan Behrense06baab2012-04-12 12:53:40 +02001366 if (ret < (int)next_block_ctx->len) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001367 pr_info("btrfsic: read block @logical %llu failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001368 next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01001369 btrfsic_release_block_ctx(next_block_ctx);
1370 *next_blockp = NULL;
1371 return -1;
1372 }
1373
1374 *next_blockp = next_block;
1375 } else {
1376 *next_blockp = NULL;
1377 }
1378 (*mirror_nump)++;
1379
1380 return 0;
1381}
1382
1383static int btrfsic_handle_extent_data(
1384 struct btrfsic_state *state,
1385 struct btrfsic_block *block,
1386 struct btrfsic_block_data_ctx *block_ctx,
1387 u32 item_offset, int force_iodone_flag)
1388{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001389 struct btrfs_fs_info *fs_info = state->fs_info;
Stefan Behrense06baab2012-04-12 12:53:40 +02001390 struct btrfs_file_extent_item file_extent_item;
1391 u64 file_extent_item_offset;
1392 u64 next_bytenr;
1393 u64 num_bytes;
1394 u64 generation;
Stefan Behrens5db02762011-11-01 17:04:16 +01001395 struct btrfsic_block_link *l;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001396 int ret;
Stefan Behrens5db02762011-11-01 17:04:16 +01001397
Stefan Behrense06baab2012-04-12 12:53:40 +02001398 file_extent_item_offset = offsetof(struct btrfs_leaf, items) +
1399 item_offset;
Stefan Behrens86ff7ff2012-04-24 18:10:16 +02001400 if (file_extent_item_offset +
1401 offsetof(struct btrfs_file_extent_item, disk_num_bytes) >
1402 block_ctx->len) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001403 pr_info("btrfsic: file item out of bounce at logical %llu, dev %s\n",
Stefan Behrens86ff7ff2012-04-24 18:10:16 +02001404 block_ctx->start, block_ctx->dev->name);
1405 return -1;
1406 }
1407
1408 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1409 file_extent_item_offset,
1410 offsetof(struct btrfs_file_extent_item, disk_num_bytes));
1411 if (BTRFS_FILE_EXTENT_REG != file_extent_item.type ||
Qu Wenruo3cae2102013-07-16 11:19:18 +08001412 btrfs_stack_file_extent_disk_bytenr(&file_extent_item) == 0) {
Stefan Behrens86ff7ff2012-04-24 18:10:16 +02001413 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04001414 pr_info("extent_data: type %u, disk_bytenr = %llu\n",
Stefan Behrens86ff7ff2012-04-24 18:10:16 +02001415 file_extent_item.type,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001416 btrfs_stack_file_extent_disk_bytenr(
1417 &file_extent_item));
Stefan Behrens86ff7ff2012-04-24 18:10:16 +02001418 return 0;
1419 }
1420
Stefan Behrense06baab2012-04-12 12:53:40 +02001421 if (file_extent_item_offset + sizeof(struct btrfs_file_extent_item) >
1422 block_ctx->len) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001423 pr_info("btrfsic: file item out of bounce at logical %llu, dev %s\n",
Stefan Behrense06baab2012-04-12 12:53:40 +02001424 block_ctx->start, block_ctx->dev->name);
1425 return -1;
1426 }
1427 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1428 file_extent_item_offset,
1429 sizeof(struct btrfs_file_extent_item));
Josef Bacike20d6c52013-11-13 21:11:49 -05001430 next_bytenr = btrfs_stack_file_extent_disk_bytenr(&file_extent_item);
1431 if (btrfs_stack_file_extent_compression(&file_extent_item) ==
1432 BTRFS_COMPRESS_NONE) {
1433 next_bytenr += btrfs_stack_file_extent_offset(&file_extent_item);
1434 num_bytes = btrfs_stack_file_extent_num_bytes(&file_extent_item);
1435 } else {
1436 num_bytes = btrfs_stack_file_extent_disk_num_bytes(&file_extent_item);
1437 }
Qu Wenruo3cae2102013-07-16 11:19:18 +08001438 generation = btrfs_stack_file_extent_generation(&file_extent_item);
Stefan Behrense06baab2012-04-12 12:53:40 +02001439
Stefan Behrens5db02762011-11-01 17:04:16 +01001440 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04001441 pr_info("extent_data: type %u, disk_bytenr = %llu, offset = %llu, num_bytes = %llu\n",
Stefan Behrense06baab2012-04-12 12:53:40 +02001442 file_extent_item.type,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001443 btrfs_stack_file_extent_disk_bytenr(&file_extent_item),
Qu Wenruo3cae2102013-07-16 11:19:18 +08001444 btrfs_stack_file_extent_offset(&file_extent_item),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001445 num_bytes);
Stefan Behrens5db02762011-11-01 17:04:16 +01001446 while (num_bytes > 0) {
1447 u32 chunk_len;
1448 int num_copies;
1449 int mirror_num;
1450
Stefan Behrense06baab2012-04-12 12:53:40 +02001451 if (num_bytes > state->datablock_size)
1452 chunk_len = state->datablock_size;
Stefan Behrens5db02762011-11-01 17:04:16 +01001453 else
1454 chunk_len = num_bytes;
1455
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001456 num_copies = btrfs_num_copies(fs_info, next_bytenr,
1457 state->datablock_size);
Stefan Behrens5db02762011-11-01 17:04:16 +01001458 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
Jeff Mahoney62e85572016-09-20 10:05:01 -04001459 pr_info("num_copies(log_bytenr=%llu) = %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001460 next_bytenr, num_copies);
Stefan Behrens5db02762011-11-01 17:04:16 +01001461 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
1462 struct btrfsic_block_data_ctx next_block_ctx;
1463 struct btrfsic_block *next_block;
1464 int block_was_created;
1465
1466 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04001467 pr_info("btrfsic_handle_extent_data(mirror_num=%d)\n",
1468 mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01001469 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04001470 pr_info("\tdisk_bytenr = %llu, num_bytes %u\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001471 next_bytenr, chunk_len);
Stefan Behrens5db02762011-11-01 17:04:16 +01001472 ret = btrfsic_map_block(state, next_bytenr,
1473 chunk_len, &next_block_ctx,
1474 mirror_num);
1475 if (ret) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001476 pr_info("btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001477 next_bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01001478 return -1;
1479 }
1480
1481 next_block = btrfsic_block_lookup_or_add(
1482 state,
1483 &next_block_ctx,
1484 "referenced ",
1485 0,
1486 force_iodone_flag,
1487 !force_iodone_flag,
1488 mirror_num,
1489 &block_was_created);
1490 if (NULL == next_block) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001491 pr_info("btrfsic: error, kmalloc failed!\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01001492 btrfsic_release_block_ctx(&next_block_ctx);
1493 return -1;
1494 }
1495 if (!block_was_created) {
Stefan Behrenscf90c592014-10-17 14:10:10 +02001496 if ((state->print_mask &
1497 BTRFSIC_PRINT_MASK_VERBOSE) &&
1498 next_block->logical_bytenr != next_bytenr &&
Stefan Behrens5db02762011-11-01 17:04:16 +01001499 !(!next_block->is_metadata &&
1500 0 == next_block->logical_bytenr)) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001501 pr_info("Referenced block @%llu (%s/%llu/%d) found in hash table, D, bytenr mismatch (!= stored %llu).\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001502 next_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01001503 next_block_ctx.dev->name,
Stefan Behrens5db02762011-11-01 17:04:16 +01001504 next_block_ctx.dev_bytenr,
1505 mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01001506 next_block->logical_bytenr);
1507 }
1508 next_block->logical_bytenr = next_bytenr;
1509 next_block->mirror_num = mirror_num;
1510 }
1511
1512 l = btrfsic_block_link_lookup_or_add(state,
1513 &next_block_ctx,
1514 next_block, block,
1515 generation);
1516 btrfsic_release_block_ctx(&next_block_ctx);
1517 if (NULL == l)
1518 return -1;
1519 }
1520
1521 next_bytenr += chunk_len;
1522 num_bytes -= chunk_len;
1523 }
1524
1525 return 0;
1526}
1527
1528static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
1529 struct btrfsic_block_data_ctx *block_ctx_out,
1530 int mirror_num)
1531{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001532 struct btrfs_fs_info *fs_info = state->fs_info;
Stefan Behrens5db02762011-11-01 17:04:16 +01001533 int ret;
1534 u64 length;
1535 struct btrfs_bio *multi = NULL;
1536 struct btrfs_device *device;
1537
1538 length = len;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001539 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
Stefan Behrens5db02762011-11-01 17:04:16 +01001540 bytenr, &length, &multi, mirror_num);
1541
Stefan Behrens61891922012-11-05 18:51:52 +01001542 if (ret) {
1543 block_ctx_out->start = 0;
1544 block_ctx_out->dev_bytenr = 0;
1545 block_ctx_out->len = 0;
1546 block_ctx_out->dev = NULL;
1547 block_ctx_out->datav = NULL;
1548 block_ctx_out->pagev = NULL;
1549 block_ctx_out->mem_to_free = NULL;
1550
1551 return ret;
1552 }
1553
Stefan Behrens5db02762011-11-01 17:04:16 +01001554 device = multi->stripes[0].dev;
Christoph Hellwigf8f84b22017-08-23 19:10:27 +02001555 block_ctx_out->dev = btrfsic_dev_state_lookup(device->bdev->bd_dev);
Stefan Behrens5db02762011-11-01 17:04:16 +01001556 block_ctx_out->dev_bytenr = multi->stripes[0].physical;
1557 block_ctx_out->start = bytenr;
1558 block_ctx_out->len = len;
Stefan Behrense06baab2012-04-12 12:53:40 +02001559 block_ctx_out->datav = NULL;
1560 block_ctx_out->pagev = NULL;
1561 block_ctx_out->mem_to_free = NULL;
Stefan Behrens5db02762011-11-01 17:04:16 +01001562
Stefan Behrens61891922012-11-05 18:51:52 +01001563 kfree(multi);
Stefan Behrens5db02762011-11-01 17:04:16 +01001564 if (NULL == block_ctx_out->dev) {
1565 ret = -ENXIO;
Jeff Mahoney62e85572016-09-20 10:05:01 -04001566 pr_info("btrfsic: error, cannot lookup dev (#1)!\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01001567 }
1568
1569 return ret;
1570}
1571
Stefan Behrens5db02762011-11-01 17:04:16 +01001572static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx)
1573{
Stefan Behrense06baab2012-04-12 12:53:40 +02001574 if (block_ctx->mem_to_free) {
1575 unsigned int num_pages;
1576
1577 BUG_ON(!block_ctx->datav);
1578 BUG_ON(!block_ctx->pagev);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001579 num_pages = (block_ctx->len + (u64)PAGE_SIZE - 1) >>
1580 PAGE_SHIFT;
Stefan Behrense06baab2012-04-12 12:53:40 +02001581 while (num_pages > 0) {
1582 num_pages--;
1583 if (block_ctx->datav[num_pages]) {
1584 kunmap(block_ctx->pagev[num_pages]);
1585 block_ctx->datav[num_pages] = NULL;
1586 }
1587 if (block_ctx->pagev[num_pages]) {
1588 __free_page(block_ctx->pagev[num_pages]);
1589 block_ctx->pagev[num_pages] = NULL;
1590 }
1591 }
1592
1593 kfree(block_ctx->mem_to_free);
1594 block_ctx->mem_to_free = NULL;
1595 block_ctx->pagev = NULL;
1596 block_ctx->datav = NULL;
Stefan Behrens5db02762011-11-01 17:04:16 +01001597 }
1598}
1599
1600static int btrfsic_read_block(struct btrfsic_state *state,
1601 struct btrfsic_block_data_ctx *block_ctx)
1602{
Stefan Behrense06baab2012-04-12 12:53:40 +02001603 unsigned int num_pages;
1604 unsigned int i;
1605 u64 dev_bytenr;
1606 int ret;
1607
1608 BUG_ON(block_ctx->datav);
1609 BUG_ON(block_ctx->pagev);
1610 BUG_ON(block_ctx->mem_to_free);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001611 if (block_ctx->dev_bytenr & ((u64)PAGE_SIZE - 1)) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001612 pr_info("btrfsic: read_block() with unaligned bytenr %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001613 block_ctx->dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01001614 return -1;
1615 }
Stefan Behrense06baab2012-04-12 12:53:40 +02001616
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001617 num_pages = (block_ctx->len + (u64)PAGE_SIZE - 1) >>
1618 PAGE_SHIFT;
Stefan Behrense06baab2012-04-12 12:53:40 +02001619 block_ctx->mem_to_free = kzalloc((sizeof(*block_ctx->datav) +
1620 sizeof(*block_ctx->pagev)) *
1621 num_pages, GFP_NOFS);
1622 if (!block_ctx->mem_to_free)
Luis de Bethencourt0b8d8ce2015-10-20 14:56:22 +01001623 return -ENOMEM;
Stefan Behrense06baab2012-04-12 12:53:40 +02001624 block_ctx->datav = block_ctx->mem_to_free;
1625 block_ctx->pagev = (struct page **)(block_ctx->datav + num_pages);
1626 for (i = 0; i < num_pages; i++) {
1627 block_ctx->pagev[i] = alloc_page(GFP_NOFS);
1628 if (!block_ctx->pagev[i])
1629 return -1;
Stefan Behrens5db02762011-11-01 17:04:16 +01001630 }
1631
Stefan Behrense06baab2012-04-12 12:53:40 +02001632 dev_bytenr = block_ctx->dev_bytenr;
1633 for (i = 0; i < num_pages;) {
1634 struct bio *bio;
1635 unsigned int j;
Stefan Behrense06baab2012-04-12 12:53:40 +02001636
David Sterbac5e4c3d2017-06-12 17:29:41 +02001637 bio = btrfs_io_bio_alloc(num_pages - i);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001638 bio_set_dev(bio, block_ctx->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001639 bio->bi_iter.bi_sector = dev_bytenr >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001640 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Stefan Behrense06baab2012-04-12 12:53:40 +02001641
1642 for (j = i; j < num_pages; j++) {
1643 ret = bio_add_page(bio, block_ctx->pagev[j],
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001644 PAGE_SIZE, 0);
1645 if (PAGE_SIZE != ret)
Stefan Behrense06baab2012-04-12 12:53:40 +02001646 break;
1647 }
1648 if (j == i) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001649 pr_info("btrfsic: error, failed to add a single page!\n");
Stefan Behrense06baab2012-04-12 12:53:40 +02001650 return -1;
1651 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001652 if (submit_bio_wait(bio)) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001653 pr_info("btrfsic: read error at logical %llu dev %s!\n",
Stefan Behrense06baab2012-04-12 12:53:40 +02001654 block_ctx->start, block_ctx->dev->name);
1655 bio_put(bio);
1656 return -1;
1657 }
1658 bio_put(bio);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001659 dev_bytenr += (j - i) * PAGE_SIZE;
Stefan Behrense06baab2012-04-12 12:53:40 +02001660 i = j;
1661 }
Fabian Frederick977ec792017-04-25 20:11:02 +02001662 for (i = 0; i < num_pages; i++)
Stefan Behrense06baab2012-04-12 12:53:40 +02001663 block_ctx->datav[i] = kmap(block_ctx->pagev[i]);
Stefan Behrens5db02762011-11-01 17:04:16 +01001664
1665 return block_ctx->len;
1666}
1667
1668static void btrfsic_dump_database(struct btrfsic_state *state)
1669{
Geliang Tangb69f2be2015-12-18 22:16:59 +08001670 const struct btrfsic_block *b_all;
Stefan Behrens5db02762011-11-01 17:04:16 +01001671
1672 BUG_ON(NULL == state);
1673
Jeff Mahoney62e85572016-09-20 10:05:01 -04001674 pr_info("all_blocks_list:\n");
Geliang Tangb69f2be2015-12-18 22:16:59 +08001675 list_for_each_entry(b_all, &state->all_blocks_list, all_blocks_node) {
1676 const struct btrfsic_block_link *l;
Stefan Behrens5db02762011-11-01 17:04:16 +01001677
Jeff Mahoney62e85572016-09-20 10:05:01 -04001678 pr_info("%c-block @%llu (%s/%llu/%d)\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01001679 btrfsic_get_block_type(state, b_all),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001680 b_all->logical_bytenr, b_all->dev_state->name,
1681 b_all->dev_bytenr, b_all->mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01001682
Geliang Tangb69f2be2015-12-18 22:16:59 +08001683 list_for_each_entry(l, &b_all->ref_to_list, node_ref_to) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001684 pr_info(" %c @%llu (%s/%llu/%d) refers %u* to %c @%llu (%s/%llu/%d)\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01001685 btrfsic_get_block_type(state, b_all),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001686 b_all->logical_bytenr, b_all->dev_state->name,
1687 b_all->dev_bytenr, b_all->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01001688 l->ref_cnt,
1689 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens5db02762011-11-01 17:04:16 +01001690 l->block_ref_to->logical_bytenr,
1691 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001692 l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01001693 l->block_ref_to->mirror_num);
1694 }
1695
Geliang Tangb69f2be2015-12-18 22:16:59 +08001696 list_for_each_entry(l, &b_all->ref_from_list, node_ref_from) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001697 pr_info(" %c @%llu (%s/%llu/%d) is ref %u* from %c @%llu (%s/%llu/%d)\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01001698 btrfsic_get_block_type(state, b_all),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001699 b_all->logical_bytenr, b_all->dev_state->name,
1700 b_all->dev_bytenr, b_all->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01001701 l->ref_cnt,
1702 btrfsic_get_block_type(state, l->block_ref_from),
Stefan Behrens5db02762011-11-01 17:04:16 +01001703 l->block_ref_from->logical_bytenr,
1704 l->block_ref_from->dev_state->name,
Stefan Behrens5db02762011-11-01 17:04:16 +01001705 l->block_ref_from->dev_bytenr,
1706 l->block_ref_from->mirror_num);
1707 }
1708
Jeff Mahoney62e85572016-09-20 10:05:01 -04001709 pr_info("\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01001710 }
1711}
1712
1713/*
1714 * Test whether the disk block contains a tree block (leaf or node)
1715 * (note that this test fails for the super block)
1716 */
1717static int btrfsic_test_for_metadata(struct btrfsic_state *state,
Stefan Behrense06baab2012-04-12 12:53:40 +02001718 char **datav, unsigned int num_pages)
Stefan Behrens5db02762011-11-01 17:04:16 +01001719{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001720 struct btrfs_fs_info *fs_info = state->fs_info;
Stefan Behrens5db02762011-11-01 17:04:16 +01001721 struct btrfs_header *h;
1722 u8 csum[BTRFS_CSUM_SIZE];
1723 u32 crc = ~(u32)0;
Stefan Behrense06baab2012-04-12 12:53:40 +02001724 unsigned int i;
Stefan Behrens5db02762011-11-01 17:04:16 +01001725
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001726 if (num_pages * PAGE_SIZE < state->metablock_size)
Stefan Behrense06baab2012-04-12 12:53:40 +02001727 return 1; /* not metadata */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001728 num_pages = state->metablock_size >> PAGE_SHIFT;
Stefan Behrense06baab2012-04-12 12:53:40 +02001729 h = (struct btrfs_header *)datav[0];
Stefan Behrens5db02762011-11-01 17:04:16 +01001730
Anand Jain44880fd2017-07-29 17:50:09 +08001731 if (memcmp(h->fsid, fs_info->fsid, BTRFS_FSID_SIZE))
Stefan Behrense06baab2012-04-12 12:53:40 +02001732 return 1;
Stefan Behrens5db02762011-11-01 17:04:16 +01001733
Stefan Behrense06baab2012-04-12 12:53:40 +02001734 for (i = 0; i < num_pages; i++) {
1735 u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001736 size_t sublen = i ? PAGE_SIZE :
1737 (PAGE_SIZE - BTRFS_CSUM_SIZE);
Stefan Behrense06baab2012-04-12 12:53:40 +02001738
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +00001739 crc = btrfs_crc32c(crc, data, sublen);
Stefan Behrense06baab2012-04-12 12:53:40 +02001740 }
Stefan Behrens5db02762011-11-01 17:04:16 +01001741 btrfs_csum_final(crc, csum);
1742 if (memcmp(csum, h->csum, state->csum_size))
Stefan Behrense06baab2012-04-12 12:53:40 +02001743 return 1;
Stefan Behrens5db02762011-11-01 17:04:16 +01001744
Stefan Behrense06baab2012-04-12 12:53:40 +02001745 return 0; /* is metadata */
Stefan Behrens5db02762011-11-01 17:04:16 +01001746}
1747
1748static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
Stefan Behrense06baab2012-04-12 12:53:40 +02001749 u64 dev_bytenr, char **mapped_datav,
1750 unsigned int num_pages,
1751 struct bio *bio, int *bio_is_patched,
Stefan Behrens5db02762011-11-01 17:04:16 +01001752 struct buffer_head *bh,
1753 int submit_bio_bh_rw)
1754{
1755 int is_metadata;
1756 struct btrfsic_block *block;
1757 struct btrfsic_block_data_ctx block_ctx;
1758 int ret;
1759 struct btrfsic_state *state = dev_state->state;
1760 struct block_device *bdev = dev_state->bdev;
Stefan Behrense06baab2012-04-12 12:53:40 +02001761 unsigned int processed_len;
Stefan Behrens5db02762011-11-01 17:04:16 +01001762
Stefan Behrens5db02762011-11-01 17:04:16 +01001763 if (NULL != bio_is_patched)
1764 *bio_is_patched = 0;
1765
Stefan Behrense06baab2012-04-12 12:53:40 +02001766again:
1767 if (num_pages == 0)
1768 return;
1769
1770 processed_len = 0;
1771 is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
1772 num_pages));
1773
Stefan Behrens5db02762011-11-01 17:04:16 +01001774 block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
1775 &state->block_hashtable);
1776 if (NULL != block) {
Stefan Behrens0b485142012-01-26 15:01:11 -05001777 u64 bytenr = 0;
Geliang Tangb69f2be2015-12-18 22:16:59 +08001778 struct btrfsic_block_link *l, *tmp;
Stefan Behrens5db02762011-11-01 17:04:16 +01001779
1780 if (block->is_superblock) {
Qu Wenruo3cae2102013-07-16 11:19:18 +08001781 bytenr = btrfs_super_bytenr((struct btrfs_super_block *)
1782 mapped_datav[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001783 if (num_pages * PAGE_SIZE <
Stefan Behrense06baab2012-04-12 12:53:40 +02001784 BTRFS_SUPER_INFO_SIZE) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001785 pr_info("btrfsic: cannot work with too short bios!\n");
Stefan Behrense06baab2012-04-12 12:53:40 +02001786 return;
1787 }
Stefan Behrens5db02762011-11-01 17:04:16 +01001788 is_metadata = 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001789 BUG_ON(BTRFS_SUPER_INFO_SIZE & (PAGE_SIZE - 1));
Stefan Behrense06baab2012-04-12 12:53:40 +02001790 processed_len = BTRFS_SUPER_INFO_SIZE;
Stefan Behrens5db02762011-11-01 17:04:16 +01001791 if (state->print_mask &
1792 BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001793 pr_info("[before new superblock is written]:\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01001794 btrfsic_dump_tree_sub(state, block, 0);
1795 }
1796 }
1797 if (is_metadata) {
1798 if (!block->is_superblock) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001799 if (num_pages * PAGE_SIZE <
Stefan Behrense06baab2012-04-12 12:53:40 +02001800 state->metablock_size) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001801 pr_info("btrfsic: cannot work with too short bios!\n");
Stefan Behrense06baab2012-04-12 12:53:40 +02001802 return;
1803 }
1804 processed_len = state->metablock_size;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001805 bytenr = btrfs_stack_header_bytenr(
1806 (struct btrfs_header *)
1807 mapped_datav[0]);
Stefan Behrens5db02762011-11-01 17:04:16 +01001808 btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
1809 dev_state,
Stefan Behrense06baab2012-04-12 12:53:40 +02001810 dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01001811 }
Stefan Behrenscf90c592014-10-17 14:10:10 +02001812 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) {
1813 if (block->logical_bytenr != bytenr &&
1814 !(!block->is_metadata &&
1815 block->logical_bytenr == 0))
Jeff Mahoney62e85572016-09-20 10:05:01 -04001816 pr_info("Written block @%llu (%s/%llu/%d) found in hash table, %c, bytenr mismatch (!= stored %llu).\n",
Stefan Behrenscf90c592014-10-17 14:10:10 +02001817 bytenr, dev_state->name,
1818 dev_bytenr,
1819 block->mirror_num,
1820 btrfsic_get_block_type(state,
1821 block),
1822 block->logical_bytenr);
1823 else
Jeff Mahoney62e85572016-09-20 10:05:01 -04001824 pr_info("Written block @%llu (%s/%llu/%d) found in hash table, %c.\n",
Stefan Behrenscf90c592014-10-17 14:10:10 +02001825 bytenr, dev_state->name,
1826 dev_bytenr, block->mirror_num,
1827 btrfsic_get_block_type(state,
1828 block));
1829 }
Stefan Behrens301993a2013-10-21 18:46:58 +02001830 block->logical_bytenr = bytenr;
Stefan Behrens5db02762011-11-01 17:04:16 +01001831 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001832 if (num_pages * PAGE_SIZE <
Stefan Behrense06baab2012-04-12 12:53:40 +02001833 state->datablock_size) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001834 pr_info("btrfsic: cannot work with too short bios!\n");
Stefan Behrense06baab2012-04-12 12:53:40 +02001835 return;
1836 }
1837 processed_len = state->datablock_size;
Stefan Behrens5db02762011-11-01 17:04:16 +01001838 bytenr = block->logical_bytenr;
1839 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04001840 pr_info("Written block @%llu (%s/%llu/%d) found in hash table, %c.\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001841 bytenr, dev_state->name, dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01001842 block->mirror_num,
1843 btrfsic_get_block_type(state, block));
1844 }
1845
1846 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04001847 pr_info("ref_to_list: %cE, ref_from_list: %cE\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01001848 list_empty(&block->ref_to_list) ? ' ' : '!',
1849 list_empty(&block->ref_from_list) ? ' ' : '!');
1850 if (btrfsic_is_block_ref_by_superblock(state, block, 0)) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001851 pr_info("btrfs: attempt to overwrite %c-block @%llu (%s/%llu/%d), old(gen=%llu, objectid=%llu, type=%d, offset=%llu), new(gen=%llu), which is referenced by most recent superblock (superblockgen=%llu)!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001852 btrfsic_get_block_type(state, block), bytenr,
1853 dev_state->name, dev_bytenr, block->mirror_num,
1854 block->generation,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001855 btrfs_disk_key_objectid(&block->disk_key),
Stefan Behrens5db02762011-11-01 17:04:16 +01001856 block->disk_key.type,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001857 btrfs_disk_key_offset(&block->disk_key),
Qu Wenruo3cae2102013-07-16 11:19:18 +08001858 btrfs_stack_header_generation(
1859 (struct btrfs_header *) mapped_datav[0]),
Stefan Behrens5db02762011-11-01 17:04:16 +01001860 state->max_superblock_generation);
1861 btrfsic_dump_tree(state);
1862 }
1863
1864 if (!block->is_iodone && !block->never_written) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001865 pr_info("btrfs: attempt to overwrite %c-block @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu, which is not yet iodone!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001866 btrfsic_get_block_type(state, block), bytenr,
1867 dev_state->name, dev_bytenr, block->mirror_num,
1868 block->generation,
Qu Wenruo3cae2102013-07-16 11:19:18 +08001869 btrfs_stack_header_generation(
1870 (struct btrfs_header *)
1871 mapped_datav[0]));
Stefan Behrens5db02762011-11-01 17:04:16 +01001872 /* it would not be safe to go on */
1873 btrfsic_dump_tree(state);
Stefan Behrense06baab2012-04-12 12:53:40 +02001874 goto continue_loop;
Stefan Behrens5db02762011-11-01 17:04:16 +01001875 }
1876
1877 /*
1878 * Clear all references of this block. Do not free
1879 * the block itself even if is not referenced anymore
Nicholas D Steeves01327612016-05-19 21:18:45 -04001880 * because it still carries valuable information
Stefan Behrens5db02762011-11-01 17:04:16 +01001881 * like whether it was ever written and IO completed.
1882 */
Geliang Tangb69f2be2015-12-18 22:16:59 +08001883 list_for_each_entry_safe(l, tmp, &block->ref_to_list,
1884 node_ref_to) {
Stefan Behrens5db02762011-11-01 17:04:16 +01001885 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1886 btrfsic_print_rem_link(state, l);
1887 l->ref_cnt--;
1888 if (0 == l->ref_cnt) {
1889 list_del(&l->node_ref_to);
1890 list_del(&l->node_ref_from);
1891 btrfsic_block_link_hashtable_remove(l);
1892 btrfsic_block_link_free(l);
1893 }
1894 }
1895
Stefan Behrens5db02762011-11-01 17:04:16 +01001896 block_ctx.dev = dev_state;
1897 block_ctx.dev_bytenr = dev_bytenr;
Stefan Behrensf382e462014-10-16 17:48:48 +02001898 block_ctx.start = bytenr;
1899 block_ctx.len = processed_len;
1900 block_ctx.pagev = NULL;
1901 block_ctx.mem_to_free = NULL;
1902 block_ctx.datav = mapped_datav;
Stefan Behrens5db02762011-11-01 17:04:16 +01001903
1904 if (is_metadata || state->include_extent_data) {
1905 block->never_written = 0;
1906 block->iodone_w_error = 0;
1907 if (NULL != bio) {
1908 block->is_iodone = 0;
1909 BUG_ON(NULL == bio_is_patched);
1910 if (!*bio_is_patched) {
1911 block->orig_bio_bh_private =
1912 bio->bi_private;
1913 block->orig_bio_bh_end_io.bio =
1914 bio->bi_end_io;
1915 block->next_in_same_bio = NULL;
1916 bio->bi_private = block;
1917 bio->bi_end_io = btrfsic_bio_end_io;
1918 *bio_is_patched = 1;
1919 } else {
1920 struct btrfsic_block *chained_block =
1921 (struct btrfsic_block *)
1922 bio->bi_private;
1923
1924 BUG_ON(NULL == chained_block);
1925 block->orig_bio_bh_private =
1926 chained_block->orig_bio_bh_private;
1927 block->orig_bio_bh_end_io.bio =
1928 chained_block->orig_bio_bh_end_io.
1929 bio;
1930 block->next_in_same_bio = chained_block;
1931 bio->bi_private = block;
1932 }
1933 } else if (NULL != bh) {
1934 block->is_iodone = 0;
1935 block->orig_bio_bh_private = bh->b_private;
1936 block->orig_bio_bh_end_io.bh = bh->b_end_io;
1937 block->next_in_same_bio = NULL;
1938 bh->b_private = block;
1939 bh->b_end_io = btrfsic_bh_end_io;
1940 } else {
1941 block->is_iodone = 1;
1942 block->orig_bio_bh_private = NULL;
1943 block->orig_bio_bh_end_io.bio = NULL;
1944 block->next_in_same_bio = NULL;
1945 }
1946 }
1947
1948 block->flush_gen = dev_state->last_flush_gen + 1;
1949 block->submit_bio_bh_rw = submit_bio_bh_rw;
1950 if (is_metadata) {
1951 block->logical_bytenr = bytenr;
1952 block->is_metadata = 1;
1953 if (block->is_superblock) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001954 BUG_ON(PAGE_SIZE !=
Stefan Behrense06baab2012-04-12 12:53:40 +02001955 BTRFS_SUPER_INFO_SIZE);
Stefan Behrens5db02762011-11-01 17:04:16 +01001956 ret = btrfsic_process_written_superblock(
1957 state,
1958 block,
1959 (struct btrfs_super_block *)
Stefan Behrense06baab2012-04-12 12:53:40 +02001960 mapped_datav[0]);
Stefan Behrens5db02762011-11-01 17:04:16 +01001961 if (state->print_mask &
1962 BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001963 pr_info("[after new superblock is written]:\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01001964 btrfsic_dump_tree_sub(state, block, 0);
1965 }
1966 } else {
1967 block->mirror_num = 0; /* unknown */
1968 ret = btrfsic_process_metablock(
1969 state,
1970 block,
1971 &block_ctx,
Stefan Behrens5db02762011-11-01 17:04:16 +01001972 0, 0);
1973 }
1974 if (ret)
Jeff Mahoney62e85572016-09-20 10:05:01 -04001975 pr_info("btrfsic: btrfsic_process_metablock(root @%llu) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001976 dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01001977 } else {
1978 block->is_metadata = 0;
1979 block->mirror_num = 0; /* unknown */
1980 block->generation = BTRFSIC_GENERATION_UNKNOWN;
1981 if (!state->include_extent_data
1982 && list_empty(&block->ref_from_list)) {
1983 /*
1984 * disk block is overwritten with extent
1985 * data (not meta data) and we are configured
1986 * to not include extent data: take the
1987 * chance and free the block's memory
1988 */
1989 btrfsic_block_hashtable_remove(block);
1990 list_del(&block->all_blocks_node);
1991 btrfsic_block_free(block);
1992 }
1993 }
1994 btrfsic_release_block_ctx(&block_ctx);
1995 } else {
1996 /* block has not been found in hash table */
1997 u64 bytenr;
1998
1999 if (!is_metadata) {
Stefan Behrense06baab2012-04-12 12:53:40 +02002000 processed_len = state->datablock_size;
Stefan Behrens5db02762011-11-01 17:04:16 +01002001 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002002 pr_info("Written block (%s/%llu/?) !found in hash table, D.\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002003 dev_state->name, dev_bytenr);
Stefan Behrense06baab2012-04-12 12:53:40 +02002004 if (!state->include_extent_data) {
2005 /* ignore that written D block */
2006 goto continue_loop;
2007 }
Stefan Behrens5db02762011-11-01 17:04:16 +01002008
2009 /* this is getting ugly for the
2010 * include_extent_data case... */
2011 bytenr = 0; /* unknown */
Stefan Behrens5db02762011-11-01 17:04:16 +01002012 } else {
Stefan Behrense06baab2012-04-12 12:53:40 +02002013 processed_len = state->metablock_size;
Qu Wenruo3cae2102013-07-16 11:19:18 +08002014 bytenr = btrfs_stack_header_bytenr(
2015 (struct btrfs_header *)
2016 mapped_datav[0]);
Stefan Behrens5db02762011-11-01 17:04:16 +01002017 btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
Stefan Behrense06baab2012-04-12 12:53:40 +02002018 dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002019 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002020 pr_info("Written block @%llu (%s/%llu/?) !found in hash table, M.\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002021 bytenr, dev_state->name, dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002022 }
Stefan Behrensf382e462014-10-16 17:48:48 +02002023
Stefan Behrens5db02762011-11-01 17:04:16 +01002024 block_ctx.dev = dev_state;
2025 block_ctx.dev_bytenr = dev_bytenr;
Stefan Behrensf382e462014-10-16 17:48:48 +02002026 block_ctx.start = bytenr;
2027 block_ctx.len = processed_len;
2028 block_ctx.pagev = NULL;
2029 block_ctx.mem_to_free = NULL;
2030 block_ctx.datav = mapped_datav;
Stefan Behrens5db02762011-11-01 17:04:16 +01002031
2032 block = btrfsic_block_alloc();
2033 if (NULL == block) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002034 pr_info("btrfsic: error, kmalloc failed!\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01002035 btrfsic_release_block_ctx(&block_ctx);
Stefan Behrense06baab2012-04-12 12:53:40 +02002036 goto continue_loop;
Stefan Behrens5db02762011-11-01 17:04:16 +01002037 }
2038 block->dev_state = dev_state;
2039 block->dev_bytenr = dev_bytenr;
2040 block->logical_bytenr = bytenr;
2041 block->is_metadata = is_metadata;
2042 block->never_written = 0;
2043 block->iodone_w_error = 0;
2044 block->mirror_num = 0; /* unknown */
2045 block->flush_gen = dev_state->last_flush_gen + 1;
2046 block->submit_bio_bh_rw = submit_bio_bh_rw;
2047 if (NULL != bio) {
2048 block->is_iodone = 0;
2049 BUG_ON(NULL == bio_is_patched);
2050 if (!*bio_is_patched) {
2051 block->orig_bio_bh_private = bio->bi_private;
2052 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
2053 block->next_in_same_bio = NULL;
2054 bio->bi_private = block;
2055 bio->bi_end_io = btrfsic_bio_end_io;
2056 *bio_is_patched = 1;
2057 } else {
2058 struct btrfsic_block *chained_block =
2059 (struct btrfsic_block *)
2060 bio->bi_private;
2061
2062 BUG_ON(NULL == chained_block);
2063 block->orig_bio_bh_private =
2064 chained_block->orig_bio_bh_private;
2065 block->orig_bio_bh_end_io.bio =
2066 chained_block->orig_bio_bh_end_io.bio;
2067 block->next_in_same_bio = chained_block;
2068 bio->bi_private = block;
2069 }
2070 } else if (NULL != bh) {
2071 block->is_iodone = 0;
2072 block->orig_bio_bh_private = bh->b_private;
2073 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2074 block->next_in_same_bio = NULL;
2075 bh->b_private = block;
2076 bh->b_end_io = btrfsic_bh_end_io;
2077 } else {
2078 block->is_iodone = 1;
2079 block->orig_bio_bh_private = NULL;
2080 block->orig_bio_bh_end_io.bio = NULL;
2081 block->next_in_same_bio = NULL;
2082 }
2083 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002084 pr_info("New written %c-block @%llu (%s/%llu/%d)\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002085 is_metadata ? 'M' : 'D',
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002086 block->logical_bytenr, block->dev_state->name,
2087 block->dev_bytenr, block->mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002088 list_add(&block->all_blocks_node, &state->all_blocks_list);
2089 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2090
2091 if (is_metadata) {
2092 ret = btrfsic_process_metablock(state, block,
Stefan Behrense06baab2012-04-12 12:53:40 +02002093 &block_ctx, 0, 0);
Stefan Behrens5db02762011-11-01 17:04:16 +01002094 if (ret)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002095 pr_info("btrfsic: process_metablock(root @%llu) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002096 dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002097 }
2098 btrfsic_release_block_ctx(&block_ctx);
2099 }
Stefan Behrense06baab2012-04-12 12:53:40 +02002100
2101continue_loop:
2102 BUG_ON(!processed_len);
2103 dev_bytenr += processed_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002104 mapped_datav += processed_len >> PAGE_SHIFT;
2105 num_pages -= processed_len >> PAGE_SHIFT;
Stefan Behrense06baab2012-04-12 12:53:40 +02002106 goto again;
Stefan Behrens5db02762011-11-01 17:04:16 +01002107}
2108
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002109static void btrfsic_bio_end_io(struct bio *bp)
Stefan Behrens5db02762011-11-01 17:04:16 +01002110{
2111 struct btrfsic_block *block = (struct btrfsic_block *)bp->bi_private;
2112 int iodone_w_error;
2113
2114 /* mutex is not held! This is not save if IO is not yet completed
2115 * on umount */
2116 iodone_w_error = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002117 if (bp->bi_status)
Stefan Behrens5db02762011-11-01 17:04:16 +01002118 iodone_w_error = 1;
2119
2120 BUG_ON(NULL == block);
2121 bp->bi_private = block->orig_bio_bh_private;
2122 bp->bi_end_io = block->orig_bio_bh_end_io.bio;
2123
2124 do {
2125 struct btrfsic_block *next_block;
2126 struct btrfsic_dev_state *const dev_state = block->dev_state;
2127
2128 if ((dev_state->state->print_mask &
2129 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
Jeff Mahoney62e85572016-09-20 10:05:01 -04002130 pr_info("bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002131 bp->bi_status,
Stefan Behrens5db02762011-11-01 17:04:16 +01002132 btrfsic_get_block_type(dev_state->state, block),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002133 block->logical_bytenr, dev_state->name,
2134 block->dev_bytenr, block->mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002135 next_block = block->next_in_same_bio;
2136 block->iodone_w_error = iodone_w_error;
Mike Christie28a8f0d2016-06-05 14:32:25 -05002137 if (block->submit_bio_bh_rw & REQ_PREFLUSH) {
Stefan Behrens5db02762011-11-01 17:04:16 +01002138 dev_state->last_flush_gen++;
2139 if ((dev_state->state->print_mask &
2140 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
Jeff Mahoney62e85572016-09-20 10:05:01 -04002141 pr_info("bio_end_io() new %s flush_gen=%llu\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002142 dev_state->name,
Stefan Behrens5db02762011-11-01 17:04:16 +01002143 dev_state->last_flush_gen);
2144 }
2145 if (block->submit_bio_bh_rw & REQ_FUA)
2146 block->flush_gen = 0; /* FUA completed means block is
2147 * on disk */
2148 block->is_iodone = 1; /* for FLUSH, this releases the block */
2149 block = next_block;
2150 } while (NULL != block);
2151
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002152 bp->bi_end_io(bp);
Stefan Behrens5db02762011-11-01 17:04:16 +01002153}
2154
2155static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate)
2156{
2157 struct btrfsic_block *block = (struct btrfsic_block *)bh->b_private;
2158 int iodone_w_error = !uptodate;
2159 struct btrfsic_dev_state *dev_state;
2160
2161 BUG_ON(NULL == block);
2162 dev_state = block->dev_state;
2163 if ((dev_state->state->print_mask & BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
Jeff Mahoney62e85572016-09-20 10:05:01 -04002164 pr_info("bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002165 iodone_w_error,
2166 btrfsic_get_block_type(dev_state->state, block),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002167 block->logical_bytenr, block->dev_state->name,
2168 block->dev_bytenr, block->mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002169
2170 block->iodone_w_error = iodone_w_error;
Mike Christie28a8f0d2016-06-05 14:32:25 -05002171 if (block->submit_bio_bh_rw & REQ_PREFLUSH) {
Stefan Behrens5db02762011-11-01 17:04:16 +01002172 dev_state->last_flush_gen++;
2173 if ((dev_state->state->print_mask &
2174 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
Jeff Mahoney62e85572016-09-20 10:05:01 -04002175 pr_info("bh_end_io() new %s flush_gen=%llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002176 dev_state->name, dev_state->last_flush_gen);
Stefan Behrens5db02762011-11-01 17:04:16 +01002177 }
2178 if (block->submit_bio_bh_rw & REQ_FUA)
2179 block->flush_gen = 0; /* FUA completed means block is on disk */
2180
2181 bh->b_private = block->orig_bio_bh_private;
2182 bh->b_end_io = block->orig_bio_bh_end_io.bh;
2183 block->is_iodone = 1; /* for FLUSH, this releases the block */
2184 bh->b_end_io(bh, uptodate);
2185}
2186
2187static int btrfsic_process_written_superblock(
2188 struct btrfsic_state *state,
2189 struct btrfsic_block *const superblock,
2190 struct btrfs_super_block *const super_hdr)
2191{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002192 struct btrfs_fs_info *fs_info = state->fs_info;
Stefan Behrens5db02762011-11-01 17:04:16 +01002193 int pass;
2194
2195 superblock->generation = btrfs_super_generation(super_hdr);
2196 if (!(superblock->generation > state->max_superblock_generation ||
2197 0 == state->max_superblock_generation)) {
2198 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002199 pr_info("btrfsic: superblock @%llu (%s/%llu/%d) with old gen %llu <= %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002200 superblock->logical_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002201 superblock->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002202 superblock->dev_bytenr, superblock->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01002203 btrfs_super_generation(super_hdr),
Stefan Behrens5db02762011-11-01 17:04:16 +01002204 state->max_superblock_generation);
2205 } else {
2206 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002207 pr_info("btrfsic: got new superblock @%llu (%s/%llu/%d) with new gen %llu > %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002208 superblock->logical_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002209 superblock->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002210 superblock->dev_bytenr, superblock->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01002211 btrfs_super_generation(super_hdr),
Stefan Behrens5db02762011-11-01 17:04:16 +01002212 state->max_superblock_generation);
2213
2214 state->max_superblock_generation =
2215 btrfs_super_generation(super_hdr);
2216 state->latest_superblock = superblock;
2217 }
2218
2219 for (pass = 0; pass < 3; pass++) {
2220 int ret;
2221 u64 next_bytenr;
2222 struct btrfsic_block *next_block;
2223 struct btrfsic_block_data_ctx tmp_next_block_ctx;
2224 struct btrfsic_block_link *l;
2225 int num_copies;
2226 int mirror_num;
2227 const char *additional_string = NULL;
Stefan Behrens35a36212013-08-14 18:12:25 +02002228 struct btrfs_disk_key tmp_disk_key = {0};
Stefan Behrens5db02762011-11-01 17:04:16 +01002229
Qu Wenruo3cae2102013-07-16 11:19:18 +08002230 btrfs_set_disk_key_objectid(&tmp_disk_key,
2231 BTRFS_ROOT_ITEM_KEY);
2232 btrfs_set_disk_key_objectid(&tmp_disk_key, 0);
Stefan Behrens5db02762011-11-01 17:04:16 +01002233
2234 switch (pass) {
2235 case 0:
Qu Wenruo3cae2102013-07-16 11:19:18 +08002236 btrfs_set_disk_key_objectid(&tmp_disk_key,
2237 BTRFS_ROOT_TREE_OBJECTID);
Stefan Behrens5db02762011-11-01 17:04:16 +01002238 additional_string = "root ";
2239 next_bytenr = btrfs_super_root(super_hdr);
2240 if (state->print_mask &
2241 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002242 pr_info("root@%llu\n", next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002243 break;
2244 case 1:
Qu Wenruo3cae2102013-07-16 11:19:18 +08002245 btrfs_set_disk_key_objectid(&tmp_disk_key,
2246 BTRFS_CHUNK_TREE_OBJECTID);
Stefan Behrens5db02762011-11-01 17:04:16 +01002247 additional_string = "chunk ";
2248 next_bytenr = btrfs_super_chunk_root(super_hdr);
2249 if (state->print_mask &
2250 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002251 pr_info("chunk@%llu\n", next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002252 break;
2253 case 2:
Qu Wenruo3cae2102013-07-16 11:19:18 +08002254 btrfs_set_disk_key_objectid(&tmp_disk_key,
2255 BTRFS_TREE_LOG_OBJECTID);
Stefan Behrens5db02762011-11-01 17:04:16 +01002256 additional_string = "log ";
2257 next_bytenr = btrfs_super_log_root(super_hdr);
2258 if (0 == next_bytenr)
2259 continue;
2260 if (state->print_mask &
2261 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002262 pr_info("log@%llu\n", next_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002263 break;
2264 }
2265
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002266 num_copies = btrfs_num_copies(fs_info, next_bytenr,
2267 BTRFS_SUPER_INFO_SIZE);
Stefan Behrens5db02762011-11-01 17:04:16 +01002268 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002269 pr_info("num_copies(log_bytenr=%llu) = %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002270 next_bytenr, num_copies);
Stefan Behrens5db02762011-11-01 17:04:16 +01002271 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2272 int was_created;
2273
2274 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002275 pr_info("btrfsic_process_written_superblock(mirror_num=%d)\n", mirror_num);
Stefan Behrense06baab2012-04-12 12:53:40 +02002276 ret = btrfsic_map_block(state, next_bytenr,
2277 BTRFS_SUPER_INFO_SIZE,
Stefan Behrens5db02762011-11-01 17:04:16 +01002278 &tmp_next_block_ctx,
2279 mirror_num);
2280 if (ret) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002281 pr_info("btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002282 next_bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002283 return -1;
2284 }
2285
2286 next_block = btrfsic_block_lookup_or_add(
2287 state,
2288 &tmp_next_block_ctx,
2289 additional_string,
2290 1, 0, 1,
2291 mirror_num,
2292 &was_created);
2293 if (NULL == next_block) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002294 pr_info("btrfsic: error, kmalloc failed!\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01002295 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2296 return -1;
2297 }
2298
2299 next_block->disk_key = tmp_disk_key;
2300 if (was_created)
2301 next_block->generation =
2302 BTRFSIC_GENERATION_UNKNOWN;
2303 l = btrfsic_block_link_lookup_or_add(
2304 state,
2305 &tmp_next_block_ctx,
2306 next_block,
2307 superblock,
2308 BTRFSIC_GENERATION_UNKNOWN);
2309 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2310 if (NULL == l)
2311 return -1;
2312 }
2313 }
2314
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302315 if (WARN_ON(-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)))
Stefan Behrens5db02762011-11-01 17:04:16 +01002316 btrfsic_dump_tree(state);
Stefan Behrens5db02762011-11-01 17:04:16 +01002317
2318 return 0;
2319}
2320
2321static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
2322 struct btrfsic_block *const block,
2323 int recursion_level)
2324{
Geliang Tangb69f2be2015-12-18 22:16:59 +08002325 const struct btrfsic_block_link *l;
Stefan Behrens5db02762011-11-01 17:04:16 +01002326 int ret = 0;
2327
2328 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2329 /*
2330 * Note that this situation can happen and does not
2331 * indicate an error in regular cases. It happens
2332 * when disk blocks are freed and later reused.
2333 * The check-integrity module is not aware of any
2334 * block free operations, it just recognizes block
2335 * write operations. Therefore it keeps the linkage
2336 * information for a block until a block is
2337 * rewritten. This can temporarily cause incorrect
2338 * and even circular linkage informations. This
2339 * causes no harm unless such blocks are referenced
2340 * by the most recent super block.
2341 */
2342 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002343 pr_info("btrfsic: abort cyclic linkage (case 1).\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01002344
2345 return ret;
2346 }
2347
2348 /*
2349 * This algorithm is recursive because the amount of used stack
2350 * space is very small and the max recursion depth is limited.
2351 */
Geliang Tangb69f2be2015-12-18 22:16:59 +08002352 list_for_each_entry(l, &block->ref_to_list, node_ref_to) {
Stefan Behrens5db02762011-11-01 17:04:16 +01002353 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002354 pr_info("rl=%d, %c @%llu (%s/%llu/%d) %u* refers to %c @%llu (%s/%llu/%d)\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002355 recursion_level,
2356 btrfsic_get_block_type(state, block),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002357 block->logical_bytenr, block->dev_state->name,
2358 block->dev_bytenr, block->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01002359 l->ref_cnt,
2360 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens5db02762011-11-01 17:04:16 +01002361 l->block_ref_to->logical_bytenr,
2362 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002363 l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002364 l->block_ref_to->mirror_num);
2365 if (l->block_ref_to->never_written) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002366 pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which is never written!\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002367 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens5db02762011-11-01 17:04:16 +01002368 l->block_ref_to->logical_bytenr,
2369 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002370 l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002371 l->block_ref_to->mirror_num);
2372 ret = -1;
2373 } else if (!l->block_ref_to->is_iodone) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002374 pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which is not yet iodone!\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002375 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens5db02762011-11-01 17:04:16 +01002376 l->block_ref_to->logical_bytenr,
2377 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002378 l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002379 l->block_ref_to->mirror_num);
2380 ret = -1;
Stefan Behrens62856a92012-07-31 11:09:44 -06002381 } else if (l->block_ref_to->iodone_w_error) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002382 pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which has write error!\n",
Stefan Behrens62856a92012-07-31 11:09:44 -06002383 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens62856a92012-07-31 11:09:44 -06002384 l->block_ref_to->logical_bytenr,
2385 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002386 l->block_ref_to->dev_bytenr,
Stefan Behrens62856a92012-07-31 11:09:44 -06002387 l->block_ref_to->mirror_num);
2388 ret = -1;
Stefan Behrens5db02762011-11-01 17:04:16 +01002389 } else if (l->parent_generation !=
2390 l->block_ref_to->generation &&
2391 BTRFSIC_GENERATION_UNKNOWN !=
2392 l->parent_generation &&
2393 BTRFSIC_GENERATION_UNKNOWN !=
2394 l->block_ref_to->generation) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002395 pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) with generation %llu != parent generation %llu!\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002396 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens5db02762011-11-01 17:04:16 +01002397 l->block_ref_to->logical_bytenr,
2398 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002399 l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002400 l->block_ref_to->mirror_num,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002401 l->block_ref_to->generation,
2402 l->parent_generation);
Stefan Behrens5db02762011-11-01 17:04:16 +01002403 ret = -1;
2404 } else if (l->block_ref_to->flush_gen >
2405 l->block_ref_to->dev_state->last_flush_gen) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002406 pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which is not flushed out of disk's write cache (block flush_gen=%llu, dev->flush_gen=%llu)!\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002407 btrfsic_get_block_type(state, l->block_ref_to),
Stefan Behrens5db02762011-11-01 17:04:16 +01002408 l->block_ref_to->logical_bytenr,
2409 l->block_ref_to->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002410 l->block_ref_to->dev_bytenr,
2411 l->block_ref_to->mirror_num, block->flush_gen,
Stefan Behrens5db02762011-11-01 17:04:16 +01002412 l->block_ref_to->dev_state->last_flush_gen);
2413 ret = -1;
2414 } else if (-1 == btrfsic_check_all_ref_blocks(state,
2415 l->block_ref_to,
2416 recursion_level +
2417 1)) {
2418 ret = -1;
2419 }
2420 }
2421
2422 return ret;
2423}
2424
2425static int btrfsic_is_block_ref_by_superblock(
2426 const struct btrfsic_state *state,
2427 const struct btrfsic_block *block,
2428 int recursion_level)
2429{
Geliang Tangb69f2be2015-12-18 22:16:59 +08002430 const struct btrfsic_block_link *l;
Stefan Behrens5db02762011-11-01 17:04:16 +01002431
2432 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2433 /* refer to comment at "abort cyclic linkage (case 1)" */
2434 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002435 pr_info("btrfsic: abort cyclic linkage (case 2).\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01002436
2437 return 0;
2438 }
2439
2440 /*
2441 * This algorithm is recursive because the amount of used stack space
2442 * is very small and the max recursion depth is limited.
2443 */
Geliang Tangb69f2be2015-12-18 22:16:59 +08002444 list_for_each_entry(l, &block->ref_from_list, node_ref_from) {
Stefan Behrens5db02762011-11-01 17:04:16 +01002445 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002446 pr_info("rl=%d, %c @%llu (%s/%llu/%d) is ref %u* from %c @%llu (%s/%llu/%d)\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002447 recursion_level,
2448 btrfsic_get_block_type(state, block),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002449 block->logical_bytenr, block->dev_state->name,
2450 block->dev_bytenr, block->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01002451 l->ref_cnt,
2452 btrfsic_get_block_type(state, l->block_ref_from),
Stefan Behrens5db02762011-11-01 17:04:16 +01002453 l->block_ref_from->logical_bytenr,
2454 l->block_ref_from->dev_state->name,
Stefan Behrens5db02762011-11-01 17:04:16 +01002455 l->block_ref_from->dev_bytenr,
2456 l->block_ref_from->mirror_num);
2457 if (l->block_ref_from->is_superblock &&
2458 state->latest_superblock->dev_bytenr ==
2459 l->block_ref_from->dev_bytenr &&
2460 state->latest_superblock->dev_state->bdev ==
2461 l->block_ref_from->dev_state->bdev)
2462 return 1;
2463 else if (btrfsic_is_block_ref_by_superblock(state,
2464 l->block_ref_from,
2465 recursion_level +
2466 1))
2467 return 1;
2468 }
2469
2470 return 0;
2471}
2472
2473static void btrfsic_print_add_link(const struct btrfsic_state *state,
2474 const struct btrfsic_block_link *l)
2475{
Jeff Mahoney62e85572016-09-20 10:05:01 -04002476 pr_info("Add %u* link from %c @%llu (%s/%llu/%d) to %c @%llu (%s/%llu/%d).\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002477 l->ref_cnt,
2478 btrfsic_get_block_type(state, l->block_ref_from),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002479 l->block_ref_from->logical_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002480 l->block_ref_from->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002481 l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01002482 btrfsic_get_block_type(state, l->block_ref_to),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002483 l->block_ref_to->logical_bytenr,
2484 l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002485 l->block_ref_to->mirror_num);
2486}
2487
2488static void btrfsic_print_rem_link(const struct btrfsic_state *state,
2489 const struct btrfsic_block_link *l)
2490{
Jeff Mahoney62e85572016-09-20 10:05:01 -04002491 pr_info("Rem %u* link from %c @%llu (%s/%llu/%d) to %c @%llu (%s/%llu/%d).\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002492 l->ref_cnt,
2493 btrfsic_get_block_type(state, l->block_ref_from),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002494 l->block_ref_from->logical_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002495 l->block_ref_from->dev_state->name,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002496 l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
Stefan Behrens5db02762011-11-01 17:04:16 +01002497 btrfsic_get_block_type(state, l->block_ref_to),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002498 l->block_ref_to->logical_bytenr,
2499 l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
Stefan Behrens5db02762011-11-01 17:04:16 +01002500 l->block_ref_to->mirror_num);
2501}
2502
2503static char btrfsic_get_block_type(const struct btrfsic_state *state,
2504 const struct btrfsic_block *block)
2505{
2506 if (block->is_superblock &&
2507 state->latest_superblock->dev_bytenr == block->dev_bytenr &&
2508 state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
2509 return 'S';
2510 else if (block->is_superblock)
2511 return 's';
2512 else if (block->is_metadata)
2513 return 'M';
2514 else
2515 return 'D';
2516}
2517
2518static void btrfsic_dump_tree(const struct btrfsic_state *state)
2519{
2520 btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
2521}
2522
2523static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
2524 const struct btrfsic_block *block,
2525 int indent_level)
2526{
Geliang Tangb69f2be2015-12-18 22:16:59 +08002527 const struct btrfsic_block_link *l;
Stefan Behrens5db02762011-11-01 17:04:16 +01002528 int indent_add;
2529 static char buf[80];
2530 int cursor_position;
2531
2532 /*
2533 * Should better fill an on-stack buffer with a complete line and
2534 * dump it at once when it is time to print a newline character.
2535 */
2536
2537 /*
2538 * This algorithm is recursive because the amount of used stack space
2539 * is very small and the max recursion depth is limited.
2540 */
Heinrich Schuchardt16ff4b42016-06-11 18:11:10 +02002541 indent_add = sprintf(buf, "%c-%llu(%s/%llu/%u)",
Stefan Behrens5db02762011-11-01 17:04:16 +01002542 btrfsic_get_block_type(state, block),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002543 block->logical_bytenr, block->dev_state->name,
2544 block->dev_bytenr, block->mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002545 if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2546 printk("[...]\n");
2547 return;
2548 }
2549 printk(buf);
2550 indent_level += indent_add;
2551 if (list_empty(&block->ref_to_list)) {
2552 printk("\n");
2553 return;
2554 }
2555 if (block->mirror_num > 1 &&
2556 !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
2557 printk(" [...]\n");
2558 return;
2559 }
2560
2561 cursor_position = indent_level;
Geliang Tangb69f2be2015-12-18 22:16:59 +08002562 list_for_each_entry(l, &block->ref_to_list, node_ref_to) {
Stefan Behrens5db02762011-11-01 17:04:16 +01002563 while (cursor_position < indent_level) {
2564 printk(" ");
2565 cursor_position++;
2566 }
2567 if (l->ref_cnt > 1)
2568 indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
2569 else
2570 indent_add = sprintf(buf, " --> ");
2571 if (indent_level + indent_add >
2572 BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2573 printk("[...]\n");
2574 cursor_position = 0;
2575 continue;
2576 }
2577
2578 printk(buf);
2579
2580 btrfsic_dump_tree_sub(state, l->block_ref_to,
2581 indent_level + indent_add);
2582 cursor_position = 0;
2583 }
2584}
2585
2586static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
2587 struct btrfsic_state *state,
2588 struct btrfsic_block_data_ctx *next_block_ctx,
2589 struct btrfsic_block *next_block,
2590 struct btrfsic_block *from_block,
2591 u64 parent_generation)
2592{
2593 struct btrfsic_block_link *l;
2594
2595 l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
2596 next_block_ctx->dev_bytenr,
2597 from_block->dev_state->bdev,
2598 from_block->dev_bytenr,
2599 &state->block_link_hashtable);
2600 if (NULL == l) {
2601 l = btrfsic_block_link_alloc();
2602 if (NULL == l) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002603 pr_info("btrfsic: error, kmalloc failed!\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01002604 return NULL;
2605 }
2606
2607 l->block_ref_to = next_block;
2608 l->block_ref_from = from_block;
2609 l->ref_cnt = 1;
2610 l->parent_generation = parent_generation;
2611
2612 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2613 btrfsic_print_add_link(state, l);
2614
2615 list_add(&l->node_ref_to, &from_block->ref_to_list);
2616 list_add(&l->node_ref_from, &next_block->ref_from_list);
2617
2618 btrfsic_block_link_hashtable_add(l,
2619 &state->block_link_hashtable);
2620 } else {
2621 l->ref_cnt++;
2622 l->parent_generation = parent_generation;
2623 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2624 btrfsic_print_add_link(state, l);
2625 }
2626
2627 return l;
2628}
2629
2630static struct btrfsic_block *btrfsic_block_lookup_or_add(
2631 struct btrfsic_state *state,
2632 struct btrfsic_block_data_ctx *block_ctx,
2633 const char *additional_string,
2634 int is_metadata,
2635 int is_iodone,
2636 int never_written,
2637 int mirror_num,
2638 int *was_created)
2639{
2640 struct btrfsic_block *block;
2641
2642 block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
2643 block_ctx->dev_bytenr,
2644 &state->block_hashtable);
2645 if (NULL == block) {
2646 struct btrfsic_dev_state *dev_state;
2647
2648 block = btrfsic_block_alloc();
2649 if (NULL == block) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002650 pr_info("btrfsic: error, kmalloc failed!\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01002651 return NULL;
2652 }
Christoph Hellwigf8f84b22017-08-23 19:10:27 +02002653 dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev->bd_dev);
Stefan Behrens5db02762011-11-01 17:04:16 +01002654 if (NULL == dev_state) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002655 pr_info("btrfsic: error, lookup dev_state failed!\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01002656 btrfsic_block_free(block);
2657 return NULL;
2658 }
2659 block->dev_state = dev_state;
2660 block->dev_bytenr = block_ctx->dev_bytenr;
2661 block->logical_bytenr = block_ctx->start;
2662 block->is_metadata = is_metadata;
2663 block->is_iodone = is_iodone;
2664 block->never_written = never_written;
2665 block->mirror_num = mirror_num;
2666 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002667 pr_info("New %s%c-block @%llu (%s/%llu/%d)\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002668 additional_string,
2669 btrfsic_get_block_type(state, block),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002670 block->logical_bytenr, dev_state->name,
2671 block->dev_bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002672 list_add(&block->all_blocks_node, &state->all_blocks_list);
2673 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2674 if (NULL != was_created)
2675 *was_created = 1;
2676 } else {
2677 if (NULL != was_created)
2678 *was_created = 0;
2679 }
2680
2681 return block;
2682}
2683
2684static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
2685 u64 bytenr,
2686 struct btrfsic_dev_state *dev_state,
Stefan Behrense06baab2012-04-12 12:53:40 +02002687 u64 dev_bytenr)
Stefan Behrens5db02762011-11-01 17:04:16 +01002688{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002689 struct btrfs_fs_info *fs_info = state->fs_info;
2690 struct btrfsic_block_data_ctx block_ctx;
Stefan Behrens5db02762011-11-01 17:04:16 +01002691 int num_copies;
2692 int mirror_num;
Stefan Behrens5db02762011-11-01 17:04:16 +01002693 int match = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002694 int ret;
Stefan Behrens5db02762011-11-01 17:04:16 +01002695
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002696 num_copies = btrfs_num_copies(fs_info, bytenr, state->metablock_size);
Stefan Behrens5db02762011-11-01 17:04:16 +01002697
2698 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
Stefan Behrense06baab2012-04-12 12:53:40 +02002699 ret = btrfsic_map_block(state, bytenr, state->metablock_size,
Stefan Behrens5db02762011-11-01 17:04:16 +01002700 &block_ctx, mirror_num);
2701 if (ret) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002702 pr_info("btrfsic: btrfsic_map_block(logical @%llu, mirror %d) failed!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002703 bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002704 continue;
2705 }
2706
2707 if (dev_state->bdev == block_ctx.dev->bdev &&
2708 dev_bytenr == block_ctx.dev_bytenr) {
2709 match++;
2710 btrfsic_release_block_ctx(&block_ctx);
2711 break;
2712 }
2713 btrfsic_release_block_ctx(&block_ctx);
2714 }
2715
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302716 if (WARN_ON(!match)) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002717 pr_info("btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio, buffer->log_bytenr=%llu, submit_bio(bdev=%s, phys_bytenr=%llu)!\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002718 bytenr, dev_state->name, dev_bytenr);
Stefan Behrens5db02762011-11-01 17:04:16 +01002719 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
Stefan Behrense06baab2012-04-12 12:53:40 +02002720 ret = btrfsic_map_block(state, bytenr,
2721 state->metablock_size,
Stefan Behrens5db02762011-11-01 17:04:16 +01002722 &block_ctx, mirror_num);
2723 if (ret)
2724 continue;
2725
Jeff Mahoney62e85572016-09-20 10:05:01 -04002726 pr_info("Read logical bytenr @%llu maps to (%s/%llu/%d)\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002727 bytenr, block_ctx.dev->name,
2728 block_ctx.dev_bytenr, mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01002729 }
Stefan Behrens5db02762011-11-01 17:04:16 +01002730 }
2731}
2732
Christoph Hellwigf8f84b22017-08-23 19:10:27 +02002733static struct btrfsic_dev_state *btrfsic_dev_state_lookup(dev_t dev)
Stefan Behrens5db02762011-11-01 17:04:16 +01002734{
Christoph Hellwigf8f84b22017-08-23 19:10:27 +02002735 return btrfsic_dev_state_hashtable_lookup(dev,
Masahiro Yamadae2c89902016-09-13 04:35:52 +09002736 &btrfsic_dev_state_hashtable);
Stefan Behrens5db02762011-11-01 17:04:16 +01002737}
2738
Mike Christie2a222ca2016-06-05 14:31:43 -05002739int btrfsic_submit_bh(int op, int op_flags, struct buffer_head *bh)
Stefan Behrens5db02762011-11-01 17:04:16 +01002740{
2741 struct btrfsic_dev_state *dev_state;
2742
2743 if (!btrfsic_is_initialized)
Mike Christie2a222ca2016-06-05 14:31:43 -05002744 return submit_bh(op, op_flags, bh);
Stefan Behrens5db02762011-11-01 17:04:16 +01002745
2746 mutex_lock(&btrfsic_mutex);
2747 /* since btrfsic_submit_bh() might also be called before
2748 * btrfsic_mount(), this might return NULL */
Christoph Hellwigf8f84b22017-08-23 19:10:27 +02002749 dev_state = btrfsic_dev_state_lookup(bh->b_bdev->bd_dev);
Stefan Behrens5db02762011-11-01 17:04:16 +01002750
2751 /* Only called to write the superblock (incl. FLUSH/FUA) */
2752 if (NULL != dev_state &&
Mike Christie2a222ca2016-06-05 14:31:43 -05002753 (op == REQ_OP_WRITE) && bh->b_size > 0) {
Stefan Behrens5db02762011-11-01 17:04:16 +01002754 u64 dev_bytenr;
2755
David Sterba9f6d2512017-06-16 01:48:05 +02002756 dev_bytenr = BTRFS_BDEV_BLOCKSIZE * bh->b_blocknr;
Stefan Behrens5db02762011-11-01 17:04:16 +01002757 if (dev_state->state->print_mask &
2758 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002759 pr_info("submit_bh(op=0x%x,0x%x, blocknr=%llu (bytenr %llu), size=%zu, data=%p, bdev=%p)\n",
Mike Christie2a222ca2016-06-05 14:31:43 -05002760 op, op_flags, (unsigned long long)bh->b_blocknr,
Geert Uytterhoeven8d78eb12013-08-20 13:20:18 +02002761 dev_bytenr, bh->b_size, bh->b_data, bh->b_bdev);
Stefan Behrens5db02762011-11-01 17:04:16 +01002762 btrfsic_process_written_block(dev_state, dev_bytenr,
Stefan Behrense06baab2012-04-12 12:53:40 +02002763 &bh->b_data, 1, NULL,
Mike Christie2a222ca2016-06-05 14:31:43 -05002764 NULL, bh, op_flags);
Mike Christie28a8f0d2016-06-05 14:32:25 -05002765 } else if (NULL != dev_state && (op_flags & REQ_PREFLUSH)) {
Stefan Behrens5db02762011-11-01 17:04:16 +01002766 if (dev_state->state->print_mask &
2767 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002768 pr_info("submit_bh(op=0x%x,0x%x FLUSH, bdev=%p)\n",
Mike Christie2a222ca2016-06-05 14:31:43 -05002769 op, op_flags, bh->b_bdev);
Stefan Behrens5db02762011-11-01 17:04:16 +01002770 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
2771 if ((dev_state->state->print_mask &
2772 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
2773 BTRFSIC_PRINT_MASK_VERBOSE)))
Jeff Mahoney62e85572016-09-20 10:05:01 -04002774 pr_info("btrfsic_submit_bh(%s) with FLUSH but dummy block already in use (ignored)!\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002775 dev_state->name);
2776 } else {
2777 struct btrfsic_block *const block =
2778 &dev_state->dummy_block_for_bio_bh_flush;
2779
2780 block->is_iodone = 0;
2781 block->never_written = 0;
2782 block->iodone_w_error = 0;
2783 block->flush_gen = dev_state->last_flush_gen + 1;
Mike Christie2a222ca2016-06-05 14:31:43 -05002784 block->submit_bio_bh_rw = op_flags;
Stefan Behrens5db02762011-11-01 17:04:16 +01002785 block->orig_bio_bh_private = bh->b_private;
2786 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2787 block->next_in_same_bio = NULL;
2788 bh->b_private = block;
2789 bh->b_end_io = btrfsic_bh_end_io;
2790 }
2791 }
2792 mutex_unlock(&btrfsic_mutex);
Mike Christie2a222ca2016-06-05 14:31:43 -05002793 return submit_bh(op, op_flags, bh);
Stefan Behrens5db02762011-11-01 17:04:16 +01002794}
2795
Mike Christie4e49ea42016-06-05 14:31:41 -05002796static void __btrfsic_submit_bio(struct bio *bio)
Stefan Behrens5db02762011-11-01 17:04:16 +01002797{
2798 struct btrfsic_dev_state *dev_state;
2799
Kent Overstreet33879d42013-11-23 22:33:32 -08002800 if (!btrfsic_is_initialized)
Stefan Behrens5db02762011-11-01 17:04:16 +01002801 return;
Stefan Behrens5db02762011-11-01 17:04:16 +01002802
2803 mutex_lock(&btrfsic_mutex);
2804 /* since btrfsic_submit_bio() is also called before
2805 * btrfsic_mount(), this might return NULL */
Christoph Hellwig74d46992017-08-23 19:10:32 +02002806 dev_state = btrfsic_dev_state_lookup(bio_dev(bio));
Stefan Behrens5db02762011-11-01 17:04:16 +01002807 if (NULL != dev_state &&
Christoph Hellwig1621f8f2016-11-25 09:07:53 +01002808 (bio_op(bio) == REQ_OP_WRITE) && bio_has_data(bio)) {
Liu Bo11b56162017-04-14 16:11:52 -07002809 unsigned int i = 0;
Stefan Behrens5db02762011-11-01 17:04:16 +01002810 u64 dev_bytenr;
Stefan Behrens56d140f2013-11-13 17:19:08 +01002811 u64 cur_bytenr;
Liu Bo11b56162017-04-14 16:11:52 -07002812 struct bio_vec bvec;
2813 struct bvec_iter iter;
Stefan Behrens5db02762011-11-01 17:04:16 +01002814 int bio_is_patched;
Stefan Behrense06baab2012-04-12 12:53:40 +02002815 char **mapped_datav;
Liu Bo11b56162017-04-14 16:11:52 -07002816 unsigned int segs = bio_segments(bio);
Stefan Behrens5db02762011-11-01 17:04:16 +01002817
Kent Overstreet4f024f32013-10-11 15:44:27 -07002818 dev_bytenr = 512 * bio->bi_iter.bi_sector;
Stefan Behrens5db02762011-11-01 17:04:16 +01002819 bio_is_patched = 0;
2820 if (dev_state->state->print_mask &
2821 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
Christoph Hellwig74d46992017-08-23 19:10:32 +02002822 pr_info("submit_bio(rw=%d,0x%x, bi_vcnt=%u, bi_sector=%llu (bytenr %llu), bi_disk=%p)\n",
Liu Bo11b56162017-04-14 16:11:52 -07002823 bio_op(bio), bio->bi_opf, segs,
Kent Overstreet4f024f32013-10-11 15:44:27 -07002824 (unsigned long long)bio->bi_iter.bi_sector,
Christoph Hellwig74d46992017-08-23 19:10:32 +02002825 dev_bytenr, bio->bi_disk);
Stefan Behrens5db02762011-11-01 17:04:16 +01002826
Liu Bo11b56162017-04-14 16:11:52 -07002827 mapped_datav = kmalloc_array(segs,
David Sterba31e818f2015-02-20 18:00:26 +01002828 sizeof(*mapped_datav), GFP_NOFS);
Stefan Behrense06baab2012-04-12 12:53:40 +02002829 if (!mapped_datav)
2830 goto leave;
Stefan Behrens56d140f2013-11-13 17:19:08 +01002831 cur_bytenr = dev_bytenr;
Christoph Hellwig1621f8f2016-11-25 09:07:53 +01002832
Liu Bo11b56162017-04-14 16:11:52 -07002833 bio_for_each_segment(bvec, bio, iter) {
2834 BUG_ON(bvec.bv_len != PAGE_SIZE);
2835 mapped_datav[i] = kmap(bvec.bv_page);
2836 i++;
Christoph Hellwig1621f8f2016-11-25 09:07:53 +01002837
Stefan Behrens56d140f2013-11-13 17:19:08 +01002838 if (dev_state->state->print_mask &
2839 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE)
Jeff Mahoney62e85572016-09-20 10:05:01 -04002840 pr_info("#%u: bytenr=%llu, len=%u, offset=%u\n",
Liu Bo11b56162017-04-14 16:11:52 -07002841 i, cur_bytenr, bvec.bv_len, bvec.bv_offset);
2842 cur_bytenr += bvec.bv_len;
Stefan Behrens5db02762011-11-01 17:04:16 +01002843 }
Stefan Behrense06baab2012-04-12 12:53:40 +02002844 btrfsic_process_written_block(dev_state, dev_bytenr,
Liu Bo11b56162017-04-14 16:11:52 -07002845 mapped_datav, segs,
Stefan Behrense06baab2012-04-12 12:53:40 +02002846 bio, &bio_is_patched,
Jens Axboe1eff9d32016-08-05 15:35:16 -06002847 NULL, bio->bi_opf);
Liu Bo11b56162017-04-14 16:11:52 -07002848 bio_for_each_segment(bvec, bio, iter)
2849 kunmap(bvec.bv_page);
Stefan Behrense06baab2012-04-12 12:53:40 +02002850 kfree(mapped_datav);
Jens Axboe1eff9d32016-08-05 15:35:16 -06002851 } else if (NULL != dev_state && (bio->bi_opf & REQ_PREFLUSH)) {
Stefan Behrens5db02762011-11-01 17:04:16 +01002852 if (dev_state->state->print_mask &
2853 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
Christoph Hellwig74d46992017-08-23 19:10:32 +02002854 pr_info("submit_bio(rw=%d,0x%x FLUSH, disk=%p)\n",
2855 bio_op(bio), bio->bi_opf, bio->bi_disk);
Stefan Behrens5db02762011-11-01 17:04:16 +01002856 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
2857 if ((dev_state->state->print_mask &
2858 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
2859 BTRFSIC_PRINT_MASK_VERBOSE)))
Jeff Mahoney62e85572016-09-20 10:05:01 -04002860 pr_info("btrfsic_submit_bio(%s) with FLUSH but dummy block already in use (ignored)!\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01002861 dev_state->name);
2862 } else {
2863 struct btrfsic_block *const block =
2864 &dev_state->dummy_block_for_bio_bh_flush;
2865
2866 block->is_iodone = 0;
2867 block->never_written = 0;
2868 block->iodone_w_error = 0;
2869 block->flush_gen = dev_state->last_flush_gen + 1;
Jens Axboe1eff9d32016-08-05 15:35:16 -06002870 block->submit_bio_bh_rw = bio->bi_opf;
Stefan Behrens5db02762011-11-01 17:04:16 +01002871 block->orig_bio_bh_private = bio->bi_private;
2872 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
2873 block->next_in_same_bio = NULL;
2874 bio->bi_private = block;
2875 bio->bi_end_io = btrfsic_bio_end_io;
2876 }
2877 }
Stefan Behrense06baab2012-04-12 12:53:40 +02002878leave:
Stefan Behrens5db02762011-11-01 17:04:16 +01002879 mutex_unlock(&btrfsic_mutex);
Kent Overstreet33879d42013-11-23 22:33:32 -08002880}
Stefan Behrens5db02762011-11-01 17:04:16 +01002881
Mike Christie4e49ea42016-06-05 14:31:41 -05002882void btrfsic_submit_bio(struct bio *bio)
Kent Overstreet33879d42013-11-23 22:33:32 -08002883{
Mike Christie4e49ea42016-06-05 14:31:41 -05002884 __btrfsic_submit_bio(bio);
2885 submit_bio(bio);
Stefan Behrens5db02762011-11-01 17:04:16 +01002886}
2887
Mike Christie4e49ea42016-06-05 14:31:41 -05002888int btrfsic_submit_bio_wait(struct bio *bio)
Kent Overstreet33879d42013-11-23 22:33:32 -08002889{
Mike Christie4e49ea42016-06-05 14:31:41 -05002890 __btrfsic_submit_bio(bio);
2891 return submit_bio_wait(bio);
Kent Overstreet33879d42013-11-23 22:33:32 -08002892}
2893
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002894int btrfsic_mount(struct btrfs_fs_info *fs_info,
Stefan Behrens5db02762011-11-01 17:04:16 +01002895 struct btrfs_fs_devices *fs_devices,
2896 int including_extent_data, u32 print_mask)
2897{
2898 int ret;
2899 struct btrfsic_state *state;
2900 struct list_head *dev_head = &fs_devices->devices;
2901 struct btrfs_device *device;
2902
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002903 if (fs_info->nodesize & ((u64)PAGE_SIZE - 1)) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002904 pr_info("btrfsic: cannot handle nodesize %d not being a multiple of PAGE_SIZE %ld!\n",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002905 fs_info->nodesize, PAGE_SIZE);
Stefan Behrense06baab2012-04-12 12:53:40 +02002906 return -1;
2907 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002908 if (fs_info->sectorsize & ((u64)PAGE_SIZE - 1)) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002909 pr_info("btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_SIZE %ld!\n",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002910 fs_info->sectorsize, PAGE_SIZE);
Stefan Behrense06baab2012-04-12 12:53:40 +02002911 return -1;
2912 }
David Sterba818e0102017-05-31 18:40:02 +02002913 state = kvzalloc(sizeof(*state), GFP_KERNEL);
Shilong Wang6b3a4d62014-10-10 17:35:59 -04002914 if (!state) {
David Sterba818e0102017-05-31 18:40:02 +02002915 pr_info("btrfs check-integrity: allocation failed!\n");
Allen Pais3afb0c52017-09-20 11:47:46 +05302916 return -ENOMEM;
Stefan Behrens5db02762011-11-01 17:04:16 +01002917 }
2918
2919 if (!btrfsic_is_initialized) {
2920 mutex_init(&btrfsic_mutex);
2921 btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
2922 btrfsic_is_initialized = 1;
2923 }
2924 mutex_lock(&btrfsic_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002925 state->fs_info = fs_info;
Stefan Behrens5db02762011-11-01 17:04:16 +01002926 state->print_mask = print_mask;
2927 state->include_extent_data = including_extent_data;
2928 state->csum_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002929 state->metablock_size = fs_info->nodesize;
2930 state->datablock_size = fs_info->sectorsize;
Stefan Behrens5db02762011-11-01 17:04:16 +01002931 INIT_LIST_HEAD(&state->all_blocks_list);
2932 btrfsic_block_hashtable_init(&state->block_hashtable);
2933 btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
2934 state->max_superblock_generation = 0;
2935 state->latest_superblock = NULL;
2936
2937 list_for_each_entry(device, dev_head, dev_list) {
2938 struct btrfsic_dev_state *ds;
Rasmus Villemoes02def692015-11-27 09:42:11 +01002939 const char *p;
Stefan Behrens5db02762011-11-01 17:04:16 +01002940
2941 if (!device->bdev || !device->name)
2942 continue;
2943
2944 ds = btrfsic_dev_state_alloc();
2945 if (NULL == ds) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04002946 pr_info("btrfs check-integrity: kmalloc() failed!\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01002947 mutex_unlock(&btrfsic_mutex);
Allen Pais3afb0c52017-09-20 11:47:46 +05302948 return -ENOMEM;
Stefan Behrens5db02762011-11-01 17:04:16 +01002949 }
2950 ds->bdev = device->bdev;
2951 ds->state = state;
2952 bdevname(ds->bdev, ds->name);
2953 ds->name[BDEVNAME_SIZE - 1] = '\0';
Rasmus Villemoes02def692015-11-27 09:42:11 +01002954 p = kbasename(ds->name);
Stefan Behrens5db02762011-11-01 17:04:16 +01002955 strlcpy(ds->name, p, sizeof(ds->name));
2956 btrfsic_dev_state_hashtable_add(ds,
2957 &btrfsic_dev_state_hashtable);
2958 }
2959
2960 ret = btrfsic_process_superblock(state, fs_devices);
2961 if (0 != ret) {
2962 mutex_unlock(&btrfsic_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002963 btrfsic_unmount(fs_devices);
Stefan Behrens5db02762011-11-01 17:04:16 +01002964 return ret;
2965 }
2966
2967 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
2968 btrfsic_dump_database(state);
2969 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
2970 btrfsic_dump_tree(state);
2971
2972 mutex_unlock(&btrfsic_mutex);
2973 return 0;
2974}
2975
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002976void btrfsic_unmount(struct btrfs_fs_devices *fs_devices)
Stefan Behrens5db02762011-11-01 17:04:16 +01002977{
Geliang Tangb69f2be2015-12-18 22:16:59 +08002978 struct btrfsic_block *b_all, *tmp_all;
Stefan Behrens5db02762011-11-01 17:04:16 +01002979 struct btrfsic_state *state;
2980 struct list_head *dev_head = &fs_devices->devices;
2981 struct btrfs_device *device;
2982
2983 if (!btrfsic_is_initialized)
2984 return;
2985
2986 mutex_lock(&btrfsic_mutex);
2987
2988 state = NULL;
2989 list_for_each_entry(device, dev_head, dev_list) {
2990 struct btrfsic_dev_state *ds;
2991
2992 if (!device->bdev || !device->name)
2993 continue;
2994
2995 ds = btrfsic_dev_state_hashtable_lookup(
Christoph Hellwigf8f84b22017-08-23 19:10:27 +02002996 device->bdev->bd_dev,
Stefan Behrens5db02762011-11-01 17:04:16 +01002997 &btrfsic_dev_state_hashtable);
2998 if (NULL != ds) {
2999 state = ds->state;
3000 btrfsic_dev_state_hashtable_remove(ds);
3001 btrfsic_dev_state_free(ds);
3002 }
3003 }
3004
3005 if (NULL == state) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04003006 pr_info("btrfsic: error, cannot find state information on umount!\n");
Stefan Behrens5db02762011-11-01 17:04:16 +01003007 mutex_unlock(&btrfsic_mutex);
3008 return;
3009 }
3010
3011 /*
3012 * Don't care about keeping the lists' state up to date,
3013 * just free all memory that was allocated dynamically.
3014 * Free the blocks and the block_links.
3015 */
Geliang Tangb69f2be2015-12-18 22:16:59 +08003016 list_for_each_entry_safe(b_all, tmp_all, &state->all_blocks_list,
3017 all_blocks_node) {
3018 struct btrfsic_block_link *l, *tmp;
Stefan Behrens5db02762011-11-01 17:04:16 +01003019
Geliang Tangb69f2be2015-12-18 22:16:59 +08003020 list_for_each_entry_safe(l, tmp, &b_all->ref_to_list,
3021 node_ref_to) {
Stefan Behrens5db02762011-11-01 17:04:16 +01003022 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
3023 btrfsic_print_rem_link(state, l);
3024
3025 l->ref_cnt--;
3026 if (0 == l->ref_cnt)
3027 btrfsic_block_link_free(l);
3028 }
3029
Stefan Behrens48235a62012-05-23 17:57:49 +02003030 if (b_all->is_iodone || b_all->never_written)
Stefan Behrens5db02762011-11-01 17:04:16 +01003031 btrfsic_block_free(b_all);
3032 else
Jeff Mahoney62e85572016-09-20 10:05:01 -04003033 pr_info("btrfs: attempt to free %c-block @%llu (%s/%llu/%d) on umount which is not yet iodone!\n",
Stefan Behrens5db02762011-11-01 17:04:16 +01003034 btrfsic_get_block_type(state, b_all),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003035 b_all->logical_bytenr, b_all->dev_state->name,
3036 b_all->dev_bytenr, b_all->mirror_num);
Stefan Behrens5db02762011-11-01 17:04:16 +01003037 }
3038
3039 mutex_unlock(&btrfsic_mutex);
3040
Wang Shilongf7493032014-11-22 21:13:10 +08003041 kvfree(state);
Stefan Behrens5db02762011-11-01 17:04:16 +01003042}