rtnl/do_setlink(): set modified when IFLA_LINKMODE is updated
[linux-2.6/btrfs-unstable.git] / fs / btrfs / check-integrity.c
blobce92ae30250fb256fbbbdb9158315c757a24adc1
1 /*
2 * Copyright (C) STRATO AG 2011. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 * This module can be used to catch cases when the btrfs kernel
21 * code executes write requests to the disk that bring the file
22 * system in an inconsistent state. In such a state, a power-loss
23 * or kernel panic event would cause that the data on disk is
24 * lost or at least damaged.
26 * Code is added that examines all block write requests during
27 * runtime (including writes of the super block). Three rules
28 * are verified and an error is printed on violation of the
29 * rules:
30 * 1. It is not allowed to write a disk block which is
31 * currently referenced by the super block (either directly
32 * or indirectly).
33 * 2. When a super block is written, it is verified that all
34 * referenced (directly or indirectly) blocks fulfill the
35 * following requirements:
36 * 2a. All referenced blocks have either been present when
37 * the file system was mounted, (i.e., they have been
38 * referenced by the super block) or they have been
39 * written since then and the write completion callback
40 * was called and no write error was indicated and a
41 * FLUSH request to the device where these blocks are
42 * located was received and completed.
43 * 2b. All referenced blocks need to have a generation
44 * number which is equal to the parent's number.
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.
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".
56 * The integrity check is enabled via mount options. These
57 * mount options are only supported if the integrity check
58 * tool is compiled by defining BTRFS_FS_CHECK_INTEGRITY.
60 * Example #1, apply integrity checks to all metadata:
61 * mount /dev/sdb1 /mnt -o check_int
63 * Example #2, apply integrity checks to all metadata and
64 * to data extents:
65 * mount /dev/sdb1 /mnt -o check_int_data
67 * Example #3, apply integrity checks to all metadata and dump
68 * the tree that the super block references to kernel messages
69 * each time after a super block was written:
70 * mount /dev/sdb1 /mnt -o check_int,check_int_print_mask=263
72 * If the integrity check tool is included and activated in
73 * the mount options, plenty of kernel memory is used, and
74 * plenty of additional CPU cycles are spent. Enabling this
75 * functionality is not intended for normal use. In most
76 * cases, unless you are a btrfs developer who needs to verify
77 * the integrity of (super)-block write requests, do not
78 * enable the config option BTRFS_FS_CHECK_INTEGRITY to
79 * include and compile the integrity check tool.
81 * Expect millions of lines of information in the kernel log with an
82 * enabled check_int_print_mask. Therefore set LOG_BUF_SHIFT in the
83 * kernel config to at least 26 (which is 64MB). Usually the value is
84 * limited to 21 (which is 2MB) in init/Kconfig. The file needs to be
85 * changed like this before LOG_BUF_SHIFT can be set to a high value:
86 * config LOG_BUF_SHIFT
87 * int "Kernel log buffer size (16 => 64KB, 17 => 128KB)"
88 * range 12 30
91 #include <linux/sched.h>
92 #include <linux/slab.h>
93 #include <linux/buffer_head.h>
94 #include <linux/mutex.h>
95 #include <linux/genhd.h>
96 #include <linux/blkdev.h>
97 #include "ctree.h"
98 #include "disk-io.h"
99 #include "hash.h"
100 #include "transaction.h"
101 #include "extent_io.h"
102 #include "volumes.h"
103 #include "print-tree.h"
104 #include "locking.h"
105 #include "check-integrity.h"
106 #include "rcu-string.h"
108 #define BTRFSIC_BLOCK_HASHTABLE_SIZE 0x10000
109 #define BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE 0x10000
110 #define BTRFSIC_DEV2STATE_HASHTABLE_SIZE 0x100
111 #define BTRFSIC_BLOCK_MAGIC_NUMBER 0x14491051
112 #define BTRFSIC_BLOCK_LINK_MAGIC_NUMBER 0x11070807
113 #define BTRFSIC_DEV2STATE_MAGIC_NUMBER 0x20111530
114 #define BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER 20111300
115 #define BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL (200 - 6) /* in characters,
116 * excluding " [...]" */
117 #define BTRFSIC_GENERATION_UNKNOWN ((u64)-1)
120 * The definition of the bitmask fields for the print_mask.
121 * They are specified with the mount option check_integrity_print_mask.
123 #define BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE 0x00000001
124 #define BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION 0x00000002
125 #define BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE 0x00000004
126 #define BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE 0x00000008
127 #define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH 0x00000010
128 #define BTRFSIC_PRINT_MASK_END_IO_BIO_BH 0x00000020
129 #define BTRFSIC_PRINT_MASK_VERBOSE 0x00000040
130 #define BTRFSIC_PRINT_MASK_VERY_VERBOSE 0x00000080
131 #define BTRFSIC_PRINT_MASK_INITIAL_TREE 0x00000100
132 #define BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES 0x00000200
133 #define BTRFSIC_PRINT_MASK_INITIAL_DATABASE 0x00000400
134 #define BTRFSIC_PRINT_MASK_NUM_COPIES 0x00000800
135 #define BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS 0x00001000
136 #define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE 0x00002000
138 struct btrfsic_dev_state;
139 struct btrfsic_state;
141 struct btrfsic_block {
142 u32 magic_num; /* only used for debug purposes */
143 unsigned int is_metadata:1; /* if it is meta-data, not data-data */
144 unsigned int is_superblock:1; /* if it is one of the superblocks */
145 unsigned int is_iodone:1; /* if is done by lower subsystem */
146 unsigned int iodone_w_error:1; /* error was indicated to endio */
147 unsigned int never_written:1; /* block was added because it was
148 * referenced, not because it was
149 * written */
150 unsigned int mirror_num; /* large enough to hold
151 * BTRFS_SUPER_MIRROR_MAX */
152 struct btrfsic_dev_state *dev_state;
153 u64 dev_bytenr; /* key, physical byte num on disk */
154 u64 logical_bytenr; /* logical byte num on disk */
155 u64 generation;
156 struct btrfs_disk_key disk_key; /* extra info to print in case of
157 * issues, will not always be correct */
158 struct list_head collision_resolving_node; /* list node */
159 struct list_head all_blocks_node; /* list node */
161 /* the following two lists contain block_link items */
162 struct list_head ref_to_list; /* list */
163 struct list_head ref_from_list; /* list */
164 struct btrfsic_block *next_in_same_bio;
165 void *orig_bio_bh_private;
166 union {
167 bio_end_io_t *bio;
168 bh_end_io_t *bh;
169 } orig_bio_bh_end_io;
170 int submit_bio_bh_rw;
171 u64 flush_gen; /* only valid if !never_written */
175 * Elements of this type are allocated dynamically and required because
176 * each block object can refer to and can be ref from multiple blocks.
177 * The key to lookup them in the hashtable is the dev_bytenr of
178 * the block ref to plus the one from the block refered from.
179 * The fact that they are searchable via a hashtable and that a
180 * ref_cnt is maintained is not required for the btrfs integrity
181 * check algorithm itself, it is only used to make the output more
182 * beautiful in case that an error is detected (an error is defined
183 * as a write operation to a block while that block is still referenced).
185 struct btrfsic_block_link {
186 u32 magic_num; /* only used for debug purposes */
187 u32 ref_cnt;
188 struct list_head node_ref_to; /* list node */
189 struct list_head node_ref_from; /* list node */
190 struct list_head collision_resolving_node; /* list node */
191 struct btrfsic_block *block_ref_to;
192 struct btrfsic_block *block_ref_from;
193 u64 parent_generation;
196 struct btrfsic_dev_state {
197 u32 magic_num; /* only used for debug purposes */
198 struct block_device *bdev;
199 struct btrfsic_state *state;
200 struct list_head collision_resolving_node; /* list node */
201 struct btrfsic_block dummy_block_for_bio_bh_flush;
202 u64 last_flush_gen;
203 char name[BDEVNAME_SIZE];
206 struct btrfsic_block_hashtable {
207 struct list_head table[BTRFSIC_BLOCK_HASHTABLE_SIZE];
210 struct btrfsic_block_link_hashtable {
211 struct list_head table[BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE];
214 struct btrfsic_dev_state_hashtable {
215 struct list_head table[BTRFSIC_DEV2STATE_HASHTABLE_SIZE];
218 struct btrfsic_block_data_ctx {
219 u64 start; /* virtual bytenr */
220 u64 dev_bytenr; /* physical bytenr on device */
221 u32 len;
222 struct btrfsic_dev_state *dev;
223 char **datav;
224 struct page **pagev;
225 void *mem_to_free;
228 /* This structure is used to implement recursion without occupying
229 * any stack space, refer to btrfsic_process_metablock() */
230 struct btrfsic_stack_frame {
231 u32 magic;
232 u32 nr;
233 int error;
234 int i;
235 int limit_nesting;
236 int num_copies;
237 int mirror_num;
238 struct btrfsic_block *block;
239 struct btrfsic_block_data_ctx *block_ctx;
240 struct btrfsic_block *next_block;
241 struct btrfsic_block_data_ctx next_block_ctx;
242 struct btrfs_header *hdr;
243 struct btrfsic_stack_frame *prev;
246 /* Some state per mounted filesystem */
247 struct btrfsic_state {
248 u32 print_mask;
249 int include_extent_data;
250 int csum_size;
251 struct list_head all_blocks_list;
252 struct btrfsic_block_hashtable block_hashtable;
253 struct btrfsic_block_link_hashtable block_link_hashtable;
254 struct btrfs_root *root;
255 u64 max_superblock_generation;
256 struct btrfsic_block *latest_superblock;
257 u32 metablock_size;
258 u32 datablock_size;
261 static void btrfsic_block_init(struct btrfsic_block *b);
262 static struct btrfsic_block *btrfsic_block_alloc(void);
263 static void btrfsic_block_free(struct btrfsic_block *b);
264 static void btrfsic_block_link_init(struct btrfsic_block_link *n);
265 static struct btrfsic_block_link *btrfsic_block_link_alloc(void);
266 static void btrfsic_block_link_free(struct btrfsic_block_link *n);
267 static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds);
268 static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void);
269 static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds);
270 static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h);
271 static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
272 struct btrfsic_block_hashtable *h);
273 static void btrfsic_block_hashtable_remove(struct btrfsic_block *b);
274 static struct btrfsic_block *btrfsic_block_hashtable_lookup(
275 struct block_device *bdev,
276 u64 dev_bytenr,
277 struct btrfsic_block_hashtable *h);
278 static void btrfsic_block_link_hashtable_init(
279 struct btrfsic_block_link_hashtable *h);
280 static void btrfsic_block_link_hashtable_add(
281 struct btrfsic_block_link *l,
282 struct btrfsic_block_link_hashtable *h);
283 static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l);
284 static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
285 struct block_device *bdev_ref_to,
286 u64 dev_bytenr_ref_to,
287 struct block_device *bdev_ref_from,
288 u64 dev_bytenr_ref_from,
289 struct btrfsic_block_link_hashtable *h);
290 static void btrfsic_dev_state_hashtable_init(
291 struct btrfsic_dev_state_hashtable *h);
292 static void btrfsic_dev_state_hashtable_add(
293 struct btrfsic_dev_state *ds,
294 struct btrfsic_dev_state_hashtable *h);
295 static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds);
296 static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(
297 struct block_device *bdev,
298 struct btrfsic_dev_state_hashtable *h);
299 static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void);
300 static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf);
301 static int btrfsic_process_superblock(struct btrfsic_state *state,
302 struct btrfs_fs_devices *fs_devices);
303 static int btrfsic_process_metablock(struct btrfsic_state *state,
304 struct btrfsic_block *block,
305 struct btrfsic_block_data_ctx *block_ctx,
306 int limit_nesting, int force_iodone_flag);
307 static void btrfsic_read_from_block_data(
308 struct btrfsic_block_data_ctx *block_ctx,
309 void *dst, u32 offset, size_t len);
310 static int btrfsic_create_link_to_next_block(
311 struct btrfsic_state *state,
312 struct btrfsic_block *block,
313 struct btrfsic_block_data_ctx
314 *block_ctx, u64 next_bytenr,
315 int limit_nesting,
316 struct btrfsic_block_data_ctx *next_block_ctx,
317 struct btrfsic_block **next_blockp,
318 int force_iodone_flag,
319 int *num_copiesp, int *mirror_nump,
320 struct btrfs_disk_key *disk_key,
321 u64 parent_generation);
322 static int btrfsic_handle_extent_data(struct btrfsic_state *state,
323 struct btrfsic_block *block,
324 struct btrfsic_block_data_ctx *block_ctx,
325 u32 item_offset, int force_iodone_flag);
326 static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
327 struct btrfsic_block_data_ctx *block_ctx_out,
328 int mirror_num);
329 static int btrfsic_map_superblock(struct btrfsic_state *state, u64 bytenr,
330 u32 len, struct block_device *bdev,
331 struct btrfsic_block_data_ctx *block_ctx_out);
332 static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx);
333 static int btrfsic_read_block(struct btrfsic_state *state,
334 struct btrfsic_block_data_ctx *block_ctx);
335 static void btrfsic_dump_database(struct btrfsic_state *state);
336 static int btrfsic_test_for_metadata(struct btrfsic_state *state,
337 char **datav, unsigned int num_pages);
338 static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
339 u64 dev_bytenr, char **mapped_datav,
340 unsigned int num_pages,
341 struct bio *bio, int *bio_is_patched,
342 struct buffer_head *bh,
343 int submit_bio_bh_rw);
344 static int btrfsic_process_written_superblock(
345 struct btrfsic_state *state,
346 struct btrfsic_block *const block,
347 struct btrfs_super_block *const super_hdr);
348 static void btrfsic_bio_end_io(struct bio *bp, int bio_error_status);
349 static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate);
350 static int btrfsic_is_block_ref_by_superblock(const struct btrfsic_state *state,
351 const struct btrfsic_block *block,
352 int recursion_level);
353 static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
354 struct btrfsic_block *const block,
355 int recursion_level);
356 static void btrfsic_print_add_link(const struct btrfsic_state *state,
357 const struct btrfsic_block_link *l);
358 static void btrfsic_print_rem_link(const struct btrfsic_state *state,
359 const struct btrfsic_block_link *l);
360 static char btrfsic_get_block_type(const struct btrfsic_state *state,
361 const struct btrfsic_block *block);
362 static void btrfsic_dump_tree(const struct btrfsic_state *state);
363 static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
364 const struct btrfsic_block *block,
365 int indent_level);
366 static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
367 struct btrfsic_state *state,
368 struct btrfsic_block_data_ctx *next_block_ctx,
369 struct btrfsic_block *next_block,
370 struct btrfsic_block *from_block,
371 u64 parent_generation);
372 static struct btrfsic_block *btrfsic_block_lookup_or_add(
373 struct btrfsic_state *state,
374 struct btrfsic_block_data_ctx *block_ctx,
375 const char *additional_string,
376 int is_metadata,
377 int is_iodone,
378 int never_written,
379 int mirror_num,
380 int *was_created);
381 static int btrfsic_process_superblock_dev_mirror(
382 struct btrfsic_state *state,
383 struct btrfsic_dev_state *dev_state,
384 struct btrfs_device *device,
385 int superblock_mirror_num,
386 struct btrfsic_dev_state **selected_dev_state,
387 struct btrfs_super_block *selected_super);
388 static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
389 struct block_device *bdev);
390 static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
391 u64 bytenr,
392 struct btrfsic_dev_state *dev_state,
393 u64 dev_bytenr);
395 static struct mutex btrfsic_mutex;
396 static int btrfsic_is_initialized;
397 static struct btrfsic_dev_state_hashtable btrfsic_dev_state_hashtable;
400 static void btrfsic_block_init(struct btrfsic_block *b)
402 b->magic_num = BTRFSIC_BLOCK_MAGIC_NUMBER;
403 b->dev_state = NULL;
404 b->dev_bytenr = 0;
405 b->logical_bytenr = 0;
406 b->generation = BTRFSIC_GENERATION_UNKNOWN;
407 b->disk_key.objectid = 0;
408 b->disk_key.type = 0;
409 b->disk_key.offset = 0;
410 b->is_metadata = 0;
411 b->is_superblock = 0;
412 b->is_iodone = 0;
413 b->iodone_w_error = 0;
414 b->never_written = 0;
415 b->mirror_num = 0;
416 b->next_in_same_bio = NULL;
417 b->orig_bio_bh_private = NULL;
418 b->orig_bio_bh_end_io.bio = NULL;
419 INIT_LIST_HEAD(&b->collision_resolving_node);
420 INIT_LIST_HEAD(&b->all_blocks_node);
421 INIT_LIST_HEAD(&b->ref_to_list);
422 INIT_LIST_HEAD(&b->ref_from_list);
423 b->submit_bio_bh_rw = 0;
424 b->flush_gen = 0;
427 static struct btrfsic_block *btrfsic_block_alloc(void)
429 struct btrfsic_block *b;
431 b = kzalloc(sizeof(*b), GFP_NOFS);
432 if (NULL != b)
433 btrfsic_block_init(b);
435 return b;
438 static void btrfsic_block_free(struct btrfsic_block *b)
440 BUG_ON(!(NULL == b || BTRFSIC_BLOCK_MAGIC_NUMBER == b->magic_num));
441 kfree(b);
444 static void btrfsic_block_link_init(struct btrfsic_block_link *l)
446 l->magic_num = BTRFSIC_BLOCK_LINK_MAGIC_NUMBER;
447 l->ref_cnt = 1;
448 INIT_LIST_HEAD(&l->node_ref_to);
449 INIT_LIST_HEAD(&l->node_ref_from);
450 INIT_LIST_HEAD(&l->collision_resolving_node);
451 l->block_ref_to = NULL;
452 l->block_ref_from = NULL;
455 static struct btrfsic_block_link *btrfsic_block_link_alloc(void)
457 struct btrfsic_block_link *l;
459 l = kzalloc(sizeof(*l), GFP_NOFS);
460 if (NULL != l)
461 btrfsic_block_link_init(l);
463 return l;
466 static void btrfsic_block_link_free(struct btrfsic_block_link *l)
468 BUG_ON(!(NULL == l || BTRFSIC_BLOCK_LINK_MAGIC_NUMBER == l->magic_num));
469 kfree(l);
472 static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds)
474 ds->magic_num = BTRFSIC_DEV2STATE_MAGIC_NUMBER;
475 ds->bdev = NULL;
476 ds->state = NULL;
477 ds->name[0] = '\0';
478 INIT_LIST_HEAD(&ds->collision_resolving_node);
479 ds->last_flush_gen = 0;
480 btrfsic_block_init(&ds->dummy_block_for_bio_bh_flush);
481 ds->dummy_block_for_bio_bh_flush.is_iodone = 1;
482 ds->dummy_block_for_bio_bh_flush.dev_state = ds;
485 static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void)
487 struct btrfsic_dev_state *ds;
489 ds = kzalloc(sizeof(*ds), GFP_NOFS);
490 if (NULL != ds)
491 btrfsic_dev_state_init(ds);
493 return ds;
496 static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds)
498 BUG_ON(!(NULL == ds ||
499 BTRFSIC_DEV2STATE_MAGIC_NUMBER == ds->magic_num));
500 kfree(ds);
503 static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h)
505 int i;
507 for (i = 0; i < BTRFSIC_BLOCK_HASHTABLE_SIZE; i++)
508 INIT_LIST_HEAD(h->table + i);
511 static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
512 struct btrfsic_block_hashtable *h)
514 const unsigned int hashval =
515 (((unsigned int)(b->dev_bytenr >> 16)) ^
516 ((unsigned int)((uintptr_t)b->dev_state->bdev))) &
517 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
519 list_add(&b->collision_resolving_node, h->table + hashval);
522 static void btrfsic_block_hashtable_remove(struct btrfsic_block *b)
524 list_del(&b->collision_resolving_node);
527 static struct btrfsic_block *btrfsic_block_hashtable_lookup(
528 struct block_device *bdev,
529 u64 dev_bytenr,
530 struct btrfsic_block_hashtable *h)
532 const unsigned int hashval =
533 (((unsigned int)(dev_bytenr >> 16)) ^
534 ((unsigned int)((uintptr_t)bdev))) &
535 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
536 struct list_head *elem;
538 list_for_each(elem, h->table + hashval) {
539 struct btrfsic_block *const b =
540 list_entry(elem, struct btrfsic_block,
541 collision_resolving_node);
543 if (b->dev_state->bdev == bdev && b->dev_bytenr == dev_bytenr)
544 return b;
547 return NULL;
550 static void btrfsic_block_link_hashtable_init(
551 struct btrfsic_block_link_hashtable *h)
553 int i;
555 for (i = 0; i < BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE; i++)
556 INIT_LIST_HEAD(h->table + i);
559 static void btrfsic_block_link_hashtable_add(
560 struct btrfsic_block_link *l,
561 struct btrfsic_block_link_hashtable *h)
563 const unsigned int hashval =
564 (((unsigned int)(l->block_ref_to->dev_bytenr >> 16)) ^
565 ((unsigned int)(l->block_ref_from->dev_bytenr >> 16)) ^
566 ((unsigned int)((uintptr_t)l->block_ref_to->dev_state->bdev)) ^
567 ((unsigned int)((uintptr_t)l->block_ref_from->dev_state->bdev)))
568 & (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
570 BUG_ON(NULL == l->block_ref_to);
571 BUG_ON(NULL == l->block_ref_from);
572 list_add(&l->collision_resolving_node, h->table + hashval);
575 static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l)
577 list_del(&l->collision_resolving_node);
580 static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
581 struct block_device *bdev_ref_to,
582 u64 dev_bytenr_ref_to,
583 struct block_device *bdev_ref_from,
584 u64 dev_bytenr_ref_from,
585 struct btrfsic_block_link_hashtable *h)
587 const unsigned int hashval =
588 (((unsigned int)(dev_bytenr_ref_to >> 16)) ^
589 ((unsigned int)(dev_bytenr_ref_from >> 16)) ^
590 ((unsigned int)((uintptr_t)bdev_ref_to)) ^
591 ((unsigned int)((uintptr_t)bdev_ref_from))) &
592 (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
593 struct list_head *elem;
595 list_for_each(elem, h->table + hashval) {
596 struct btrfsic_block_link *const l =
597 list_entry(elem, struct btrfsic_block_link,
598 collision_resolving_node);
600 BUG_ON(NULL == l->block_ref_to);
601 BUG_ON(NULL == l->block_ref_from);
602 if (l->block_ref_to->dev_state->bdev == bdev_ref_to &&
603 l->block_ref_to->dev_bytenr == dev_bytenr_ref_to &&
604 l->block_ref_from->dev_state->bdev == bdev_ref_from &&
605 l->block_ref_from->dev_bytenr == dev_bytenr_ref_from)
606 return l;
609 return NULL;
612 static void btrfsic_dev_state_hashtable_init(
613 struct btrfsic_dev_state_hashtable *h)
615 int i;
617 for (i = 0; i < BTRFSIC_DEV2STATE_HASHTABLE_SIZE; i++)
618 INIT_LIST_HEAD(h->table + i);
621 static void btrfsic_dev_state_hashtable_add(
622 struct btrfsic_dev_state *ds,
623 struct btrfsic_dev_state_hashtable *h)
625 const unsigned int hashval =
626 (((unsigned int)((uintptr_t)ds->bdev)) &
627 (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
629 list_add(&ds->collision_resolving_node, h->table + hashval);
632 static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds)
634 list_del(&ds->collision_resolving_node);
637 static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(
638 struct block_device *bdev,
639 struct btrfsic_dev_state_hashtable *h)
641 const unsigned int hashval =
642 (((unsigned int)((uintptr_t)bdev)) &
643 (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
644 struct list_head *elem;
646 list_for_each(elem, h->table + hashval) {
647 struct btrfsic_dev_state *const ds =
648 list_entry(elem, struct btrfsic_dev_state,
649 collision_resolving_node);
651 if (ds->bdev == bdev)
652 return ds;
655 return NULL;
658 static int btrfsic_process_superblock(struct btrfsic_state *state,
659 struct btrfs_fs_devices *fs_devices)
661 int ret = 0;
662 struct btrfs_super_block *selected_super;
663 struct list_head *dev_head = &fs_devices->devices;
664 struct btrfs_device *device;
665 struct btrfsic_dev_state *selected_dev_state = NULL;
666 int pass;
668 BUG_ON(NULL == state);
669 selected_super = kzalloc(sizeof(*selected_super), GFP_NOFS);
670 if (NULL == selected_super) {
671 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
672 return -1;
675 list_for_each_entry(device, dev_head, dev_list) {
676 int i;
677 struct btrfsic_dev_state *dev_state;
679 if (!device->bdev || !device->name)
680 continue;
682 dev_state = btrfsic_dev_state_lookup(device->bdev);
683 BUG_ON(NULL == dev_state);
684 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
685 ret = btrfsic_process_superblock_dev_mirror(
686 state, dev_state, device, i,
687 &selected_dev_state, selected_super);
688 if (0 != ret && 0 == i) {
689 kfree(selected_super);
690 return ret;
695 if (NULL == state->latest_superblock) {
696 printk(KERN_INFO "btrfsic: no superblock found!\n");
697 kfree(selected_super);
698 return -1;
701 state->csum_size = btrfs_super_csum_size(selected_super);
703 for (pass = 0; pass < 3; pass++) {
704 int num_copies;
705 int mirror_num;
706 u64 next_bytenr;
708 switch (pass) {
709 case 0:
710 next_bytenr = btrfs_super_root(selected_super);
711 if (state->print_mask &
712 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
713 printk(KERN_INFO "root@%llu\n", next_bytenr);
714 break;
715 case 1:
716 next_bytenr = btrfs_super_chunk_root(selected_super);
717 if (state->print_mask &
718 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
719 printk(KERN_INFO "chunk@%llu\n", next_bytenr);
720 break;
721 case 2:
722 next_bytenr = btrfs_super_log_root(selected_super);
723 if (0 == next_bytenr)
724 continue;
725 if (state->print_mask &
726 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
727 printk(KERN_INFO "log@%llu\n", next_bytenr);
728 break;
731 num_copies =
732 btrfs_num_copies(state->root->fs_info,
733 next_bytenr, state->metablock_size);
734 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
735 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
736 next_bytenr, num_copies);
738 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
739 struct btrfsic_block *next_block;
740 struct btrfsic_block_data_ctx tmp_next_block_ctx;
741 struct btrfsic_block_link *l;
743 ret = btrfsic_map_block(state, next_bytenr,
744 state->metablock_size,
745 &tmp_next_block_ctx,
746 mirror_num);
747 if (ret) {
748 printk(KERN_INFO "btrfsic:"
749 " btrfsic_map_block(root @%llu,"
750 " mirror %d) failed!\n",
751 next_bytenr, mirror_num);
752 kfree(selected_super);
753 return -1;
756 next_block = btrfsic_block_hashtable_lookup(
757 tmp_next_block_ctx.dev->bdev,
758 tmp_next_block_ctx.dev_bytenr,
759 &state->block_hashtable);
760 BUG_ON(NULL == next_block);
762 l = btrfsic_block_link_hashtable_lookup(
763 tmp_next_block_ctx.dev->bdev,
764 tmp_next_block_ctx.dev_bytenr,
765 state->latest_superblock->dev_state->
766 bdev,
767 state->latest_superblock->dev_bytenr,
768 &state->block_link_hashtable);
769 BUG_ON(NULL == l);
771 ret = btrfsic_read_block(state, &tmp_next_block_ctx);
772 if (ret < (int)PAGE_CACHE_SIZE) {
773 printk(KERN_INFO
774 "btrfsic: read @logical %llu failed!\n",
775 tmp_next_block_ctx.start);
776 btrfsic_release_block_ctx(&tmp_next_block_ctx);
777 kfree(selected_super);
778 return -1;
781 ret = btrfsic_process_metablock(state,
782 next_block,
783 &tmp_next_block_ctx,
784 BTRFS_MAX_LEVEL + 3, 1);
785 btrfsic_release_block_ctx(&tmp_next_block_ctx);
789 kfree(selected_super);
790 return ret;
793 static int btrfsic_process_superblock_dev_mirror(
794 struct btrfsic_state *state,
795 struct btrfsic_dev_state *dev_state,
796 struct btrfs_device *device,
797 int superblock_mirror_num,
798 struct btrfsic_dev_state **selected_dev_state,
799 struct btrfs_super_block *selected_super)
801 struct btrfs_super_block *super_tmp;
802 u64 dev_bytenr;
803 struct buffer_head *bh;
804 struct btrfsic_block *superblock_tmp;
805 int pass;
806 struct block_device *const superblock_bdev = device->bdev;
808 /* super block bytenr is always the unmapped device bytenr */
809 dev_bytenr = btrfs_sb_offset(superblock_mirror_num);
810 if (dev_bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
811 return -1;
812 bh = __bread(superblock_bdev, dev_bytenr / 4096,
813 BTRFS_SUPER_INFO_SIZE);
814 if (NULL == bh)
815 return -1;
816 super_tmp = (struct btrfs_super_block *)
817 (bh->b_data + (dev_bytenr & 4095));
819 if (btrfs_super_bytenr(super_tmp) != dev_bytenr ||
820 btrfs_super_magic(super_tmp) != BTRFS_MAGIC ||
821 memcmp(device->uuid, super_tmp->dev_item.uuid, BTRFS_UUID_SIZE) ||
822 btrfs_super_nodesize(super_tmp) != state->metablock_size ||
823 btrfs_super_leafsize(super_tmp) != state->metablock_size ||
824 btrfs_super_sectorsize(super_tmp) != state->datablock_size) {
825 brelse(bh);
826 return 0;
829 superblock_tmp =
830 btrfsic_block_hashtable_lookup(superblock_bdev,
831 dev_bytenr,
832 &state->block_hashtable);
833 if (NULL == superblock_tmp) {
834 superblock_tmp = btrfsic_block_alloc();
835 if (NULL == superblock_tmp) {
836 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
837 brelse(bh);
838 return -1;
840 /* for superblock, only the dev_bytenr makes sense */
841 superblock_tmp->dev_bytenr = dev_bytenr;
842 superblock_tmp->dev_state = dev_state;
843 superblock_tmp->logical_bytenr = dev_bytenr;
844 superblock_tmp->generation = btrfs_super_generation(super_tmp);
845 superblock_tmp->is_metadata = 1;
846 superblock_tmp->is_superblock = 1;
847 superblock_tmp->is_iodone = 1;
848 superblock_tmp->never_written = 0;
849 superblock_tmp->mirror_num = 1 + superblock_mirror_num;
850 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
851 printk_in_rcu(KERN_INFO "New initial S-block (bdev %p, %s)"
852 " @%llu (%s/%llu/%d)\n",
853 superblock_bdev,
854 rcu_str_deref(device->name), dev_bytenr,
855 dev_state->name, dev_bytenr,
856 superblock_mirror_num);
857 list_add(&superblock_tmp->all_blocks_node,
858 &state->all_blocks_list);
859 btrfsic_block_hashtable_add(superblock_tmp,
860 &state->block_hashtable);
863 /* select the one with the highest generation field */
864 if (btrfs_super_generation(super_tmp) >
865 state->max_superblock_generation ||
866 0 == state->max_superblock_generation) {
867 memcpy(selected_super, super_tmp, sizeof(*selected_super));
868 *selected_dev_state = dev_state;
869 state->max_superblock_generation =
870 btrfs_super_generation(super_tmp);
871 state->latest_superblock = superblock_tmp;
874 for (pass = 0; pass < 3; pass++) {
875 u64 next_bytenr;
876 int num_copies;
877 int mirror_num;
878 const char *additional_string = NULL;
879 struct btrfs_disk_key tmp_disk_key;
881 tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
882 tmp_disk_key.offset = 0;
883 switch (pass) {
884 case 0:
885 btrfs_set_disk_key_objectid(&tmp_disk_key,
886 BTRFS_ROOT_TREE_OBJECTID);
887 additional_string = "initial root ";
888 next_bytenr = btrfs_super_root(super_tmp);
889 break;
890 case 1:
891 btrfs_set_disk_key_objectid(&tmp_disk_key,
892 BTRFS_CHUNK_TREE_OBJECTID);
893 additional_string = "initial chunk ";
894 next_bytenr = btrfs_super_chunk_root(super_tmp);
895 break;
896 case 2:
897 btrfs_set_disk_key_objectid(&tmp_disk_key,
898 BTRFS_TREE_LOG_OBJECTID);
899 additional_string = "initial log ";
900 next_bytenr = btrfs_super_log_root(super_tmp);
901 if (0 == next_bytenr)
902 continue;
903 break;
906 num_copies =
907 btrfs_num_copies(state->root->fs_info,
908 next_bytenr, state->metablock_size);
909 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
910 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
911 next_bytenr, num_copies);
912 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
913 struct btrfsic_block *next_block;
914 struct btrfsic_block_data_ctx tmp_next_block_ctx;
915 struct btrfsic_block_link *l;
917 if (btrfsic_map_block(state, next_bytenr,
918 state->metablock_size,
919 &tmp_next_block_ctx,
920 mirror_num)) {
921 printk(KERN_INFO "btrfsic: btrfsic_map_block("
922 "bytenr @%llu, mirror %d) failed!\n",
923 next_bytenr, mirror_num);
924 brelse(bh);
925 return -1;
928 next_block = btrfsic_block_lookup_or_add(
929 state, &tmp_next_block_ctx,
930 additional_string, 1, 1, 0,
931 mirror_num, NULL);
932 if (NULL == next_block) {
933 btrfsic_release_block_ctx(&tmp_next_block_ctx);
934 brelse(bh);
935 return -1;
938 next_block->disk_key = tmp_disk_key;
939 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
940 l = btrfsic_block_link_lookup_or_add(
941 state, &tmp_next_block_ctx,
942 next_block, superblock_tmp,
943 BTRFSIC_GENERATION_UNKNOWN);
944 btrfsic_release_block_ctx(&tmp_next_block_ctx);
945 if (NULL == l) {
946 brelse(bh);
947 return -1;
951 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES)
952 btrfsic_dump_tree_sub(state, superblock_tmp, 0);
954 brelse(bh);
955 return 0;
958 static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void)
960 struct btrfsic_stack_frame *sf;
962 sf = kzalloc(sizeof(*sf), GFP_NOFS);
963 if (NULL == sf)
964 printk(KERN_INFO "btrfsic: alloc memory failed!\n");
965 else
966 sf->magic = BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER;
967 return sf;
970 static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf)
972 BUG_ON(!(NULL == sf ||
973 BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER == sf->magic));
974 kfree(sf);
977 static int btrfsic_process_metablock(
978 struct btrfsic_state *state,
979 struct btrfsic_block *const first_block,
980 struct btrfsic_block_data_ctx *const first_block_ctx,
981 int first_limit_nesting, int force_iodone_flag)
983 struct btrfsic_stack_frame initial_stack_frame = { 0 };
984 struct btrfsic_stack_frame *sf;
985 struct btrfsic_stack_frame *next_stack;
986 struct btrfs_header *const first_hdr =
987 (struct btrfs_header *)first_block_ctx->datav[0];
989 BUG_ON(!first_hdr);
990 sf = &initial_stack_frame;
991 sf->error = 0;
992 sf->i = -1;
993 sf->limit_nesting = first_limit_nesting;
994 sf->block = first_block;
995 sf->block_ctx = first_block_ctx;
996 sf->next_block = NULL;
997 sf->hdr = first_hdr;
998 sf->prev = NULL;
1000 continue_with_new_stack_frame:
1001 sf->block->generation = le64_to_cpu(sf->hdr->generation);
1002 if (0 == sf->hdr->level) {
1003 struct btrfs_leaf *const leafhdr =
1004 (struct btrfs_leaf *)sf->hdr;
1006 if (-1 == sf->i) {
1007 sf->nr = btrfs_stack_header_nritems(&leafhdr->header);
1009 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1010 printk(KERN_INFO
1011 "leaf %llu items %d generation %llu"
1012 " owner %llu\n",
1013 sf->block_ctx->start, sf->nr,
1014 btrfs_stack_header_generation(
1015 &leafhdr->header),
1016 btrfs_stack_header_owner(
1017 &leafhdr->header));
1020 continue_with_current_leaf_stack_frame:
1021 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1022 sf->i++;
1023 sf->num_copies = 0;
1026 if (sf->i < sf->nr) {
1027 struct btrfs_item disk_item;
1028 u32 disk_item_offset =
1029 (uintptr_t)(leafhdr->items + sf->i) -
1030 (uintptr_t)leafhdr;
1031 struct btrfs_disk_key *disk_key;
1032 u8 type;
1033 u32 item_offset;
1034 u32 item_size;
1036 if (disk_item_offset + sizeof(struct btrfs_item) >
1037 sf->block_ctx->len) {
1038 leaf_item_out_of_bounce_error:
1039 printk(KERN_INFO
1040 "btrfsic: leaf item out of bounce at logical %llu, dev %s\n",
1041 sf->block_ctx->start,
1042 sf->block_ctx->dev->name);
1043 goto one_stack_frame_backwards;
1045 btrfsic_read_from_block_data(sf->block_ctx,
1046 &disk_item,
1047 disk_item_offset,
1048 sizeof(struct btrfs_item));
1049 item_offset = btrfs_stack_item_offset(&disk_item);
1050 item_size = btrfs_stack_item_size(&disk_item);
1051 disk_key = &disk_item.key;
1052 type = btrfs_disk_key_type(disk_key);
1054 if (BTRFS_ROOT_ITEM_KEY == type) {
1055 struct btrfs_root_item root_item;
1056 u32 root_item_offset;
1057 u64 next_bytenr;
1059 root_item_offset = item_offset +
1060 offsetof(struct btrfs_leaf, items);
1061 if (root_item_offset + item_size >
1062 sf->block_ctx->len)
1063 goto leaf_item_out_of_bounce_error;
1064 btrfsic_read_from_block_data(
1065 sf->block_ctx, &root_item,
1066 root_item_offset,
1067 item_size);
1068 next_bytenr = btrfs_root_bytenr(&root_item);
1070 sf->error =
1071 btrfsic_create_link_to_next_block(
1072 state,
1073 sf->block,
1074 sf->block_ctx,
1075 next_bytenr,
1076 sf->limit_nesting,
1077 &sf->next_block_ctx,
1078 &sf->next_block,
1079 force_iodone_flag,
1080 &sf->num_copies,
1081 &sf->mirror_num,
1082 disk_key,
1083 btrfs_root_generation(
1084 &root_item));
1085 if (sf->error)
1086 goto one_stack_frame_backwards;
1088 if (NULL != sf->next_block) {
1089 struct btrfs_header *const next_hdr =
1090 (struct btrfs_header *)
1091 sf->next_block_ctx.datav[0];
1093 next_stack =
1094 btrfsic_stack_frame_alloc();
1095 if (NULL == next_stack) {
1096 sf->error = -1;
1097 btrfsic_release_block_ctx(
1098 &sf->
1099 next_block_ctx);
1100 goto one_stack_frame_backwards;
1103 next_stack->i = -1;
1104 next_stack->block = sf->next_block;
1105 next_stack->block_ctx =
1106 &sf->next_block_ctx;
1107 next_stack->next_block = NULL;
1108 next_stack->hdr = next_hdr;
1109 next_stack->limit_nesting =
1110 sf->limit_nesting - 1;
1111 next_stack->prev = sf;
1112 sf = next_stack;
1113 goto continue_with_new_stack_frame;
1115 } else if (BTRFS_EXTENT_DATA_KEY == type &&
1116 state->include_extent_data) {
1117 sf->error = btrfsic_handle_extent_data(
1118 state,
1119 sf->block,
1120 sf->block_ctx,
1121 item_offset,
1122 force_iodone_flag);
1123 if (sf->error)
1124 goto one_stack_frame_backwards;
1127 goto continue_with_current_leaf_stack_frame;
1129 } else {
1130 struct btrfs_node *const nodehdr = (struct btrfs_node *)sf->hdr;
1132 if (-1 == sf->i) {
1133 sf->nr = btrfs_stack_header_nritems(&nodehdr->header);
1135 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1136 printk(KERN_INFO "node %llu level %d items %d"
1137 " generation %llu owner %llu\n",
1138 sf->block_ctx->start,
1139 nodehdr->header.level, sf->nr,
1140 btrfs_stack_header_generation(
1141 &nodehdr->header),
1142 btrfs_stack_header_owner(
1143 &nodehdr->header));
1146 continue_with_current_node_stack_frame:
1147 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1148 sf->i++;
1149 sf->num_copies = 0;
1152 if (sf->i < sf->nr) {
1153 struct btrfs_key_ptr key_ptr;
1154 u32 key_ptr_offset;
1155 u64 next_bytenr;
1157 key_ptr_offset = (uintptr_t)(nodehdr->ptrs + sf->i) -
1158 (uintptr_t)nodehdr;
1159 if (key_ptr_offset + sizeof(struct btrfs_key_ptr) >
1160 sf->block_ctx->len) {
1161 printk(KERN_INFO
1162 "btrfsic: node item out of bounce at logical %llu, dev %s\n",
1163 sf->block_ctx->start,
1164 sf->block_ctx->dev->name);
1165 goto one_stack_frame_backwards;
1167 btrfsic_read_from_block_data(
1168 sf->block_ctx, &key_ptr, key_ptr_offset,
1169 sizeof(struct btrfs_key_ptr));
1170 next_bytenr = btrfs_stack_key_blockptr(&key_ptr);
1172 sf->error = btrfsic_create_link_to_next_block(
1173 state,
1174 sf->block,
1175 sf->block_ctx,
1176 next_bytenr,
1177 sf->limit_nesting,
1178 &sf->next_block_ctx,
1179 &sf->next_block,
1180 force_iodone_flag,
1181 &sf->num_copies,
1182 &sf->mirror_num,
1183 &key_ptr.key,
1184 btrfs_stack_key_generation(&key_ptr));
1185 if (sf->error)
1186 goto one_stack_frame_backwards;
1188 if (NULL != sf->next_block) {
1189 struct btrfs_header *const next_hdr =
1190 (struct btrfs_header *)
1191 sf->next_block_ctx.datav[0];
1193 next_stack = btrfsic_stack_frame_alloc();
1194 if (NULL == next_stack) {
1195 sf->error = -1;
1196 goto one_stack_frame_backwards;
1199 next_stack->i = -1;
1200 next_stack->block = sf->next_block;
1201 next_stack->block_ctx = &sf->next_block_ctx;
1202 next_stack->next_block = NULL;
1203 next_stack->hdr = next_hdr;
1204 next_stack->limit_nesting =
1205 sf->limit_nesting - 1;
1206 next_stack->prev = sf;
1207 sf = next_stack;
1208 goto continue_with_new_stack_frame;
1211 goto continue_with_current_node_stack_frame;
1215 one_stack_frame_backwards:
1216 if (NULL != sf->prev) {
1217 struct btrfsic_stack_frame *const prev = sf->prev;
1219 /* the one for the initial block is freed in the caller */
1220 btrfsic_release_block_ctx(sf->block_ctx);
1222 if (sf->error) {
1223 prev->error = sf->error;
1224 btrfsic_stack_frame_free(sf);
1225 sf = prev;
1226 goto one_stack_frame_backwards;
1229 btrfsic_stack_frame_free(sf);
1230 sf = prev;
1231 goto continue_with_new_stack_frame;
1232 } else {
1233 BUG_ON(&initial_stack_frame != sf);
1236 return sf->error;
1239 static void btrfsic_read_from_block_data(
1240 struct btrfsic_block_data_ctx *block_ctx,
1241 void *dstv, u32 offset, size_t len)
1243 size_t cur;
1244 size_t offset_in_page;
1245 char *kaddr;
1246 char *dst = (char *)dstv;
1247 size_t start_offset = block_ctx->start & ((u64)PAGE_CACHE_SIZE - 1);
1248 unsigned long i = (start_offset + offset) >> PAGE_CACHE_SHIFT;
1250 WARN_ON(offset + len > block_ctx->len);
1251 offset_in_page = (start_offset + offset) & (PAGE_CACHE_SIZE - 1);
1253 while (len > 0) {
1254 cur = min(len, ((size_t)PAGE_CACHE_SIZE - offset_in_page));
1255 BUG_ON(i >= (block_ctx->len + PAGE_CACHE_SIZE - 1) >>
1256 PAGE_CACHE_SHIFT);
1257 kaddr = block_ctx->datav[i];
1258 memcpy(dst, kaddr + offset_in_page, cur);
1260 dst += cur;
1261 len -= cur;
1262 offset_in_page = 0;
1263 i++;
1267 static int btrfsic_create_link_to_next_block(
1268 struct btrfsic_state *state,
1269 struct btrfsic_block *block,
1270 struct btrfsic_block_data_ctx *block_ctx,
1271 u64 next_bytenr,
1272 int limit_nesting,
1273 struct btrfsic_block_data_ctx *next_block_ctx,
1274 struct btrfsic_block **next_blockp,
1275 int force_iodone_flag,
1276 int *num_copiesp, int *mirror_nump,
1277 struct btrfs_disk_key *disk_key,
1278 u64 parent_generation)
1280 struct btrfsic_block *next_block = NULL;
1281 int ret;
1282 struct btrfsic_block_link *l;
1283 int did_alloc_block_link;
1284 int block_was_created;
1286 *next_blockp = NULL;
1287 if (0 == *num_copiesp) {
1288 *num_copiesp =
1289 btrfs_num_copies(state->root->fs_info,
1290 next_bytenr, state->metablock_size);
1291 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1292 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
1293 next_bytenr, *num_copiesp);
1294 *mirror_nump = 1;
1297 if (*mirror_nump > *num_copiesp)
1298 return 0;
1300 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1301 printk(KERN_INFO
1302 "btrfsic_create_link_to_next_block(mirror_num=%d)\n",
1303 *mirror_nump);
1304 ret = btrfsic_map_block(state, next_bytenr,
1305 state->metablock_size,
1306 next_block_ctx, *mirror_nump);
1307 if (ret) {
1308 printk(KERN_INFO
1309 "btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
1310 next_bytenr, *mirror_nump);
1311 btrfsic_release_block_ctx(next_block_ctx);
1312 *next_blockp = NULL;
1313 return -1;
1316 next_block = btrfsic_block_lookup_or_add(state,
1317 next_block_ctx, "referenced ",
1318 1, force_iodone_flag,
1319 !force_iodone_flag,
1320 *mirror_nump,
1321 &block_was_created);
1322 if (NULL == next_block) {
1323 btrfsic_release_block_ctx(next_block_ctx);
1324 *next_blockp = NULL;
1325 return -1;
1327 if (block_was_created) {
1328 l = NULL;
1329 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
1330 } else {
1331 if (next_block->logical_bytenr != next_bytenr &&
1332 !(!next_block->is_metadata &&
1333 0 == next_block->logical_bytenr)) {
1334 printk(KERN_INFO
1335 "Referenced block @%llu (%s/%llu/%d)"
1336 " found in hash table, %c,"
1337 " bytenr mismatch (!= stored %llu).\n",
1338 next_bytenr, next_block_ctx->dev->name,
1339 next_block_ctx->dev_bytenr, *mirror_nump,
1340 btrfsic_get_block_type(state, next_block),
1341 next_block->logical_bytenr);
1342 } else if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1343 printk(KERN_INFO
1344 "Referenced block @%llu (%s/%llu/%d)"
1345 " found in hash table, %c.\n",
1346 next_bytenr, next_block_ctx->dev->name,
1347 next_block_ctx->dev_bytenr, *mirror_nump,
1348 btrfsic_get_block_type(state, next_block));
1349 next_block->logical_bytenr = next_bytenr;
1351 next_block->mirror_num = *mirror_nump;
1352 l = btrfsic_block_link_hashtable_lookup(
1353 next_block_ctx->dev->bdev,
1354 next_block_ctx->dev_bytenr,
1355 block_ctx->dev->bdev,
1356 block_ctx->dev_bytenr,
1357 &state->block_link_hashtable);
1360 next_block->disk_key = *disk_key;
1361 if (NULL == l) {
1362 l = btrfsic_block_link_alloc();
1363 if (NULL == l) {
1364 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
1365 btrfsic_release_block_ctx(next_block_ctx);
1366 *next_blockp = NULL;
1367 return -1;
1370 did_alloc_block_link = 1;
1371 l->block_ref_to = next_block;
1372 l->block_ref_from = block;
1373 l->ref_cnt = 1;
1374 l->parent_generation = parent_generation;
1376 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1377 btrfsic_print_add_link(state, l);
1379 list_add(&l->node_ref_to, &block->ref_to_list);
1380 list_add(&l->node_ref_from, &next_block->ref_from_list);
1382 btrfsic_block_link_hashtable_add(l,
1383 &state->block_link_hashtable);
1384 } else {
1385 did_alloc_block_link = 0;
1386 if (0 == limit_nesting) {
1387 l->ref_cnt++;
1388 l->parent_generation = parent_generation;
1389 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1390 btrfsic_print_add_link(state, l);
1394 if (limit_nesting > 0 && did_alloc_block_link) {
1395 ret = btrfsic_read_block(state, next_block_ctx);
1396 if (ret < (int)next_block_ctx->len) {
1397 printk(KERN_INFO
1398 "btrfsic: read block @logical %llu failed!\n",
1399 next_bytenr);
1400 btrfsic_release_block_ctx(next_block_ctx);
1401 *next_blockp = NULL;
1402 return -1;
1405 *next_blockp = next_block;
1406 } else {
1407 *next_blockp = NULL;
1409 (*mirror_nump)++;
1411 return 0;
1414 static int btrfsic_handle_extent_data(
1415 struct btrfsic_state *state,
1416 struct btrfsic_block *block,
1417 struct btrfsic_block_data_ctx *block_ctx,
1418 u32 item_offset, int force_iodone_flag)
1420 int ret;
1421 struct btrfs_file_extent_item file_extent_item;
1422 u64 file_extent_item_offset;
1423 u64 next_bytenr;
1424 u64 num_bytes;
1425 u64 generation;
1426 struct btrfsic_block_link *l;
1428 file_extent_item_offset = offsetof(struct btrfs_leaf, items) +
1429 item_offset;
1430 if (file_extent_item_offset +
1431 offsetof(struct btrfs_file_extent_item, disk_num_bytes) >
1432 block_ctx->len) {
1433 printk(KERN_INFO
1434 "btrfsic: file item out of bounce at logical %llu, dev %s\n",
1435 block_ctx->start, block_ctx->dev->name);
1436 return -1;
1439 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1440 file_extent_item_offset,
1441 offsetof(struct btrfs_file_extent_item, disk_num_bytes));
1442 if (BTRFS_FILE_EXTENT_REG != file_extent_item.type ||
1443 btrfs_stack_file_extent_disk_bytenr(&file_extent_item) == 0) {
1444 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1445 printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu\n",
1446 file_extent_item.type,
1447 btrfs_stack_file_extent_disk_bytenr(
1448 &file_extent_item));
1449 return 0;
1452 if (file_extent_item_offset + sizeof(struct btrfs_file_extent_item) >
1453 block_ctx->len) {
1454 printk(KERN_INFO
1455 "btrfsic: file item out of bounce at logical %llu, dev %s\n",
1456 block_ctx->start, block_ctx->dev->name);
1457 return -1;
1459 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1460 file_extent_item_offset,
1461 sizeof(struct btrfs_file_extent_item));
1462 next_bytenr = btrfs_stack_file_extent_disk_bytenr(&file_extent_item);
1463 if (btrfs_stack_file_extent_compression(&file_extent_item) ==
1464 BTRFS_COMPRESS_NONE) {
1465 next_bytenr += btrfs_stack_file_extent_offset(&file_extent_item);
1466 num_bytes = btrfs_stack_file_extent_num_bytes(&file_extent_item);
1467 } else {
1468 num_bytes = btrfs_stack_file_extent_disk_num_bytes(&file_extent_item);
1470 generation = btrfs_stack_file_extent_generation(&file_extent_item);
1472 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1473 printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu,"
1474 " offset = %llu, num_bytes = %llu\n",
1475 file_extent_item.type,
1476 btrfs_stack_file_extent_disk_bytenr(&file_extent_item),
1477 btrfs_stack_file_extent_offset(&file_extent_item),
1478 num_bytes);
1479 while (num_bytes > 0) {
1480 u32 chunk_len;
1481 int num_copies;
1482 int mirror_num;
1484 if (num_bytes > state->datablock_size)
1485 chunk_len = state->datablock_size;
1486 else
1487 chunk_len = num_bytes;
1489 num_copies =
1490 btrfs_num_copies(state->root->fs_info,
1491 next_bytenr, state->datablock_size);
1492 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1493 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
1494 next_bytenr, num_copies);
1495 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
1496 struct btrfsic_block_data_ctx next_block_ctx;
1497 struct btrfsic_block *next_block;
1498 int block_was_created;
1500 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1501 printk(KERN_INFO "btrfsic_handle_extent_data("
1502 "mirror_num=%d)\n", mirror_num);
1503 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1504 printk(KERN_INFO
1505 "\tdisk_bytenr = %llu, num_bytes %u\n",
1506 next_bytenr, chunk_len);
1507 ret = btrfsic_map_block(state, next_bytenr,
1508 chunk_len, &next_block_ctx,
1509 mirror_num);
1510 if (ret) {
1511 printk(KERN_INFO
1512 "btrfsic: btrfsic_map_block(@%llu,"
1513 " mirror=%d) failed!\n",
1514 next_bytenr, mirror_num);
1515 return -1;
1518 next_block = btrfsic_block_lookup_or_add(
1519 state,
1520 &next_block_ctx,
1521 "referenced ",
1523 force_iodone_flag,
1524 !force_iodone_flag,
1525 mirror_num,
1526 &block_was_created);
1527 if (NULL == next_block) {
1528 printk(KERN_INFO
1529 "btrfsic: error, kmalloc failed!\n");
1530 btrfsic_release_block_ctx(&next_block_ctx);
1531 return -1;
1533 if (!block_was_created) {
1534 if (next_block->logical_bytenr != next_bytenr &&
1535 !(!next_block->is_metadata &&
1536 0 == next_block->logical_bytenr)) {
1537 printk(KERN_INFO
1538 "Referenced block"
1539 " @%llu (%s/%llu/%d)"
1540 " found in hash table, D,"
1541 " bytenr mismatch"
1542 " (!= stored %llu).\n",
1543 next_bytenr,
1544 next_block_ctx.dev->name,
1545 next_block_ctx.dev_bytenr,
1546 mirror_num,
1547 next_block->logical_bytenr);
1549 next_block->logical_bytenr = next_bytenr;
1550 next_block->mirror_num = mirror_num;
1553 l = btrfsic_block_link_lookup_or_add(state,
1554 &next_block_ctx,
1555 next_block, block,
1556 generation);
1557 btrfsic_release_block_ctx(&next_block_ctx);
1558 if (NULL == l)
1559 return -1;
1562 next_bytenr += chunk_len;
1563 num_bytes -= chunk_len;
1566 return 0;
1569 static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
1570 struct btrfsic_block_data_ctx *block_ctx_out,
1571 int mirror_num)
1573 int ret;
1574 u64 length;
1575 struct btrfs_bio *multi = NULL;
1576 struct btrfs_device *device;
1578 length = len;
1579 ret = btrfs_map_block(state->root->fs_info, READ,
1580 bytenr, &length, &multi, mirror_num);
1582 if (ret) {
1583 block_ctx_out->start = 0;
1584 block_ctx_out->dev_bytenr = 0;
1585 block_ctx_out->len = 0;
1586 block_ctx_out->dev = NULL;
1587 block_ctx_out->datav = NULL;
1588 block_ctx_out->pagev = NULL;
1589 block_ctx_out->mem_to_free = NULL;
1591 return ret;
1594 device = multi->stripes[0].dev;
1595 block_ctx_out->dev = btrfsic_dev_state_lookup(device->bdev);
1596 block_ctx_out->dev_bytenr = multi->stripes[0].physical;
1597 block_ctx_out->start = bytenr;
1598 block_ctx_out->len = len;
1599 block_ctx_out->datav = NULL;
1600 block_ctx_out->pagev = NULL;
1601 block_ctx_out->mem_to_free = NULL;
1603 kfree(multi);
1604 if (NULL == block_ctx_out->dev) {
1605 ret = -ENXIO;
1606 printk(KERN_INFO "btrfsic: error, cannot lookup dev (#1)!\n");
1609 return ret;
1612 static int btrfsic_map_superblock(struct btrfsic_state *state, u64 bytenr,
1613 u32 len, struct block_device *bdev,
1614 struct btrfsic_block_data_ctx *block_ctx_out)
1616 block_ctx_out->dev = btrfsic_dev_state_lookup(bdev);
1617 block_ctx_out->dev_bytenr = bytenr;
1618 block_ctx_out->start = bytenr;
1619 block_ctx_out->len = len;
1620 block_ctx_out->datav = NULL;
1621 block_ctx_out->pagev = NULL;
1622 block_ctx_out->mem_to_free = NULL;
1623 if (NULL != block_ctx_out->dev) {
1624 return 0;
1625 } else {
1626 printk(KERN_INFO "btrfsic: error, cannot lookup dev (#2)!\n");
1627 return -ENXIO;
1631 static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx)
1633 if (block_ctx->mem_to_free) {
1634 unsigned int num_pages;
1636 BUG_ON(!block_ctx->datav);
1637 BUG_ON(!block_ctx->pagev);
1638 num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
1639 PAGE_CACHE_SHIFT;
1640 while (num_pages > 0) {
1641 num_pages--;
1642 if (block_ctx->datav[num_pages]) {
1643 kunmap(block_ctx->pagev[num_pages]);
1644 block_ctx->datav[num_pages] = NULL;
1646 if (block_ctx->pagev[num_pages]) {
1647 __free_page(block_ctx->pagev[num_pages]);
1648 block_ctx->pagev[num_pages] = NULL;
1652 kfree(block_ctx->mem_to_free);
1653 block_ctx->mem_to_free = NULL;
1654 block_ctx->pagev = NULL;
1655 block_ctx->datav = NULL;
1659 static int btrfsic_read_block(struct btrfsic_state *state,
1660 struct btrfsic_block_data_ctx *block_ctx)
1662 unsigned int num_pages;
1663 unsigned int i;
1664 u64 dev_bytenr;
1665 int ret;
1667 BUG_ON(block_ctx->datav);
1668 BUG_ON(block_ctx->pagev);
1669 BUG_ON(block_ctx->mem_to_free);
1670 if (block_ctx->dev_bytenr & ((u64)PAGE_CACHE_SIZE - 1)) {
1671 printk(KERN_INFO
1672 "btrfsic: read_block() with unaligned bytenr %llu\n",
1673 block_ctx->dev_bytenr);
1674 return -1;
1677 num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
1678 PAGE_CACHE_SHIFT;
1679 block_ctx->mem_to_free = kzalloc((sizeof(*block_ctx->datav) +
1680 sizeof(*block_ctx->pagev)) *
1681 num_pages, GFP_NOFS);
1682 if (!block_ctx->mem_to_free)
1683 return -1;
1684 block_ctx->datav = block_ctx->mem_to_free;
1685 block_ctx->pagev = (struct page **)(block_ctx->datav + num_pages);
1686 for (i = 0; i < num_pages; i++) {
1687 block_ctx->pagev[i] = alloc_page(GFP_NOFS);
1688 if (!block_ctx->pagev[i])
1689 return -1;
1692 dev_bytenr = block_ctx->dev_bytenr;
1693 for (i = 0; i < num_pages;) {
1694 struct bio *bio;
1695 unsigned int j;
1697 bio = btrfs_io_bio_alloc(GFP_NOFS, num_pages - i);
1698 if (!bio) {
1699 printk(KERN_INFO
1700 "btrfsic: bio_alloc() for %u pages failed!\n",
1701 num_pages - i);
1702 return -1;
1704 bio->bi_bdev = block_ctx->dev->bdev;
1705 bio->bi_iter.bi_sector = dev_bytenr >> 9;
1707 for (j = i; j < num_pages; j++) {
1708 ret = bio_add_page(bio, block_ctx->pagev[j],
1709 PAGE_CACHE_SIZE, 0);
1710 if (PAGE_CACHE_SIZE != ret)
1711 break;
1713 if (j == i) {
1714 printk(KERN_INFO
1715 "btrfsic: error, failed to add a single page!\n");
1716 return -1;
1718 if (submit_bio_wait(READ, bio)) {
1719 printk(KERN_INFO
1720 "btrfsic: read error at logical %llu dev %s!\n",
1721 block_ctx->start, block_ctx->dev->name);
1722 bio_put(bio);
1723 return -1;
1725 bio_put(bio);
1726 dev_bytenr += (j - i) * PAGE_CACHE_SIZE;
1727 i = j;
1729 for (i = 0; i < num_pages; i++) {
1730 block_ctx->datav[i] = kmap(block_ctx->pagev[i]);
1731 if (!block_ctx->datav[i]) {
1732 printk(KERN_INFO "btrfsic: kmap() failed (dev %s)!\n",
1733 block_ctx->dev->name);
1734 return -1;
1738 return block_ctx->len;
1741 static void btrfsic_dump_database(struct btrfsic_state *state)
1743 struct list_head *elem_all;
1745 BUG_ON(NULL == state);
1747 printk(KERN_INFO "all_blocks_list:\n");
1748 list_for_each(elem_all, &state->all_blocks_list) {
1749 const struct btrfsic_block *const b_all =
1750 list_entry(elem_all, struct btrfsic_block,
1751 all_blocks_node);
1752 struct list_head *elem_ref_to;
1753 struct list_head *elem_ref_from;
1755 printk(KERN_INFO "%c-block @%llu (%s/%llu/%d)\n",
1756 btrfsic_get_block_type(state, b_all),
1757 b_all->logical_bytenr, b_all->dev_state->name,
1758 b_all->dev_bytenr, b_all->mirror_num);
1760 list_for_each(elem_ref_to, &b_all->ref_to_list) {
1761 const struct btrfsic_block_link *const l =
1762 list_entry(elem_ref_to,
1763 struct btrfsic_block_link,
1764 node_ref_to);
1766 printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
1767 " refers %u* to"
1768 " %c @%llu (%s/%llu/%d)\n",
1769 btrfsic_get_block_type(state, b_all),
1770 b_all->logical_bytenr, b_all->dev_state->name,
1771 b_all->dev_bytenr, b_all->mirror_num,
1772 l->ref_cnt,
1773 btrfsic_get_block_type(state, l->block_ref_to),
1774 l->block_ref_to->logical_bytenr,
1775 l->block_ref_to->dev_state->name,
1776 l->block_ref_to->dev_bytenr,
1777 l->block_ref_to->mirror_num);
1780 list_for_each(elem_ref_from, &b_all->ref_from_list) {
1781 const struct btrfsic_block_link *const l =
1782 list_entry(elem_ref_from,
1783 struct btrfsic_block_link,
1784 node_ref_from);
1786 printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
1787 " is ref %u* from"
1788 " %c @%llu (%s/%llu/%d)\n",
1789 btrfsic_get_block_type(state, b_all),
1790 b_all->logical_bytenr, b_all->dev_state->name,
1791 b_all->dev_bytenr, b_all->mirror_num,
1792 l->ref_cnt,
1793 btrfsic_get_block_type(state, l->block_ref_from),
1794 l->block_ref_from->logical_bytenr,
1795 l->block_ref_from->dev_state->name,
1796 l->block_ref_from->dev_bytenr,
1797 l->block_ref_from->mirror_num);
1800 printk(KERN_INFO "\n");
1805 * Test whether the disk block contains a tree block (leaf or node)
1806 * (note that this test fails for the super block)
1808 static int btrfsic_test_for_metadata(struct btrfsic_state *state,
1809 char **datav, unsigned int num_pages)
1811 struct btrfs_header *h;
1812 u8 csum[BTRFS_CSUM_SIZE];
1813 u32 crc = ~(u32)0;
1814 unsigned int i;
1816 if (num_pages * PAGE_CACHE_SIZE < state->metablock_size)
1817 return 1; /* not metadata */
1818 num_pages = state->metablock_size >> PAGE_CACHE_SHIFT;
1819 h = (struct btrfs_header *)datav[0];
1821 if (memcmp(h->fsid, state->root->fs_info->fsid, BTRFS_UUID_SIZE))
1822 return 1;
1824 for (i = 0; i < num_pages; i++) {
1825 u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE);
1826 size_t sublen = i ? PAGE_CACHE_SIZE :
1827 (PAGE_CACHE_SIZE - BTRFS_CSUM_SIZE);
1829 crc = btrfs_crc32c(crc, data, sublen);
1831 btrfs_csum_final(crc, csum);
1832 if (memcmp(csum, h->csum, state->csum_size))
1833 return 1;
1835 return 0; /* is metadata */
1838 static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
1839 u64 dev_bytenr, char **mapped_datav,
1840 unsigned int num_pages,
1841 struct bio *bio, int *bio_is_patched,
1842 struct buffer_head *bh,
1843 int submit_bio_bh_rw)
1845 int is_metadata;
1846 struct btrfsic_block *block;
1847 struct btrfsic_block_data_ctx block_ctx;
1848 int ret;
1849 struct btrfsic_state *state = dev_state->state;
1850 struct block_device *bdev = dev_state->bdev;
1851 unsigned int processed_len;
1853 if (NULL != bio_is_patched)
1854 *bio_is_patched = 0;
1856 again:
1857 if (num_pages == 0)
1858 return;
1860 processed_len = 0;
1861 is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
1862 num_pages));
1864 block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
1865 &state->block_hashtable);
1866 if (NULL != block) {
1867 u64 bytenr = 0;
1868 struct list_head *elem_ref_to;
1869 struct list_head *tmp_ref_to;
1871 if (block->is_superblock) {
1872 bytenr = btrfs_super_bytenr((struct btrfs_super_block *)
1873 mapped_datav[0]);
1874 if (num_pages * PAGE_CACHE_SIZE <
1875 BTRFS_SUPER_INFO_SIZE) {
1876 printk(KERN_INFO
1877 "btrfsic: cannot work with too short bios!\n");
1878 return;
1880 is_metadata = 1;
1881 BUG_ON(BTRFS_SUPER_INFO_SIZE & (PAGE_CACHE_SIZE - 1));
1882 processed_len = BTRFS_SUPER_INFO_SIZE;
1883 if (state->print_mask &
1884 BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) {
1885 printk(KERN_INFO
1886 "[before new superblock is written]:\n");
1887 btrfsic_dump_tree_sub(state, block, 0);
1890 if (is_metadata) {
1891 if (!block->is_superblock) {
1892 if (num_pages * PAGE_CACHE_SIZE <
1893 state->metablock_size) {
1894 printk(KERN_INFO
1895 "btrfsic: cannot work with too short bios!\n");
1896 return;
1898 processed_len = state->metablock_size;
1899 bytenr = btrfs_stack_header_bytenr(
1900 (struct btrfs_header *)
1901 mapped_datav[0]);
1902 btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
1903 dev_state,
1904 dev_bytenr);
1906 if (block->logical_bytenr != bytenr &&
1907 !(!block->is_metadata &&
1908 block->logical_bytenr == 0))
1909 printk(KERN_INFO
1910 "Written block @%llu (%s/%llu/%d)"
1911 " found in hash table, %c,"
1912 " bytenr mismatch"
1913 " (!= stored %llu).\n",
1914 bytenr, dev_state->name, dev_bytenr,
1915 block->mirror_num,
1916 btrfsic_get_block_type(state, block),
1917 block->logical_bytenr);
1918 else if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1919 printk(KERN_INFO
1920 "Written block @%llu (%s/%llu/%d)"
1921 " found in hash table, %c.\n",
1922 bytenr, dev_state->name, dev_bytenr,
1923 block->mirror_num,
1924 btrfsic_get_block_type(state, block));
1925 block->logical_bytenr = bytenr;
1926 } else {
1927 if (num_pages * PAGE_CACHE_SIZE <
1928 state->datablock_size) {
1929 printk(KERN_INFO
1930 "btrfsic: cannot work with too short bios!\n");
1931 return;
1933 processed_len = state->datablock_size;
1934 bytenr = block->logical_bytenr;
1935 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1936 printk(KERN_INFO
1937 "Written block @%llu (%s/%llu/%d)"
1938 " found in hash table, %c.\n",
1939 bytenr, dev_state->name, dev_bytenr,
1940 block->mirror_num,
1941 btrfsic_get_block_type(state, block));
1944 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1945 printk(KERN_INFO
1946 "ref_to_list: %cE, ref_from_list: %cE\n",
1947 list_empty(&block->ref_to_list) ? ' ' : '!',
1948 list_empty(&block->ref_from_list) ? ' ' : '!');
1949 if (btrfsic_is_block_ref_by_superblock(state, block, 0)) {
1950 printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
1951 " @%llu (%s/%llu/%d), old(gen=%llu,"
1952 " objectid=%llu, type=%d, offset=%llu),"
1953 " new(gen=%llu),"
1954 " which is referenced by most recent superblock"
1955 " (superblockgen=%llu)!\n",
1956 btrfsic_get_block_type(state, block), bytenr,
1957 dev_state->name, dev_bytenr, block->mirror_num,
1958 block->generation,
1959 btrfs_disk_key_objectid(&block->disk_key),
1960 block->disk_key.type,
1961 btrfs_disk_key_offset(&block->disk_key),
1962 btrfs_stack_header_generation(
1963 (struct btrfs_header *) mapped_datav[0]),
1964 state->max_superblock_generation);
1965 btrfsic_dump_tree(state);
1968 if (!block->is_iodone && !block->never_written) {
1969 printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
1970 " @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu,"
1971 " which is not yet iodone!\n",
1972 btrfsic_get_block_type(state, block), bytenr,
1973 dev_state->name, dev_bytenr, block->mirror_num,
1974 block->generation,
1975 btrfs_stack_header_generation(
1976 (struct btrfs_header *)
1977 mapped_datav[0]));
1978 /* it would not be safe to go on */
1979 btrfsic_dump_tree(state);
1980 goto continue_loop;
1984 * Clear all references of this block. Do not free
1985 * the block itself even if is not referenced anymore
1986 * because it still carries valueable information
1987 * like whether it was ever written and IO completed.
1989 list_for_each_safe(elem_ref_to, tmp_ref_to,
1990 &block->ref_to_list) {
1991 struct btrfsic_block_link *const l =
1992 list_entry(elem_ref_to,
1993 struct btrfsic_block_link,
1994 node_ref_to);
1996 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1997 btrfsic_print_rem_link(state, l);
1998 l->ref_cnt--;
1999 if (0 == l->ref_cnt) {
2000 list_del(&l->node_ref_to);
2001 list_del(&l->node_ref_from);
2002 btrfsic_block_link_hashtable_remove(l);
2003 btrfsic_block_link_free(l);
2007 if (block->is_superblock)
2008 ret = btrfsic_map_superblock(state, bytenr,
2009 processed_len,
2010 bdev, &block_ctx);
2011 else
2012 ret = btrfsic_map_block(state, bytenr, processed_len,
2013 &block_ctx, 0);
2014 if (ret) {
2015 printk(KERN_INFO
2016 "btrfsic: btrfsic_map_block(root @%llu)"
2017 " failed!\n", bytenr);
2018 goto continue_loop;
2020 block_ctx.datav = mapped_datav;
2021 /* the following is required in case of writes to mirrors,
2022 * use the same that was used for the lookup */
2023 block_ctx.dev = dev_state;
2024 block_ctx.dev_bytenr = dev_bytenr;
2026 if (is_metadata || state->include_extent_data) {
2027 block->never_written = 0;
2028 block->iodone_w_error = 0;
2029 if (NULL != bio) {
2030 block->is_iodone = 0;
2031 BUG_ON(NULL == bio_is_patched);
2032 if (!*bio_is_patched) {
2033 block->orig_bio_bh_private =
2034 bio->bi_private;
2035 block->orig_bio_bh_end_io.bio =
2036 bio->bi_end_io;
2037 block->next_in_same_bio = NULL;
2038 bio->bi_private = block;
2039 bio->bi_end_io = btrfsic_bio_end_io;
2040 *bio_is_patched = 1;
2041 } else {
2042 struct btrfsic_block *chained_block =
2043 (struct btrfsic_block *)
2044 bio->bi_private;
2046 BUG_ON(NULL == chained_block);
2047 block->orig_bio_bh_private =
2048 chained_block->orig_bio_bh_private;
2049 block->orig_bio_bh_end_io.bio =
2050 chained_block->orig_bio_bh_end_io.
2051 bio;
2052 block->next_in_same_bio = chained_block;
2053 bio->bi_private = block;
2055 } else if (NULL != bh) {
2056 block->is_iodone = 0;
2057 block->orig_bio_bh_private = bh->b_private;
2058 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2059 block->next_in_same_bio = NULL;
2060 bh->b_private = block;
2061 bh->b_end_io = btrfsic_bh_end_io;
2062 } else {
2063 block->is_iodone = 1;
2064 block->orig_bio_bh_private = NULL;
2065 block->orig_bio_bh_end_io.bio = NULL;
2066 block->next_in_same_bio = NULL;
2070 block->flush_gen = dev_state->last_flush_gen + 1;
2071 block->submit_bio_bh_rw = submit_bio_bh_rw;
2072 if (is_metadata) {
2073 block->logical_bytenr = bytenr;
2074 block->is_metadata = 1;
2075 if (block->is_superblock) {
2076 BUG_ON(PAGE_CACHE_SIZE !=
2077 BTRFS_SUPER_INFO_SIZE);
2078 ret = btrfsic_process_written_superblock(
2079 state,
2080 block,
2081 (struct btrfs_super_block *)
2082 mapped_datav[0]);
2083 if (state->print_mask &
2084 BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) {
2085 printk(KERN_INFO
2086 "[after new superblock is written]:\n");
2087 btrfsic_dump_tree_sub(state, block, 0);
2089 } else {
2090 block->mirror_num = 0; /* unknown */
2091 ret = btrfsic_process_metablock(
2092 state,
2093 block,
2094 &block_ctx,
2095 0, 0);
2097 if (ret)
2098 printk(KERN_INFO
2099 "btrfsic: btrfsic_process_metablock"
2100 "(root @%llu) failed!\n",
2101 dev_bytenr);
2102 } else {
2103 block->is_metadata = 0;
2104 block->mirror_num = 0; /* unknown */
2105 block->generation = BTRFSIC_GENERATION_UNKNOWN;
2106 if (!state->include_extent_data
2107 && list_empty(&block->ref_from_list)) {
2109 * disk block is overwritten with extent
2110 * data (not meta data) and we are configured
2111 * to not include extent data: take the
2112 * chance and free the block's memory
2114 btrfsic_block_hashtable_remove(block);
2115 list_del(&block->all_blocks_node);
2116 btrfsic_block_free(block);
2119 btrfsic_release_block_ctx(&block_ctx);
2120 } else {
2121 /* block has not been found in hash table */
2122 u64 bytenr;
2124 if (!is_metadata) {
2125 processed_len = state->datablock_size;
2126 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2127 printk(KERN_INFO "Written block (%s/%llu/?)"
2128 " !found in hash table, D.\n",
2129 dev_state->name, dev_bytenr);
2130 if (!state->include_extent_data) {
2131 /* ignore that written D block */
2132 goto continue_loop;
2135 /* this is getting ugly for the
2136 * include_extent_data case... */
2137 bytenr = 0; /* unknown */
2138 block_ctx.start = bytenr;
2139 block_ctx.len = processed_len;
2140 block_ctx.mem_to_free = NULL;
2141 block_ctx.pagev = NULL;
2142 } else {
2143 processed_len = state->metablock_size;
2144 bytenr = btrfs_stack_header_bytenr(
2145 (struct btrfs_header *)
2146 mapped_datav[0]);
2147 btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
2148 dev_bytenr);
2149 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2150 printk(KERN_INFO
2151 "Written block @%llu (%s/%llu/?)"
2152 " !found in hash table, M.\n",
2153 bytenr, dev_state->name, dev_bytenr);
2155 ret = btrfsic_map_block(state, bytenr, processed_len,
2156 &block_ctx, 0);
2157 if (ret) {
2158 printk(KERN_INFO
2159 "btrfsic: btrfsic_map_block(root @%llu)"
2160 " failed!\n",
2161 dev_bytenr);
2162 goto continue_loop;
2165 block_ctx.datav = mapped_datav;
2166 /* the following is required in case of writes to mirrors,
2167 * use the same that was used for the lookup */
2168 block_ctx.dev = dev_state;
2169 block_ctx.dev_bytenr = dev_bytenr;
2171 block = btrfsic_block_alloc();
2172 if (NULL == block) {
2173 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
2174 btrfsic_release_block_ctx(&block_ctx);
2175 goto continue_loop;
2177 block->dev_state = dev_state;
2178 block->dev_bytenr = dev_bytenr;
2179 block->logical_bytenr = bytenr;
2180 block->is_metadata = is_metadata;
2181 block->never_written = 0;
2182 block->iodone_w_error = 0;
2183 block->mirror_num = 0; /* unknown */
2184 block->flush_gen = dev_state->last_flush_gen + 1;
2185 block->submit_bio_bh_rw = submit_bio_bh_rw;
2186 if (NULL != bio) {
2187 block->is_iodone = 0;
2188 BUG_ON(NULL == bio_is_patched);
2189 if (!*bio_is_patched) {
2190 block->orig_bio_bh_private = bio->bi_private;
2191 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
2192 block->next_in_same_bio = NULL;
2193 bio->bi_private = block;
2194 bio->bi_end_io = btrfsic_bio_end_io;
2195 *bio_is_patched = 1;
2196 } else {
2197 struct btrfsic_block *chained_block =
2198 (struct btrfsic_block *)
2199 bio->bi_private;
2201 BUG_ON(NULL == chained_block);
2202 block->orig_bio_bh_private =
2203 chained_block->orig_bio_bh_private;
2204 block->orig_bio_bh_end_io.bio =
2205 chained_block->orig_bio_bh_end_io.bio;
2206 block->next_in_same_bio = chained_block;
2207 bio->bi_private = block;
2209 } else if (NULL != bh) {
2210 block->is_iodone = 0;
2211 block->orig_bio_bh_private = bh->b_private;
2212 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2213 block->next_in_same_bio = NULL;
2214 bh->b_private = block;
2215 bh->b_end_io = btrfsic_bh_end_io;
2216 } else {
2217 block->is_iodone = 1;
2218 block->orig_bio_bh_private = NULL;
2219 block->orig_bio_bh_end_io.bio = NULL;
2220 block->next_in_same_bio = NULL;
2222 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2223 printk(KERN_INFO
2224 "New written %c-block @%llu (%s/%llu/%d)\n",
2225 is_metadata ? 'M' : 'D',
2226 block->logical_bytenr, block->dev_state->name,
2227 block->dev_bytenr, block->mirror_num);
2228 list_add(&block->all_blocks_node, &state->all_blocks_list);
2229 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2231 if (is_metadata) {
2232 ret = btrfsic_process_metablock(state, block,
2233 &block_ctx, 0, 0);
2234 if (ret)
2235 printk(KERN_INFO
2236 "btrfsic: process_metablock(root @%llu)"
2237 " failed!\n",
2238 dev_bytenr);
2240 btrfsic_release_block_ctx(&block_ctx);
2243 continue_loop:
2244 BUG_ON(!processed_len);
2245 dev_bytenr += processed_len;
2246 mapped_datav += processed_len >> PAGE_CACHE_SHIFT;
2247 num_pages -= processed_len >> PAGE_CACHE_SHIFT;
2248 goto again;
2251 static void btrfsic_bio_end_io(struct bio *bp, int bio_error_status)
2253 struct btrfsic_block *block = (struct btrfsic_block *)bp->bi_private;
2254 int iodone_w_error;
2256 /* mutex is not held! This is not save if IO is not yet completed
2257 * on umount */
2258 iodone_w_error = 0;
2259 if (bio_error_status)
2260 iodone_w_error = 1;
2262 BUG_ON(NULL == block);
2263 bp->bi_private = block->orig_bio_bh_private;
2264 bp->bi_end_io = block->orig_bio_bh_end_io.bio;
2266 do {
2267 struct btrfsic_block *next_block;
2268 struct btrfsic_dev_state *const dev_state = block->dev_state;
2270 if ((dev_state->state->print_mask &
2271 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2272 printk(KERN_INFO
2273 "bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n",
2274 bio_error_status,
2275 btrfsic_get_block_type(dev_state->state, block),
2276 block->logical_bytenr, dev_state->name,
2277 block->dev_bytenr, block->mirror_num);
2278 next_block = block->next_in_same_bio;
2279 block->iodone_w_error = iodone_w_error;
2280 if (block->submit_bio_bh_rw & REQ_FLUSH) {
2281 dev_state->last_flush_gen++;
2282 if ((dev_state->state->print_mask &
2283 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2284 printk(KERN_INFO
2285 "bio_end_io() new %s flush_gen=%llu\n",
2286 dev_state->name,
2287 dev_state->last_flush_gen);
2289 if (block->submit_bio_bh_rw & REQ_FUA)
2290 block->flush_gen = 0; /* FUA completed means block is
2291 * on disk */
2292 block->is_iodone = 1; /* for FLUSH, this releases the block */
2293 block = next_block;
2294 } while (NULL != block);
2296 bp->bi_end_io(bp, bio_error_status);
2299 static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate)
2301 struct btrfsic_block *block = (struct btrfsic_block *)bh->b_private;
2302 int iodone_w_error = !uptodate;
2303 struct btrfsic_dev_state *dev_state;
2305 BUG_ON(NULL == block);
2306 dev_state = block->dev_state;
2307 if ((dev_state->state->print_mask & BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2308 printk(KERN_INFO
2309 "bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n",
2310 iodone_w_error,
2311 btrfsic_get_block_type(dev_state->state, block),
2312 block->logical_bytenr, block->dev_state->name,
2313 block->dev_bytenr, block->mirror_num);
2315 block->iodone_w_error = iodone_w_error;
2316 if (block->submit_bio_bh_rw & REQ_FLUSH) {
2317 dev_state->last_flush_gen++;
2318 if ((dev_state->state->print_mask &
2319 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2320 printk(KERN_INFO
2321 "bh_end_io() new %s flush_gen=%llu\n",
2322 dev_state->name, dev_state->last_flush_gen);
2324 if (block->submit_bio_bh_rw & REQ_FUA)
2325 block->flush_gen = 0; /* FUA completed means block is on disk */
2327 bh->b_private = block->orig_bio_bh_private;
2328 bh->b_end_io = block->orig_bio_bh_end_io.bh;
2329 block->is_iodone = 1; /* for FLUSH, this releases the block */
2330 bh->b_end_io(bh, uptodate);
2333 static int btrfsic_process_written_superblock(
2334 struct btrfsic_state *state,
2335 struct btrfsic_block *const superblock,
2336 struct btrfs_super_block *const super_hdr)
2338 int pass;
2340 superblock->generation = btrfs_super_generation(super_hdr);
2341 if (!(superblock->generation > state->max_superblock_generation ||
2342 0 == state->max_superblock_generation)) {
2343 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2344 printk(KERN_INFO
2345 "btrfsic: superblock @%llu (%s/%llu/%d)"
2346 " with old gen %llu <= %llu\n",
2347 superblock->logical_bytenr,
2348 superblock->dev_state->name,
2349 superblock->dev_bytenr, superblock->mirror_num,
2350 btrfs_super_generation(super_hdr),
2351 state->max_superblock_generation);
2352 } else {
2353 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2354 printk(KERN_INFO
2355 "btrfsic: got new superblock @%llu (%s/%llu/%d)"
2356 " with new gen %llu > %llu\n",
2357 superblock->logical_bytenr,
2358 superblock->dev_state->name,
2359 superblock->dev_bytenr, superblock->mirror_num,
2360 btrfs_super_generation(super_hdr),
2361 state->max_superblock_generation);
2363 state->max_superblock_generation =
2364 btrfs_super_generation(super_hdr);
2365 state->latest_superblock = superblock;
2368 for (pass = 0; pass < 3; pass++) {
2369 int ret;
2370 u64 next_bytenr;
2371 struct btrfsic_block *next_block;
2372 struct btrfsic_block_data_ctx tmp_next_block_ctx;
2373 struct btrfsic_block_link *l;
2374 int num_copies;
2375 int mirror_num;
2376 const char *additional_string = NULL;
2377 struct btrfs_disk_key tmp_disk_key = {0};
2379 btrfs_set_disk_key_objectid(&tmp_disk_key,
2380 BTRFS_ROOT_ITEM_KEY);
2381 btrfs_set_disk_key_objectid(&tmp_disk_key, 0);
2383 switch (pass) {
2384 case 0:
2385 btrfs_set_disk_key_objectid(&tmp_disk_key,
2386 BTRFS_ROOT_TREE_OBJECTID);
2387 additional_string = "root ";
2388 next_bytenr = btrfs_super_root(super_hdr);
2389 if (state->print_mask &
2390 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2391 printk(KERN_INFO "root@%llu\n", next_bytenr);
2392 break;
2393 case 1:
2394 btrfs_set_disk_key_objectid(&tmp_disk_key,
2395 BTRFS_CHUNK_TREE_OBJECTID);
2396 additional_string = "chunk ";
2397 next_bytenr = btrfs_super_chunk_root(super_hdr);
2398 if (state->print_mask &
2399 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2400 printk(KERN_INFO "chunk@%llu\n", next_bytenr);
2401 break;
2402 case 2:
2403 btrfs_set_disk_key_objectid(&tmp_disk_key,
2404 BTRFS_TREE_LOG_OBJECTID);
2405 additional_string = "log ";
2406 next_bytenr = btrfs_super_log_root(super_hdr);
2407 if (0 == next_bytenr)
2408 continue;
2409 if (state->print_mask &
2410 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2411 printk(KERN_INFO "log@%llu\n", next_bytenr);
2412 break;
2415 num_copies =
2416 btrfs_num_copies(state->root->fs_info,
2417 next_bytenr, BTRFS_SUPER_INFO_SIZE);
2418 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
2419 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
2420 next_bytenr, num_copies);
2421 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2422 int was_created;
2424 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2425 printk(KERN_INFO
2426 "btrfsic_process_written_superblock("
2427 "mirror_num=%d)\n", mirror_num);
2428 ret = btrfsic_map_block(state, next_bytenr,
2429 BTRFS_SUPER_INFO_SIZE,
2430 &tmp_next_block_ctx,
2431 mirror_num);
2432 if (ret) {
2433 printk(KERN_INFO
2434 "btrfsic: btrfsic_map_block(@%llu,"
2435 " mirror=%d) failed!\n",
2436 next_bytenr, mirror_num);
2437 return -1;
2440 next_block = btrfsic_block_lookup_or_add(
2441 state,
2442 &tmp_next_block_ctx,
2443 additional_string,
2444 1, 0, 1,
2445 mirror_num,
2446 &was_created);
2447 if (NULL == next_block) {
2448 printk(KERN_INFO
2449 "btrfsic: error, kmalloc failed!\n");
2450 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2451 return -1;
2454 next_block->disk_key = tmp_disk_key;
2455 if (was_created)
2456 next_block->generation =
2457 BTRFSIC_GENERATION_UNKNOWN;
2458 l = btrfsic_block_link_lookup_or_add(
2459 state,
2460 &tmp_next_block_ctx,
2461 next_block,
2462 superblock,
2463 BTRFSIC_GENERATION_UNKNOWN);
2464 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2465 if (NULL == l)
2466 return -1;
2470 if (WARN_ON(-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)))
2471 btrfsic_dump_tree(state);
2473 return 0;
2476 static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
2477 struct btrfsic_block *const block,
2478 int recursion_level)
2480 struct list_head *elem_ref_to;
2481 int ret = 0;
2483 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2485 * Note that this situation can happen and does not
2486 * indicate an error in regular cases. It happens
2487 * when disk blocks are freed and later reused.
2488 * The check-integrity module is not aware of any
2489 * block free operations, it just recognizes block
2490 * write operations. Therefore it keeps the linkage
2491 * information for a block until a block is
2492 * rewritten. This can temporarily cause incorrect
2493 * and even circular linkage informations. This
2494 * causes no harm unless such blocks are referenced
2495 * by the most recent super block.
2497 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2498 printk(KERN_INFO
2499 "btrfsic: abort cyclic linkage (case 1).\n");
2501 return ret;
2505 * This algorithm is recursive because the amount of used stack
2506 * space is very small and the max recursion depth is limited.
2508 list_for_each(elem_ref_to, &block->ref_to_list) {
2509 const struct btrfsic_block_link *const l =
2510 list_entry(elem_ref_to, struct btrfsic_block_link,
2511 node_ref_to);
2513 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2514 printk(KERN_INFO
2515 "rl=%d, %c @%llu (%s/%llu/%d)"
2516 " %u* refers to %c @%llu (%s/%llu/%d)\n",
2517 recursion_level,
2518 btrfsic_get_block_type(state, block),
2519 block->logical_bytenr, block->dev_state->name,
2520 block->dev_bytenr, block->mirror_num,
2521 l->ref_cnt,
2522 btrfsic_get_block_type(state, l->block_ref_to),
2523 l->block_ref_to->logical_bytenr,
2524 l->block_ref_to->dev_state->name,
2525 l->block_ref_to->dev_bytenr,
2526 l->block_ref_to->mirror_num);
2527 if (l->block_ref_to->never_written) {
2528 printk(KERN_INFO "btrfs: attempt to write superblock"
2529 " which references block %c @%llu (%s/%llu/%d)"
2530 " which is never written!\n",
2531 btrfsic_get_block_type(state, l->block_ref_to),
2532 l->block_ref_to->logical_bytenr,
2533 l->block_ref_to->dev_state->name,
2534 l->block_ref_to->dev_bytenr,
2535 l->block_ref_to->mirror_num);
2536 ret = -1;
2537 } else if (!l->block_ref_to->is_iodone) {
2538 printk(KERN_INFO "btrfs: attempt to write superblock"
2539 " which references block %c @%llu (%s/%llu/%d)"
2540 " which is not yet iodone!\n",
2541 btrfsic_get_block_type(state, l->block_ref_to),
2542 l->block_ref_to->logical_bytenr,
2543 l->block_ref_to->dev_state->name,
2544 l->block_ref_to->dev_bytenr,
2545 l->block_ref_to->mirror_num);
2546 ret = -1;
2547 } else if (l->block_ref_to->iodone_w_error) {
2548 printk(KERN_INFO "btrfs: attempt to write superblock"
2549 " which references block %c @%llu (%s/%llu/%d)"
2550 " which has write error!\n",
2551 btrfsic_get_block_type(state, l->block_ref_to),
2552 l->block_ref_to->logical_bytenr,
2553 l->block_ref_to->dev_state->name,
2554 l->block_ref_to->dev_bytenr,
2555 l->block_ref_to->mirror_num);
2556 ret = -1;
2557 } else if (l->parent_generation !=
2558 l->block_ref_to->generation &&
2559 BTRFSIC_GENERATION_UNKNOWN !=
2560 l->parent_generation &&
2561 BTRFSIC_GENERATION_UNKNOWN !=
2562 l->block_ref_to->generation) {
2563 printk(KERN_INFO "btrfs: attempt to write superblock"
2564 " which references block %c @%llu (%s/%llu/%d)"
2565 " with generation %llu !="
2566 " parent generation %llu!\n",
2567 btrfsic_get_block_type(state, l->block_ref_to),
2568 l->block_ref_to->logical_bytenr,
2569 l->block_ref_to->dev_state->name,
2570 l->block_ref_to->dev_bytenr,
2571 l->block_ref_to->mirror_num,
2572 l->block_ref_to->generation,
2573 l->parent_generation);
2574 ret = -1;
2575 } else if (l->block_ref_to->flush_gen >
2576 l->block_ref_to->dev_state->last_flush_gen) {
2577 printk(KERN_INFO "btrfs: attempt to write superblock"
2578 " which references block %c @%llu (%s/%llu/%d)"
2579 " which is not flushed out of disk's write cache"
2580 " (block flush_gen=%llu,"
2581 " dev->flush_gen=%llu)!\n",
2582 btrfsic_get_block_type(state, l->block_ref_to),
2583 l->block_ref_to->logical_bytenr,
2584 l->block_ref_to->dev_state->name,
2585 l->block_ref_to->dev_bytenr,
2586 l->block_ref_to->mirror_num, block->flush_gen,
2587 l->block_ref_to->dev_state->last_flush_gen);
2588 ret = -1;
2589 } else if (-1 == btrfsic_check_all_ref_blocks(state,
2590 l->block_ref_to,
2591 recursion_level +
2592 1)) {
2593 ret = -1;
2597 return ret;
2600 static int btrfsic_is_block_ref_by_superblock(
2601 const struct btrfsic_state *state,
2602 const struct btrfsic_block *block,
2603 int recursion_level)
2605 struct list_head *elem_ref_from;
2607 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2608 /* refer to comment at "abort cyclic linkage (case 1)" */
2609 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2610 printk(KERN_INFO
2611 "btrfsic: abort cyclic linkage (case 2).\n");
2613 return 0;
2617 * This algorithm is recursive because the amount of used stack space
2618 * is very small and the max recursion depth is limited.
2620 list_for_each(elem_ref_from, &block->ref_from_list) {
2621 const struct btrfsic_block_link *const l =
2622 list_entry(elem_ref_from, struct btrfsic_block_link,
2623 node_ref_from);
2625 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2626 printk(KERN_INFO
2627 "rl=%d, %c @%llu (%s/%llu/%d)"
2628 " is ref %u* from %c @%llu (%s/%llu/%d)\n",
2629 recursion_level,
2630 btrfsic_get_block_type(state, block),
2631 block->logical_bytenr, block->dev_state->name,
2632 block->dev_bytenr, block->mirror_num,
2633 l->ref_cnt,
2634 btrfsic_get_block_type(state, l->block_ref_from),
2635 l->block_ref_from->logical_bytenr,
2636 l->block_ref_from->dev_state->name,
2637 l->block_ref_from->dev_bytenr,
2638 l->block_ref_from->mirror_num);
2639 if (l->block_ref_from->is_superblock &&
2640 state->latest_superblock->dev_bytenr ==
2641 l->block_ref_from->dev_bytenr &&
2642 state->latest_superblock->dev_state->bdev ==
2643 l->block_ref_from->dev_state->bdev)
2644 return 1;
2645 else if (btrfsic_is_block_ref_by_superblock(state,
2646 l->block_ref_from,
2647 recursion_level +
2649 return 1;
2652 return 0;
2655 static void btrfsic_print_add_link(const struct btrfsic_state *state,
2656 const struct btrfsic_block_link *l)
2658 printk(KERN_INFO
2659 "Add %u* link from %c @%llu (%s/%llu/%d)"
2660 " to %c @%llu (%s/%llu/%d).\n",
2661 l->ref_cnt,
2662 btrfsic_get_block_type(state, l->block_ref_from),
2663 l->block_ref_from->logical_bytenr,
2664 l->block_ref_from->dev_state->name,
2665 l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
2666 btrfsic_get_block_type(state, l->block_ref_to),
2667 l->block_ref_to->logical_bytenr,
2668 l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
2669 l->block_ref_to->mirror_num);
2672 static void btrfsic_print_rem_link(const struct btrfsic_state *state,
2673 const struct btrfsic_block_link *l)
2675 printk(KERN_INFO
2676 "Rem %u* link from %c @%llu (%s/%llu/%d)"
2677 " to %c @%llu (%s/%llu/%d).\n",
2678 l->ref_cnt,
2679 btrfsic_get_block_type(state, l->block_ref_from),
2680 l->block_ref_from->logical_bytenr,
2681 l->block_ref_from->dev_state->name,
2682 l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
2683 btrfsic_get_block_type(state, l->block_ref_to),
2684 l->block_ref_to->logical_bytenr,
2685 l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
2686 l->block_ref_to->mirror_num);
2689 static char btrfsic_get_block_type(const struct btrfsic_state *state,
2690 const struct btrfsic_block *block)
2692 if (block->is_superblock &&
2693 state->latest_superblock->dev_bytenr == block->dev_bytenr &&
2694 state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
2695 return 'S';
2696 else if (block->is_superblock)
2697 return 's';
2698 else if (block->is_metadata)
2699 return 'M';
2700 else
2701 return 'D';
2704 static void btrfsic_dump_tree(const struct btrfsic_state *state)
2706 btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
2709 static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
2710 const struct btrfsic_block *block,
2711 int indent_level)
2713 struct list_head *elem_ref_to;
2714 int indent_add;
2715 static char buf[80];
2716 int cursor_position;
2719 * Should better fill an on-stack buffer with a complete line and
2720 * dump it at once when it is time to print a newline character.
2724 * This algorithm is recursive because the amount of used stack space
2725 * is very small and the max recursion depth is limited.
2727 indent_add = sprintf(buf, "%c-%llu(%s/%llu/%d)",
2728 btrfsic_get_block_type(state, block),
2729 block->logical_bytenr, block->dev_state->name,
2730 block->dev_bytenr, block->mirror_num);
2731 if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2732 printk("[...]\n");
2733 return;
2735 printk(buf);
2736 indent_level += indent_add;
2737 if (list_empty(&block->ref_to_list)) {
2738 printk("\n");
2739 return;
2741 if (block->mirror_num > 1 &&
2742 !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
2743 printk(" [...]\n");
2744 return;
2747 cursor_position = indent_level;
2748 list_for_each(elem_ref_to, &block->ref_to_list) {
2749 const struct btrfsic_block_link *const l =
2750 list_entry(elem_ref_to, struct btrfsic_block_link,
2751 node_ref_to);
2753 while (cursor_position < indent_level) {
2754 printk(" ");
2755 cursor_position++;
2757 if (l->ref_cnt > 1)
2758 indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
2759 else
2760 indent_add = sprintf(buf, " --> ");
2761 if (indent_level + indent_add >
2762 BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2763 printk("[...]\n");
2764 cursor_position = 0;
2765 continue;
2768 printk(buf);
2770 btrfsic_dump_tree_sub(state, l->block_ref_to,
2771 indent_level + indent_add);
2772 cursor_position = 0;
2776 static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
2777 struct btrfsic_state *state,
2778 struct btrfsic_block_data_ctx *next_block_ctx,
2779 struct btrfsic_block *next_block,
2780 struct btrfsic_block *from_block,
2781 u64 parent_generation)
2783 struct btrfsic_block_link *l;
2785 l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
2786 next_block_ctx->dev_bytenr,
2787 from_block->dev_state->bdev,
2788 from_block->dev_bytenr,
2789 &state->block_link_hashtable);
2790 if (NULL == l) {
2791 l = btrfsic_block_link_alloc();
2792 if (NULL == l) {
2793 printk(KERN_INFO
2794 "btrfsic: error, kmalloc" " failed!\n");
2795 return NULL;
2798 l->block_ref_to = next_block;
2799 l->block_ref_from = from_block;
2800 l->ref_cnt = 1;
2801 l->parent_generation = parent_generation;
2803 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2804 btrfsic_print_add_link(state, l);
2806 list_add(&l->node_ref_to, &from_block->ref_to_list);
2807 list_add(&l->node_ref_from, &next_block->ref_from_list);
2809 btrfsic_block_link_hashtable_add(l,
2810 &state->block_link_hashtable);
2811 } else {
2812 l->ref_cnt++;
2813 l->parent_generation = parent_generation;
2814 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2815 btrfsic_print_add_link(state, l);
2818 return l;
2821 static struct btrfsic_block *btrfsic_block_lookup_or_add(
2822 struct btrfsic_state *state,
2823 struct btrfsic_block_data_ctx *block_ctx,
2824 const char *additional_string,
2825 int is_metadata,
2826 int is_iodone,
2827 int never_written,
2828 int mirror_num,
2829 int *was_created)
2831 struct btrfsic_block *block;
2833 block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
2834 block_ctx->dev_bytenr,
2835 &state->block_hashtable);
2836 if (NULL == block) {
2837 struct btrfsic_dev_state *dev_state;
2839 block = btrfsic_block_alloc();
2840 if (NULL == block) {
2841 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
2842 return NULL;
2844 dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev);
2845 if (NULL == dev_state) {
2846 printk(KERN_INFO
2847 "btrfsic: error, lookup dev_state failed!\n");
2848 btrfsic_block_free(block);
2849 return NULL;
2851 block->dev_state = dev_state;
2852 block->dev_bytenr = block_ctx->dev_bytenr;
2853 block->logical_bytenr = block_ctx->start;
2854 block->is_metadata = is_metadata;
2855 block->is_iodone = is_iodone;
2856 block->never_written = never_written;
2857 block->mirror_num = mirror_num;
2858 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2859 printk(KERN_INFO
2860 "New %s%c-block @%llu (%s/%llu/%d)\n",
2861 additional_string,
2862 btrfsic_get_block_type(state, block),
2863 block->logical_bytenr, dev_state->name,
2864 block->dev_bytenr, mirror_num);
2865 list_add(&block->all_blocks_node, &state->all_blocks_list);
2866 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2867 if (NULL != was_created)
2868 *was_created = 1;
2869 } else {
2870 if (NULL != was_created)
2871 *was_created = 0;
2874 return block;
2877 static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
2878 u64 bytenr,
2879 struct btrfsic_dev_state *dev_state,
2880 u64 dev_bytenr)
2882 int num_copies;
2883 int mirror_num;
2884 int ret;
2885 struct btrfsic_block_data_ctx block_ctx;
2886 int match = 0;
2888 num_copies = btrfs_num_copies(state->root->fs_info,
2889 bytenr, state->metablock_size);
2891 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2892 ret = btrfsic_map_block(state, bytenr, state->metablock_size,
2893 &block_ctx, mirror_num);
2894 if (ret) {
2895 printk(KERN_INFO "btrfsic:"
2896 " btrfsic_map_block(logical @%llu,"
2897 " mirror %d) failed!\n",
2898 bytenr, mirror_num);
2899 continue;
2902 if (dev_state->bdev == block_ctx.dev->bdev &&
2903 dev_bytenr == block_ctx.dev_bytenr) {
2904 match++;
2905 btrfsic_release_block_ctx(&block_ctx);
2906 break;
2908 btrfsic_release_block_ctx(&block_ctx);
2911 if (WARN_ON(!match)) {
2912 printk(KERN_INFO "btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio,"
2913 " buffer->log_bytenr=%llu, submit_bio(bdev=%s,"
2914 " phys_bytenr=%llu)!\n",
2915 bytenr, dev_state->name, dev_bytenr);
2916 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2917 ret = btrfsic_map_block(state, bytenr,
2918 state->metablock_size,
2919 &block_ctx, mirror_num);
2920 if (ret)
2921 continue;
2923 printk(KERN_INFO "Read logical bytenr @%llu maps to"
2924 " (%s/%llu/%d)\n",
2925 bytenr, block_ctx.dev->name,
2926 block_ctx.dev_bytenr, mirror_num);
2931 static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
2932 struct block_device *bdev)
2934 struct btrfsic_dev_state *ds;
2936 ds = btrfsic_dev_state_hashtable_lookup(bdev,
2937 &btrfsic_dev_state_hashtable);
2938 return ds;
2941 int btrfsic_submit_bh(int rw, struct buffer_head *bh)
2943 struct btrfsic_dev_state *dev_state;
2945 if (!btrfsic_is_initialized)
2946 return submit_bh(rw, bh);
2948 mutex_lock(&btrfsic_mutex);
2949 /* since btrfsic_submit_bh() might also be called before
2950 * btrfsic_mount(), this might return NULL */
2951 dev_state = btrfsic_dev_state_lookup(bh->b_bdev);
2953 /* Only called to write the superblock (incl. FLUSH/FUA) */
2954 if (NULL != dev_state &&
2955 (rw & WRITE) && bh->b_size > 0) {
2956 u64 dev_bytenr;
2958 dev_bytenr = 4096 * bh->b_blocknr;
2959 if (dev_state->state->print_mask &
2960 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2961 printk(KERN_INFO
2962 "submit_bh(rw=0x%x, blocknr=%llu (bytenr %llu),"
2963 " size=%zu, data=%p, bdev=%p)\n",
2964 rw, (unsigned long long)bh->b_blocknr,
2965 dev_bytenr, bh->b_size, bh->b_data, bh->b_bdev);
2966 btrfsic_process_written_block(dev_state, dev_bytenr,
2967 &bh->b_data, 1, NULL,
2968 NULL, bh, rw);
2969 } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
2970 if (dev_state->state->print_mask &
2971 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2972 printk(KERN_INFO
2973 "submit_bh(rw=0x%x FLUSH, bdev=%p)\n",
2974 rw, bh->b_bdev);
2975 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
2976 if ((dev_state->state->print_mask &
2977 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
2978 BTRFSIC_PRINT_MASK_VERBOSE)))
2979 printk(KERN_INFO
2980 "btrfsic_submit_bh(%s) with FLUSH"
2981 " but dummy block already in use"
2982 " (ignored)!\n",
2983 dev_state->name);
2984 } else {
2985 struct btrfsic_block *const block =
2986 &dev_state->dummy_block_for_bio_bh_flush;
2988 block->is_iodone = 0;
2989 block->never_written = 0;
2990 block->iodone_w_error = 0;
2991 block->flush_gen = dev_state->last_flush_gen + 1;
2992 block->submit_bio_bh_rw = rw;
2993 block->orig_bio_bh_private = bh->b_private;
2994 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2995 block->next_in_same_bio = NULL;
2996 bh->b_private = block;
2997 bh->b_end_io = btrfsic_bh_end_io;
3000 mutex_unlock(&btrfsic_mutex);
3001 return submit_bh(rw, bh);
3004 static void __btrfsic_submit_bio(int rw, struct bio *bio)
3006 struct btrfsic_dev_state *dev_state;
3008 if (!btrfsic_is_initialized)
3009 return;
3011 mutex_lock(&btrfsic_mutex);
3012 /* since btrfsic_submit_bio() is also called before
3013 * btrfsic_mount(), this might return NULL */
3014 dev_state = btrfsic_dev_state_lookup(bio->bi_bdev);
3015 if (NULL != dev_state &&
3016 (rw & WRITE) && NULL != bio->bi_io_vec) {
3017 unsigned int i;
3018 u64 dev_bytenr;
3019 u64 cur_bytenr;
3020 int bio_is_patched;
3021 char **mapped_datav;
3023 dev_bytenr = 512 * bio->bi_iter.bi_sector;
3024 bio_is_patched = 0;
3025 if (dev_state->state->print_mask &
3026 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
3027 printk(KERN_INFO
3028 "submit_bio(rw=0x%x, bi_vcnt=%u,"
3029 " bi_sector=%llu (bytenr %llu), bi_bdev=%p)\n",
3030 rw, bio->bi_vcnt,
3031 (unsigned long long)bio->bi_iter.bi_sector,
3032 dev_bytenr, bio->bi_bdev);
3034 mapped_datav = kmalloc(sizeof(*mapped_datav) * bio->bi_vcnt,
3035 GFP_NOFS);
3036 if (!mapped_datav)
3037 goto leave;
3038 cur_bytenr = dev_bytenr;
3039 for (i = 0; i < bio->bi_vcnt; i++) {
3040 BUG_ON(bio->bi_io_vec[i].bv_len != PAGE_CACHE_SIZE);
3041 mapped_datav[i] = kmap(bio->bi_io_vec[i].bv_page);
3042 if (!mapped_datav[i]) {
3043 while (i > 0) {
3044 i--;
3045 kunmap(bio->bi_io_vec[i].bv_page);
3047 kfree(mapped_datav);
3048 goto leave;
3050 if (dev_state->state->print_mask &
3051 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE)
3052 printk(KERN_INFO
3053 "#%u: bytenr=%llu, len=%u, offset=%u\n",
3054 i, cur_bytenr, bio->bi_io_vec[i].bv_len,
3055 bio->bi_io_vec[i].bv_offset);
3056 cur_bytenr += bio->bi_io_vec[i].bv_len;
3058 btrfsic_process_written_block(dev_state, dev_bytenr,
3059 mapped_datav, bio->bi_vcnt,
3060 bio, &bio_is_patched,
3061 NULL, rw);
3062 while (i > 0) {
3063 i--;
3064 kunmap(bio->bi_io_vec[i].bv_page);
3066 kfree(mapped_datav);
3067 } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
3068 if (dev_state->state->print_mask &
3069 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
3070 printk(KERN_INFO
3071 "submit_bio(rw=0x%x FLUSH, bdev=%p)\n",
3072 rw, bio->bi_bdev);
3073 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
3074 if ((dev_state->state->print_mask &
3075 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
3076 BTRFSIC_PRINT_MASK_VERBOSE)))
3077 printk(KERN_INFO
3078 "btrfsic_submit_bio(%s) with FLUSH"
3079 " but dummy block already in use"
3080 " (ignored)!\n",
3081 dev_state->name);
3082 } else {
3083 struct btrfsic_block *const block =
3084 &dev_state->dummy_block_for_bio_bh_flush;
3086 block->is_iodone = 0;
3087 block->never_written = 0;
3088 block->iodone_w_error = 0;
3089 block->flush_gen = dev_state->last_flush_gen + 1;
3090 block->submit_bio_bh_rw = rw;
3091 block->orig_bio_bh_private = bio->bi_private;
3092 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
3093 block->next_in_same_bio = NULL;
3094 bio->bi_private = block;
3095 bio->bi_end_io = btrfsic_bio_end_io;
3098 leave:
3099 mutex_unlock(&btrfsic_mutex);
3102 void btrfsic_submit_bio(int rw, struct bio *bio)
3104 __btrfsic_submit_bio(rw, bio);
3105 submit_bio(rw, bio);
3108 int btrfsic_submit_bio_wait(int rw, struct bio *bio)
3110 __btrfsic_submit_bio(rw, bio);
3111 return submit_bio_wait(rw, bio);
3114 int btrfsic_mount(struct btrfs_root *root,
3115 struct btrfs_fs_devices *fs_devices,
3116 int including_extent_data, u32 print_mask)
3118 int ret;
3119 struct btrfsic_state *state;
3120 struct list_head *dev_head = &fs_devices->devices;
3121 struct btrfs_device *device;
3123 if (root->nodesize != root->leafsize) {
3124 printk(KERN_INFO
3125 "btrfsic: cannot handle nodesize %d != leafsize %d!\n",
3126 root->nodesize, root->leafsize);
3127 return -1;
3129 if (root->nodesize & ((u64)PAGE_CACHE_SIZE - 1)) {
3130 printk(KERN_INFO
3131 "btrfsic: cannot handle nodesize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3132 root->nodesize, PAGE_CACHE_SIZE);
3133 return -1;
3135 if (root->leafsize & ((u64)PAGE_CACHE_SIZE - 1)) {
3136 printk(KERN_INFO
3137 "btrfsic: cannot handle leafsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3138 root->leafsize, PAGE_CACHE_SIZE);
3139 return -1;
3141 if (root->sectorsize & ((u64)PAGE_CACHE_SIZE - 1)) {
3142 printk(KERN_INFO
3143 "btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3144 root->sectorsize, PAGE_CACHE_SIZE);
3145 return -1;
3147 state = kzalloc(sizeof(*state), GFP_NOFS);
3148 if (NULL == state) {
3149 printk(KERN_INFO "btrfs check-integrity: kmalloc() failed!\n");
3150 return -1;
3153 if (!btrfsic_is_initialized) {
3154 mutex_init(&btrfsic_mutex);
3155 btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
3156 btrfsic_is_initialized = 1;
3158 mutex_lock(&btrfsic_mutex);
3159 state->root = root;
3160 state->print_mask = print_mask;
3161 state->include_extent_data = including_extent_data;
3162 state->csum_size = 0;
3163 state->metablock_size = root->nodesize;
3164 state->datablock_size = root->sectorsize;
3165 INIT_LIST_HEAD(&state->all_blocks_list);
3166 btrfsic_block_hashtable_init(&state->block_hashtable);
3167 btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
3168 state->max_superblock_generation = 0;
3169 state->latest_superblock = NULL;
3171 list_for_each_entry(device, dev_head, dev_list) {
3172 struct btrfsic_dev_state *ds;
3173 char *p;
3175 if (!device->bdev || !device->name)
3176 continue;
3178 ds = btrfsic_dev_state_alloc();
3179 if (NULL == ds) {
3180 printk(KERN_INFO
3181 "btrfs check-integrity: kmalloc() failed!\n");
3182 mutex_unlock(&btrfsic_mutex);
3183 return -1;
3185 ds->bdev = device->bdev;
3186 ds->state = state;
3187 bdevname(ds->bdev, ds->name);
3188 ds->name[BDEVNAME_SIZE - 1] = '\0';
3189 for (p = ds->name; *p != '\0'; p++);
3190 while (p > ds->name && *p != '/')
3191 p--;
3192 if (*p == '/')
3193 p++;
3194 strlcpy(ds->name, p, sizeof(ds->name));
3195 btrfsic_dev_state_hashtable_add(ds,
3196 &btrfsic_dev_state_hashtable);
3199 ret = btrfsic_process_superblock(state, fs_devices);
3200 if (0 != ret) {
3201 mutex_unlock(&btrfsic_mutex);
3202 btrfsic_unmount(root, fs_devices);
3203 return ret;
3206 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
3207 btrfsic_dump_database(state);
3208 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
3209 btrfsic_dump_tree(state);
3211 mutex_unlock(&btrfsic_mutex);
3212 return 0;
3215 void btrfsic_unmount(struct btrfs_root *root,
3216 struct btrfs_fs_devices *fs_devices)
3218 struct list_head *elem_all;
3219 struct list_head *tmp_all;
3220 struct btrfsic_state *state;
3221 struct list_head *dev_head = &fs_devices->devices;
3222 struct btrfs_device *device;
3224 if (!btrfsic_is_initialized)
3225 return;
3227 mutex_lock(&btrfsic_mutex);
3229 state = NULL;
3230 list_for_each_entry(device, dev_head, dev_list) {
3231 struct btrfsic_dev_state *ds;
3233 if (!device->bdev || !device->name)
3234 continue;
3236 ds = btrfsic_dev_state_hashtable_lookup(
3237 device->bdev,
3238 &btrfsic_dev_state_hashtable);
3239 if (NULL != ds) {
3240 state = ds->state;
3241 btrfsic_dev_state_hashtable_remove(ds);
3242 btrfsic_dev_state_free(ds);
3246 if (NULL == state) {
3247 printk(KERN_INFO
3248 "btrfsic: error, cannot find state information"
3249 " on umount!\n");
3250 mutex_unlock(&btrfsic_mutex);
3251 return;
3255 * Don't care about keeping the lists' state up to date,
3256 * just free all memory that was allocated dynamically.
3257 * Free the blocks and the block_links.
3259 list_for_each_safe(elem_all, tmp_all, &state->all_blocks_list) {
3260 struct btrfsic_block *const b_all =
3261 list_entry(elem_all, struct btrfsic_block,
3262 all_blocks_node);
3263 struct list_head *elem_ref_to;
3264 struct list_head *tmp_ref_to;
3266 list_for_each_safe(elem_ref_to, tmp_ref_to,
3267 &b_all->ref_to_list) {
3268 struct btrfsic_block_link *const l =
3269 list_entry(elem_ref_to,
3270 struct btrfsic_block_link,
3271 node_ref_to);
3273 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
3274 btrfsic_print_rem_link(state, l);
3276 l->ref_cnt--;
3277 if (0 == l->ref_cnt)
3278 btrfsic_block_link_free(l);
3281 if (b_all->is_iodone || b_all->never_written)
3282 btrfsic_block_free(b_all);
3283 else
3284 printk(KERN_INFO "btrfs: attempt to free %c-block"
3285 " @%llu (%s/%llu/%d) on umount which is"
3286 " not yet iodone!\n",
3287 btrfsic_get_block_type(state, b_all),
3288 b_all->logical_bytenr, b_all->dev_state->name,
3289 b_all->dev_bytenr, b_all->mirror_num);
3292 mutex_unlock(&btrfsic_mutex);
3294 kfree(state);