1 // SPDX-License-Identifier: GPL-2.0+
4 * Print whether the file start block is stripe-aligned.
5 * Copyright (c) 2010 Eric Sandeen <sandeen@sandeen.net>
13 #include <sys/ioctl.h>
14 #include <linux/fiemap.h>
17 #ifndef FIEMAP_EXTENT_SHARED
18 # define FIEMAP_EXTENT_SHARED 0x00002000
21 #define FIEMAP_EXTENT_ACCEPTABLE (FIEMAP_EXTENT_LAST | \
22 FIEMAP_EXTENT_DATA_ENCRYPTED | FIEMAP_EXTENT_ENCODED | \
23 FIEMAP_EXTENT_UNWRITTEN | FIEMAP_EXTENT_MERGED | \
27 * If only filename given, print first block.
29 * If filename & sunit (in blocks) given, print whether we are well-aligned
32 int main(int argc, char ** argv)
36 struct fiemap_extent *fe;
39 int sunit = 0; /* in blocks */
41 unsigned long long block;
44 printf("Usage: %s <filename> <sunit in blocks>\n", argv[0]);
49 sunit = atoi(argv[2]);
51 fd = open(filename, O_RDONLY);
53 perror("can't open file\n");
57 ret = fstatfs(fd, &sb);
64 fie = calloc(1, sizeof(struct fiemap) + sizeof(struct fiemap_extent));
71 fie->fm_flags = FIEMAP_FLAG_SYNC;
72 fie->fm_extent_count = 1;
74 ret = ioctl(fd, FS_IOC_FIEMAP, fie);
76 unsigned int bmap = 0;
78 ret = ioctl(fd, FIBMAP, &bmap);
83 fprintf(stderr, "fibmap returned no result\n");
93 if (fie->fm_mapped_extents != 1) {
94 printf("%s: no extents?\n", filename);
99 fe = &fie->fm_extents[0];
100 if (fe->fe_flags & ~FIEMAP_EXTENT_ACCEPTABLE) {
101 printf("%s: bad flags 0x%x\n", filename, fe->fe_flags);
107 block = fie->fm_extents[0].fe_physical / sb.f_bsize;
110 printf("%s: Start block %llu not multiple of sunit %u\n",
111 filename, block, sunit);
114 printf("%s: well-aligned\n", filename);