2 * Copyright (C) 2009 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
24 #include "kerncompat.h"
28 #include "print-tree.h"
29 #include "transaction.h"
34 #define BUFFER_SIZE SZ_64K
36 /* we write the mirror info to stdout unless they are dumping the data
39 static FILE *info_file
;
41 static int map_one_extent(struct btrfs_fs_info
*fs_info
,
42 u64
*logical_ret
, u64
*len_ret
, int search_foward
)
44 struct btrfs_path
*path
;
51 logical
= *logical_ret
;
53 path
= btrfs_alloc_path();
57 key
.objectid
= logical
;
61 ret
= btrfs_search_slot(NULL
, fs_info
->extent_root
, &key
, path
,
69 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
70 if ((search_foward
&& key
.objectid
< logical
) ||
71 (!search_foward
&& key
.objectid
> logical
) ||
72 (key
.type
!= BTRFS_EXTENT_ITEM_KEY
&&
73 key
.type
!= BTRFS_METADATA_ITEM_KEY
)) {
75 ret
= btrfs_previous_extent_item(fs_info
->extent_root
,
78 ret
= btrfs_next_item(fs_info
->extent_root
, path
);
83 logical
= key
.objectid
;
84 if (key
.type
== BTRFS_METADATA_ITEM_KEY
)
85 len
= fs_info
->nodesize
;
90 btrfs_free_path(path
);
92 *logical_ret
= logical
;
99 static int __print_mapping_info(struct btrfs_fs_info
*fs_info
, u64 logical
,
100 u64 len
, int mirror_num
)
102 struct btrfs_multi_bio
*multi
= NULL
;
107 while (cur_offset
< len
) {
108 struct btrfs_device
*device
;
111 cur_len
= len
- cur_offset
;
112 ret
= btrfs_map_block(fs_info
, READ
, logical
+ cur_offset
,
113 &cur_len
, &multi
, mirror_num
, NULL
);
116 "Error: fails to map mirror%d logical %llu: %s\n",
117 mirror_num
, logical
, strerror(-ret
));
120 for (i
= 0; i
< multi
->num_stripes
; i
++) {
121 device
= multi
->stripes
[i
].dev
;
123 "mirror %d logical %Lu physical %Lu device %s\n",
124 mirror_num
, logical
+ cur_offset
,
125 multi
->stripes
[0].physical
,
130 cur_offset
+= cur_len
;
136 * Logical and len is the exact value of a extent.
137 * And offset is the offset inside the extent. It's only used for case
138 * where user only want to print part of the extent.
140 * Caller *MUST* ensure the range [logical,logical+len) are in one extent.
141 * Or we can encounter the following case, causing a -ENOENT error:
142 * |<-----given parameter------>|
143 * |<------ Extent A ----->|
145 static int print_mapping_info(struct btrfs_fs_info
*fs_info
, u64 logical
,
152 num_copies
= btrfs_num_copies(fs_info
, logical
, len
);
153 for (mirror_num
= 1; mirror_num
<= num_copies
; mirror_num
++) {
154 ret
= __print_mapping_info(fs_info
, logical
, len
, mirror_num
);
161 /* Same requisition as print_mapping_info function */
162 static int write_extent_content(struct btrfs_fs_info
*fs_info
, int out_fd
,
163 u64 logical
, u64 length
, int mirror
)
165 char buffer
[BUFFER_SIZE
];
170 while (cur_offset
< length
) {
171 cur_len
= min_t(u64
, length
- cur_offset
, BUFFER_SIZE
);
172 ret
= read_extent_data(fs_info
, buffer
,
173 logical
+ cur_offset
, &cur_len
, mirror
);
176 "Failed to read extent at [%llu, %llu]: %s\n",
177 logical
, logical
+ length
, strerror(-ret
));
180 ret
= write(out_fd
, buffer
, cur_len
);
181 if (ret
< 0 || ret
!= cur_len
) {
184 fprintf(stderr
, "output file write failed: %s\n",
188 cur_offset
+= cur_len
;
193 __attribute__((noreturn
))
194 static void print_usage(void)
196 printf("usage: btrfs-map-logical [options] device\n");
197 printf("\t-l Logical extent to map\n");
198 printf("\t-c Copy of the extent to read (usually 1 or 2)\n");
199 printf("\t-o Output file to hold the extent\n");
200 printf("\t-b Number of bytes to read\n");
204 int main(int argc
, char **argv
)
206 struct cache_tree root_cache
;
207 struct btrfs_root
*root
;
209 char *output_file
= NULL
;
221 static const struct option long_options
[] = {
222 /* { "byte-count", 1, NULL, 'b' }, */
223 { "logical", required_argument
, NULL
, 'l' },
224 { "copy", required_argument
, NULL
, 'c' },
225 { "output", required_argument
, NULL
, 'o' },
226 { "bytes", required_argument
, NULL
, 'b' },
230 c
= getopt_long(argc
, argv
, "l:c:o:b:", long_options
, NULL
);
235 logical
= arg_strtou64(optarg
);
238 copy
= arg_strtou64(optarg
);
241 bytes
= arg_strtou64(optarg
);
244 output_file
= strdup(optarg
);
251 if (check_argc_min(argc
- optind
, 1))
259 cache_tree_init(&root_cache
);
261 root
= open_ctree(dev
, 0, 0);
263 fprintf(stderr
, "Open ctree failed\n");
270 if (strcmp(output_file
, "-") == 0) {
274 out_fd
= open(output_file
, O_RDWR
| O_CREAT
, 0600);
277 ret
= ftruncate(out_fd
, 0);
288 bytes
= root
->fs_info
->nodesize
;
289 cur_logical
= logical
;
292 /* First find the nearest extent */
293 ret
= map_one_extent(root
->fs_info
, &cur_logical
, &cur_len
, 0);
295 fprintf(stderr
, "Failed to find extent at [%llu,%llu): %s\n",
296 cur_logical
, cur_logical
+ cur_len
, strerror(-ret
));
300 * Normally, search backward should be OK, but for special case like
301 * given logical is quite small where no extents are before it,
302 * we need to search forward.
305 ret
= map_one_extent(root
->fs_info
, &cur_logical
, &cur_len
, 1);
308 "Failed to find extent at [%llu,%llu): %s\n",
309 cur_logical
, cur_logical
+ cur_len
,
315 "Failed to find any extent at [%llu,%llu)\n",
316 cur_logical
, cur_logical
+ cur_len
);
321 while (cur_logical
+ cur_len
>= logical
&& cur_logical
< logical
+
327 ret
= map_one_extent(root
->fs_info
, &cur_logical
, &cur_len
, 1);
332 /* check again if there is overlap. */
333 if (cur_logical
+ cur_len
< logical
||
334 cur_logical
>= logical
+ bytes
)
337 real_logical
= max(logical
, cur_logical
);
338 real_len
= min(logical
+ bytes
, cur_logical
+ cur_len
) -
341 ret
= print_mapping_info(root
->fs_info
, real_logical
, real_len
);
344 if (output_file
&& out_fd
!= -1) {
345 ret
= write_extent_content(root
->fs_info
, out_fd
,
346 real_logical
, real_len
, copy
);
351 cur_logical
+= cur_len
;
355 fprintf(stderr
, "No extent found at range [%llu,%llu)\n",
356 logical
, logical
+ bytes
);
359 if (output_file
&& out_fd
!= 1)
366 btrfs_close_all_devices();