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.
20 #include <sys/ioctl.h>
23 #include "kerncompat.h"
29 char *path_for_root(int fd, u64 root);
31 static const char * const inspect_cmd_group_usage[] = {
32 "btrfs inspect-internal <command> <args>",
36 static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
40 struct btrfs_ioctl_ino_path_args ipa;
41 struct btrfs_data_container *fspath;
43 fspath = malloc(4096);
49 ipa.fspath = (u64)fspath;
51 ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
53 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
58 printf("ioctl ret=%d, bytes_left=%lu, bytes_missing=%lu, "
59 "cnt=%d, missed=%d\n", ret,
60 (unsigned long)fspath->bytes_left,
61 (unsigned long)fspath->bytes_missing,
62 fspath->elem_cnt, fspath->elem_missed);
64 for (i = 0; i < fspath->elem_cnt; ++i) {
65 char **str = (char **)fspath->val;
66 str[i] += (unsigned long)fspath->val;
68 printf("%s/%s\n", prepend, str[i]);
70 printf("%s\n", str[i]);
78 static const char * const cmd_inode_resolve_usage[] = {
79 "btrfs inspect-internal inode-resolve [-v] <inode> <path>",
80 "Get file system paths for the given inode",
84 static int cmd_inode_resolve(int argc, char **argv)
91 int c = getopt(argc, argv, "v");
100 usage(cmd_inode_resolve_usage);
104 if (check_argc_exact(argc - optind, 2))
105 usage(cmd_inode_resolve_usage);
107 fd = open_file_or_dir(argv[optind+1]);
109 fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
113 return __ino_to_path_fd(atoll(argv[optind]), fd, verbose,
117 static const char * const cmd_logical_resolve_usage[] = {
118 "btrfs inspect-internal logical-resolve [-Pv] <logical> <path>",
119 "Get file system paths for the given logical address",
123 static int cmd_logical_resolve(int argc, char **argv)
131 struct btrfs_ioctl_logical_ino_args loi;
132 struct btrfs_data_container *inodes;
133 char full_path[4096];
138 int c = getopt(argc, argv, "Pv");
150 usage(cmd_logical_resolve_usage);
154 if (check_argc_exact(argc - optind, 2))
155 usage(cmd_logical_resolve_usage);
157 inodes = malloc(4096);
161 loi.logical = atoll(argv[optind]);
163 loi.inodes = (u64)inodes;
165 fd = open_file_or_dir(argv[optind+1]);
167 fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
172 ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
174 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
179 printf("ioctl ret=%d, bytes_left=%lu, bytes_missing=%lu, "
180 "cnt=%d, missed=%d\n", ret,
181 (unsigned long)inodes->bytes_left,
182 (unsigned long)inodes->bytes_missing,
183 inodes->elem_cnt, inodes->elem_missed);
185 bytes_left = sizeof(full_path);
186 ret = snprintf(full_path, bytes_left, "%s/", argv[optind+1]);
187 path_ptr = full_path + ret;
188 bytes_left -= ret + 1;
189 BUG_ON(bytes_left < 0);
191 for (i = 0; i < inodes->elem_cnt; i += 3) {
192 u64 inum = inodes->val[i];
193 u64 offset = inodes->val[i+1];
194 u64 root = inodes->val[i+2];
199 name = path_for_root(fd, root);
201 return PTR_ERR(name);
207 ret = snprintf(path_ptr, bytes_left, "%s",
209 BUG_ON(ret >= bytes_left);
211 path_fd = open_file_or_dir(full_path);
213 fprintf(stderr, "ERROR: can't access "
214 "'%s'\n", full_path);
218 __ino_to_path_fd(inum, path_fd, verbose, full_path);
220 printf("inode %llu offset %llu root %llu\n", inum,
230 const struct cmd_group inspect_cmd_group = {
231 inspect_cmd_group_usage, NULL, {
232 { "inode-resolve", cmd_inode_resolve, cmd_inode_resolve_usage,
234 { "logical-resolve", cmd_logical_resolve,
235 cmd_logical_resolve_usage, NULL, 0 },
240 int cmd_inspect(int argc, char **argv)
242 return handle_command_group(&inspect_cmd_group, argc, argv);