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