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