Whamcloud - gitweb
LU-13474 gss: do not return -ERESTART when gss rpc times out
[fs/lustre-release.git] / lustre / tests / parse_foreign_dir.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 <linux/lustre/lustre_idl.h>
14
15 int main(int argc, char **argv)
16 {
17         int c, i;
18         char *dname = "DIR";
19         size_t len, len2;
20         struct lmv_foreign_md *lfm;
21
22         while ((c = getopt(argc, argv, "d:")) != -1) {
23                 switch (c) {
24                 case 'd':
25                         dname = optarg;
26                         break;
27                 case 'h':
28                         fprintf(stderr, "Usage: %s -d <dirname>\n", argv[0]);
29                         break;
30                 }
31         }
32
33         len = getxattr(dname, "trusted.lmv", NULL, 0);
34         if (len == -1) {
35                 perror("getxattr()");
36                 exit(1);
37         }
38         if (len > XATTR_SIZE_MAX || len <= 0) {
39                 fprintf(stderr, "invalid LMV EA length %zu "
40                         "(must be 0 < len < "
41                         "XATTR_SIZE_MAX(%u))\n", len,
42                         XATTR_SIZE_MAX);
43                 exit(1);
44         }
45
46         lfm = malloc(len);
47         if (lfm == NULL) {
48                 perror("malloc()");
49                 exit(1);
50         }
51
52         len2 = getxattr(dname, "trusted.lmv", lfm, len);
53         if (len2 == -1) {
54                 perror("getxattr()");
55                 exit(1);
56         }
57
58         if (len != len2)
59                 fprintf(stderr, "trusted.lmv xattr size changed, before=%zu "
60                         "now=%zu\n", len, len2);
61
62         if (len2 < offsetof(struct lmv_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 != LMV_MAGIC_FOREIGN) {
72                 if (lfm->lfm_magic == bswap_32(LMV_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, "wrong internal length=%u(0x%x) vs "
84                                 "xattr size=%zu\n", lfm->lfm_length,
85                                 lfm->lfm_length, len2);
86         }
87
88         if (lfm->lfm_magic == bswap_32(LMV_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, "lmv_xattr_size: %zu\n", len2);
96         fprintf(stdout, "lmv_foreign_magic: 0x%x\n", lfm->lfm_magic);
97         fprintf(stdout, "lmv_foreign_size: %u\n", lfm->lfm_length);
98         fprintf(stdout, "lmv_foreign_type: %u\n", lfm->lfm_type);
99         fprintf(stdout, "lmv_foreign_flags: %u\n", lfm->lfm_flags);
100         fprintf(stdout, "lmv_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 }