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