]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/fiemap.c
fiemap: Fix semantics of max_extents (-n arguments)
[thirdparty/xfsprogs-dev.git] / io / fiemap.c
1 /*
2 * Copyright (c) 2010 Red Hat, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "platform_defs.h"
20 #include "command.h"
21 #include "input.h"
22 #include <linux/fiemap.h>
23 #include "init.h"
24 #include "io.h"
25
26 #define EXTENT_BATCH 32
27
28 static cmdinfo_t fiemap_cmd;
29 static int max_extents = -1;
30
31 static void
32 fiemap_help(void)
33 {
34 printf(_(
35 "\n"
36 " prints the block mapping for a file's data or attribute forks"
37 "\n"
38 " Example:\n"
39 " 'fiemap -v' - tabular format verbose map\n"
40 "\n"
41 " fiemap prints the map of disk blocks used by the current file.\n"
42 " The map lists each extent used by the file, as well as regions in the\n"
43 " file that do not have any corresponding blocks (holes).\n"
44 " By default, each line of the listing takes the following form:\n"
45 " extent: [startoffset..endoffset]: startblock..endblock\n"
46 " Holes are marked by replacing the startblock..endblock with 'hole'.\n"
47 " All the file offsets and disk blocks are in units of 512-byte blocks.\n"
48 " -a -- prints the attribute fork map instead of the data fork.\n"
49 " -l -- also displays the length of each extent in 512-byte blocks.\n"
50 " -n -- query n extents.\n"
51 " -v -- Verbose information\n"
52 "\n"));
53 }
54
55 static void
56 print_hole(
57 int foff_w,
58 int boff_w,
59 int tot_w,
60 int cur_extent,
61 int lflag,
62 bool plain,
63 __u64 llast,
64 __u64 lstart)
65 {
66 char lbuf[48];
67
68 if (plain) {
69 printf("\t%d: [%llu..%llu]: hole", cur_extent,
70 llast, lstart - 1ULL);
71 if (lflag)
72 printf(_(" %llu blocks\n"), lstart - llast);
73 else
74 printf("\n");
75 } else {
76 snprintf(lbuf, sizeof(lbuf), "[%llu..%llu]:", llast,
77 lstart - 1ULL);
78 printf("%4d: %-*s %-*s %*llu\n", cur_extent, foff_w, lbuf,
79 boff_w, _("hole"), tot_w, lstart - llast);
80 }
81
82
83 }
84
85 static int
86 print_verbose(
87 struct fiemap_extent *extent,
88 int foff_w,
89 int boff_w,
90 int tot_w,
91 int flg_w,
92 int cur_extent,
93 __u64 last_logical)
94 {
95 __u64 lstart;
96 __u64 llast;
97 __u64 len;
98 __u64 block;
99 char lbuf[48];
100 char bbuf[48];
101 char flgbuf[16];
102
103 llast = BTOBBT(last_logical);
104 lstart = BTOBBT(extent->fe_logical);
105 len = BTOBBT(extent->fe_length);
106 block = BTOBBT(extent->fe_physical);
107
108 memset(lbuf, 0, sizeof(lbuf));
109 memset(bbuf, 0, sizeof(bbuf));
110
111 if (cur_extent == 0) {
112 printf("%4s: %-*s %-*s %*s %*s\n", _("EXT"),
113 foff_w, _("FILE-OFFSET"),
114 boff_w, _("BLOCK-RANGE"),
115 tot_w, _("TOTAL"),
116 flg_w, _("FLAGS"));
117 }
118
119 if (lstart != llast) {
120 print_hole(foff_w, boff_w, tot_w, cur_extent, 0, false, llast,
121 lstart);
122 cur_extent++;
123 }
124
125 if (cur_extent == max_extents)
126 return 1;
127
128 snprintf(lbuf, sizeof(lbuf), "[%llu..%llu]:", lstart,
129 lstart + len - 1ULL);
130 snprintf(bbuf, sizeof(bbuf), "%llu..%llu", block, block + len - 1ULL);
131 snprintf(flgbuf, sizeof(flgbuf), "0x%x", extent->fe_flags);
132 printf("%4d: %-*s %-*s %*llu %*s\n", cur_extent, foff_w, lbuf,
133 boff_w, bbuf, tot_w, len, flg_w, flgbuf);
134
135 return 2;
136 }
137
138 static int
139 print_plain(
140 struct fiemap_extent *extent,
141 int lflag,
142 int cur_extent,
143 __u64 last_logical)
144 {
145 __u64 lstart;
146 __u64 llast;
147 __u64 block;
148 __u64 len;
149
150 llast = BTOBBT(last_logical);
151 lstart = BTOBBT(extent->fe_logical);
152 len = BTOBBT(extent->fe_length);
153 block = BTOBBT(extent->fe_physical);
154
155 if (lstart != llast) {
156 print_hole(0, 0, 0, cur_extent, lflag, true, llast, lstart);
157 cur_extent++;
158 }
159
160 if (cur_extent == max_extents)
161 return 1;
162
163 printf("\t%d: [%llu..%llu]: %llu..%llu", cur_extent,
164 lstart, lstart + len - 1ULL, block,
165 block + len - 1ULL);
166
167 if (lflag)
168 printf(_(" %llu blocks\n"), len);
169 else
170 printf("\n");
171 return 2;
172 }
173
174 /*
175 * Calculate the proper extent table format based on first
176 * set of extents
177 */
178 static void
179 calc_print_format(
180 struct fiemap *fiemap,
181 int *foff_w,
182 int *boff_w,
183 int *tot_w,
184 int *flg_w)
185 {
186 int i;
187 char lbuf[32];
188 char bbuf[32];
189 __u64 logical;
190 __u64 block;
191 __u64 len;
192 struct fiemap_extent *extent;
193
194 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
195
196 extent = &fiemap->fm_extents[i];
197 logical = BTOBBT(extent->fe_logical);
198 len = BTOBBT(extent->fe_length);
199 block = BTOBBT(extent->fe_physical);
200
201 snprintf(lbuf, sizeof(lbuf), "[%llu..%llu]", logical,
202 logical + len - 1);
203 snprintf(bbuf, sizeof(bbuf), "%llu..%llu", block,
204 block + len - 1);
205 *foff_w = max(*foff_w, strlen(lbuf));
206 *boff_w = max(*boff_w, strlen(bbuf));
207 *tot_w = max(*tot_w, numlen(len, 10));
208 *flg_w = max(*flg_w, numlen(extent->fe_flags, 16));
209 if (extent->fe_flags & FIEMAP_EXTENT_LAST)
210 break;
211 }
212 }
213
214 int
215 fiemap_f(
216 int argc,
217 char **argv)
218 {
219 struct fiemap *fiemap;
220 int last = 0;
221 int lflag = 0;
222 int vflag = 0;
223 int fiemap_flags = FIEMAP_FLAG_SYNC;
224 int c;
225 int i;
226 int map_size;
227 int ret;
228 int cur_extent = 0;
229 int foff_w = 16; /* 16 just for a good minimum range */
230 int boff_w = 16;
231 int tot_w = 5; /* 5 since its just one number */
232 int flg_w = 5;
233 __u64 last_logical = 0;
234 struct stat st;
235
236 while ((c = getopt(argc, argv, "aln:v")) != EOF) {
237 switch (c) {
238 case 'a':
239 fiemap_flags |= FIEMAP_FLAG_XATTR;
240 break;
241 case 'l':
242 lflag = 1;
243 break;
244 case 'n':
245 max_extents = atoi(optarg);
246 break;
247 case 'v':
248 vflag++;
249 break;
250 default:
251 return command_usage(&fiemap_cmd);
252 }
253 }
254
255 map_size = sizeof(struct fiemap) +
256 (EXTENT_BATCH * sizeof(struct fiemap_extent));
257 fiemap = malloc(map_size);
258 if (!fiemap) {
259 fprintf(stderr, _("%s: malloc of %d bytes failed.\n"),
260 progname, map_size);
261 exitcode = 1;
262 return 0;
263 }
264
265 printf("%s:\n", file->name);
266
267 while (!last && (cur_extent != max_extents)) {
268
269 memset(fiemap, 0, map_size);
270 fiemap->fm_flags = fiemap_flags;
271 fiemap->fm_start = last_logical;
272 fiemap->fm_length = -1LL;
273 fiemap->fm_extent_count = EXTENT_BATCH;
274
275 ret = ioctl(file->fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
276 if (ret < 0) {
277 fprintf(stderr, "%s: ioctl(FS_IOC_FIEMAP) [\"%s\"]: "
278 "%s\n", progname, file->name, strerror(errno));
279 free(fiemap);
280 exitcode = 1;
281 return 0;
282 }
283
284 /* No more extents to map, exit */
285 if (!fiemap->fm_mapped_extents)
286 break;
287
288 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
289 struct fiemap_extent *extent;
290 int num_printed = 0;
291
292 extent = &fiemap->fm_extents[i];
293 if (vflag) {
294 if (cur_extent == 0) {
295 calc_print_format(fiemap, &foff_w,
296 &boff_w, &tot_w,
297 &flg_w);
298 }
299
300 num_printed = print_verbose(extent, foff_w,
301 boff_w, tot_w,
302 flg_w, cur_extent,
303 last_logical);
304 } else
305 num_printed = print_plain(extent, lflag,
306 cur_extent,
307 last_logical);
308
309 cur_extent += num_printed;
310 last_logical = extent->fe_logical + extent->fe_length;
311
312 if (extent->fe_flags & FIEMAP_EXTENT_LAST) {
313 last = 1;
314 break;
315 }
316
317 if (cur_extent == max_extents)
318 break;
319 }
320 }
321
322 if (cur_extent == max_extents)
323 goto out;
324
325 memset(&st, 0, sizeof(st));
326 if (fstat(file->fd, &st)) {
327 fprintf(stderr, "%s: fstat failed: %s\n", progname,
328 strerror(errno));
329 free(fiemap);
330 exitcode = 1;
331 return 0;
332 }
333
334 if (cur_extent && last_logical < st.st_size)
335 print_hole(foff_w, boff_w, tot_w, cur_extent, lflag, !vflag,
336 BTOBBT(last_logical), BTOBBT(st.st_size));
337
338 out:
339 free(fiemap);
340 return 0;
341 }
342
343 void
344 fiemap_init(void)
345 {
346 fiemap_cmd.name = "fiemap";
347 fiemap_cmd.cfunc = fiemap_f;
348 fiemap_cmd.argmin = 0;
349 fiemap_cmd.argmax = -1;
350 fiemap_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
351 fiemap_cmd.args = _("[-alv] [-n nx]");
352 fiemap_cmd.oneline = _("print block mapping for a file");
353 fiemap_cmd.help = fiemap_help;
354
355 add_command(&fiemap_cmd);
356 }