]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/bmap.c
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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.
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.
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
19 #include "platform_defs.h"
25 static cmdinfo_t bmap_cmd
;
32 " prints the block mapping for an XFS file's data or attribute forks"
35 " 'bmap -vp' - tabular format verbose map, including unwritten extents\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 " This works only if the kernel was compiled in debug mode.\n"
47 " -d -- suppresses a DMAPI read event, offline portions shown as holes.\n"
48 " -e -- print delayed allocation extents.\n"
49 " -l -- also displays the length of each extent in 512-byte blocks.\n"
50 " -n -- query n extents.\n"
51 " -p -- obtain all unwritten extents as well (w/ -v show which are unwritten.)\n"
52 " -v -- Verbose information, specify ag info. Show flags legend on 2nd -v\n"
53 " Note: the bmap for non-regular files can be obtained provided the file\n"
54 " was opened appropriately (in particular, must be opened read-only).\n"
65 struct xfs_fsop_geom fsgeo
;
76 int bmv_iflags
= 0; /* flags for XFS_IOC_GETBMAPX */
81 while ((c
= getopt(argc
, argv
, "acdeln:pv")) != EOF
) {
83 case 'a': /* Attribute fork. */
84 bmv_iflags
|= BMV_IF_ATTRFORK
;
87 case 'c': /* CoW fork. */
88 bmv_iflags
|= BMV_IF_COWFORK
| BMV_IF_DELALLOC
;
92 bmv_iflags
|= BMV_IF_DELALLOC
;
94 case 'l': /* list number of blocks with each extent */
97 case 'n': /* number of extents specified */
101 /* do not recall possibly offline DMAPI files */
102 bmv_iflags
|= BMV_IF_NO_DMAPI_READ
;
105 /* report unwritten preallocated blocks */
107 bmv_iflags
|= BMV_IF_PREALLOC
;
109 case 'v': /* Verbose output */
113 return command_usage(&bmap_cmd
);
117 bmv_iflags
&= ~(BMV_IF_PREALLOC
|BMV_IF_NO_DMAPI_READ
);
120 c
= xfsctl(file
->name
, file
->fd
, XFS_IOC_FSGEOMETRY_V1
, &fsgeo
);
123 _("%s: can't get geometry [\"%s\"]: %s\n"),
124 progname
, file
->name
, strerror(errno
));
128 c
= xfsctl(file
->name
, file
->fd
, FS_IOC_FSGETXATTR
, &fsx
);
131 _("%s: cannot read attrs on \"%s\": %s\n"),
132 progname
, file
->name
, strerror(errno
));
137 if (fsx
.fsx_xflags
== FS_XFLAG_REALTIME
) {
139 * ag info not applicable to rt, continue
146 map_size
= nflag
? nflag
+2 : 32; /* initial guess - 32 */
147 map
= malloc(map_size
*sizeof(*map
));
149 fprintf(stderr
, _("%s: malloc of %d bytes failed.\n"),
150 progname
, (int)(map_size
* sizeof(*map
)));
156 /* Try the xfsctl(XFS_IOC_GETBMAPX) for the number of extents specified
157 * by nflag, or the initial guess number of extents (32).
159 * If there are more extents than we guessed, use xfsctl
160 * (FS_IOC_FSGETXATTR[A]) to get the extent count, realloc some more
161 * space based on this count, and try again.
163 * If the initial FGETBMAPX attempt returns EINVAL, this may mean
164 * that we tried the FGETBMAPX on a zero length file. If we get
165 * EINVAL, check the length with fstat() and return "no extents"
166 * if the length == 0.
168 * Why not do the xfsctl(FS_IOC_FSGETXATTR[A]) first? Two reasons:
169 * (1) The extent count may be wrong for a file with delayed
170 * allocation blocks. The XFS_IOC_GETBMAPX forces the real
171 * allocation and fixes up the extent count.
172 * (2) For XFS_IOC_GETBMAP[X] on a DMAPI file that has been moved
173 * offline by a DMAPI application (e.g., DMF) the
174 * FS_IOC_FSGETXATTR only reflects the extents actually online.
175 * Doing XFS_IOC_GETBMAPX call first forces that data blocks online
176 * and then everything proceeds normally (see PV #545725).
178 * If you don't want this behavior on a DMAPI offline file,
179 * try the "-d" option which sets the BMV_IF_NO_DMAPI_READ
180 * iflag for XFS_IOC_GETBMAPX.
183 do { /* loop a miximum of two times */
185 memset(map
, 0, sizeof(*map
)); /* zero header */
187 map
->bmv_length
= -1;
188 map
->bmv_count
= map_size
;
189 map
->bmv_iflags
= bmv_iflags
;
191 i
= xfsctl(file
->name
, file
->fd
, XFS_IOC_GETBMAPX
, map
);
194 && !aflag
&& filesize() == 0) {
197 fprintf(stderr
, _("%s: xfsctl(XFS_IOC_GETBMAPX)"
198 " iflags=0x%x [\"%s\"]: %s\n"),
199 progname
, map
->bmv_iflags
, file
->name
,
208 if (map
->bmv_entries
< map
->bmv_count
-1)
210 /* Get number of extents from xfsctl FS_IOC_FSGETXATTR[A]
213 i
= xfsctl(file
->name
, file
->fd
, aflag
?
214 XFS_IOC_FSGETXATTRA
: FS_IOC_FSGETXATTR
, &fsx
);
216 fprintf(stderr
, "%s: xfsctl(FS_IOC_FSGETXATTR%s) "
217 "[\"%s\"]: %s\n", progname
, aflag
? "A" : "",
218 file
->name
, strerror(errno
));
223 if (2 * fsx
.fsx_nextents
> map_size
) {
224 map_size
= 2 * fsx
.fsx_nextents
+ 1;
225 map
= realloc(map
, map_size
*sizeof(*map
));
228 _("%s: cannot realloc %d bytes\n"),
229 progname
, (int)(map_size
*sizeof(*map
)));
234 } while (++loop
< 2);
236 if (map
->bmv_entries
<= 0) {
237 printf(_("%s: no extents\n"), file
->name
);
242 egcnt
= nflag
? min(nflag
, map
->bmv_entries
) : map
->bmv_entries
;
243 printf("%s:\n", file
->name
);
245 for (i
= 0; i
< egcnt
; i
++) {
246 printf("\t%d: [%lld..%lld]: ", i
,
247 (long long) map
[i
+ 1].bmv_offset
,
248 (long long)(map
[i
+ 1].bmv_offset
+
249 map
[i
+ 1].bmv_length
- 1LL));
250 if (map
[i
+ 1].bmv_block
== -1)
252 else if (map
[i
+ 1].bmv_block
== -2)
253 printf(_("delalloc"));
256 (long long) map
[i
+ 1].bmv_block
,
257 (long long)(map
[i
+ 1].bmv_block
+
258 map
[i
+ 1].bmv_length
- 1LL));
262 printf(_(" %lld blocks\n"),
263 (long long)map
[i
+1].bmv_length
);
269 * Verbose mode displays:
270 * extent: [startoffset..endoffset]: startblock..endblock \
271 * ag# (agoffset..agendoffset) totalbbs
273 #define MINRANGE_WIDTH 16
274 #define MINAG_WIDTH 2
275 #define MINTOT_WIDTH 5
276 #define NFLG 6 /* count of flags */
277 #define FLG_NULL 0000000 /* Null flag */
278 #define FLG_SHARED 0100000 /* shared extent */
279 #define FLG_PRE 0010000 /* Unwritten extent */
280 #define FLG_BSU 0001000 /* Not on begin of stripe unit */
281 #define FLG_ESU 0000100 /* Not on end of stripe unit */
282 #define FLG_BSW 0000010 /* Not on begin of stripe width */
283 #define FLG_ESW 0000001 /* Not on end of stripe width */
285 off64_t agoff
, bbperag
;
286 int foff_w
, boff_w
, aoff_w
, tot_w
, agno_w
;
287 char rbuf
[32], bbuf
[32], abuf
[32];
290 foff_w
= boff_w
= aoff_w
= MINRANGE_WIDTH
;
291 tot_w
= MINTOT_WIDTH
;
293 sunit
= swidth
= bbperag
= 0;
295 bbperag
= (off64_t
)fsgeo
.agblocks
*
296 (off64_t
)fsgeo
.blocksize
/ BBSIZE
;
297 sunit
= (fsgeo
.sunit
* fsgeo
.blocksize
) / BBSIZE
;
298 swidth
= (fsgeo
.swidth
* fsgeo
.blocksize
) / BBSIZE
;
303 * Go through the extents and figure out the width
304 * needed for all columns.
306 for (i
= 0; i
< egcnt
; i
++) {
307 snprintf(rbuf
, sizeof(rbuf
), "[%lld..%lld]:",
308 (long long) map
[i
+ 1].bmv_offset
,
309 (long long)(map
[i
+ 1].bmv_offset
+
310 map
[i
+ 1].bmv_length
- 1LL));
311 if (map
[i
+ 1].bmv_oflags
& BMV_OF_PREALLOC
)
313 if (map
[i
+ 1].bmv_block
== -1) {
314 foff_w
= max(foff_w
, strlen(rbuf
));
316 numlen(map
[i
+1].bmv_length
, 10));
318 snprintf(bbuf
, sizeof(bbuf
), "%lld..%lld",
319 (long long) map
[i
+ 1].bmv_block
,
320 (long long)(map
[i
+ 1].bmv_block
+
321 map
[i
+ 1].bmv_length
- 1LL));
322 boff_w
= max(boff_w
, strlen(bbuf
));
324 agno
= map
[i
+ 1].bmv_block
/ bbperag
;
325 agoff
= map
[i
+ 1].bmv_block
-
327 snprintf(abuf
, sizeof(abuf
),
331 map
[i
+ 1].bmv_length
- 1LL));
332 aoff_w
= max(aoff_w
, strlen(abuf
));
335 foff_w
= max(foff_w
, strlen(rbuf
));
337 numlen(map
[i
+1].bmv_length
, 10));
340 agno_w
= is_rt
? 0 : max(MINAG_WIDTH
, numlen(fsgeo
.agcount
, 10));
341 printf("%4s: %-*s %-*s %*s %-*s %*s%s\n",
343 foff_w
, _("FILE-OFFSET"),
344 boff_w
, is_rt
? _("RT-BLOCK-RANGE") : _("BLOCK-RANGE"),
345 agno_w
, is_rt
? "" : _("AG"),
346 aoff_w
, is_rt
? "" : _("AG-OFFSET"),
348 flg
? _(" FLAGS") : "");
349 for (i
= 0; i
< egcnt
; i
++) {
351 if (map
[i
+ 1].bmv_oflags
& BMV_OF_PREALLOC
) {
354 if (map
[i
+ 1].bmv_oflags
& BMV_OF_SHARED
)
356 if (map
[i
+ 1].bmv_oflags
& BMV_OF_DELALLOC
)
357 map
[i
+ 1].bmv_block
= -2;
359 * If striping enabled, determine if extent starts/ends
360 * on a stripe unit boundary.
363 if (map
[i
+ 1].bmv_block
% sunit
!= 0) {
366 if (((map
[i
+ 1].bmv_block
+
367 map
[i
+ 1].bmv_length
) % sunit
) != 0) {
370 if (map
[i
+ 1].bmv_block
% swidth
!= 0) {
373 if (((map
[i
+ 1].bmv_block
+
374 map
[i
+ 1].bmv_length
) % swidth
) != 0) {
378 snprintf(rbuf
, sizeof(rbuf
), "[%lld..%lld]:",
379 (long long) map
[i
+ 1].bmv_offset
,
380 (long long)(map
[i
+ 1].bmv_offset
+
381 map
[i
+ 1].bmv_length
- 1LL));
382 if (map
[i
+ 1].bmv_block
== -1) {
383 printf("%4d: %-*s %-*s %*s %-*s %*lld\n",
389 tot_w
, (long long)map
[i
+1].bmv_length
);
390 } else if (map
[i
+ 1].bmv_block
== -2) {
391 printf("%4d: %-*s %-*s %*s %-*s %*lld\n",
394 boff_w
, _("delalloc"),
397 tot_w
, (long long)map
[i
+1].bmv_length
);
399 snprintf(bbuf
, sizeof(bbuf
), "%lld..%lld",
400 (long long) map
[i
+ 1].bmv_block
,
401 (long long)(map
[i
+ 1].bmv_block
+
402 map
[i
+ 1].bmv_length
- 1LL));
403 printf("%4d: %-*s %-*s", i
, foff_w
, rbuf
,
406 agno
= map
[i
+ 1].bmv_block
/ bbperag
;
407 agoff
= map
[i
+ 1].bmv_block
-
409 snprintf(abuf
, sizeof(abuf
),
413 map
[i
+ 1].bmv_length
- 1LL));
414 printf(" %*d %-*s", agno_w
, agno
,
418 printf(" %*lld", tot_w
,
419 (long long)map
[i
+1].bmv_length
);
420 if (flg
== FLG_NULL
&& !pflag
) {
423 printf(" %-*.*o\n", NFLG
, NFLG
, flg
);
427 if ((flg
|| pflag
) && vflag
> 1) {
428 printf(_(" FLAG Values:\n"));
429 printf(_(" %*.*o Shared extent\n"),
430 NFLG
+1, NFLG
+1, FLG_SHARED
);
431 printf(_(" %*.*o Unwritten preallocated extent\n"),
432 NFLG
+1, NFLG
+1, FLG_PRE
);
433 printf(_(" %*.*o Doesn't begin on stripe unit\n"),
434 NFLG
+1, NFLG
+1, FLG_BSU
);
435 printf(_(" %*.*o Doesn't end on stripe unit\n"),
436 NFLG
+1, NFLG
+1, FLG_ESU
);
437 printf(_(" %*.*o Doesn't begin on stripe width\n"),
438 NFLG
+1, NFLG
+1, FLG_BSW
);
439 printf(_(" %*.*o Doesn't end on stripe width\n"),
440 NFLG
+1, NFLG
+1, FLG_ESW
);
450 bmap_cmd
.name
= "bmap";
451 bmap_cmd
.cfunc
= bmap_f
;
453 bmap_cmd
.argmax
= -1;
454 bmap_cmd
.flags
= CMD_NOMAP_OK
;
455 bmap_cmd
.args
= _("[-adlpv] [-n nx]");
456 bmap_cmd
.oneline
= _("print block mapping for an XFS file");
457 bmap_cmd
.help
= bmap_help
;
459 add_command(&bmap_cmd
);