generic/574: fix sporadic failure with test_dummy_encryption
[xfstests-dev.git] / src / t_stripealign.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * t_stripealign.c
4  * Print whether the file start block is stripe-aligned.
5  * Copyright (c) 2010 Eric Sandeen <sandeen@sandeen.net>
6  */
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/vfs.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <sys/ioctl.h>
14 #include <linux/fiemap.h>
15 #include <linux/fs.h>
16
17 #ifndef FIEMAP_EXTENT_SHARED
18 # define FIEMAP_EXTENT_SHARED   0x00002000
19 #endif
20
21 #define FIEMAP_EXTENT_ACCEPTABLE        (FIEMAP_EXTENT_LAST | \
22                 FIEMAP_EXTENT_DATA_ENCRYPTED | FIEMAP_EXTENT_ENCODED | \
23                 FIEMAP_EXTENT_UNWRITTEN | FIEMAP_EXTENT_MERGED | \
24                 FIEMAP_EXTENT_SHARED)
25
26 /*
27  * If only filename given, print first block.
28  *
29  * If filename & sunit (in blocks) given, print whether we are well-aligned
30  */
31
32 int main(int argc, char ** argv)
33 {
34         struct statfs           sb;
35         struct fiemap           *fie;
36         struct fiemap_extent    *fe;
37         int                     fd;
38         int                     ret;
39         int                     sunit = 0;      /* in blocks */
40         char                    *filename;
41         unsigned long long      block;
42
43         if (argc < 3) {
44                 printf("Usage: %s <filename> <sunit in blocks>\n", argv[0]);
45                 return 1;
46         }
47
48         filename = argv[1];
49         sunit = atoi(argv[2]);
50
51         fd = open(filename, O_RDONLY);
52         if (fd < 0) {
53                 perror("can't open file\n");
54                 return 1;
55         }
56
57         ret = fstatfs(fd, &sb);
58         if (ret) {
59                 perror(filename);
60                 close(fd);
61                 return 1;
62         }
63
64         fie = calloc(1, sizeof(struct fiemap) + sizeof(struct fiemap_extent));
65         if (!fie) {
66                 close(fd);
67                 perror("malloc");
68                 return 1;
69         }
70         fie->fm_length = 1;
71         fie->fm_flags = FIEMAP_FLAG_SYNC;
72         fie->fm_extent_count = 1;
73
74         ret = ioctl(fd, FS_IOC_FIEMAP, fie);
75         if (ret < 0) {
76                 unsigned int    bmap = 0;
77
78                 ret = ioctl(fd, FIBMAP, &bmap);
79                 if (ret <= 0) {
80                         if (ret < 0)
81                                 perror("fibmap");
82                         else
83                                 fprintf(stderr, "fibmap returned no result\n");
84                         free(fie);
85                         close(fd);
86                         return 1;
87                 }
88                 block = bmap;
89                 goto check;
90         }
91
92
93         if (fie->fm_mapped_extents != 1) {
94                 printf("%s: no extents?\n", filename);
95                 free(fie);
96                 close(fd);
97                 return 1;
98         }
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);
102                 free(fie);
103                 close(fd);
104                 return 1;
105         }
106
107         block = fie->fm_extents[0].fe_physical / sb.f_bsize;
108 check:
109         if (block % sunit) {
110                 printf("%s: Start block %llu not multiple of sunit %u\n",
111                         filename, block, sunit);
112                 return 1;
113         } else
114                 printf("%s: well-aligned\n", filename);
115         free(fie);
116         close(fd);
117
118         return 0;
119 }