Cast args to min to fix compiler warnings
[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 #include "btrfs-list.h"
28
29 static const char * const inspect_cmd_group_usage[] = {
30         "btrfs inspect-internal <command> <args>",
31         NULL
32 };
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] [-s bufsize] <logical> <path>",
117         "Get file system paths for the given logical address",
118         "-P          skip the path resolving and print the inodes instead",
119         "-v          verbose mode",
120         "-s bufsize  set inode container's size. This is used to increase inode",
121         "            container's size in case it is not enough to read all the ",
122         "            resolved results. The max value one can set is 64k",
123         NULL
124 };
125
126 static int cmd_logical_resolve(int argc, char **argv)
127 {
128         int ret;
129         int fd;
130         int i;
131         int verbose = 0;
132         int getpath = 1;
133         int bytes_left;
134         struct btrfs_ioctl_logical_ino_args loi;
135         struct btrfs_data_container *inodes;
136         u64 size = 4096;
137         char full_path[4096];
138         char *path_ptr;
139
140         optind = 1;
141         while (1) {
142                 int c = getopt(argc, argv, "Pvs:");
143                 if (c < 0)
144                         break;
145
146                 switch (c) {
147                 case 'P':
148                         getpath = 0;
149                         break;
150                 case 'v':
151                         verbose = 1;
152                         break;
153                 case 's':
154                         size = atoll(optarg);
155                         break;
156                 default:
157                         usage(cmd_logical_resolve_usage);
158                 }
159         }
160
161         if (check_argc_exact(argc - optind, 2))
162                 usage(cmd_logical_resolve_usage);
163
164         size = min(size, (u64)64 * 1024);
165         inodes = malloc(size);
166         if (!inodes)
167                 return 1;
168
169         loi.logical = atoll(argv[optind]);
170         loi.size = size;
171         loi.inodes = (u64)inodes;
172
173         fd = open_file_or_dir(argv[optind+1]);
174         if (fd < 0) {
175                 fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
176                 ret = 12;
177                 goto out;
178         }
179
180         ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
181         if (ret) {
182                 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
183                 goto out;
184         }
185
186         if (verbose)
187                 printf("ioctl ret=%d, total_size=%llu, bytes_left=%lu, "
188                         "bytes_missing=%lu, cnt=%d, missed=%d\n",
189                         ret, size,
190                         (unsigned long)inodes->bytes_left,
191                         (unsigned long)inodes->bytes_missing,
192                         inodes->elem_cnt, inodes->elem_missed);
193
194         bytes_left = sizeof(full_path);
195         ret = snprintf(full_path, bytes_left, "%s/", argv[optind+1]);
196         path_ptr = full_path + ret;
197         bytes_left -= ret + 1;
198         BUG_ON(bytes_left < 0);
199
200         for (i = 0; i < inodes->elem_cnt; i += 3) {
201                 u64 inum = inodes->val[i];
202                 u64 offset = inodes->val[i+1];
203                 u64 root = inodes->val[i+2];
204                 int path_fd;
205                 char *name;
206
207                 if (getpath) {
208                         name = btrfs_list_path_for_root(fd, root);
209                         if (IS_ERR(name))
210                                 return PTR_ERR(name);
211                         if (!name) {
212                                 path_ptr[-1] = '\0';
213                                 path_fd = fd;
214                         } else {
215                                 path_ptr[-1] = '/';
216                                 ret = snprintf(path_ptr, bytes_left, "%s",
217                                                 name);
218                                 BUG_ON(ret >= bytes_left);
219                                 free(name);
220                                 path_fd = open_file_or_dir(full_path);
221                                 if (path_fd < 0) {
222                                         fprintf(stderr, "ERROR: can't access "
223                                                 "'%s'\n", full_path);
224                                         goto out;
225                                 }
226                         }
227                         __ino_to_path_fd(inum, path_fd, verbose, full_path);
228                 } else {
229                         printf("inode %llu offset %llu root %llu\n", inum,
230                                 offset, root);
231                 }
232         }
233
234 out:
235         free(inodes);
236         return ret;
237 }
238
239 const struct cmd_group inspect_cmd_group = {
240         inspect_cmd_group_usage, NULL, {
241                 { "inode-resolve", cmd_inode_resolve, cmd_inode_resolve_usage,
242                         NULL, 0 },
243                 { "logical-resolve", cmd_logical_resolve,
244                         cmd_logical_resolve_usage, NULL, 0 },
245                 { 0, 0, 0, 0, 0 }
246         }
247 };
248
249 int cmd_inspect(int argc, char **argv)
250 {
251         return handle_command_group(&inspect_cmd_group, argc, argv);
252 }