Btrfs-progs: switch all existing commands to a new parser
[platform/upstream/btrfs-progs.git] / cmds-inspect.c
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.
5  *
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.
10  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <sys/ioctl.h>
21 #include <errno.h>
22
23 #include "kerncompat.h"
24 #include "ioctl.h"
25
26 #include "commands.h"
27
28 /* btrfs-list.c */
29 char *path_for_root(int fd, u64 root);
30
31 static const char inspect_cmd_group_usage[] =
32         "btrfs inspect-internal <command> <args>";
33
34 static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
35 {
36         int ret;
37         int i;
38         struct btrfs_ioctl_ino_path_args ipa;
39         struct btrfs_data_container *fspath;
40
41         fspath = malloc(4096);
42         if (!fspath)
43                 return 1;
44
45         ipa.inum = inum;
46         ipa.size = 4096;
47         ipa.fspath = (u64)fspath;
48
49         ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
50         if (ret) {
51                 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
52                 goto out;
53         }
54
55         if (verbose)
56                 printf("ioctl ret=%d, bytes_left=%lu, bytes_missing=%lu, "
57                         "cnt=%d, missed=%d\n", ret,
58                         (unsigned long)fspath->bytes_left,
59                         (unsigned long)fspath->bytes_missing,
60                         fspath->elem_cnt, fspath->elem_missed);
61
62         for (i = 0; i < fspath->elem_cnt; ++i) {
63                 char **str = (char **)fspath->val;
64                 str[i] += (unsigned long)fspath->val;
65                 if (prepend)
66                         printf("%s/%s\n", prepend, str[i]);
67                 else
68                         printf("%s\n", str[i]);
69         }
70
71 out:
72         free(fspath);
73         return ret;
74 }
75
76 static const char * const cmd_inode_resolve_usage[] = {
77         "btrfs inspect-internal inode-resolve [-v] <inode> <path>",
78         "Get file system paths for the given inode",
79         NULL
80 };
81
82 static int cmd_inode_resolve(int argc, char **argv)
83 {
84         int fd;
85         int verbose = 0;
86
87         optind = 1;
88         while (1) {
89                 int c = getopt(argc, argv, "v");
90                 if (c < 0)
91                         break;
92
93                 switch (c) {
94                 case 'v':
95                         verbose = 1;
96                         break;
97                 default:
98                         usage(cmd_inode_resolve_usage);
99                 }
100         }
101
102         if (check_argc_exact(argc - optind, 2))
103                 usage(cmd_inode_resolve_usage);
104
105         fd = open_file_or_dir(argv[optind+1]);
106         if (fd < 0) {
107                 fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
108                 return 12;
109         }
110
111         return __ino_to_path_fd(atoll(argv[optind]), fd, verbose,
112                                 argv[optind+1]);
113 }
114
115 static const char * const cmd_logical_resolve_usage[] = {
116         "btrfs inspect-internal logical-resolve [-Pv] <logical> <path>",
117         "Get file system paths for the given logical address",
118         NULL
119 };
120
121 static int cmd_logical_resolve(int argc, char **argv)
122 {
123         int ret;
124         int fd;
125         int i;
126         int verbose = 0;
127         int getpath = 1;
128         int bytes_left;
129         struct btrfs_ioctl_logical_ino_args loi;
130         struct btrfs_data_container *inodes;
131         char full_path[4096];
132         char *path_ptr;
133
134         optind = 1;
135         while (1) {
136                 int c = getopt(argc, argv, "Pv");
137                 if (c < 0)
138                         break;
139
140                 switch (c) {
141                 case 'P':
142                         getpath = 0;
143                         break;
144                 case 'v':
145                         verbose = 1;
146                         break;
147                 default:
148                         usage(cmd_logical_resolve_usage);
149                 }
150         }
151
152         if (check_argc_exact(argc - optind, 2))
153                 usage(cmd_logical_resolve_usage);
154
155         inodes = malloc(4096);
156         if (!inodes)
157                 return 1;
158
159         loi.logical = atoll(argv[optind]);
160         loi.size = 4096;
161         loi.inodes = (u64)inodes;
162
163         fd = open_file_or_dir(argv[optind+1]);
164         if (fd < 0) {
165                 fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
166                 ret = 12;
167                 goto out;
168         }
169
170         ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
171         if (ret) {
172                 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
173                 goto out;
174         }
175
176         if (verbose)
177                 printf("ioctl ret=%d, bytes_left=%lu, bytes_missing=%lu, "
178                         "cnt=%d, missed=%d\n", ret,
179                         (unsigned long)inodes->bytes_left,
180                         (unsigned long)inodes->bytes_missing,
181                         inodes->elem_cnt, inodes->elem_missed);
182
183         bytes_left = sizeof(full_path);
184         ret = snprintf(full_path, bytes_left, "%s/", argv[optind+1]);
185         path_ptr = full_path + ret;
186         bytes_left -= ret + 1;
187         BUG_ON(bytes_left < 0);
188
189         for (i = 0; i < inodes->elem_cnt; i += 3) {
190                 u64 inum = inodes->val[i];
191                 u64 offset = inodes->val[i+1];
192                 u64 root = inodes->val[i+2];
193                 int path_fd;
194                 char *name;
195
196                 if (getpath) {
197                         name = path_for_root(fd, root);
198                         if (IS_ERR(name))
199                                 return PTR_ERR(name);
200                         if (!name) {
201                                 path_ptr[-1] = '\0';
202                                 path_fd = fd;
203                         } else {
204                                 path_ptr[-1] = '/';
205                                 ret = snprintf(path_ptr, bytes_left, "%s",
206                                                 name);
207                                 BUG_ON(ret >= bytes_left);
208                                 free(name);
209                                 path_fd = open_file_or_dir(full_path);
210                                 if (path_fd < 0) {
211                                         fprintf(stderr, "ERROR: can't access "
212                                                 "'%s'\n", full_path);
213                                         goto out;
214                                 }
215                         }
216                         __ino_to_path_fd(inum, path_fd, verbose, full_path);
217                 } else {
218                         printf("inode %llu offset %llu root %llu\n", inum,
219                                 offset, root);
220                 }
221         }
222
223 out:
224         free(inodes);
225         return ret;
226 }
227
228 const struct cmd_group inspect_cmd_group = {
229         inspect_cmd_group_usage, NULL, {
230                 { "inode-resolve", cmd_inode_resolve, cmd_inode_resolve_usage,
231                         NULL, 0 },
232                 { "logical-resolve", cmd_logical_resolve,
233                         cmd_logical_resolve_usage, NULL, 0 },
234                 { 0, 0, 0, 0, 0 }
235         }
236 };
237
238 int cmd_inspect(int argc, char **argv)
239 {
240         return handle_command_group(&inspect_cmd_group, argc, argv);
241 }