Whamcloud - gitweb
LU-13745 tests: skip sanity test_426 for 4.18+
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/tests/ll_dirstripe_verify.c
33  *
34  * ll_dirstripe_verify <dir> <file>:
35  * - to verify if the file has the same lov_user_md setting as the parent dir.
36  * - if dir's offset is set -1, ll_dirstripe_verify <dir> <file1> <file2>
37  *      is used to further verify if file1 and file2's obdidx is continuous.
38  */
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <sys/ioctl.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <errno.h>
48 #include <dirent.h>
49
50 #include <libcfs/util/param.h>
51 #include <libcfs/util/string.h>
52 #include <lustre/lustreapi.h>
53 #include <linux/lustre/lustre_idl.h>
54
55 #define MAX_LOV_UUID_COUNT      1000
56
57 /*
58  * Returns bytes read on success and a negative value on failure.
59  * If zero bytes are read it will be treated as failure as such
60  * zero cannot be returned from this function.
61  */
62 int read_proc_entry(char *proc_path, char *buf, int len)
63 {
64         int rc, fd;
65
66         memset(buf, 0, len);
67
68         fd = open(proc_path, O_RDONLY);
69         if (fd < 0) {
70                 llapi_error(LLAPI_MSG_ERROR, -errno, "cannot open '%s'",
71                             proc_path);
72                 return -2;
73         }
74
75         rc = read(fd, buf, len - 1);
76         if (rc < 0) {
77                 llapi_error(LLAPI_MSG_ERROR, -errno,
78                             "error reading from '%s'", proc_path);
79                 rc = -3;
80         } else if (rc == 0) {
81                 llapi_err_noerrno(LLAPI_MSG_ERROR,
82                                   "read zero bytes from '%s'", proc_path);
83                 rc = -4;
84         } else if (buf[rc - 1] == '\n') {
85                 buf[rc - 1] = '\0'; /* Remove trailing newline */
86         }
87
88         close(fd);
89
90         return rc;
91 }
92
93 int compare(struct obd_uuid *puuid, struct lov_user_md *lum_dir,
94             struct lov_user_md *lum_file1, struct lov_user_md *lum_file2)
95 {
96         int stripe_count = 0, min_stripe_count = 0, def_stripe_count = 1;
97         int stripe_size = 0;
98         int stripe_offset = -1;
99         int ost_count;
100         char buf[128];
101         glob_t path;
102         int i;
103
104         if (cfs_get_param_paths(&path, "lov/%s/stripecount", puuid->uuid) != 0)
105                 return 2;
106         if (read_proc_entry(path.gl_pathv[0], buf, sizeof(buf)) < 0) {
107                 cfs_free_param_data(&path);
108                 return 5;
109         }
110         cfs_free_param_data(&path);
111         def_stripe_count = (short)atoi(buf);
112
113         if (cfs_get_param_paths(&path, "lov/%s/numobd", puuid->uuid) != 0)
114                 return 2;
115         if (read_proc_entry(path.gl_pathv[0], buf, sizeof(buf)) < 0) {
116                 cfs_free_param_data(&path);
117                 return 6;
118         }
119         cfs_free_param_data(&path);
120         ost_count = atoi(buf);
121
122         if (!lum_dir) {
123                 stripe_count = def_stripe_count;
124                 min_stripe_count = -1;
125         } else {
126                 stripe_count = (signed short)lum_dir->lmm_stripe_count;
127                 printf("dir stripe %d, ", stripe_count);
128                 min_stripe_count = 1;
129         }
130
131         printf("default stripe %d, ost count %d\n",
132                def_stripe_count, ost_count);
133
134         if (stripe_count == 0) {
135                 min_stripe_count = -1;
136                 stripe_count = 1;
137         }
138
139         stripe_count = (stripe_count > 0 && stripe_count <= ost_count) ?
140                                                 stripe_count : ost_count;
141         min_stripe_count = min_stripe_count > 0 ? stripe_count :
142                                                 ((stripe_count + 1) / 2);
143
144         if (lum_file1->lmm_stripe_count != stripe_count ||
145             lum_file1->lmm_stripe_count < min_stripe_count) {
146                 llapi_err_noerrno(LLAPI_MSG_ERROR,
147                                   "file1 stripe count %d != dir %d\n",
148                                   lum_file1->lmm_stripe_count, stripe_count);
149                 return 7;
150         }
151
152         if (lum_file1->lmm_stripe_count < stripe_count)
153                 llapi_err_noerrno(LLAPI_MSG_WARN,
154                                   "warning: file1 used fewer stripes %d < dir %d (likely due to bug 4900)\n",
155                                   lum_file1->lmm_stripe_count, stripe_count);
156
157         if (lum_dir)
158                 stripe_size = (int)lum_dir->lmm_stripe_size;
159         if (stripe_size == 0) {
160                 if (cfs_get_param_paths(&path, "lov/%s/stripesize",
161                                         puuid->uuid) != 0)
162                         return 2;
163                 if (read_proc_entry(path.gl_pathv[0], buf, sizeof(buf)) < 0) {
164                         cfs_free_param_data(&path);
165                         return 5;
166                 }
167                 cfs_free_param_data(&path);
168
169                 stripe_size = atoi(buf);
170         }
171
172         if (lum_file1->lmm_stripe_size != stripe_size) {
173                 llapi_err_noerrno(LLAPI_MSG_ERROR,
174                                   "file1 stripe size %d != dir %d\n",
175                                   lum_file1->lmm_stripe_size, stripe_size);
176                 return 8;
177         }
178
179         if (lum_dir)
180                 stripe_offset = (short int)lum_dir->lmm_stripe_offset;
181         if (stripe_offset != -1) {
182                 for (i = 0; i < stripe_count; i++)
183                         if (lum_file1->lmm_objects[i].l_ost_idx !=
184                             (stripe_offset + i) % ost_count) {
185                                 llapi_err_noerrno(LLAPI_MSG_WARN,
186                                                   "warning: file1 non-sequential stripe[%d] %d != %d\n",
187                                                   i, lum_file1->lmm_objects[i].l_ost_idx,
188                                                   (stripe_offset + i) %
189                                                   ost_count);
190                         }
191         } else if (lum_file2) {
192                 int next, idx, stripe = stripe_count - 1;
193
194                 next = (lum_file1->lmm_objects[stripe].l_ost_idx + 1) %
195                         ost_count;
196                 idx = lum_file2->lmm_objects[0].l_ost_idx;
197                 if (idx != next) {
198                         llapi_err_noerrno(LLAPI_MSG_WARN,
199                                           "warning: non-sequential file1 stripe[%d] %d != file2 stripe[0] %d\n",
200                                           stripe,
201                                           lum_file1->lmm_objects[stripe].l_ost_idx,
202                                           idx);
203                 }
204         }
205
206         return 0;
207 }
208
209 int compare_lum(struct obd_uuid *puuid, struct lov_user_md *lum_dir,
210                 struct lov_user_md *lum_file1, struct lov_user_md *lum_file2)
211 {
212         struct lov_comp_md_v1 *comp_dir, *comp_file1;
213         struct lov_user_md *sub_dir, *sub_file1;
214         int i, rc = 0;
215
216         if (!lum_dir || lum_dir->lmm_magic != LOV_MAGIC_COMP_V1)
217                 return compare(puuid, lum_dir, lum_file1, lum_file2);
218
219         comp_dir = (struct lov_comp_md_v1 *)lum_dir;
220         comp_file1 = (struct lov_comp_md_v1 *)lum_file1;
221
222         if (lum_file1->lmm_magic != lum_dir->lmm_magic) {
223                 llapi_err_noerrno(LLAPI_MSG_ERROR, "file1 magic %#x != %#x\n",
224                                   lum_file1->lmm_magic, lum_dir->lmm_magic);
225                 return 10;
226         }
227
228         if (comp_file1->lcm_entry_count != comp_dir->lcm_entry_count) {
229                 llapi_err_noerrno(LLAPI_MSG_ERROR, "file1 comp cnt %d != %d\n",
230                                   comp_file1->lcm_entry_count,
231                                   comp_dir->lcm_entry_count);
232                 return 11;
233         }
234
235         for (i = 0; i < comp_dir->lcm_entry_count; i++) {
236                 sub_dir = (struct lov_user_md *)((char *)comp_dir +
237                                 comp_dir->lcm_entries[i].lcme_offset);
238                 sub_file1 = (struct lov_user_md *)((char *)comp_file1 +
239                                 comp_file1->lcm_entries[i].lcme_offset);
240
241                 rc = compare(puuid, sub_dir, sub_file1, NULL);
242                 if (rc)
243                         break;
244         }
245
246         return rc;
247 }
248
249 int main(int argc, char **argv)
250 {
251         struct lov_user_md *lum_dir, *lum_file1 = NULL, *lum_file2 = NULL;
252         struct obd_uuid uuid;
253         int lum_size, rc;
254         DIR *dir;
255
256         if (argc < 3) {
257                 llapi_err_noerrno(LLAPI_MSG_ERROR,
258                                   "Usage: %s <dirname> <filename1> [filename2]\n",
259                                   argv[0]);
260                 return 1;
261         }
262
263         dir = opendir(argv[1]);
264         if (!dir) {
265                 rc = -errno;
266                 llapi_error(LLAPI_MSG_ERROR, rc,
267                             "error: %s opendir failed", argv[1]);
268                 return rc;
269         }
270
271         lum_size = lov_user_md_size(MAX_LOV_UUID_COUNT, LOV_USER_MAGIC);
272         lum_dir = (struct lov_user_md *)malloc(lum_size);
273         if (!lum_dir) {
274                 rc = -ENOMEM;
275                 llapi_error(LLAPI_MSG_ERROR, rc,
276                             "error: can't allocate %d bytes for dir EA",
277                             lum_size);
278                 goto cleanup;
279         }
280
281         rc = llapi_file_get_stripe(argv[1], lum_dir);
282         if (rc == -ENODATA) {
283                 char root[PATH_MAX], path[PATH_MAX + 2];
284
285                 rc = llapi_search_mounts(argv[1], 0, root, NULL);
286                 if (rc) {
287                         llapi_error(LLAPI_MSG_ERROR, rc,
288                                     "error: can't get root path for %s\n",
289                                     argv[1]);
290                         goto cleanup;
291                 }
292
293                 snprintf(path, sizeof(path), "%s/.", root);
294                 rc = llapi_file_get_stripe(path, lum_dir);
295                 if (rc == -ENODATA) {
296                         free(lum_dir);
297                         lum_dir = NULL;
298                 } else if (rc) {
299                         llapi_error(LLAPI_MSG_ERROR, rc,
300                                     "error: cant't get root's LOVEA for %s\n",
301                                     path);
302                         goto cleanup;
303                 }
304         } else if (rc) {
305                 llapi_error(LLAPI_MSG_ERROR, rc,
306                             "error: can't get LOVEA for %s", argv[1]);
307                 goto cleanup;
308         }
309
310         /* XXX should be llapi_lov_getname() */
311         rc = llapi_file_get_lov_uuid(argv[1], &uuid);
312         if (rc) {
313                 llapi_error(LLAPI_MSG_ERROR, rc,
314                             "error: can't get lov name for %s",
315                             argv[1]);
316                 return rc;
317         }
318
319         lum_file1 = malloc(lum_size);
320         if (!lum_file1) {
321                 rc = -ENOMEM;
322                 llapi_error(LLAPI_MSG_ERROR, rc,
323                             "error: can't allocate %d bytes for EA",
324                             lum_size);
325                 goto cleanup;
326         }
327
328         rc = llapi_file_get_stripe(argv[2], lum_file1);
329         if (rc) {
330                 llapi_error(LLAPI_MSG_ERROR, rc,
331                             "error: unable to get EA for %s", argv[2]);
332                 goto cleanup;
333         }
334
335         if (argc == 4) {
336                 lum_file2 = (struct lov_user_md *)malloc(lum_size);
337                 if (!lum_file2) {
338                         rc = -ENOMEM;
339                         llapi_error(LLAPI_MSG_ERROR, rc,
340                                     "error: can't allocate %d bytes for file2 EA",
341                                     lum_size);
342                         goto cleanup;
343                 }
344
345                 rc = llapi_file_get_stripe(argv[3], lum_file2);
346                 if (rc) {
347                         llapi_error(LLAPI_MSG_ERROR, rc,
348                                     "error: can't get EA for %s", argv[3]);
349                         goto cleanup;
350                 }
351         }
352
353         rc = compare_lum(&uuid, lum_dir, lum_file1, lum_file2);
354
355 cleanup:
356         closedir(dir);
357         if (lum_dir)
358                 free(lum_dir);
359         if (lum_file1)
360                 free(lum_file1);
361         if (lum_file2)
362                 free(lum_file2);
363
364         return rc;
365 }