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.
21 #include <sys/ioctl.h>
24 #include "kerncompat.h"
28 #include "btrfs-list.h"
30 static const char * const inspect_cmd_group_usage[] = {
31 "btrfs inspect-internal <command> <args>",
35 static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
39 struct btrfs_ioctl_ino_path_args ipa;
40 struct btrfs_data_container *fspath;
42 fspath = malloc(4096);
48 ipa.fspath = (uintptr_t)fspath;
50 ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
52 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
57 printf("ioctl ret=%d, bytes_left=%lu, bytes_missing=%lu, "
58 "cnt=%d, missed=%d\n", ret,
59 (unsigned long)fspath->bytes_left,
60 (unsigned long)fspath->bytes_missing,
61 fspath->elem_cnt, fspath->elem_missed);
63 for (i = 0; i < fspath->elem_cnt; ++i) {
64 char **str = (char **)fspath->val;
65 str[i] += (unsigned long)fspath->val;
67 printf("%s/%s\n", prepend, str[i]);
69 printf("%s\n", str[i]);
77 static const char * const cmd_inode_resolve_usage[] = {
78 "btrfs inspect-internal inode-resolve [-v] <inode> <path>",
79 "Get file system paths for the given inode",
83 static int cmd_inode_resolve(int argc, char **argv)
90 int c = getopt(argc, argv, "v");
99 usage(cmd_inode_resolve_usage);
103 if (check_argc_exact(argc - optind, 2))
104 usage(cmd_inode_resolve_usage);
106 fd = open_file_or_dir(argv[optind+1]);
108 fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
112 return __ino_to_path_fd(atoll(argv[optind]), fd, verbose,
116 static const char * const cmd_logical_resolve_usage[] = {
117 "btrfs inspect-internal logical-resolve [-Pv] [-s bufsize] <logical> <path>",
118 "Get file system paths for the given logical address",
119 "-P skip the path resolving and print the inodes instead",
121 "-s bufsize set inode container's size. This is used to increase inode",
122 " container's size in case it is not enough to read all the ",
123 " resolved results. The max value one can set is 64k",
127 static int cmd_logical_resolve(int argc, char **argv)
135 struct btrfs_ioctl_logical_ino_args loi;
136 struct btrfs_data_container *inodes;
138 char full_path[4096];
143 int c = getopt(argc, argv, "Pvs:");
155 size = atoll(optarg);
158 usage(cmd_logical_resolve_usage);
162 if (check_argc_exact(argc - optind, 2))
163 usage(cmd_logical_resolve_usage);
165 size = min(size, (u64)64 * 1024);
166 inodes = malloc(size);
170 loi.logical = atoll(argv[optind]);
172 loi.inodes = (uintptr_t)inodes;
174 fd = open_file_or_dir(argv[optind+1]);
176 fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
181 ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
183 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
188 printf("ioctl ret=%d, total_size=%llu, bytes_left=%lu, "
189 "bytes_missing=%lu, cnt=%d, missed=%d\n",
191 (unsigned long)inodes->bytes_left,
192 (unsigned long)inodes->bytes_missing,
193 inodes->elem_cnt, inodes->elem_missed);
195 bytes_left = sizeof(full_path);
196 ret = snprintf(full_path, bytes_left, "%s/", argv[optind+1]);
197 path_ptr = full_path + ret;
198 bytes_left -= ret + 1;
199 BUG_ON(bytes_left < 0);
201 for (i = 0; i < inodes->elem_cnt; i += 3) {
202 u64 inum = inodes->val[i];
203 u64 offset = inodes->val[i+1];
204 u64 root = inodes->val[i+2];
209 name = btrfs_list_path_for_root(fd, root);
211 return PTR_ERR(name);
217 ret = snprintf(path_ptr, bytes_left, "%s",
219 BUG_ON(ret >= bytes_left);
221 path_fd = open_file_or_dir(full_path);
223 fprintf(stderr, "ERROR: can't access "
224 "'%s'\n", full_path);
228 __ino_to_path_fd(inum, path_fd, verbose, full_path);
230 printf("inode %llu offset %llu root %llu\n", inum,
240 const struct cmd_group inspect_cmd_group = {
241 inspect_cmd_group_usage, NULL, {
242 { "inode-resolve", cmd_inode_resolve, cmd_inode_resolve_usage,
244 { "logical-resolve", cmd_logical_resolve,
245 cmd_logical_resolve_usage, NULL, 0 },
250 int cmd_inspect(int argc, char **argv)
252 return handle_command_group(&inspect_cmd_group, argc, argv);