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 "send-utils.h"
31 #include "btrfs-list.h"
33 static const char * const inspect_cmd_group_usage
[] = {
34 "btrfs inspect-internal <command> <args>",
38 static int __ino_to_path_fd(u64 inum
, int fd
, int verbose
, const char *prepend
)
42 struct btrfs_ioctl_ino_path_args ipa
;
43 struct btrfs_data_container
*fspath
;
45 fspath
= malloc(4096);
49 memset(fspath
, 0, sizeof(*fspath
));
52 ipa
.fspath
= (uintptr_t)fspath
;
54 ret
= ioctl(fd
, BTRFS_IOC_INO_PATHS
, &ipa
);
56 printf("ioctl ret=%d, error: %s\n", ret
, strerror(errno
));
61 printf("ioctl ret=%d, bytes_left=%lu, bytes_missing=%lu, "
62 "cnt=%d, missed=%d\n", ret
,
63 (unsigned long)fspath
->bytes_left
,
64 (unsigned long)fspath
->bytes_missing
,
65 fspath
->elem_cnt
, fspath
->elem_missed
);
67 for (i
= 0; i
< fspath
->elem_cnt
; ++i
) {
70 ptr
= (u64
)(unsigned long)fspath
->val
;
71 ptr
+= fspath
->val
[i
];
72 str
= (char *)(unsigned long)ptr
;
74 printf("%s/%s\n", prepend
, str
);
84 static const char * const cmd_inode_resolve_usage
[] = {
85 "btrfs inspect-internal inode-resolve [-v] <inode> <path>",
86 "Get file system paths for the given inode",
90 static int cmd_inode_resolve(int argc
, char **argv
)
95 DIR *dirstream
= NULL
;
99 int c
= getopt(argc
, argv
, "v");
108 usage(cmd_inode_resolve_usage
);
112 if (check_argc_exact(argc
- optind
, 2))
113 usage(cmd_inode_resolve_usage
);
115 fd
= open_file_or_dir(argv
[optind
+1], &dirstream
);
117 fprintf(stderr
, "ERROR: can't access '%s'\n", argv
[optind
+1]);
121 ret
= __ino_to_path_fd(atoll(argv
[optind
]), fd
, verbose
,
123 close_file_or_dir(fd
, dirstream
);
128 static const char * const cmd_logical_resolve_usage
[] = {
129 "btrfs inspect-internal logical-resolve [-Pv] [-s bufsize] <logical> <path>",
130 "Get file system paths for the given logical address",
131 "-P skip the path resolving and print the inodes instead",
133 "-s bufsize set inode container's size. This is used to increase inode",
134 " container's size in case it is not enough to read all the ",
135 " resolved results. The max value one can set is 64k",
139 static int cmd_logical_resolve(int argc
, char **argv
)
147 struct btrfs_ioctl_logical_ino_args loi
;
148 struct btrfs_data_container
*inodes
;
150 char full_path
[4096];
152 DIR *dirstream
= NULL
;
156 int c
= getopt(argc
, argv
, "Pvs:");
168 size
= atoll(optarg
);
171 usage(cmd_logical_resolve_usage
);
175 if (check_argc_exact(argc
- optind
, 2))
176 usage(cmd_logical_resolve_usage
);
178 size
= min(size
, (u64
)64 * 1024);
179 inodes
= malloc(size
);
183 memset(inodes
, 0, sizeof(*inodes
));
184 loi
.logical
= atoll(argv
[optind
]);
186 loi
.inodes
= (uintptr_t)inodes
;
188 fd
= open_file_or_dir(argv
[optind
+1], &dirstream
);
190 fprintf(stderr
, "ERROR: can't access '%s'\n", argv
[optind
+1]);
195 ret
= ioctl(fd
, BTRFS_IOC_LOGICAL_INO
, &loi
);
197 printf("ioctl ret=%d, error: %s\n", ret
, strerror(errno
));
202 printf("ioctl ret=%d, total_size=%llu, bytes_left=%lu, "
203 "bytes_missing=%lu, cnt=%d, missed=%d\n",
205 (unsigned long)inodes
->bytes_left
,
206 (unsigned long)inodes
->bytes_missing
,
207 inodes
->elem_cnt
, inodes
->elem_missed
);
209 bytes_left
= sizeof(full_path
);
210 ret
= snprintf(full_path
, bytes_left
, "%s/", argv
[optind
+1]);
211 path_ptr
= full_path
+ ret
;
212 bytes_left
-= ret
+ 1;
213 BUG_ON(bytes_left
< 0);
215 for (i
= 0; i
< inodes
->elem_cnt
; i
+= 3) {
216 u64 inum
= inodes
->val
[i
];
217 u64 offset
= inodes
->val
[i
+1];
218 u64 root
= inodes
->val
[i
+2];
224 name
= btrfs_list_path_for_root(fd
, root
);
234 ret
= snprintf(path_ptr
, bytes_left
, "%s",
236 BUG_ON(ret
>= bytes_left
);
238 path_fd
= open_file_or_dir(full_path
, &dirs
);
240 fprintf(stderr
, "ERROR: can't access "
241 "'%s'\n", full_path
);
245 __ino_to_path_fd(inum
, path_fd
, verbose
, full_path
);
247 close_file_or_dir(path_fd
, dirs
);
249 printf("inode %llu offset %llu root %llu\n", inum
,
255 close_file_or_dir(fd
, dirstream
);
260 static const char * const cmd_subvolid_resolve_usage
[] = {
261 "btrfs inspect-internal subvolid-resolve <subvolid> <path>",
262 "Get file system paths for the given subvolume ID.",
266 static int cmd_subvolid_resolve(int argc
, char **argv
)
271 char path
[BTRFS_PATH_NAME_MAX
+ 1];
272 DIR *dirstream
= NULL
;
274 if (check_argc_exact(argc
, 3))
275 usage(cmd_subvolid_resolve_usage
);
277 fd
= open_file_or_dir(argv
[2], &dirstream
);
279 fprintf(stderr
, "ERROR: can't access '%s'\n", argv
[2]);
284 subvol_id
= atoll(argv
[1]);
285 ret
= btrfs_subvolid_resolve(fd
, path
, sizeof(path
), subvol_id
);
289 "%s: btrfs_subvolid_resolve(subvol_id %llu) failed with ret=%d\n",
290 argv
[0], (unsigned long long)subvol_id
, ret
);
294 path
[BTRFS_PATH_NAME_MAX
] = '\0';
295 printf("%s\n", path
);
298 close_file_or_dir(fd
, dirstream
);
302 const struct cmd_group inspect_cmd_group
= {
303 inspect_cmd_group_usage
, NULL
, {
304 { "inode-resolve", cmd_inode_resolve
, cmd_inode_resolve_usage
,
306 { "logical-resolve", cmd_logical_resolve
,
307 cmd_logical_resolve_usage
, NULL
, 0 },
308 { "subvolid-resolve", cmd_subvolid_resolve
,
309 cmd_subvolid_resolve_usage
, NULL
, 0 },
314 int cmd_inspect(int argc
, char **argv
)
316 return handle_command_group(&inspect_cmd_group
, argc
, argv
);