Whamcloud - gitweb
Branch HEAD
[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 <obd.h>
22 #include <lustre_lib.h>
23 #include <lustre/liblustreapi.h>
24 #include <obd_lov.h>
25
26 #include <lnet/lnetctl.h>
27
28
29 #define MAX_LOV_UUID_COUNT      1000
30 union {
31         struct obd_uuid uuid;
32         char name[0];
33 } lov;
34 #define lov_uuid lov.uuid
35 #define lov_name lov.name
36
37 /* Returns bytes read on success and a negative value on failure.
38  * If zero bytes are read it will be treated as failure as such
39  * zero cannot be returned from this function.
40  */
41 int read_proc_entry(char *proc_path, char *buf, int len)
42 {
43         int rc, fd;
44
45         memset(buf, 0, len);
46
47         fd = open(proc_path, O_RDONLY);
48         if (fd == -1) {
49                 llapi_err(LLAPI_MSG_ERROR, "open('%s') failed: %s\n",
50                           proc_path);
51                 return -2;
52         }
53
54         rc = read(fd, buf, len - 1);
55         if (rc < 0) {
56                 llapi_err(LLAPI_MSG_ERROR, "read('%s') failed: %s\n",proc_path);
57                 rc = -3;
58         } else if (rc == 0) {
59                 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
60                           "read('%s') zero bytes\n", proc_path);
61                 rc = -4;
62         } else if (/* rc > 0 && */ buf[rc - 1] == '\n') {
63                 buf[rc - 1] = '\0'; /* Remove trailing newline */
64         }
65         close(fd);
66
67         return (rc);
68 }
69
70 int compare(struct lov_user_md *lum_dir, struct lov_user_md *lum_file1,
71             struct lov_user_md *lum_file2)
72 {
73         int stripe_count = 0, min_stripe_count = 0, def_stripe_count = 1;
74         int stripe_size = 0;
75         int stripe_offset = -1;
76         int ost_count;
77         char buf[128];
78         char lov_path[PATH_MAX];
79         char tmp_path[PATH_MAX];
80         int i;
81         FILE *fp;
82
83         fp = popen("\\ls -d  /proc/fs/lustre/lov/*clilov* | head -1", "r");
84         if (!fp) {
85                 llapi_err(LLAPI_MSG_ERROR,
86                           "open(lustre/lov/*clilov*) failed: %s\n");
87                 return 2;
88         }
89         if (fscanf(fp, "%s", lov_path) < 1) {
90                 llapi_err(LLAPI_MSG_ERROR,
91                           "read(lustre/lov/*clilov*) failed: %s\n");
92                 pclose(fp);
93                 return 3;
94         }
95         pclose(fp);
96
97         snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/stripecount",
98                  lov_path);
99         if (read_proc_entry(tmp_path, buf, sizeof(buf)) < 0)
100                 return 5;
101         def_stripe_count = (short)atoi(buf);
102
103         snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/numobd", lov_path);
104         if (read_proc_entry(tmp_path, buf, sizeof(buf)) < 0)
105                 return 6;
106         ost_count = atoi(buf);
107
108         if (lum_dir == NULL) {
109                 stripe_count = def_stripe_count;
110                 min_stripe_count = -1;
111         } else {
112                 stripe_count = (signed short)lum_dir->lmm_stripe_count;
113                 printf("dir stripe %d, ", stripe_count);
114                 min_stripe_count = 1;
115         }
116
117         printf("default stripe %d, ost count %d\n",
118                def_stripe_count, ost_count);
119
120         if (stripe_count == 0) {
121                 min_stripe_count = -1;
122                 stripe_count = 1;
123         }
124
125         stripe_count = (stripe_count > 0 && stripe_count <= ost_count) ?
126                                                 stripe_count : ost_count;
127         min_stripe_count = min_stripe_count > 0 ? stripe_count :
128                                                 ((stripe_count + 1) / 2);
129
130         if (lum_file1->lmm_stripe_count != stripe_count ||
131             lum_file1->lmm_stripe_count < min_stripe_count) {
132                 llapi_err(LLAPI_MSG_ERROR, "file1 stripe count %d != dir %d\n",
133                           lum_file1->lmm_stripe_count, stripe_count);
134                 return 7;
135         }
136
137         if (lum_file1->lmm_stripe_count < stripe_count)
138                 llapi_err(LLAPI_MSG_WARN, "warning: file1 used fewer stripes"
139                           " %d < dir %d (likely due to bug 4900)\n",
140                           lum_file1->lmm_stripe_count, stripe_count);
141
142         if (lum_dir != NULL)
143                 stripe_size = (int)lum_dir->lmm_stripe_size;
144         if (stripe_size == 0) {
145                 snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/stripesize",
146                          lov_path);
147                 if (read_proc_entry(tmp_path, buf, sizeof(buf)) < 0)
148                         return 5;
149
150                 stripe_size = atoi(buf);
151         }
152
153         if (lum_file1->lmm_stripe_size != stripe_size) {
154                 llapi_err(LLAPI_MSG_ERROR, "file1 stripe size %d != dir %d\n",
155                           lum_file1->lmm_stripe_size, stripe_size);
156                 return 8;
157         }
158
159         if (lum_dir != NULL)
160                 stripe_offset = (short int)lum_dir->lmm_stripe_offset;
161         if (stripe_offset != -1) {
162                 for (i = 0; i < stripe_count; i++)
163                         if (lum_file1->lmm_objects[i].l_ost_idx !=
164                             (stripe_offset + i) % ost_count) {
165                                 llapi_err(LLAPI_MSG_WARN,
166                                           "warning: file1 non-sequential "
167                                           "stripe[%d] %d != %d\n", i,
168                                           lum_file1->lmm_objects[i].l_ost_idx,
169                                           (stripe_offset + i) % ost_count);
170                         }
171         } else if (lum_file2 != NULL) {
172                 int next, idx, stripe = stripe_count - 1;
173                 next = (lum_file1->lmm_objects[stripe].l_ost_idx + 1) %
174                        ost_count;
175                 idx = lum_file2->lmm_objects[0].l_ost_idx;
176                 if (idx != next) {
177                         llapi_err(LLAPI_MSG_WARN, "warning: non-sequential "
178                                   "file1 stripe[%d] %d != file2 stripe[0] %d\n",
179                                   stripe, lum_file1->lmm_objects[stripe].l_ost_idx,
180                                   idx);
181                 }
182         }
183
184         return 0;
185 }
186
187 int main(int argc, char **argv)
188 {
189         DIR * dir;
190         struct lov_user_md *lum_dir, *lum_file1 = NULL, *lum_file2 = NULL;
191         int rc;
192         int lum_size;
193
194         if (argc < 3) {
195                 llapi_err(LLAPI_MSG_ERROR,
196                           "Usage: %s <dirname> <filename1> [filename2]\n",
197                           argv[0]);
198                 return 1;
199         }
200
201         dir = opendir(argv[1]);
202         if (dir == NULL) {
203                 rc = errno;
204                 llapi_err(LLAPI_MSG_ERROR,
205                           "error: %s opendir failed\n", argv[1]);
206                 return rc;
207         }
208
209         lum_size = lov_mds_md_size(MAX_LOV_UUID_COUNT);
210         if ((lum_dir = (struct lov_user_md *)malloc(lum_size)) == NULL) {
211                 rc = ENOMEM;
212                 llapi_err(LLAPI_MSG_ERROR, "error: can't allocate %d bytes "
213                           "for dir EA", lum_size);
214                 goto cleanup;
215         }
216
217         rc = llapi_file_get_stripe(argv[1], lum_dir);
218         if (rc) {
219                 if (errno == ENODATA) {
220                         free(lum_dir);
221                         lum_dir = NULL;
222                 } else {
223                         rc = errno;
224                         llapi_err(LLAPI_MSG_ERROR,
225                                   "error: can't get EA for %s\n", argv[1]);
226                         goto cleanup;
227                 }
228         }
229
230         /* XXX should be llapi_lov_getname() */
231         rc = llapi_file_get_lov_uuid(argv[1], &lov_uuid);
232         if (rc) {
233                 rc = errno;
234                 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name for %s\n",
235                           argv[1]);
236                 return rc;
237         }
238
239         if ((lum_file1 = (struct lov_user_md *)malloc(lum_size)) == NULL) {
240                 rc = ENOMEM;
241                 llapi_err(LLAPI_MSG_ERROR,
242                           "error: can't allocate %d bytes for EA\n", lum_size);
243                 goto cleanup;
244         }
245
246         rc = llapi_file_get_stripe(argv[2], lum_file1);
247         if (rc) {
248                 rc = errno;
249                 llapi_err(LLAPI_MSG_ERROR,
250                           "error: unable to get EA for %s\n", argv[2]);
251                 goto cleanup;
252         }
253
254         if (argc == 4) {
255                 lum_file2 = (struct lov_user_md *)malloc(lum_size);
256                 if (lum_file2 == NULL) {
257                         rc = ENOMEM;
258                         llapi_err(LLAPI_MSG_ERROR, "error: can't allocate %d "
259                                   "bytes for file2 EA\n", lum_size);
260                         goto cleanup;
261                 }
262
263                 rc = llapi_file_get_stripe(argv[3], lum_file2);
264                 if (rc) {
265                         rc = errno;
266                         llapi_err(LLAPI_MSG_ERROR,
267                                   "error: can't get EA for %s\n", argv[3]);
268                         goto cleanup;
269                 }
270         }
271
272         rc = compare(lum_dir, lum_file1, lum_file2);
273
274 cleanup:
275         closedir(dir);
276         if (lum_dir != NULL)
277                 free(lum_dir);
278         if (lum_file1 != NULL)
279                 free(lum_file1);
280         if (lum_file2 != NULL)
281                 free(lum_file2);
282
283         return rc;
284 }