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