Whamcloud - gitweb
LU-10657 utils: fd leak in mirror_split()
[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 /* Returns bytes read on success and a negative value on failure.
58  * If zero bytes are read it will be treated as failure as such
59  * zero cannot be returned from this function.
60  */
61 int read_proc_entry(char *proc_path, char *buf, int len)
62 {
63         int rc, fd;
64
65         memset(buf, 0, len);
66
67         fd = open(proc_path, O_RDONLY);
68         if (fd < 0) {
69                 llapi_error(LLAPI_MSG_ERROR, -errno, "cannot open '%s'",
70                             proc_path);
71                 return -2;
72         }
73
74         rc = read(fd, buf, len - 1);
75         if (rc < 0) {
76                 llapi_error(LLAPI_MSG_ERROR, -errno,
77                             "error reading from '%s'", proc_path);
78                 rc = -3;
79         } else if (rc == 0) {
80                 llapi_err_noerrno(LLAPI_MSG_ERROR,
81                                   "read zero bytes from '%s'", proc_path);
82                 rc = -4;
83         } else if (buf[rc - 1] == '\n') {
84                 buf[rc - 1] = '\0'; /* Remove trailing newline */
85         }
86
87         close(fd);
88
89         return rc;
90 }
91
92 int compare(struct obd_uuid *puuid, struct lov_user_md *lum_dir,
93             struct lov_user_md *lum_file1, struct lov_user_md *lum_file2)
94 {
95         int stripe_count = 0, min_stripe_count = 0, def_stripe_count = 1;
96         int stripe_size = 0;
97         int stripe_offset = -1;
98         int ost_count;
99         char buf[128];
100         glob_t path;
101         int i;
102
103         if (cfs_get_param_paths(&path, "lov/%s/stripecount", puuid->uuid) != 0)
104                 return 2;
105         if (read_proc_entry(path.gl_pathv[0], buf, sizeof(buf)) < 0) {
106                 cfs_free_param_data(&path);
107                 return 5;
108         }
109         cfs_free_param_data(&path);
110         def_stripe_count = (short)atoi(buf);
111
112         if (cfs_get_param_paths(&path, "lov/%s/numobd", puuid->uuid) != 0)
113                 return 2;
114         if (read_proc_entry(path.gl_pathv[0], buf, sizeof(buf)) < 0) {
115                 cfs_free_param_data(&path);
116                 return 6;
117         }
118         cfs_free_param_data(&path);
119         ost_count = atoi(buf);
120
121         if (lum_dir == NULL) {
122                 stripe_count = def_stripe_count;
123                 min_stripe_count = -1;
124         } else {
125                 stripe_count = (signed short)lum_dir->lmm_stripe_count;
126                 printf("dir stripe %d, ", stripe_count);
127                 min_stripe_count = 1;
128         }
129
130         printf("default stripe %d, ost count %d\n",
131                def_stripe_count, ost_count);
132
133         if (stripe_count == 0) {
134                 min_stripe_count = -1;
135                 stripe_count = 1;
136         }
137
138         stripe_count = (stripe_count > 0 && stripe_count <= ost_count) ?
139                                                 stripe_count : ost_count;
140         min_stripe_count = min_stripe_count > 0 ? stripe_count :
141                                                 ((stripe_count + 1) / 2);
142
143         if (lum_file1->lmm_stripe_count != stripe_count ||
144             lum_file1->lmm_stripe_count < min_stripe_count) {
145                 llapi_err_noerrno(LLAPI_MSG_ERROR,
146                                   "file1 stripe count %d != dir %d\n",
147                                   lum_file1->lmm_stripe_count, stripe_count);
148                 return 7;
149         }
150
151         if (lum_file1->lmm_stripe_count < stripe_count)
152                 llapi_err_noerrno(LLAPI_MSG_WARN,
153                                   "warning: file1 used fewer stripes"
154                                   " %d < dir %d (likely due to bug 4900)\n",
155                                   lum_file1->lmm_stripe_count, stripe_count);
156
157         if (lum_dir != NULL)
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 != NULL)
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 "
187                                           "stripe[%d] %d != %d\n", i,
188                                           lum_file1->lmm_objects[i].l_ost_idx,
189                                           (stripe_offset + i) % ost_count);
190                         }
191         } else if (lum_file2 != NULL) {
192                 int next, idx, stripe = stripe_count - 1;
193                 next = (lum_file1->lmm_objects[stripe].l_ost_idx + 1) %
194                        ost_count;
195                 idx = lum_file2->lmm_objects[0].l_ost_idx;
196                 if (idx != next) {
197                         llapi_err_noerrno(LLAPI_MSG_WARN,
198                                   "warning: non-sequential "
199                                   "file1 stripe[%d] %d != file2 stripe[0] %d\n",
200                                   stripe, lum_file1->lmm_objects[stripe].l_ost_idx,
201                                   idx);
202                 }
203         }
204
205         return 0;
206 }
207
208 int compare_lum(struct obd_uuid *puuid, struct lov_user_md *lum_dir,
209                 struct lov_user_md *lum_file1, struct lov_user_md *lum_file2)
210 {
211         struct lov_comp_md_v1 *comp_dir, *comp_file1;
212         struct lov_user_md *sub_dir, *sub_file1;
213         int i, rc = 0;
214
215         if (lum_dir == NULL || lum_dir->lmm_magic != LOV_MAGIC_COMP_V1)
216                 return compare(puuid, lum_dir, lum_file1, lum_file2);
217
218         comp_dir = (struct lov_comp_md_v1 *)lum_dir;
219         comp_file1 = (struct lov_comp_md_v1 *)lum_file1;
220
221         if (lum_file1->lmm_magic != lum_dir->lmm_magic) {
222                 llapi_err_noerrno(LLAPI_MSG_ERROR, "file1 magic %#x != %#x\n",
223                                   lum_file1->lmm_magic, lum_dir->lmm_magic);
224                 return 10;
225         }
226
227         if (comp_file1->lcm_entry_count != comp_dir->lcm_entry_count) {
228                 llapi_err_noerrno(LLAPI_MSG_ERROR, "file1 comp cnt %d != %d\n",
229                                   comp_file1->lcm_entry_count,
230                                   comp_dir->lcm_entry_count);
231                 return 11;
232         }
233
234         for (i = 0; i < comp_dir->lcm_entry_count; i++) {
235                 sub_dir = (struct lov_user_md *)((char *)comp_dir +
236                                 comp_dir->lcm_entries[i].lcme_offset);
237                 sub_file1 = (struct lov_user_md *)((char *)comp_file1 +
238                                 comp_file1->lcm_entries[i].lcme_offset);
239
240                 rc = compare(puuid, sub_dir, sub_file1, NULL);
241                 if (rc)
242                         break;
243         }
244
245         return rc;
246 }
247
248 int main(int argc, char **argv)
249 {
250         struct lov_user_md *lum_dir, *lum_file1 = NULL, *lum_file2 = NULL;
251         struct obd_uuid uuid;
252         int lum_size, rc;
253         DIR *dir;
254
255         if (argc < 3) {
256                 llapi_err_noerrno(LLAPI_MSG_ERROR,
257                                   "Usage: %s <dirname> <filename1> [filename2]\n",
258                                   argv[0]);
259                 return 1;
260         }
261
262         dir = opendir(argv[1]);
263         if (dir == NULL) {
264                 rc = -errno;
265                 llapi_error(LLAPI_MSG_ERROR, rc,
266                             "error: %s opendir failed", argv[1]);
267                 return rc;
268         }
269
270         lum_size = lov_user_md_size(MAX_LOV_UUID_COUNT, LOV_USER_MAGIC);
271         lum_dir = (struct lov_user_md *)malloc(lum_size);
272         if (lum_dir == NULL) {
273                 rc = -ENOMEM;
274                 llapi_error(LLAPI_MSG_ERROR, rc,
275                             "error: can't allocate %d bytes "
276                             "for dir EA", lum_size);
277                 goto cleanup;
278         }
279
280         rc = llapi_file_get_stripe(argv[1], lum_dir);
281         if (rc == -ENODATA) {
282                 char root[PATH_MAX], path[PATH_MAX + 2];
283
284                 rc = llapi_search_mounts(argv[1], 0, root, NULL);
285                 if (rc) {
286                         llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get "
287                                     "root path for %s\n", argv[1]);
288                         goto cleanup;
289                 }
290
291                 snprintf(path, sizeof(path), "%s/.", root);
292                 rc = llapi_file_get_stripe(path, lum_dir);
293                 if (rc == -ENODATA) {
294                         free(lum_dir);
295                         lum_dir = NULL;
296                 } else if (rc) {
297                         llapi_error(LLAPI_MSG_ERROR, rc, "error: cant't get "
298                                     "root's LOVEA for %s\n", path);
299                         goto cleanup;
300                 }
301         } else if (rc) {
302                 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get LOVEA for "
303                             "%s", argv[1]);
304                 goto cleanup;
305         }
306
307         /* XXX should be llapi_lov_getname() */
308         rc = llapi_file_get_lov_uuid(argv[1], &uuid);
309         if (rc) {
310                 llapi_error(LLAPI_MSG_ERROR, rc,
311                             "error: can't get lov name for %s",
312                             argv[1]);
313                 return rc;
314         }
315
316         lum_file1 = malloc(lum_size);
317         if (lum_file1 == NULL) {
318                 rc = -ENOMEM;
319                 llapi_error(LLAPI_MSG_ERROR, rc,
320                             "error: can't allocate %d bytes for EA",
321                             lum_size);
322                 goto cleanup;
323         }
324
325         rc = llapi_file_get_stripe(argv[2], lum_file1);
326         if (rc) {
327                 llapi_error(LLAPI_MSG_ERROR, rc,
328                             "error: unable to get EA for %s", argv[2]);
329                 goto cleanup;
330         }
331
332         if (argc == 4) {
333                 lum_file2 = (struct lov_user_md *)malloc(lum_size);
334                 if (lum_file2 == NULL) {
335                         rc = -ENOMEM;
336                         llapi_error(LLAPI_MSG_ERROR, rc,
337                                     "error: can't allocate %d "
338                                     "bytes for file2 EA", lum_size);
339                         goto cleanup;
340                 }
341
342                 rc = llapi_file_get_stripe(argv[3], lum_file2);
343                 if (rc) {
344                         llapi_error(LLAPI_MSG_ERROR, rc,
345                                     "error: can't get EA for %s", argv[3]);
346                         goto cleanup;
347                 }
348         }
349
350         rc = compare_lum(&uuid, lum_dir, lum_file1, lum_file2);
351
352 cleanup:
353         closedir(dir);
354         if (lum_dir != NULL)
355                 free(lum_dir);
356         if (lum_file1 != NULL)
357                 free(lum_file1);
358         if (lum_file2 != NULL)
359                 free(lum_file2);
360
361         return rc;
362 }