Whamcloud - gitweb
b=4834
[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 <portals/ptlctl.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 = -1, 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 = -1;
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;
56         int stripe_size;
57         int stripe_offset;
58         int ost_count;
59         char buf[128];
60         char lov_path[PATH_MAX];
61         char tmp_path[PATH_MAX];
62         int i;
63
64         if (read_proc_entry("/proc/fs/lustre/llite/fs0/lov/common_name",
65                             buf, sizeof(buf)) <= 0)
66                 return -1;
67
68         snprintf(lov_path, sizeof(lov_path) - 1, "/proc/fs/lustre/lov/%s", buf);
69
70         stripe_count = (int)lum_dir->lmm_stripe_count;
71         if (stripe_count == 0) {
72                 snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/stripecount", lov_path);
73                 if (read_proc_entry(tmp_path, buf, sizeof(buf)) <= 0)
74                         return -1;
75
76                 stripe_count = atoi(buf);
77         }
78
79         stripe_size = (int)lum_dir->lmm_stripe_size;
80         if (stripe_size == 0) {
81                 snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/stripesize", lov_path);
82                 if (read_proc_entry(tmp_path, buf, sizeof(buf)) <= 0)
83                         return -1;
84
85                 stripe_size = atoi(buf);
86         }
87
88         snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/numobd", lov_path);
89         if (read_proc_entry(tmp_path, buf, sizeof(buf)) <= 0)
90                 return -1;
91
92         ost_count = atoi(buf);
93         stripe_count = stripe_count ? stripe_count : ost_count;
94
95         if ((lum_file1->lmm_stripe_count != stripe_count) ||
96             (lum_file1->lmm_stripe_size != stripe_size))
97                 return -1;
98
99         stripe_offset = (short int)lum_dir->lmm_stripe_offset;
100         if (stripe_offset != -1) {
101                 for (i = 0; i < stripe_count; i++)
102                         if (lum_file1->lmm_objects[i].l_ost_idx !=
103                             (stripe_offset + i) % ost_count)
104                                 return -1;
105         } else if (lum_file2 != NULL) {
106                 int next, idx;
107                 next = (lum_file1->lmm_objects[stripe_count-1].l_ost_idx + 1)
108                        % ost_count;
109                 idx = lum_file2->lmm_objects[0].l_ost_idx;
110                 if (idx != next)
111                         return -1;
112         }
113
114         return 0;
115 }
116
117 int main(int argc, char **argv)
118 {
119         DIR * dir;
120         struct lov_user_md *lum_dir, *lum_file1 = NULL, *lum_file2 = NULL;
121         int rc;
122         int lum_size;
123         char *fname;
124
125         if (argc < 3) {
126                 fprintf(stderr, "Usage: %s <dirname> <filename1> [filename2]\n",
127                         argv[0]);
128                 exit(1);
129         }
130
131         dir = opendir(argv[1]);
132         if (dir  == NULL) {
133                 fprintf(stderr, "%s opendir failed\n", argv[1]);
134                 return errno;
135         }
136
137         lum_size = lov_mds_md_size(MAX_LOV_UUID_COUNT);
138         if ((lum_dir = (struct lov_user_md *)malloc(lum_size)) == NULL) {
139                 fprintf(stderr, "unable to allocate memory for ioctl's");
140                 return errno;
141         }
142
143         rc = ioctl(dirfd(dir), LL_IOC_LOV_GETSTRIPE, lum_dir);
144         if (rc) {
145                 if (errno == ENODATA) {
146                         lum_dir->lmm_stripe_size = 0;
147                         lum_dir->lmm_stripe_count = 0;
148                         lum_dir->lmm_stripe_offset = -1;
149                 } else {
150                         rc = errno;
151                         goto cleanup;
152                 }
153         }
154
155         if ((lum_file1 = (struct lov_user_md *)malloc(lum_size)) == NULL) {
156                 fprintf(stderr, "unable to allocate memory for ioctl's");
157                 rc = errno;
158                 goto cleanup;
159         }
160
161         fname = strrchr(argv[2], '/');
162         fname++;
163         strncpy((char *)lum_file1, fname, lum_size);
164         rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, lum_file1);
165         if (rc) {
166                 rc = errno;
167                 goto cleanup;
168         }
169
170         if (argc == 4) {
171                 lum_file2 = (struct lov_user_md *)malloc(lum_size);
172                 if (lum_file2 == NULL) {
173                         fprintf(stderr,
174                                 "unable to allocate memory for ioctl's");
175                         rc = errno;
176                         goto cleanup;
177                 }
178
179                 fname = strrchr(argv[3], '/');
180                 fname++;
181                 strncpy((char *)lum_file2, fname, lum_size);
182                 rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, lum_file2);
183                 if (rc) {
184                         rc = errno;
185                         goto cleanup;
186                 }
187         }
188
189         rc = compare(lum_dir, lum_file1, lum_file2);
190
191 cleanup:
192         if (lum_dir != NULL)
193                 free(lum_dir);
194         if (lum_file1 != NULL)
195                 free(lum_file1);
196         if (lum_file2 != NULL)
197                 free(lum_file2);
198
199         return rc;
200 }