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