Whamcloud - gitweb
LU-15152 tests: auster reports wrong testsuite status
[fs/lustre-release.git] / lustre / tests / create_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
10 #include <lustre/lustreapi.h>
11
12 int main(int argc, char **argv)
13 {
14         int c, fd;
15         char *fname = "FILE";
16         char *xval = "UUID@UUID";
17         size_t len;
18         struct lov_foreign_md *lfm;
19         char *end;
20         __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0xda05;
21
22         while ((c = getopt(argc, argv, "f:x:t:F:")) != -1) {
23                 switch (c) {
24                 case 'f':
25                         fname = optarg;
26                         break;
27                 case 'x':
28                         xval = optarg;
29                         break;
30                 case 't':
31                         type = strtoul(optarg, &end, 0);
32                         if (*end != '\0') {
33                                 fprintf(stderr,
34                                         "%s: invalid type '%s'\n", argv[0],
35                                         optarg);
36                                 exit(1);
37                         }
38                         break;
39                 case 'F':
40                         errno = 0;
41                         flags = strtoul(optarg, &end, 0);
42                         if (errno != 0 || *end != '\0' ||
43                             flags >= UINT32_MAX) {
44                                 fprintf(stderr,
45                                         "%s: invalid flags '%s'\n", argv[0],
46                                         optarg);
47                                 exit(1);
48                         }
49                         break;
50                 case 'h':
51                         fprintf(stderr,
52                                 "Usage: %s -f <filename> -x <LOV EA content>\n",
53                                 argv[0]);
54                         break;
55                 }
56         }
57
58         len = strlen(xval);
59         if (len > XATTR_SIZE_MAX || len <= 0) {
60                 fprintf(stderr,
61                         "invalid LOV EA length %zu > XATTR_SIZE_MAX (%u)\n",
62                         len, XATTR_SIZE_MAX);
63                 exit(1);
64         }
65
66         fd = open(fname, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, 0644);
67         if (fd == -1) {
68                 perror("open()");
69                 exit(1);
70         }
71
72         lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
73         if (lfm == NULL) {
74                 perror("malloc()");
75                 exit(1);
76         }
77
78         lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
79         lfm->lfm_length = len;
80         lfm->lfm_type = type;
81         lfm->lfm_flags = flags;
82         memcpy(lfm->lfm_value, xval, len);
83
84         if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
85                 perror("ioctl(LL_IOC_LOV_SETSTRIPE)");
86                 exit(1);
87         }
88
89         close(fd);
90         return 0;
91 }