btrfs-progs: tests: add mkfs tests
[btrfs-progs-unstable/devel.git] / cmds-inspect.c
blob40ab49b85c0e83852f50f9750c216efa987f4113
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <stdint.h>
21 #include <sys/ioctl.h>
22 #include <errno.h>
23 #include <getopt.h>
24 #include <limits.h>
26 #include "kerncompat.h"
27 #include "ioctl.h"
28 #include "utils.h"
29 #include "ctree.h"
30 #include "send-utils.h"
31 #include "disk-io.h"
32 #include "commands.h"
33 #include "btrfs-list.h"
35 static const char * const inspect_cmd_group_usage[] = {
36 "btrfs inspect-internal <command> <args>",
37 NULL
40 static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
42 int ret;
43 int i;
44 struct btrfs_ioctl_ino_path_args ipa;
45 struct btrfs_data_container *fspath;
47 fspath = malloc(4096);
48 if (!fspath)
49 return -ENOMEM;
51 memset(fspath, 0, sizeof(*fspath));
52 ipa.inum = inum;
53 ipa.size = 4096;
54 ipa.fspath = ptr_to_u64(fspath);
56 ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
57 if (ret) {
58 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
59 goto out;
62 if (verbose)
63 printf("ioctl ret=%d, bytes_left=%lu, bytes_missing=%lu, "
64 "cnt=%d, missed=%d\n", ret,
65 (unsigned long)fspath->bytes_left,
66 (unsigned long)fspath->bytes_missing,
67 fspath->elem_cnt, fspath->elem_missed);
69 for (i = 0; i < fspath->elem_cnt; ++i) {
70 u64 ptr;
71 char *str;
72 ptr = (u64)(unsigned long)fspath->val;
73 ptr += fspath->val[i];
74 str = (char *)(unsigned long)ptr;
75 if (prepend)
76 printf("%s/%s\n", prepend, str);
77 else
78 printf("%s\n", str);
81 out:
82 free(fspath);
83 return !!ret;
86 static const char * const cmd_inspect_inode_resolve_usage[] = {
87 "btrfs inspect-internal inode-resolve [-v] <inode> <path>",
88 "Get file system paths for the given inode",
89 "",
90 "-v verbose mode",
91 NULL
94 static int cmd_inspect_inode_resolve(int argc, char **argv)
96 int fd;
97 int verbose = 0;
98 int ret;
99 DIR *dirstream = NULL;
101 optind = 1;
102 while (1) {
103 int c = getopt(argc, argv, "v");
104 if (c < 0)
105 break;
107 switch (c) {
108 case 'v':
109 verbose = 1;
110 break;
111 default:
112 usage(cmd_inspect_inode_resolve_usage);
116 if (check_argc_exact(argc - optind, 2))
117 usage(cmd_inspect_inode_resolve_usage);
119 fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
120 if (fd < 0)
121 return 1;
123 ret = __ino_to_path_fd(arg_strtou64(argv[optind]), fd, verbose,
124 argv[optind+1]);
125 close_file_or_dir(fd, dirstream);
126 return !!ret;
130 static const char * const cmd_inspect_logical_resolve_usage[] = {
131 "btrfs inspect-internal logical-resolve [-Pv] [-s bufsize] <logical> <path>",
132 "Get file system paths for the given logical address",
133 "-P skip the path resolving and print the inodes instead",
134 "-v verbose mode",
135 "-s bufsize set inode container's size. This is used to increase inode",
136 " container's size in case it is not enough to read all the ",
137 " resolved results. The max value one can set is 64k",
138 NULL
141 static int cmd_inspect_logical_resolve(int argc, char **argv)
143 int ret;
144 int fd;
145 int i;
146 int verbose = 0;
147 int getpath = 1;
148 int bytes_left;
149 struct btrfs_ioctl_logical_ino_args loi;
150 struct btrfs_data_container *inodes;
151 u64 size = 4096;
152 char full_path[4096];
153 char *path_ptr;
154 DIR *dirstream = NULL;
156 optind = 1;
157 while (1) {
158 int c = getopt(argc, argv, "Pvs:");
159 if (c < 0)
160 break;
162 switch (c) {
163 case 'P':
164 getpath = 0;
165 break;
166 case 'v':
167 verbose = 1;
168 break;
169 case 's':
170 size = arg_strtou64(optarg);
171 break;
172 default:
173 usage(cmd_inspect_logical_resolve_usage);
177 if (check_argc_exact(argc - optind, 2))
178 usage(cmd_inspect_logical_resolve_usage);
180 size = min(size, (u64)64 * 1024);
181 inodes = malloc(size);
182 if (!inodes)
183 return 1;
185 memset(inodes, 0, sizeof(*inodes));
186 loi.logical = arg_strtou64(argv[optind]);
187 loi.size = size;
188 loi.inodes = ptr_to_u64(inodes);
190 fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
191 if (fd < 0) {
192 ret = 12;
193 goto out;
196 ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
197 if (ret) {
198 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
199 goto out;
202 if (verbose)
203 printf("ioctl ret=%d, total_size=%llu, bytes_left=%lu, "
204 "bytes_missing=%lu, cnt=%d, missed=%d\n",
205 ret, size,
206 (unsigned long)inodes->bytes_left,
207 (unsigned long)inodes->bytes_missing,
208 inodes->elem_cnt, inodes->elem_missed);
210 bytes_left = sizeof(full_path);
211 ret = snprintf(full_path, bytes_left, "%s/", argv[optind+1]);
212 path_ptr = full_path + ret;
213 bytes_left -= ret + 1;
214 BUG_ON(bytes_left < 0);
216 for (i = 0; i < inodes->elem_cnt; i += 3) {
217 u64 inum = inodes->val[i];
218 u64 offset = inodes->val[i+1];
219 u64 root = inodes->val[i+2];
220 int path_fd;
221 char *name;
222 DIR *dirs = NULL;
224 if (getpath) {
225 name = btrfs_list_path_for_root(fd, root);
226 if (IS_ERR(name)) {
227 ret = PTR_ERR(name);
228 goto out;
230 if (!name) {
231 path_ptr[-1] = '\0';
232 path_fd = fd;
233 } else {
234 path_ptr[-1] = '/';
235 ret = snprintf(path_ptr, bytes_left, "%s",
236 name);
237 BUG_ON(ret >= bytes_left);
238 free(name);
239 path_fd = btrfs_open_dir(full_path, &dirs, 1);
240 if (path_fd < 0) {
241 ret = -ENOENT;
242 goto out;
245 __ino_to_path_fd(inum, path_fd, verbose, full_path);
246 if (path_fd != fd)
247 close_file_or_dir(path_fd, dirs);
248 } else {
249 printf("inode %llu offset %llu root %llu\n", inum,
250 offset, root);
254 out:
255 close_file_or_dir(fd, dirstream);
256 free(inodes);
257 return !!ret;
260 static const char * const cmd_inspect_subvolid_resolve_usage[] = {
261 "btrfs inspect-internal subvolid-resolve <subvolid> <path>",
262 "Get file system paths for the given subvolume ID.",
263 NULL
266 static int cmd_inspect_subvolid_resolve(int argc, char **argv)
268 int ret;
269 int fd = -1;
270 u64 subvol_id;
271 char path[PATH_MAX];
272 DIR *dirstream = NULL;
274 if (check_argc_exact(argc, 3))
275 usage(cmd_inspect_subvolid_resolve_usage);
277 fd = btrfs_open_dir(argv[2], &dirstream, 1);
278 if (fd < 0) {
279 ret = -ENOENT;
280 goto out;
283 subvol_id = arg_strtou64(argv[1]);
284 ret = btrfs_subvolid_resolve(fd, path, sizeof(path), subvol_id);
286 if (ret) {
287 fprintf(stderr,
288 "%s: btrfs_subvolid_resolve(subvol_id %llu) failed with ret=%d\n",
289 argv[0], (unsigned long long)subvol_id, ret);
290 goto out;
293 path[PATH_MAX - 1] = '\0';
294 printf("%s\n", path);
296 out:
297 close_file_or_dir(fd, dirstream);
298 return ret ? 1 : 0;
301 static const char* const cmd_inspect_rootid_usage[] = {
302 "btrfs inspect-internal rootid <path>",
303 "Get tree ID of the containing subvolume of path.",
304 NULL
307 static int cmd_inspect_rootid(int argc, char **argv)
309 int ret;
310 int fd = -1;
311 u64 rootid;
312 DIR *dirstream = NULL;
314 if (check_argc_exact(argc, 2))
315 usage(cmd_inspect_rootid_usage);
317 fd = btrfs_open_dir(argv[1], &dirstream, 1);
318 if (fd < 0) {
319 ret = -ENOENT;
320 goto out;
323 ret = lookup_ino_rootid(fd, &rootid);
324 if (ret) {
325 fprintf(stderr, "%s: rootid failed with ret=%d\n",
326 argv[0], ret);
327 goto out;
330 printf("%llu\n", (unsigned long long)rootid);
331 out:
332 close_file_or_dir(fd, dirstream);
334 return !!ret;
337 struct dev_extent_elem {
338 u64 start;
339 /* inclusive end */
340 u64 end;
341 struct list_head list;
344 static int add_dev_extent(struct list_head *list,
345 const u64 start, const u64 end,
346 const int append)
348 struct dev_extent_elem *e;
350 e = malloc(sizeof(*e));
351 if (!e)
352 return -ENOMEM;
354 e->start = start;
355 e->end = end;
357 if (append)
358 list_add_tail(&e->list, list);
359 else
360 list_add(&e->list, list);
362 return 0;
365 static void free_dev_extent_list(struct list_head *list)
367 while (!list_empty(list)) {
368 struct dev_extent_elem *e;
370 e = list_first_entry(list, struct dev_extent_elem, list);
371 list_del(&e->list);
372 free(e);
376 static int hole_includes_sb_mirror(const u64 start, const u64 end)
378 int i;
379 int ret = 0;
381 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
382 u64 bytenr = btrfs_sb_offset(i);
384 if (bytenr >= start && bytenr <= end) {
385 ret = 1;
386 break;
390 return ret;
393 static void adjust_dev_min_size(struct list_head *extents,
394 struct list_head *holes,
395 u64 *min_size)
398 * If relocation of the block group of a device extent must happen (see
399 * below) scratch space is used for the relocation. So track here the
400 * size of the largest device extent that has to be relocated. We track
401 * only the largest and not the sum of the sizes of all relocated block
402 * groups because after each block group is relocated the running
403 * transaction is committed so that pinned space is released.
405 u64 scratch_space = 0;
408 * List of device extents is sorted by descending order of the extent's
409 * end offset. If some extent goes beyond the computed minimum size,
410 * which initially matches the sum of the lenghts of all extents,
411 * we need to check if the extent can be relocated to an hole in the
412 * device between [0, *min_size[ (which is what the resize ioctl does).
414 while (!list_empty(extents)) {
415 struct dev_extent_elem *e;
416 struct dev_extent_elem *h;
417 int found = 0;
418 u64 extent_len;
419 u64 hole_len = 0;
421 e = list_first_entry(extents, struct dev_extent_elem, list);
422 if (e->end <= *min_size)
423 break;
426 * Our extent goes beyond the computed *min_size. See if we can
427 * find a hole large enough to relocate it to. If not we must stop
428 * and set *min_size to the end of the extent.
430 extent_len = e->end - e->start + 1;
431 list_for_each_entry(h, holes, list) {
432 hole_len = h->end - h->start + 1;
433 if (hole_len >= extent_len) {
434 found = 1;
435 break;
439 if (!found) {
440 *min_size = e->end + 1;
441 break;
445 * If the hole found contains the location for a superblock
446 * mirror, we are pessimistic and require allocating one
447 * more extent of the same size. This is because the block
448 * group could be in the worst case used by a single extent
449 * with a size >= (block_group.length - superblock.size).
451 if (hole_includes_sb_mirror(h->start,
452 h->start + extent_len - 1))
453 *min_size += extent_len;
455 if (hole_len > extent_len) {
456 h->start += extent_len;
457 } else {
458 list_del(&h->list);
459 free(h);
462 list_del(&e->list);
463 free(e);
465 if (extent_len > scratch_space)
466 scratch_space = extent_len;
469 if (scratch_space) {
470 *min_size += scratch_space;
472 * Chunk allocation requires inserting/updating items in the
473 * chunk tree, so often this can lead to the need of allocating
474 * a new system chunk too, which has a maximum size of 32Mb.
476 *min_size += 32 * 1024 * 1024;
480 static int print_min_dev_size(int fd, u64 devid)
482 int ret = 1;
484 * Device allocations starts at 1Mb or at the value passed through the
485 * mount option alloc_start if it's bigger than 1Mb. The alloc_start
486 * option is used for debugging and testing only, and recently the
487 * possibility of deprecating/removing it has been discussed, so we
488 * ignore it here.
490 u64 min_size = 1 * 1024 * 1024ull;
491 struct btrfs_ioctl_search_args args;
492 struct btrfs_ioctl_search_key *sk = &args.key;
493 u64 last_pos = (u64)-1;
494 LIST_HEAD(extents);
495 LIST_HEAD(holes);
497 memset(&args, 0, sizeof(args));
498 sk->tree_id = BTRFS_DEV_TREE_OBJECTID;
499 sk->min_objectid = devid;
500 sk->max_objectid = devid;
501 sk->max_type = BTRFS_DEV_EXTENT_KEY;
502 sk->min_type = BTRFS_DEV_EXTENT_KEY;
503 sk->min_offset = 0;
504 sk->max_offset = (u64)-1;
505 sk->min_transid = 0;
506 sk->max_transid = (u64)-1;
507 sk->nr_items = 4096;
509 while (1) {
510 int i;
511 struct btrfs_ioctl_search_header *sh;
512 unsigned long off = 0;
514 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
515 if (ret < 0) {
516 fprintf(stderr,
517 "Error invoking tree search ioctl: %s\n",
518 strerror(errno));
519 ret = 1;
520 goto out;
523 if (sk->nr_items == 0)
524 break;
526 for (i = 0; i < sk->nr_items; i++) {
527 struct btrfs_dev_extent *extent;
528 u64 len;
530 sh = (struct btrfs_ioctl_search_header *)(args.buf +
531 off);
532 off += sizeof(*sh);
533 extent = (struct btrfs_dev_extent *)(args.buf + off);
534 off += sh->len;
536 sk->min_objectid = sh->objectid;
537 sk->min_type = sh->type;
538 sk->min_offset = sh->offset + 1;
540 if (sh->objectid != devid ||
541 sh->type != BTRFS_DEV_EXTENT_KEY)
542 continue;
544 len = btrfs_stack_dev_extent_length(extent);
545 min_size += len;
546 ret = add_dev_extent(&extents, sh->offset,
547 sh->offset + len - 1, 0);
549 if (!ret && last_pos != (u64)-1 &&
550 last_pos != sh->offset)
551 ret = add_dev_extent(&holes, last_pos,
552 sh->offset - 1, 1);
553 if (ret) {
554 fprintf(stderr, "Error: %s\n", strerror(-ret));
555 ret = 1;
556 goto out;
559 last_pos = sh->offset + len;
562 if (sk->min_type != BTRFS_DEV_EXTENT_KEY ||
563 sk->min_objectid != devid)
564 break;
567 adjust_dev_min_size(&extents, &holes, &min_size);
568 printf("%llu bytes (%s)\n", min_size, pretty_size(min_size));
569 ret = 0;
570 out:
571 free_dev_extent_list(&extents);
572 free_dev_extent_list(&holes);
574 return ret;
577 static const char* const cmd_inspect_min_dev_size_usage[] = {
578 "btrfs inspect-internal min-dev-size [options] <path>",
579 "Get the minimum size the device can be shrunk to. The",
580 "device id 1 is used by default.",
581 "--id DEVID specify the device id to query",
582 NULL
585 static int cmd_inspect_min_dev_size(int argc, char **argv)
587 int ret;
588 int fd = -1;
589 DIR *dirstream = NULL;
590 u64 devid = 1;
592 while (1) {
593 int c;
594 enum { GETOPT_VAL_DEVID = 256 };
595 static const struct option long_options[] = {
596 { "id", required_argument, NULL, GETOPT_VAL_DEVID },
597 {NULL, 0, NULL, 0}
600 c = getopt_long(argc, argv, "", long_options, NULL);
601 if (c < 0)
602 break;
604 switch (c) {
605 case GETOPT_VAL_DEVID:
606 devid = arg_strtou64(optarg);
607 break;
608 default:
609 usage(cmd_inspect_min_dev_size_usage);
612 if (check_argc_exact(argc - optind, 1))
613 usage(cmd_inspect_min_dev_size_usage);
615 fd = btrfs_open_dir(argv[optind], &dirstream, 1);
616 if (fd < 0) {
617 ret = -ENOENT;
618 goto out;
621 ret = print_min_dev_size(fd, devid);
622 close_file_or_dir(fd, dirstream);
623 out:
624 return !!ret;
627 static const char inspect_cmd_group_info[] =
628 "query various internal information";
630 const struct cmd_group inspect_cmd_group = {
631 inspect_cmd_group_usage, inspect_cmd_group_info, {
632 { "inode-resolve", cmd_inspect_inode_resolve,
633 cmd_inspect_inode_resolve_usage, NULL, 0 },
634 { "logical-resolve", cmd_inspect_logical_resolve,
635 cmd_inspect_logical_resolve_usage, NULL, 0 },
636 { "subvolid-resolve", cmd_inspect_subvolid_resolve,
637 cmd_inspect_subvolid_resolve_usage, NULL, 0 },
638 { "rootid", cmd_inspect_rootid, cmd_inspect_rootid_usage, NULL,
639 0 },
640 { "min-dev-size", cmd_inspect_min_dev_size,
641 cmd_inspect_min_dev_size_usage, NULL, 0 },
642 NULL_CMD_STRUCT
646 int cmd_inspect(int argc, char **argv)
648 return handle_command_group(&inspect_cmd_group, argc, argv);