4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/tests/ll_dirstripe_verify.c
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.
48 #include <sys/ioctl.h>
54 #include <lustre/lustreapi.h>
56 #define MAX_LOV_UUID_COUNT 1000
61 #define lov_uuid lov.uuid
62 #define lov_name lov.name
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.
68 int read_proc_entry(char *proc_path, char *buf, int len)
74 fd = open(proc_path, O_RDONLY);
76 llapi_error(LLAPI_MSG_ERROR, -errno, "cannot open '%s'",
81 rc = read(fd, buf, len - 1);
83 llapi_error(LLAPI_MSG_ERROR, -errno,
84 "error reading from '%s'", proc_path);
87 llapi_err_noerrno(LLAPI_MSG_ERROR,
88 "read zero bytes from '%s'", proc_path);
90 } else if (buf[rc - 1] == '\n') {
91 buf[rc - 1] = '\0'; /* Remove trailing newline */
99 int compare(struct lov_user_md *lum_dir, struct lov_user_md *lum_file1,
100 struct lov_user_md *lum_file2)
102 int stripe_count = 0, min_stripe_count = 0, def_stripe_count = 1;
104 int stripe_offset = -1;
107 char lov_path[PATH_MAX];
108 char tmp_path[PATH_MAX];
112 fp = popen("\\ls -d /proc/fs/lustre/lov/*clilov* | head -1", "r");
114 llapi_error(LLAPI_MSG_ERROR, -errno,
115 "open(lustre/lov/*clilov*) failed");
119 if (fscanf(fp, "%s", lov_path) < 1) {
120 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
121 "read(lustre/lov/*clilov*) failed");
128 snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/stripecount",
130 if (read_proc_entry(tmp_path, buf, sizeof(buf)) < 0)
132 def_stripe_count = (short)atoi(buf);
134 snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/numobd", lov_path);
135 if (read_proc_entry(tmp_path, buf, sizeof(buf)) < 0)
137 ost_count = atoi(buf);
139 if (lum_dir == NULL) {
140 stripe_count = def_stripe_count;
141 min_stripe_count = -1;
143 stripe_count = (signed short)lum_dir->lmm_stripe_count;
144 printf("dir stripe %d, ", stripe_count);
145 min_stripe_count = 1;
148 printf("default stripe %d, ost count %d\n",
149 def_stripe_count, ost_count);
151 if (stripe_count == 0) {
152 min_stripe_count = -1;
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);
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);
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);
176 stripe_size = (int)lum_dir->lmm_stripe_size;
177 if (stripe_size == 0) {
178 snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/stripesize",
180 if (read_proc_entry(tmp_path, buf, sizeof(buf)) < 0)
183 stripe_size = atoi(buf);
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);
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);
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) %
209 idx = lum_file2->lmm_objects[0].l_ost_idx;
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,
222 int main(int argc, char **argv)
225 struct lov_user_md *lum_dir, *lum_file1 = NULL, *lum_file2 = NULL;
230 llapi_err_noerrno(LLAPI_MSG_ERROR,
231 "Usage: %s <dirname> <filename1> [filename2]\n",
236 dir = opendir(argv[1]);
239 llapi_error(LLAPI_MSG_ERROR, rc,
240 "error: %s opendir failed", argv[1]);
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) {
248 llapi_error(LLAPI_MSG_ERROR, rc,
249 "error: can't allocate %d bytes "
250 "for dir EA", lum_size);
254 rc = llapi_file_get_stripe(argv[1], lum_dir);
256 if (rc == -ENODATA) {
260 llapi_error(LLAPI_MSG_ERROR, rc,
261 "error: can't get EA for %s", argv[1]);
266 /* XXX should be llapi_lov_getname() */
267 rc = llapi_file_get_lov_uuid(argv[1], &lov_uuid);
269 llapi_error(LLAPI_MSG_ERROR, rc,
270 "error: can't get lov name for %s",
275 lum_file1 = malloc(lum_size);
276 if (lum_file1 == NULL) {
278 llapi_error(LLAPI_MSG_ERROR, rc,
279 "error: can't allocate %d bytes for EA",
284 rc = llapi_file_get_stripe(argv[2], lum_file1);
286 llapi_error(LLAPI_MSG_ERROR, rc,
287 "error: unable to get EA for %s", argv[2]);
292 lum_file2 = (struct lov_user_md *)malloc(lum_size);
293 if (lum_file2 == NULL) {
295 llapi_error(LLAPI_MSG_ERROR, rc,
296 "error: can't allocate %d "
297 "bytes for file2 EA", lum_size);
301 rc = llapi_file_get_stripe(argv[3], lum_file2);
303 llapi_error(LLAPI_MSG_ERROR, rc,
304 "error: can't get EA for %s", argv[3]);
309 rc = compare(lum_dir, lum_file1, lum_file2);
315 if (lum_file1 != NULL)
317 if (lum_file2 != NULL)