Whamcloud - gitweb
Merge "LU-9121 lnet: User Defined Selection Policy (UDSP)"
[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                         flags = strtoul(optarg, &end, 0);
41                         if (*end != '\0') {
42                                 fprintf(stderr,
43                                         "%s: invalid flags '%s'\n", argv[0],
44                                         optarg);
45                                 exit(1);
46                         }
47                         break;
48                 case 'h':
49                         fprintf(stderr,
50                                 "Usage: %s -f <filename> -x <LOV EA content>\n",
51                                 argv[0]);
52                         break;
53                 }
54         }
55
56         len = strlen(xval);
57         if (len > XATTR_SIZE_MAX || len <= 0) {
58                 fprintf(stderr,
59                         "invalid LOV EA length %zu > XATTR_SIZE_MAX (%u)\n",
60                         len, XATTR_SIZE_MAX);
61                 exit(1);
62         }
63
64         fd = open(fname, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, 0644);
65         if (fd == -1) {
66                 perror("open()");
67                 exit(1);
68         }
69
70         lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
71         if (lfm == NULL) {
72                 perror("malloc()");
73                 exit(1);
74         }
75
76         lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
77         lfm->lfm_length = len;
78         lfm->lfm_type = type;
79         lfm->lfm_flags = flags;
80         memcpy(lfm->lfm_value, xval, len);
81
82         if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
83                 perror("ioctl(LL_IOC_LOV_SETSTRIPE)");
84                 exit(1);
85         }
86
87         close(fd);
88         return 0;
89 }