btrfs-progs: update man page for btrfs filesystem label
[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 #include "ctree.h"
28 #include "send-utils.h"
29
30 #include "commands.h"
31 #include "btrfs-list.h"
32
33 static const char * const inspect_cmd_group_usage[] = {
34         "btrfs inspect-internal <command> <args>",
35         NULL
36 };
37
38 static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
39 {
40         int ret;
41         int i;
42         struct btrfs_ioctl_ino_path_args ipa;
43         struct btrfs_data_container *fspath;
44
45         fspath = malloc(4096);
46         if (!fspath)
47                 return 1;
48
49         memset(fspath, 0, sizeof(*fspath));
50         ipa.inum = inum;
51         ipa.size = 4096;
52         ipa.fspath = (uintptr_t)fspath;
53
54         ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
55         if (ret) {
56                 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
57                 goto out;
58         }
59
60         if (verbose)
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);
66
67         for (i = 0; i < fspath->elem_cnt; ++i) {
68                 u64 ptr;
69                 char *str;
70                 ptr = (u64)(unsigned long)fspath->val;
71                 ptr += fspath->val[i];
72                 str = (char *)(unsigned long)ptr;
73                 if (prepend)
74                         printf("%s/%s\n", prepend, str);
75                 else
76                         printf("%s\n", str);
77         }
78
79 out:
80         free(fspath);
81         return ret;
82 }
83
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",
87         NULL
88 };
89
90 static int cmd_inode_resolve(int argc, char **argv)
91 {
92         int fd;
93         int verbose = 0;
94         int ret;
95
96         optind = 1;
97         while (1) {
98                 int c = getopt(argc, argv, "v");
99                 if (c < 0)
100                         break;
101
102                 switch (c) {
103                 case 'v':
104                         verbose = 1;
105                         break;
106                 default:
107                         usage(cmd_inode_resolve_usage);
108                 }
109         }
110
111         if (check_argc_exact(argc - optind, 2))
112                 usage(cmd_inode_resolve_usage);
113
114         fd = open_file_or_dir(argv[optind+1]);
115         if (fd < 0) {
116                 fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
117                 return 12;
118         }
119
120         ret = __ino_to_path_fd(atoll(argv[optind]), fd, verbose,
121                                argv[optind+1]);
122         close(fd);
123         return ret;
124
125 }
126
127 static const char * const cmd_logical_resolve_usage[] = {
128         "btrfs inspect-internal logical-resolve [-Pv] [-s bufsize] <logical> <path>",
129         "Get file system paths for the given logical address",
130         "-P          skip the path resolving and print the inodes instead",
131         "-v          verbose mode",
132         "-s bufsize  set inode container's size. This is used to increase inode",
133         "            container's size in case it is not enough to read all the ",
134         "            resolved results. The max value one can set is 64k",
135         NULL
136 };
137
138 static int cmd_logical_resolve(int argc, char **argv)
139 {
140         int ret;
141         int fd;
142         int i;
143         int verbose = 0;
144         int getpath = 1;
145         int bytes_left;
146         struct btrfs_ioctl_logical_ino_args loi;
147         struct btrfs_data_container *inodes;
148         u64 size = 4096;
149         char full_path[4096];
150         char *path_ptr;
151
152         optind = 1;
153         while (1) {
154                 int c = getopt(argc, argv, "Pvs:");
155                 if (c < 0)
156                         break;
157
158                 switch (c) {
159                 case 'P':
160                         getpath = 0;
161                         break;
162                 case 'v':
163                         verbose = 1;
164                         break;
165                 case 's':
166                         size = atoll(optarg);
167                         break;
168                 default:
169                         usage(cmd_logical_resolve_usage);
170                 }
171         }
172
173         if (check_argc_exact(argc - optind, 2))
174                 usage(cmd_logical_resolve_usage);
175
176         size = min(size, (u64)64 * 1024);
177         inodes = malloc(size);
178         if (!inodes)
179                 return 1;
180
181         memset(inodes, 0, sizeof(*inodes));
182         loi.logical = atoll(argv[optind]);
183         loi.size = size;
184         loi.inodes = (uintptr_t)inodes;
185
186         fd = open_file_or_dir(argv[optind+1]);
187         if (fd < 0) {
188                 fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
189                 ret = 12;
190                 goto out;
191         }
192
193         ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
194         if (ret) {
195                 printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
196                 goto out;
197         }
198
199         if (verbose)
200                 printf("ioctl ret=%d, total_size=%llu, bytes_left=%lu, "
201                         "bytes_missing=%lu, cnt=%d, missed=%d\n",
202                         ret, size,
203                         (unsigned long)inodes->bytes_left,
204                         (unsigned long)inodes->bytes_missing,
205                         inodes->elem_cnt, inodes->elem_missed);
206
207         bytes_left = sizeof(full_path);
208         ret = snprintf(full_path, bytes_left, "%s/", argv[optind+1]);
209         path_ptr = full_path + ret;
210         bytes_left -= ret + 1;
211         BUG_ON(bytes_left < 0);
212
213         for (i = 0; i < inodes->elem_cnt; i += 3) {
214                 u64 inum = inodes->val[i];
215                 u64 offset = inodes->val[i+1];
216                 u64 root = inodes->val[i+2];
217                 int path_fd;
218                 char *name;
219
220                 if (getpath) {
221                         name = btrfs_list_path_for_root(fd, root);
222                         if (IS_ERR(name)) {
223                                 ret = PTR_ERR(name);
224                                 goto out;
225                         }
226                         if (!name) {
227                                 path_ptr[-1] = '\0';
228                                 path_fd = fd;
229                         } else {
230                                 path_ptr[-1] = '/';
231                                 ret = snprintf(path_ptr, bytes_left, "%s",
232                                                 name);
233                                 BUG_ON(ret >= bytes_left);
234                                 free(name);
235                                 path_fd = open_file_or_dir(full_path);
236                                 if (path_fd < 0) {
237                                         fprintf(stderr, "ERROR: can't access "
238                                                 "'%s'\n", full_path);
239                                         goto out;
240                                 }
241                         }
242                         __ino_to_path_fd(inum, path_fd, verbose, full_path);
243                         if (path_fd != fd)
244                                 close(path_fd);
245                 } else {
246                         printf("inode %llu offset %llu root %llu\n", inum,
247                                 offset, root);
248                 }
249         }
250
251 out:
252         if (fd >= 0)
253                 close(fd);
254         free(inodes);
255         return ret;
256 }
257
258 static const char * const cmd_subvolid_resolve_usage[] = {
259         "btrfs inspect-internal subvolid-resolve <subvolid> <path>",
260         "Get file system paths for the given subvolume ID.",
261         NULL
262 };
263
264 static int cmd_subvolid_resolve(int argc, char **argv)
265 {
266         int ret;
267         int fd = -1;
268         u64 subvol_id;
269         char path[BTRFS_PATH_NAME_MAX + 1];
270
271         if (check_argc_exact(argc, 3))
272                 usage(cmd_subvolid_resolve_usage);
273
274         fd = open_file_or_dir(argv[2]);
275         if (fd < 0) {
276                 fprintf(stderr, "ERROR: can't access '%s'\n", argv[2]);
277                 ret = -ENOENT;
278                 goto out;
279         }
280
281         subvol_id = atoll(argv[1]);
282         ret = btrfs_subvolid_resolve(fd, path, sizeof(path), subvol_id);
283
284         if (ret) {
285                 fprintf(stderr,
286                         "%s: btrfs_subvolid_resolve(subvol_id %llu) failed with ret=%d\n",
287                         argv[0], (unsigned long long)subvol_id, ret);
288                 goto out;
289         }
290
291         path[BTRFS_PATH_NAME_MAX] = '\0';
292         printf("%s\n", path);
293
294 out:
295         if (fd >= 0)
296                 close(fd);
297         return ret ? 1 : 0;
298 }
299
300 const struct cmd_group inspect_cmd_group = {
301         inspect_cmd_group_usage, NULL, {
302                 { "inode-resolve", cmd_inode_resolve, cmd_inode_resolve_usage,
303                         NULL, 0 },
304                 { "logical-resolve", cmd_logical_resolve,
305                         cmd_logical_resolve_usage, NULL, 0 },
306                 { "subvolid-resolve", cmd_subvolid_resolve,
307                         cmd_subvolid_resolve_usage, NULL, 0 },
308                 { 0, 0, 0, 0, 0 }
309         }
310 };
311
312 int cmd_inspect(int argc, char **argv)
313 {
314         return handle_command_group(&inspect_cmd_group, argc, argv);
315 }