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"
29 #include "btrfs-list.h"
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);
47 memset(fspath, 0, sizeof(*fspath));
50 ipa.fspath = (uintptr_t)fspath;
52 ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
54 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
59 printf("ioctl ret=%d, bytes_left=%lu, bytes_missing=%lu, "
60 "cnt=%d, missed=%d\n", ret,
61 (unsigned long)fspath->bytes_left,
62 (unsigned long)fspath->bytes_missing,
63 fspath->elem_cnt, fspath->elem_missed);
65 for (i = 0; i < fspath->elem_cnt; ++i) {
66 char **str = (char **)fspath->val;
67 str[i] += (unsigned long)fspath->val;
69 printf("%s/%s\n", prepend, str[i]);
71 printf("%s\n", str[i]);
79 static const char * const cmd_inode_resolve_usage[] = {
80 "btrfs inspect-internal inode-resolve [-v] <inode> <path>",
81 "Get file system paths for the given inode",
85 static int cmd_inode_resolve(int argc, char **argv)
93 int c = getopt(argc, argv, "v");
102 usage(cmd_inode_resolve_usage);
106 if (check_argc_exact(argc - optind, 2))
107 usage(cmd_inode_resolve_usage);
109 fd = open_file_or_dir(argv[optind+1]);
111 fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
115 ret = __ino_to_path_fd(atoll(argv[optind]), fd, verbose,
122 static const char * const cmd_logical_resolve_usage[] = {
123 "btrfs inspect-internal logical-resolve [-Pv] [-s bufsize] <logical> <path>",
124 "Get file system paths for the given logical address",
125 "-P skip the path resolving and print the inodes instead",
127 "-s bufsize set inode container's size. This is used to increase inode",
128 " container's size in case it is not enough to read all the ",
129 " resolved results. The max value one can set is 64k",
133 static int cmd_logical_resolve(int argc, char **argv)
141 struct btrfs_ioctl_logical_ino_args loi;
142 struct btrfs_data_container *inodes;
144 char full_path[4096];
149 int c = getopt(argc, argv, "Pvs:");
161 size = atoll(optarg);
164 usage(cmd_logical_resolve_usage);
168 if (check_argc_exact(argc - optind, 2))
169 usage(cmd_logical_resolve_usage);
171 size = min(size, (u64)64 * 1024);
172 inodes = malloc(size);
176 memset(inodes, 0, sizeof(*inodes));
177 loi.logical = atoll(argv[optind]);
179 loi.inodes = (uintptr_t)inodes;
181 fd = open_file_or_dir(argv[optind+1]);
183 fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
188 ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
190 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
195 printf("ioctl ret=%d, total_size=%llu, bytes_left=%lu, "
196 "bytes_missing=%lu, cnt=%d, missed=%d\n",
198 (unsigned long)inodes->bytes_left,
199 (unsigned long)inodes->bytes_missing,
200 inodes->elem_cnt, inodes->elem_missed);
202 bytes_left = sizeof(full_path);
203 ret = snprintf(full_path, bytes_left, "%s/", argv[optind+1]);
204 path_ptr = full_path + ret;
205 bytes_left -= ret + 1;
206 BUG_ON(bytes_left < 0);
208 for (i = 0; i < inodes->elem_cnt; i += 3) {
209 u64 inum = inodes->val[i];
210 u64 offset = inodes->val[i+1];
211 u64 root = inodes->val[i+2];
216 name = btrfs_list_path_for_root(fd, root);
226 ret = snprintf(path_ptr, bytes_left, "%s",
228 BUG_ON(ret >= bytes_left);
230 path_fd = open_file_or_dir(full_path);
232 fprintf(stderr, "ERROR: can't access "
233 "'%s'\n", full_path);
237 __ino_to_path_fd(inum, path_fd, verbose, full_path);
241 printf("inode %llu offset %llu root %llu\n", inum,
253 const struct cmd_group inspect_cmd_group = {
254 inspect_cmd_group_usage, NULL, {
255 { "inode-resolve", cmd_inode_resolve, cmd_inode_resolve_usage,
257 { "logical-resolve", cmd_logical_resolve,
258 cmd_logical_resolve_usage, NULL, 0 },
263 int cmd_inspect(int argc, char **argv)
265 return handle_command_group(&inspect_cmd_group, argc, argv);