Merge branch 'cov-fixes-v1-integration-20130201' of http://git.zabbo.net/cgit/btrfs...
[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 #include "utils.h"
27
28 #include "commands.h"
29 #include "btrfs-list.h"
30
31 static const char * const inspect_cmd_group_usage[] = {
32         "btrfs inspect-internal <command> <args>",
33         NULL
34 };
35
36 static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
37 {
38         int ret;
39         int i;
40         struct btrfs_ioctl_ino_path_args ipa;
41         struct btrfs_data_container *fspath;
42
43         fspath = malloc(4096);
44         if (!fspath)
45                 return 1;
46
47         memset(fspath, 0, sizeof(*fspath));
48         ipa.inum = inum;
49         ipa.size = 4096;
50         ipa.fspath = (uintptr_t)fspath;
51
52         ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
53         if (ret) {
54                 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
55                 goto out;
56         }
57
58         if (verbose)
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);
64
65         for (i = 0; i < fspath->elem_cnt; ++i) {
66                 char **str = (char **)fspath->val;
67                 str[i] += (unsigned long)fspath->val;
68                 if (prepend)
69                         printf("%s/%s\n", prepend, str[i]);
70                 else
71                         printf("%s\n", str[i]);
72         }
73
74 out:
75         free(fspath);
76         return ret;
77 }
78
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",
82         NULL
83 };
84
85 static int cmd_inode_resolve(int argc, char **argv)
86 {
87         int fd;
88         int verbose = 0;
89         int ret;
90
91         optind = 1;
92         while (1) {
93                 int c = getopt(argc, argv, "v");
94                 if (c < 0)
95                         break;
96
97                 switch (c) {
98                 case 'v':
99                         verbose = 1;
100                         break;
101                 default:
102                         usage(cmd_inode_resolve_usage);
103                 }
104         }
105
106         if (check_argc_exact(argc - optind, 2))
107                 usage(cmd_inode_resolve_usage);
108
109         fd = open_file_or_dir(argv[optind+1]);
110         if (fd < 0) {
111                 fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
112                 return 12;
113         }
114
115         ret = __ino_to_path_fd(atoll(argv[optind]), fd, verbose,
116                                argv[optind+1]);
117         close(fd);
118         return ret;
119
120 }
121
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",
126         "-v          verbose mode",
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",
130         NULL
131 };
132
133 static int cmd_logical_resolve(int argc, char **argv)
134 {
135         int ret;
136         int fd;
137         int i;
138         int verbose = 0;
139         int getpath = 1;
140         int bytes_left;
141         struct btrfs_ioctl_logical_ino_args loi;
142         struct btrfs_data_container *inodes;
143         u64 size = 4096;
144         char full_path[4096];
145         char *path_ptr;
146
147         optind = 1;
148         while (1) {
149                 int c = getopt(argc, argv, "Pvs:");
150                 if (c < 0)
151                         break;
152
153                 switch (c) {
154                 case 'P':
155                         getpath = 0;
156                         break;
157                 case 'v':
158                         verbose = 1;
159                         break;
160                 case 's':
161                         size = atoll(optarg);
162                         break;
163                 default:
164                         usage(cmd_logical_resolve_usage);
165                 }
166         }
167
168         if (check_argc_exact(argc - optind, 2))
169                 usage(cmd_logical_resolve_usage);
170
171         size = min(size, (u64)64 * 1024);
172         inodes = malloc(size);
173         if (!inodes)
174                 return 1;
175
176         memset(inodes, 0, sizeof(*inodes));
177         loi.logical = atoll(argv[optind]);
178         loi.size = size;
179         loi.inodes = (uintptr_t)inodes;
180
181         fd = open_file_or_dir(argv[optind+1]);
182         if (fd < 0) {
183                 fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
184                 ret = 12;
185                 goto out;
186         }
187
188         ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
189         if (ret) {
190                 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
191                 goto out;
192         }
193
194         if (verbose)
195                 printf("ioctl ret=%d, total_size=%llu, bytes_left=%lu, "
196                         "bytes_missing=%lu, cnt=%d, missed=%d\n",
197                         ret, size,
198                         (unsigned long)inodes->bytes_left,
199                         (unsigned long)inodes->bytes_missing,
200                         inodes->elem_cnt, inodes->elem_missed);
201
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);
207
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];
212                 int path_fd;
213                 char *name;
214
215                 if (getpath) {
216                         name = btrfs_list_path_for_root(fd, root);
217                         if (IS_ERR(name)) {
218                                 ret = PTR_ERR(name);
219                                 goto out;
220                         }
221                         if (!name) {
222                                 path_ptr[-1] = '\0';
223                                 path_fd = fd;
224                         } else {
225                                 path_ptr[-1] = '/';
226                                 ret = snprintf(path_ptr, bytes_left, "%s",
227                                                 name);
228                                 BUG_ON(ret >= bytes_left);
229                                 free(name);
230                                 path_fd = open_file_or_dir(full_path);
231                                 if (path_fd < 0) {
232                                         fprintf(stderr, "ERROR: can't access "
233                                                 "'%s'\n", full_path);
234                                         goto out;
235                                 }
236                         }
237                         __ino_to_path_fd(inum, path_fd, verbose, full_path);
238                         if (path_fd != fd)
239                                 close(path_fd);
240                 } else {
241                         printf("inode %llu offset %llu root %llu\n", inum,
242                                 offset, root);
243                 }
244         }
245
246 out:
247         if (fd >= 0)
248                 close(fd);
249         free(inodes);
250         return ret;
251 }
252
253 const struct cmd_group inspect_cmd_group = {
254         inspect_cmd_group_usage, NULL, {
255                 { "inode-resolve", cmd_inode_resolve, cmd_inode_resolve_usage,
256                         NULL, 0 },
257                 { "logical-resolve", cmd_logical_resolve,
258                         cmd_logical_resolve_usage, NULL, 0 },
259                 { 0, 0, 0, 0, 0 }
260         }
261 };
262
263 int cmd_inspect(int argc, char **argv)
264 {
265         return handle_command_group(&inspect_cmd_group, argc, argv);
266 }