Whamcloud - gitweb
LU-14155 mdc: avoid easize set to 0
[fs/lustre-release.git] / lustre / tests / parse_foreign_file.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <sys/ioctl.h>
7 #include <sys/xattr.h>
8 #include <sys/file.h>
9 #include <sys/types.h>
10 #include <sys/xattr.h>
11 #include <byteswap.h>
12
13 #include <lustre/lustreapi.h>
14
15 int main(int argc, char **argv)
16 {
17         int c, i;
18         char *fname = "FILE";
19         size_t len, len2;
20         struct lov_foreign_md *lfm;
21
22         while ((c = getopt(argc, argv, "f:")) != -1) {
23                 switch (c) {
24                 case 'f':
25                         fname = optarg;
26                         break;
27                 case 'h':
28                         fprintf(stderr, "Usage: %s -f <filename>\n", argv[0]);
29                         break;
30                 }
31         }
32
33         len = getxattr(fname, "lustre.lov", NULL, 0);
34         if (len == -1) {
35                 perror("getxattr()");
36                 exit(1);
37         }
38         if (len > XATTR_SIZE_MAX || len <= 0) {
39                 fprintf(stderr,
40                         "invalid LOV EA length %zu > XATTR_SIZE_MAX (%u)\n",
41                         len, XATTR_SIZE_MAX);
42                 exit(1);
43         }
44
45         lfm = malloc(len);
46         if (lfm == NULL) {
47                 perror("malloc()");
48                 exit(1);
49         }
50
51         len2 = getxattr(fname, "lustre.lov", lfm, len);
52         if (len2 == -1) {
53                 perror("getxattr()");
54                 exit(1);
55         }
56
57         if (len != len2)
58                 fprintf(stderr,
59                         "trusted.lov xattr size changed, before=%zu now=%zu\n",
60                         len, len2);
61
62         if (len2 < offsetof(struct lov_foreign_md, lfm_value)) {
63                 fprintf(stderr, "trusted.lov size=%zu too small\n", len2);
64                 fprintf(stderr, "printing its content in hex anyway:\n");
65                 for (i = 0; i < len2; i++)
66                         fprintf(stderr, "%02x", *((char *)lfm + i));
67                 exit(1);
68         }
69
70
71         if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
72                 if (lfm->lfm_magic == bswap_32(LOV_USER_MAGIC_FOREIGN))
73                         fprintf(stderr, "magic is swapped\n");
74                 else
75                         fprintf(stderr, "wrong magic=(0x%x)\n", lfm->lfm_magic);
76         }
77
78         if (lfm->lfm_length != len2 - offsetof(typeof(*lfm), lfm_value)) {
79                 if (bswap_32(lfm->lfm_length) == len2 - offsetof(typeof(*lfm),
80                     lfm_value))
81                         fprintf(stderr, "length is swapped\n");
82                 else
83                         fprintf(stderr,
84                                 "wrong internal length=%u vs xattr size=%zu\n",
85                                 lfm->lfm_length, len2);
86         }
87
88         if (lfm->lfm_magic == bswap_32(LOV_USER_MAGIC_FOREIGN)) {
89                 lfm->lfm_magic = bswap_32(lfm->lfm_magic);
90                 lfm->lfm_length = bswap_32(lfm->lfm_length);
91                 lfm->lfm_type = bswap_32(lfm->lfm_type);
92                 lfm->lfm_flags = bswap_32(lfm->lfm_flags);
93         }
94
95         fprintf(stdout, "lov_xattr_size: %zu\n", len2);
96         fprintf(stdout, "lov_foreign_magic: 0x%08X\n", lfm->lfm_magic);
97         fprintf(stdout, "lov_foreign_size: %u\n", lfm->lfm_length);
98         fprintf(stdout, "lov_foreign_type: %u\n", lfm->lfm_type);
99         fprintf(stdout, "lov_foreign_flags: 0x%08X\n", lfm->lfm_flags);
100         fprintf(stdout, "lov_foreign_value: 0x");
101         for (i = 0; i < len2 - offsetof(typeof(*lfm), lfm_value); i++)
102                 fprintf(stdout, "%02x", lfm->lfm_value[i]);
103
104         fprintf(stdout, "\n");
105
106         return 0;
107 }