]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/bmap.c
2e4ff7b2343219028a218a4e6bf22f5a12573139
[thirdparty/xfsprogs-dev.git] / io / bmap.c
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, 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 "init.h"
23 #include "io.h"
24
25 static cmdinfo_t bmap_cmd;
26
27 static void
28 bmap_help(void)
29 {
30 printf(_(
31 "\n"
32 " prints the block mapping for an XFS file's data or attribute forks"
33 "\n"
34 " Example:\n"
35 " 'bmap -vp' - tabular format verbose map, including unwritten extents\n"
36 "\n"
37 " bmap prints the map of disk blocks used by the current file.\n"
38 " The map lists each extent used by the file, as well as regions in the\n"
39 " file that do not have any corresponding blocks (holes).\n"
40 " By default, each line of the listing takes the following form:\n"
41 " extent: [startoffset..endoffset]: startblock..endblock\n"
42 " Holes are marked by replacing the startblock..endblock with 'hole'.\n"
43 " All the file offsets and disk blocks are in units of 512-byte blocks.\n"
44 " -a -- prints the attribute fork map instead of the data fork.\n"
45 " -c -- prints the copy-on-write fork map instead of the data fork.\n"
46 " -d -- suppresses a DMAPI read event, offline portions shown as holes.\n"
47 " -e -- print delayed allocation extents.\n"
48 " -l -- also displays the length of each extent in 512-byte blocks.\n"
49 " -n -- query n extents.\n"
50 " -p -- obtain all unwritten extents as well (w/ -v show which are unwritten.)\n"
51 " -v -- Verbose information, specify ag info. Show flags legend on 2nd -v\n"
52 " Note: the bmap for non-regular files can be obtained provided the file\n"
53 " was opened appropriately (in particular, must be opened read-only).\n"
54 "\n"));
55 }
56
57 int
58 bmap_f(
59 int argc,
60 char **argv)
61 {
62 struct fsxattr fsx;
63 struct getbmapx *map;
64 struct xfs_fsop_geom fsgeo;
65 int map_size;
66 int loop = 0;
67 int flg = 0;
68 int aflag = 0;
69 int cflag = 0;
70 int lflag = 0;
71 int nflag = 0;
72 int pflag = 0;
73 int vflag = 0;
74 int is_rt = 0;
75 int bmv_iflags = 0; /* flags for XFS_IOC_GETBMAPX */
76 int i = 0;
77 int c;
78 int egcnt;
79
80 while ((c = getopt(argc, argv, "acdeln:pv")) != EOF) {
81 switch (c) {
82 case 'a': /* Attribute fork. */
83 bmv_iflags |= BMV_IF_ATTRFORK;
84 aflag = 1;
85 break;
86 case 'c': /* CoW fork. */
87 bmv_iflags |= BMV_IF_COWFORK | BMV_IF_DELALLOC;
88 cflag = 1;
89 break;
90 case 'e':
91 bmv_iflags |= BMV_IF_DELALLOC;
92 break;
93 case 'l': /* list number of blocks with each extent */
94 lflag = 1;
95 break;
96 case 'n': /* number of extents specified */
97 nflag = atoi(optarg);
98 break;
99 case 'd':
100 /* do not recall possibly offline DMAPI files */
101 bmv_iflags |= BMV_IF_NO_DMAPI_READ;
102 break;
103 case 'p':
104 /* report unwritten preallocated blocks */
105 pflag = 1;
106 bmv_iflags |= BMV_IF_PREALLOC;
107 break;
108 case 'v': /* Verbose output */
109 vflag++;
110 break;
111 default:
112 return command_usage(&bmap_cmd);
113 }
114 }
115 if (aflag || cflag)
116 bmv_iflags &= ~(BMV_IF_PREALLOC|BMV_IF_NO_DMAPI_READ);
117
118 if (vflag) {
119 c = xfsctl(file->name, file->fd, XFS_IOC_FSGEOMETRY_V1, &fsgeo);
120 if (c < 0) {
121 fprintf(stderr,
122 _("%s: can't get geometry [\"%s\"]: %s\n"),
123 progname, file->name, strerror(errno));
124 exitcode = 1;
125 return 0;
126 }
127 c = xfsctl(file->name, file->fd, FS_IOC_FSGETXATTR, &fsx);
128 if (c < 0) {
129 fprintf(stderr,
130 _("%s: cannot read attrs on \"%s\": %s\n"),
131 progname, file->name, strerror(errno));
132 exitcode = 1;
133 return 0;
134 }
135
136 if (fsx.fsx_xflags == FS_XFLAG_REALTIME) {
137 /*
138 * ag info not applicable to rt, continue
139 * without ag output.
140 */
141 is_rt = 1;
142 }
143 }
144
145 map_size = nflag ? nflag+2 : 32; /* initial guess - 32 */
146 map = malloc(map_size*sizeof(*map));
147 if (map == NULL) {
148 fprintf(stderr, _("%s: malloc of %d bytes failed.\n"),
149 progname, (int)(map_size * sizeof(*map)));
150 exitcode = 1;
151 return 0;
152 }
153
154
155 /* Try the xfsctl(XFS_IOC_GETBMAPX) for the number of extents specified
156 * by nflag, or the initial guess number of extents (32).
157 *
158 * If there are more extents than we guessed, use xfsctl
159 * (FS_IOC_FSGETXATTR[A]) to get the extent count, realloc some more
160 * space based on this count, and try again.
161 *
162 * If the initial FGETBMAPX attempt returns EINVAL, this may mean
163 * that we tried the FGETBMAPX on a zero length file. If we get
164 * EINVAL, check the length with fstat() and return "no extents"
165 * if the length == 0.
166 *
167 * Why not do the xfsctl(FS_IOC_FSGETXATTR[A]) first? Two reasons:
168 * (1) The extent count may be wrong for a file with delayed
169 * allocation blocks. The XFS_IOC_GETBMAPX forces the real
170 * allocation and fixes up the extent count.
171 * (2) For XFS_IOC_GETBMAP[X] on a DMAPI file that has been moved
172 * offline by a DMAPI application (e.g., DMF) the
173 * FS_IOC_FSGETXATTR only reflects the extents actually online.
174 * Doing XFS_IOC_GETBMAPX call first forces that data blocks online
175 * and then everything proceeds normally (see PV #545725).
176 *
177 * If you don't want this behavior on a DMAPI offline file,
178 * try the "-d" option which sets the BMV_IF_NO_DMAPI_READ
179 * iflag for XFS_IOC_GETBMAPX.
180 */
181
182 do { /* loop a miximum of two times */
183
184 memset(map, 0, sizeof(*map)); /* zero header */
185
186 map->bmv_length = -1;
187 map->bmv_count = map_size;
188 map->bmv_iflags = bmv_iflags;
189
190 i = xfsctl(file->name, file->fd, XFS_IOC_GETBMAPX, map);
191 if (i < 0) {
192 if ( errno == EINVAL
193 && !aflag && filesize() == 0) {
194 break;
195 } else {
196 fprintf(stderr, _("%s: xfsctl(XFS_IOC_GETBMAPX)"
197 " iflags=0x%x [\"%s\"]: %s\n"),
198 progname, map->bmv_iflags, file->name,
199 strerror(errno));
200 free(map);
201 exitcode = 1;
202 return 0;
203 }
204 }
205 if (nflag)
206 break;
207 if (map->bmv_entries < map->bmv_count-1)
208 break;
209 /* Get number of extents from xfsctl FS_IOC_FSGETXATTR[A]
210 * syscall.
211 */
212 i = xfsctl(file->name, file->fd, aflag ?
213 XFS_IOC_FSGETXATTRA : FS_IOC_FSGETXATTR, &fsx);
214 if (i < 0) {
215 fprintf(stderr, "%s: xfsctl(FS_IOC_FSGETXATTR%s) "
216 "[\"%s\"]: %s\n", progname, aflag ? "A" : "",
217 file->name, strerror(errno));
218 free(map);
219 exitcode = 1;
220 return 0;
221 }
222 if (2 * fsx.fsx_nextents > map_size) {
223 map_size = 2 * fsx.fsx_nextents + 1;
224 map = realloc(map, map_size*sizeof(*map));
225 if (map == NULL) {
226 fprintf(stderr,
227 _("%s: cannot realloc %d bytes\n"),
228 progname, (int)(map_size*sizeof(*map)));
229 exitcode = 1;
230 return 0;
231 }
232 }
233 } while (++loop < 2);
234 if (!nflag) {
235 if (map->bmv_entries <= 0) {
236 printf(_("%s: no extents\n"), file->name);
237 free(map);
238 return 0;
239 }
240 }
241 egcnt = nflag ? min(nflag, map->bmv_entries) : map->bmv_entries;
242 printf("%s:\n", file->name);
243 if (!vflag) {
244 for (i = 0; i < egcnt; i++) {
245 printf("\t%d: [%lld..%lld]: ", i,
246 (long long) map[i + 1].bmv_offset,
247 (long long)(map[i + 1].bmv_offset +
248 map[i + 1].bmv_length - 1LL));
249 if (map[i + 1].bmv_block == -1)
250 printf(_("hole"));
251 else if (map[i + 1].bmv_block == -2)
252 printf(_("delalloc"));
253 else {
254 printf("%lld..%lld",
255 (long long) map[i + 1].bmv_block,
256 (long long)(map[i + 1].bmv_block +
257 map[i + 1].bmv_length - 1LL));
258
259 }
260 if (lflag)
261 printf(_(" %lld blocks\n"),
262 (long long)map[i+1].bmv_length);
263 else
264 printf("\n");
265 }
266 } else {
267 /*
268 * Verbose mode displays:
269 * extent: [startoffset..endoffset]: startblock..endblock \
270 * ag# (agoffset..agendoffset) totalbbs
271 */
272 #define MINRANGE_WIDTH 16
273 #define MINAG_WIDTH 2
274 #define MINTOT_WIDTH 5
275 #define NFLG 6 /* count of flags */
276 #define FLG_NULL 0000000 /* Null flag */
277 #define FLG_SHARED 0100000 /* shared extent */
278 #define FLG_PRE 0010000 /* Unwritten extent */
279 #define FLG_BSU 0001000 /* Not on begin of stripe unit */
280 #define FLG_ESU 0000100 /* Not on end of stripe unit */
281 #define FLG_BSW 0000010 /* Not on begin of stripe width */
282 #define FLG_ESW 0000001 /* Not on end of stripe width */
283 int agno;
284 off64_t agoff, bbperag;
285 int foff_w, boff_w, aoff_w, tot_w, agno_w;
286 char rbuf[32], bbuf[32], abuf[32];
287 int sunit, swidth;
288
289 foff_w = boff_w = aoff_w = MINRANGE_WIDTH;
290 tot_w = MINTOT_WIDTH;
291 if (is_rt)
292 sunit = swidth = bbperag = 0;
293 else {
294 bbperag = (off64_t)fsgeo.agblocks *
295 (off64_t)fsgeo.blocksize / BBSIZE;
296 sunit = (fsgeo.sunit * fsgeo.blocksize) / BBSIZE;
297 swidth = (fsgeo.swidth * fsgeo.blocksize) / BBSIZE;
298 }
299 flg = sunit | pflag;
300
301 /*
302 * Go through the extents and figure out the width
303 * needed for all columns.
304 */
305 for (i = 0; i < egcnt; i++) {
306 snprintf(rbuf, sizeof(rbuf), "[%lld..%lld]:",
307 (long long) map[i + 1].bmv_offset,
308 (long long)(map[i + 1].bmv_offset +
309 map[i + 1].bmv_length - 1LL));
310 if (map[i + 1].bmv_oflags & BMV_OF_PREALLOC)
311 flg = 1;
312 if (map[i + 1].bmv_block == -1) {
313 foff_w = max(foff_w, strlen(rbuf));
314 tot_w = max(tot_w,
315 numlen(map[i+1].bmv_length, 10));
316 } else {
317 snprintf(bbuf, sizeof(bbuf), "%lld..%lld",
318 (long long) map[i + 1].bmv_block,
319 (long long)(map[i + 1].bmv_block +
320 map[i + 1].bmv_length - 1LL));
321 boff_w = max(boff_w, strlen(bbuf));
322 if (!is_rt) {
323 agno = map[i + 1].bmv_block / bbperag;
324 agoff = map[i + 1].bmv_block -
325 (agno * bbperag);
326 snprintf(abuf, sizeof(abuf),
327 "(%lld..%lld)",
328 (long long)agoff,
329 (long long)(agoff +
330 map[i + 1].bmv_length - 1LL));
331 aoff_w = max(aoff_w, strlen(abuf));
332 } else
333 aoff_w = 0;
334 foff_w = max(foff_w, strlen(rbuf));
335 tot_w = max(tot_w,
336 numlen(map[i+1].bmv_length, 10));
337 }
338 }
339 agno_w = is_rt ? 0 : max(MINAG_WIDTH, numlen(fsgeo.agcount, 10));
340 printf("%4s: %-*s %-*s %*s %-*s %*s%s\n",
341 _("EXT"),
342 foff_w, _("FILE-OFFSET"),
343 boff_w, is_rt ? _("RT-BLOCK-RANGE") : _("BLOCK-RANGE"),
344 agno_w, is_rt ? "" : _("AG"),
345 aoff_w, is_rt ? "" : _("AG-OFFSET"),
346 tot_w, _("TOTAL"),
347 flg ? _(" FLAGS") : "");
348 for (i = 0; i < egcnt; i++) {
349 flg = FLG_NULL;
350 if (map[i + 1].bmv_oflags & BMV_OF_PREALLOC) {
351 flg |= FLG_PRE;
352 }
353 if (map[i + 1].bmv_oflags & BMV_OF_SHARED)
354 flg |= FLG_SHARED;
355 if (map[i + 1].bmv_oflags & BMV_OF_DELALLOC)
356 map[i + 1].bmv_block = -2;
357 /*
358 * If striping enabled, determine if extent starts/ends
359 * on a stripe unit boundary.
360 */
361 if (sunit) {
362 if (map[i + 1].bmv_block % sunit != 0) {
363 flg |= FLG_BSU;
364 }
365 if (((map[i + 1].bmv_block +
366 map[i + 1].bmv_length ) % sunit ) != 0) {
367 flg |= FLG_ESU;
368 }
369 if (map[i + 1].bmv_block % swidth != 0) {
370 flg |= FLG_BSW;
371 }
372 if (((map[i + 1].bmv_block +
373 map[i + 1].bmv_length ) % swidth ) != 0) {
374 flg |= FLG_ESW;
375 }
376 }
377 snprintf(rbuf, sizeof(rbuf), "[%lld..%lld]:",
378 (long long) map[i + 1].bmv_offset,
379 (long long)(map[i + 1].bmv_offset +
380 map[i + 1].bmv_length - 1LL));
381 if (map[i + 1].bmv_block == -1) {
382 printf("%4d: %-*s %-*s %*s %-*s %*lld\n",
383 i,
384 foff_w, rbuf,
385 boff_w, _("hole"),
386 agno_w, "",
387 aoff_w, "",
388 tot_w, (long long)map[i+1].bmv_length);
389 } else if (map[i + 1].bmv_block == -2) {
390 printf("%4d: %-*s %-*s %*s %-*s %*lld\n",
391 i,
392 foff_w, rbuf,
393 boff_w, _("delalloc"),
394 agno_w, "",
395 aoff_w, "",
396 tot_w, (long long)map[i+1].bmv_length);
397 } else {
398 snprintf(bbuf, sizeof(bbuf), "%lld..%lld",
399 (long long) map[i + 1].bmv_block,
400 (long long)(map[i + 1].bmv_block +
401 map[i + 1].bmv_length - 1LL));
402 printf("%4d: %-*s %-*s", i, foff_w, rbuf,
403 boff_w, bbuf);
404 if (!is_rt) {
405 agno = map[i + 1].bmv_block / bbperag;
406 agoff = map[i + 1].bmv_block -
407 (agno * bbperag);
408 snprintf(abuf, sizeof(abuf),
409 "(%lld..%lld)",
410 (long long)agoff,
411 (long long)(agoff +
412 map[i + 1].bmv_length - 1LL));
413 printf(" %*d %-*s", agno_w, agno,
414 aoff_w, abuf);
415 } else
416 printf(" ");
417 printf(" %*lld", tot_w,
418 (long long)map[i+1].bmv_length);
419 if (flg == FLG_NULL && !pflag) {
420 printf("\n");
421 } else {
422 printf(" %-*.*o\n", NFLG, NFLG, flg);
423 }
424 }
425 }
426 if ((flg || pflag) && vflag > 1) {
427 printf(_(" FLAG Values:\n"));
428 printf(_(" %*.*o Shared extent\n"),
429 NFLG+1, NFLG+1, FLG_SHARED);
430 printf(_(" %*.*o Unwritten preallocated extent\n"),
431 NFLG+1, NFLG+1, FLG_PRE);
432 printf(_(" %*.*o Doesn't begin on stripe unit\n"),
433 NFLG+1, NFLG+1, FLG_BSU);
434 printf(_(" %*.*o Doesn't end on stripe unit\n"),
435 NFLG+1, NFLG+1, FLG_ESU);
436 printf(_(" %*.*o Doesn't begin on stripe width\n"),
437 NFLG+1, NFLG+1, FLG_BSW);
438 printf(_(" %*.*o Doesn't end on stripe width\n"),
439 NFLG+1, NFLG+1, FLG_ESW);
440 }
441 }
442 free(map);
443 return 0;
444 }
445
446 void
447 bmap_init(void)
448 {
449 bmap_cmd.name = "bmap";
450 bmap_cmd.cfunc = bmap_f;
451 bmap_cmd.argmin = 0;
452 bmap_cmd.argmax = -1;
453 bmap_cmd.flags = CMD_NOMAP_OK;
454 bmap_cmd.args = _("[-adlpv] [-n nx]");
455 bmap_cmd.oneline = _("print block mapping for an XFS file");
456 bmap_cmd.help = bmap_help;
457
458 add_command(&bmap_cmd);
459 }