Whamcloud - gitweb
d5269b4004bbdfdeb75ed42dcdf281727e64ae11
[fs/lustre-release.git] / lustre / tests / ll_dirstripe_verify.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/tests/ll_dirstripe_verify.c
37  *
38  * ll_dirstripe_verify <dir> <file>:
39  * - to verify if the file has the same lov_user_md setting as the parent dir.
40  * - if dir's offset is set -1, ll_dirstripe_verify <dir> <file1> <file2>
41  *      is used to further verify if file1 and file2's obdidx is continuous.
42  */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ctype.h>
48 #include <sys/ioctl.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <errno.h>
52 #include <dirent.h>
53
54 #include <liblustre.h>
55 #include <obd.h>
56 #include <lustre_lib.h>
57 #include <lustre/lustreapi.h>
58 #include <obd_lov.h>
59
60 #include <lnet/lnetctl.h>
61
62
63 #define MAX_LOV_UUID_COUNT      1000
64 union {
65         struct obd_uuid uuid;
66         char name[0];
67 } lov;
68 #define lov_uuid lov.uuid
69 #define lov_name lov.name
70
71 /* Returns bytes read on success and a negative value on failure.
72  * If zero bytes are read it will be treated as failure as such
73  * zero cannot be returned from this function.
74  */
75 int read_proc_entry(char *proc_path, char *buf, int len)
76 {
77         int rc, fd;
78
79         memset(buf, 0, len);
80
81         fd = open(proc_path, O_RDONLY);
82         if (fd == -1) {
83                 llapi_error(LLAPI_MSG_ERROR, -errno, "open('%s') failed: %s\n",
84                             proc_path);
85                 return -2;
86         }
87
88         rc = read(fd, buf, len - 1);
89         if (rc < 0) {
90                 llapi_error(LLAPI_MSG_ERROR, -errno,
91                             "read('%s') failed: %s\n", proc_path);
92                 rc = -3;
93         } else if (rc == 0) {
94                 llapi_err_noerrno(LLAPI_MSG_ERROR,
95                                   "read('%s') zero bytes\n", proc_path);
96                 rc = -4;
97         } else if (/* rc > 0 && */ buf[rc - 1] == '\n') {
98                 buf[rc - 1] = '\0'; /* Remove trailing newline */
99         }
100         close(fd);
101
102         return (rc);
103 }
104
105 int compare(struct lov_user_md *lum_dir, struct lov_user_md *lum_file1,
106             struct lov_user_md *lum_file2)
107 {
108         int stripe_count = 0, min_stripe_count = 0, def_stripe_count = 1;
109         int stripe_size = 0;
110         int stripe_offset = -1;
111         int ost_count;
112         char buf[128];
113         char lov_path[PATH_MAX];
114         char tmp_path[PATH_MAX];
115         int i;
116         FILE *fp;
117
118         fp = popen("\\ls -d  /proc/fs/lustre/lov/*clilov* | head -1", "r");
119         if (!fp) {
120                 llapi_error(LLAPI_MSG_ERROR, -errno,
121                             "open(lustre/lov/*clilov*) failed: %s\n");
122                 return 2;
123         }
124         if (fscanf(fp, "%s", lov_path) < 1) {
125                 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
126                             "read(lustre/lov/*clilov*) failed: %s\n");
127                 pclose(fp);
128                 return 3;
129         }
130         pclose(fp);
131
132         snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/stripecount",
133                  lov_path);
134         if (read_proc_entry(tmp_path, buf, sizeof(buf)) < 0)
135                 return 5;
136         def_stripe_count = (short)atoi(buf);
137
138         snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/numobd", lov_path);
139         if (read_proc_entry(tmp_path, buf, sizeof(buf)) < 0)
140                 return 6;
141         ost_count = atoi(buf);
142
143         if (lum_dir == NULL) {
144                 stripe_count = def_stripe_count;
145                 min_stripe_count = -1;
146         } else {
147                 stripe_count = (signed short)lum_dir->lmm_stripe_count;
148                 printf("dir stripe %d, ", stripe_count);
149                 min_stripe_count = 1;
150         }
151
152         printf("default stripe %d, ost count %d\n",
153                def_stripe_count, ost_count);
154
155         if (stripe_count == 0) {
156                 min_stripe_count = -1;
157                 stripe_count = 1;
158         }
159
160         stripe_count = (stripe_count > 0 && stripe_count <= ost_count) ?
161                                                 stripe_count : ost_count;
162         min_stripe_count = min_stripe_count > 0 ? stripe_count :
163                                                 ((stripe_count + 1) / 2);
164
165         if (lum_file1->lmm_stripe_count != stripe_count ||
166             lum_file1->lmm_stripe_count < min_stripe_count) {
167                 llapi_err_noerrno(LLAPI_MSG_ERROR,
168                                   "file1 stripe count %d != dir %d\n",
169                                   lum_file1->lmm_stripe_count, stripe_count);
170                 return 7;
171         }
172
173         if (lum_file1->lmm_stripe_count < stripe_count)
174                 llapi_err_noerrno(LLAPI_MSG_WARN,
175                                   "warning: file1 used fewer stripes"
176                                   " %d < dir %d (likely due to bug 4900)\n",
177                                   lum_file1->lmm_stripe_count, stripe_count);
178
179         if (lum_dir != NULL)
180                 stripe_size = (int)lum_dir->lmm_stripe_size;
181         if (stripe_size == 0) {
182                 snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/stripesize",
183                          lov_path);
184                 if (read_proc_entry(tmp_path, buf, sizeof(buf)) < 0)
185                         return 5;
186
187                 stripe_size = atoi(buf);
188         }
189
190         if (lum_file1->lmm_stripe_size != stripe_size) {
191                 llapi_err_noerrno(LLAPI_MSG_ERROR,
192                                   "file1 stripe size %d != dir %d\n",
193                                   lum_file1->lmm_stripe_size, stripe_size);
194                 return 8;
195         }
196
197         if (lum_dir != NULL)
198                 stripe_offset = (short int)lum_dir->lmm_stripe_offset;
199         if (stripe_offset != -1) {
200                 for (i = 0; i < stripe_count; i++)
201                         if (lum_file1->lmm_objects[i].l_ost_idx !=
202                             (stripe_offset + i) % ost_count) {
203                                 llapi_err_noerrno(LLAPI_MSG_WARN,
204                                           "warning: file1 non-sequential "
205                                           "stripe[%d] %d != %d\n", i,
206                                           lum_file1->lmm_objects[i].l_ost_idx,
207                                           (stripe_offset + i) % ost_count);
208                         }
209         } else if (lum_file2 != NULL) {
210                 int next, idx, stripe = stripe_count - 1;
211                 next = (lum_file1->lmm_objects[stripe].l_ost_idx + 1) %
212                        ost_count;
213                 idx = lum_file2->lmm_objects[0].l_ost_idx;
214                 if (idx != next) {
215                         llapi_err_noerrno(LLAPI_MSG_WARN,
216                                   "warning: non-sequential "
217                                   "file1 stripe[%d] %d != file2 stripe[0] %d\n",
218                                   stripe, lum_file1->lmm_objects[stripe].l_ost_idx,
219                                   idx);
220                 }
221         }
222
223         return 0;
224 }
225
226 int main(int argc, char **argv)
227 {
228         DIR * dir;
229         struct lov_user_md *lum_dir, *lum_file1 = NULL, *lum_file2 = NULL;
230         int rc;
231         int lum_size;
232
233         if (argc < 3) {
234                 llapi_err_noerrno(LLAPI_MSG_ERROR,
235                                 "Usage: %s <dirname> <filename1> [filename2]\n",
236                                 argv[0]);
237                 return 1;
238         }
239
240         dir = opendir(argv[1]);
241         if (dir == NULL) {
242                 rc = -errno;
243                 llapi_error(LLAPI_MSG_ERROR, rc,
244                             "error: %s opendir failed\n", argv[1]);
245                 return rc;
246         }
247
248         lum_size = lov_user_md_size(MAX_LOV_UUID_COUNT, LOV_USER_MAGIC);
249         lum_dir = (struct lov_user_md *)malloc(lum_size);
250         if (lum_dir == NULL) {
251                 rc = -ENOMEM;
252                 llapi_error(LLAPI_MSG_ERROR, rc,
253                             "error: can't allocate %d bytes "
254                             "for dir EA", lum_size);
255                 goto cleanup;
256         }
257
258         rc = llapi_file_get_stripe(argv[1], lum_dir);
259         if (rc) {
260                 if (rc == -ENODATA) {
261                         free(lum_dir);
262                         lum_dir = NULL;
263                 } else {
264                         llapi_error(LLAPI_MSG_ERROR, rc,
265                                     "error: can't get EA for %s\n", argv[1]);
266                         goto cleanup;
267                 }
268         }
269
270         /* XXX should be llapi_lov_getname() */
271         rc = llapi_file_get_lov_uuid(argv[1], &lov_uuid);
272         if (rc) {
273                 llapi_error(LLAPI_MSG_ERROR, rc,
274                             "error: can't get lov name for %s\n",
275                             argv[1]);
276                 return rc;
277         }
278
279         if ((lum_file1 = (struct lov_user_md *)malloc(lum_size)) == NULL) {
280                 rc = -ENOMEM;
281                 llapi_error(LLAPI_MSG_ERROR, rc,
282                             "error: can't allocate %d bytes for EA\n",
283                             lum_size);
284                 goto cleanup;
285         }
286
287         rc = llapi_file_get_stripe(argv[2], lum_file1);
288         if (rc) {
289                 llapi_error(LLAPI_MSG_ERROR, rc,
290                             "error: unable to get EA for %s\n", argv[2]);
291                 goto cleanup;
292         }
293
294         if (argc == 4) {
295                 lum_file2 = (struct lov_user_md *)malloc(lum_size);
296                 if (lum_file2 == NULL) {
297                         rc = -ENOMEM;
298                         llapi_error(LLAPI_MSG_ERROR, rc,
299                                     "error: can't allocate %d "
300                                     "bytes for file2 EA\n", lum_size);
301                         goto cleanup;
302                 }
303
304                 rc = llapi_file_get_stripe(argv[3], lum_file2);
305                 if (rc) {
306                         llapi_error(LLAPI_MSG_ERROR, rc,
307                                     "error: can't get EA for %s\n", argv[3]);
308                         goto cleanup;
309                 }
310         }
311
312         rc = compare(lum_dir, lum_file1, lum_file2);
313
314 cleanup:
315         closedir(dir);
316         if (lum_dir != NULL)
317                 free(lum_dir);
318         if (lum_file1 != NULL)
319                 free(lum_file1);
320         if (lum_file2 != NULL)
321                 free(lum_file2);
322
323         return rc;
324 }