Whamcloud - gitweb
b=8007
[fs/lustre-release.git] / lustre / tests / ll_dirstripe_verify.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * ll_dirstripe_verify <dir> <file>:
5  * - to verify if the file has the same lov_user_md setting as the parent dir.
6  * - if dir's offset is set -1, ll_dirstripe_verify <dir> <file1> <file2>
7  *      is used to further verify if file1 and file2's obdidx is continuous.
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <sys/ioctl.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <dirent.h>
19
20 #include <liblustre.h>
21 #include <linux/obd.h>
22 #include <linux/lustre_lib.h>
23 #include <lustre/lustre_user.h>
24 #include <linux/obd_lov.h>
25
26 #include <lnet/lnetctl.h>
27
28
29 #define MAX_LOV_UUID_COUNT      1000
30
31 int read_proc_entry(char *proc_path, char *buf, int len)
32 {
33         int rcnt = -2, fd;
34
35         if ((fd = open(proc_path, O_RDONLY)) == -1) {
36                 fprintf(stderr, "open('%s') failed: %s\n",
37                         proc_path, strerror(errno));
38                 rcnt = -3;
39         } else if ((rcnt = read(fd, buf, len)) <= 0) {
40                 fprintf(stderr, "read('%s') failed: %s\n",
41                         proc_path, strerror(errno));
42         } else {
43                 buf[rcnt - 1] = '\0';
44         }
45
46         if (fd >= 0)
47                 close(fd);
48
49         return (rcnt);
50 }
51
52 int compare(struct lov_user_md *lum_dir, struct lov_user_md *lum_file1,
53             struct lov_user_md *lum_file2)
54 {
55         int stripe_count = 0;
56         int stripe_size = 0;
57         int stripe_offset = -1;
58         int ost_count;
59         char buf[128];
60         char lov_path[PATH_MAX];
61         char tmp_path[PATH_MAX];
62         int i, rc;
63
64         rc = read_proc_entry("/proc/fs/lustre/llite/fs0/lov/common_name",
65                              buf, sizeof(buf)) <= 0;
66         if (rc < 0)
67                 return -rc;
68
69         snprintf(lov_path, sizeof(lov_path) - 1, "/proc/fs/lustre/lov/%s", buf);
70
71         if (lum_dir == NULL) {
72                 snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/stripecount",
73                          lov_path);
74                 if (read_proc_entry(tmp_path, buf, sizeof(buf)) <= 0)
75                         return 5;
76
77                 stripe_count = atoi(buf);
78         } else {
79                 stripe_count = (int)lum_dir->lmm_stripe_count;
80         }
81         if (stripe_count == 0)
82                 stripe_count = 1;
83
84         snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/numobd", lov_path);
85         if (read_proc_entry(tmp_path, buf, sizeof(buf)) <= 0)
86                 return 6;
87
88         ost_count = atoi(buf);
89         stripe_count = stripe_count > 0 ? stripe_count : ost_count;
90
91         if (lum_file1->lmm_stripe_count != stripe_count) {
92                 fprintf(stderr, "file1 stripe count %d != dir %d\n",
93                         lum_file1->lmm_stripe_count, stripe_count);
94                 return 7;
95         }
96
97         if (lum_dir != NULL)
98                 stripe_size = (int)lum_dir->lmm_stripe_size;
99         if (stripe_size == 0) {
100                 snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/stripesize",
101                          lov_path);
102                 if (read_proc_entry(tmp_path, buf, sizeof(buf)) <= 0)
103                         return 5;
104
105                 stripe_size = atoi(buf);
106         }
107
108         if (lum_file1->lmm_stripe_size != stripe_size) {
109                 fprintf(stderr, "file1 stripe size %d != dir %d\n",
110                         lum_file1->lmm_stripe_size, stripe_size);
111                 return 8;
112         }
113
114         if (lum_dir != NULL)
115                 stripe_offset = (short int)lum_dir->lmm_stripe_offset;
116         if (stripe_offset != -1) {
117                 for (i = 0; i < stripe_count; i++)
118                         if (lum_file1->lmm_objects[i].l_ost_idx !=
119                             (stripe_offset + i) % ost_count) {
120                                 fprintf(stderr, "warning: file1 non-sequential "
121                                         "stripe[%d] %d != %d\n", i,
122                                         lum_file1->lmm_objects[i].l_ost_idx,
123                                         (stripe_offset + i) % ost_count);
124                         }
125         } else if (lum_file2 != NULL) {
126                 int next, idx, stripe = stripe_count - 1;
127                 next = (lum_file1->lmm_objects[stripe].l_ost_idx + 1) %
128                        ost_count;
129                 idx = lum_file2->lmm_objects[0].l_ost_idx;
130                 if (idx != next) {
131                         fprintf(stderr, "warning: non-sequential "
132                                 "file1 stripe[%d] %d != file2 stripe[0] %d\n",
133                                 stripe,
134                                 lum_file1->lmm_objects[stripe].l_ost_idx, idx);
135                 }
136         }
137
138         return 0;
139 }
140
141 int main(int argc, char **argv)
142 {
143         DIR * dir;
144         struct lov_user_md *lum_dir, *lum_file1 = NULL, *lum_file2 = NULL;
145         int rc;
146         int lum_size;
147         char *fname;
148
149         if (argc < 3) {
150                 fprintf(stderr, "Usage: %s <dirname> <filename1> [filename2]\n",
151                         argv[0]);
152                 exit(1);
153         }
154
155         dir = opendir(argv[1]);
156         if (dir == NULL) {
157                 fprintf(stderr, "%s opendir failed\n", argv[1]);
158                 return errno;
159         }
160
161         lum_size = lov_mds_md_size(MAX_LOV_UUID_COUNT);
162         if ((lum_dir = (struct lov_user_md *)malloc(lum_size)) == NULL) {
163                 fprintf(stderr, "unable to allocate memory for ioctl's");
164                 return errno;
165         }
166
167         rc = ioctl(dirfd(dir), LL_IOC_LOV_GETSTRIPE, lum_dir);
168         if (rc) {
169                 if (errno == ENODATA) {
170                         free(lum_dir);
171                         lum_dir = NULL;
172                 } else {
173                         rc = errno;
174                         goto cleanup;
175                 }
176         }
177
178         if ((lum_file1 = (struct lov_user_md *)malloc(lum_size)) == NULL) {
179                 fprintf(stderr, "unable to allocate memory for ioctl's");
180                 rc = errno;
181                 goto cleanup;
182         }
183
184         fname = strrchr(argv[2], '/');
185         fname++;
186         strncpy((char *)lum_file1, fname, lum_size);
187         rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, lum_file1);
188         if (rc) {
189                 rc = errno;
190                 goto cleanup;
191         }
192
193         if (argc == 4) {
194                 lum_file2 = (struct lov_user_md *)malloc(lum_size);
195                 if (lum_file2 == NULL) {
196                         fprintf(stderr,
197                                 "unable to allocate memory for ioctl's");
198                         rc = errno;
199                         goto cleanup;
200                 }
201
202                 fname = strrchr(argv[3], '/');
203                 fname++;
204                 strncpy((char *)lum_file2, fname, lum_size);
205                 rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, lum_file2);
206                 if (rc) {
207                         rc = errno;
208                         goto cleanup;
209                 }
210         }
211
212         rc = compare(lum_dir, lum_file1, lum_file2);
213
214 cleanup:
215         if (lum_dir != NULL)
216                 free(lum_dir);
217         if (lum_file1 != NULL)
218                 free(lum_file1);
219         if (lum_file2 != NULL)
220                 free(lum_file2);
221
222         return rc;
223 }