common/rc: factor out _scratch_xfs_[get|set]_sb_field
[xfstests-dev.git] / src / t_stripealign.c
1 /*
2  * t_stripealign.c
3  *
4  * Print whether the file start block is stripe-aligned.
5  *
6  * Copyright (c) 2010 Eric Sandeen <sandeen@sandeen.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  */
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <sys/ioctl.h>
20
21 #define FIBMAP          _IO(0x00, 1)    /* bmap access */
22
23 /*
24  * If only filename given, print first block.
25  *
26  * If filename & sunit (in blocks) given, print whether we are well-aligned
27  */
28
29 int main(int argc, char ** argv)
30 {
31         int     fd;
32         int     ret;
33         int     sunit = 0;      /* in blocks */
34         char    *filename;
35         unsigned int    block = 0;
36
37         if (argc < 3) {
38                 printf("Usage: %s <filename> <sunit in blocks>\n", argv[0]);
39                 return 1;
40         }
41
42         filename = argv[1];
43         sunit = atoi(argv[2]);
44
45         fd = open(filename, O_RDONLY);
46         if (fd < 0) {
47                 perror("can't open file\n");
48                 return 1;
49         }
50
51         ret = ioctl(fd, FIBMAP, &block);
52         if (ret < 0) {
53                 close(fd);
54                 perror("fibmap");
55                 return 1;
56         }
57
58         close(fd);
59
60         if (block % sunit) {
61                 printf("%s: Start block %u not multiple of sunit %u\n",
62                         filename, block, sunit);
63                 return 1;
64         } else
65                 printf("%s: well-aligned\n", filename);
66
67         return 0;
68 }