Whamcloud - gitweb
LU-14876 out: don't connect to busy MDS-MDS export
[fs/lustre-release.git] / lustre / tests / foreign_symlink_striping.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <libgen.h>
7 #include <sys/ioctl.h>
8 #include <sys/xattr.h>
9 #include <sys/file.h>
10
11 #include <lustre/lustreapi.h>
12 #include <linux/lustre/lustre_idl.h>
13
14 int main(int argc, char **argv)
15 {
16         char *foreign = NULL;
17         int c, rc, fd;
18         bool f_opt = false, d_opt = false, h_opt = false;
19         /* buf must be large enough to receive biggest possible
20          * foreign LOV/LMV
21          */
22         char buf[XATTR_SIZE_MAX];
23         struct lmv_foreign_md *lfm = (void *)buf;
24
25         while ((c = getopt(argc, argv, "hf:d:")) != -1) {
26                 switch (c) {
27                 case 'd':
28                         foreign = optarg;
29                         if (f_opt || d_opt) {
30                                 fprintf(stderr,
31                                         "only one foreign symlink file or dir can be specified at a time\n");
32                                 exit(1);
33                         }
34                         d_opt = true;
35                         break;
36                 case 'f':
37                         foreign = optarg;
38                         if (f_opt || d_opt) {
39                                 fprintf(stderr,
40                                         "only one foreign symlink file or dir can be specified at a time\n");
41                                 exit(1);
42                         }
43                         f_opt = true;
44                         break;
45                 case 'h':
46                         h_opt = true;
47                 default:
48                         fprintf(stderr,
49                                 "Usage: %s [-[f,d] <foreign file/dir pathname>]\n",
50                                 argv[0]);
51                         exit(h_opt ? 0 : 1);
52                         break;
53                 }
54         }
55
56         if (foreign == NULL) {
57                 fprintf(stderr,
58                         "a foreign file/dir pathname must be provided\n");
59                 exit(0);
60         }
61
62         /* in case foreign fake symlink feature is active, file/dir must be
63          * opened with O_NOFOLLOW to avoid symlink resolution
64          */
65         fd = open(foreign, O_RDONLY|O_NONBLOCK|O_NOFOLLOW);
66         if (fd < 0) {
67                 fprintf(stderr, "open() of '%s' error, rc : %d\n", foreign, fd);
68                 perror("open()");
69                 exit(1);
70         }
71
72         rc = snprintf(buf, PATH_MAX, "%s", foreign);
73         if (rc >= PATH_MAX || rc < 0) {
74                 fprintf(stderr,
75                         "unexpected return code or size from snprintf() : %d\n",
76                         rc);
77                 exit(1);
78         }
79
80         if (f_opt) {
81                 rc = ioctl(fd, LL_IOC_LOV_GETSTRIPE, &buf);
82         } else if (d_opt) {
83                 lfm->lfm_magic = LMV_MAGIC_V1;
84                 rc = ioctl(fd, LL_IOC_LMV_GETSTRIPE, &buf);
85         }
86
87         if (rc) {
88                 fprintf(stderr, "%s: %s error: %s\n", foreign,
89                         f_opt ? "getstripe" : "getdirstripe", strerror(errno));
90                 exit(1);
91         }
92
93         if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN &&
94             lfm->lfm_magic != LMV_MAGIC_FOREIGN)
95                 fprintf(stderr, "unexpected magic : 0x%08X, expected 0x%08X\n",
96                         lfm->lfm_magic, LOV_USER_MAGIC_FOREIGN);
97         if (lfm->lfm_type != LU_FOREIGN_TYPE_SYMLINK)
98                 fprintf(stderr, "unexpected type : 0x%08X, expected 0x%08X\n",
99                         lfm->lfm_type, LU_FOREIGN_TYPE_SYMLINK);
100         printf("lfm_magic: 0x%08X, lfm_length: %u, lfm_type: 0x%08X, lfm_flags: 0x%08X, lfm_value: '%.*s'\n",
101                lfm->lfm_magic, lfm->lfm_length, lfm->lfm_type, lfm->lfm_flags,
102                lfm->lfm_length, lfm->lfm_value);
103
104         return rc;
105 }