1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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 <liblustre.h>
56 #include <lustre_lib.h>
57 #include <lustre/liblustreapi.h>
60 #include <lnet/lnetctl.h>
63 #define MAX_LOV_UUID_COUNT 1000
68 #define lov_uuid lov.uuid
69 #define lov_name lov.name
71 /* Returns bytes read on success and a negative value on failure.
72 * If zero bytes are read it will be treated as failure as such
73 * zero cannot be returned from this function.
75 int read_proc_entry(char *proc_path, char *buf, int len)
81 fd = open(proc_path, O_RDONLY);
83 llapi_err(LLAPI_MSG_ERROR, "open('%s') failed: %s\n",
88 rc = read(fd, buf, len - 1);
90 llapi_err(LLAPI_MSG_ERROR, "read('%s') failed: %s\n",proc_path);
93 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
94 "read('%s') zero bytes\n", proc_path);
96 } else if (/* rc > 0 && */ buf[rc - 1] == '\n') {
97 buf[rc - 1] = '\0'; /* Remove trailing newline */
104 int compare(struct lov_user_md *lum_dir, struct lov_user_md *lum_file1,
105 struct lov_user_md *lum_file2)
107 int stripe_count = 0, min_stripe_count = 0, def_stripe_count = 1;
109 int stripe_offset = -1;
112 char lov_path[PATH_MAX];
113 char tmp_path[PATH_MAX];
117 fp = popen("\\ls -d /proc/fs/lustre/lov/*lov* | head -1", "r");
119 llapi_err(LLAPI_MSG_ERROR,
120 "open(lustre/lov/*lov*) failed: %s\n");
123 if (fscanf(fp, "%s", lov_path) < 1) {
124 llapi_err(LLAPI_MSG_ERROR,"read lustre/lov/*lov* failed: %s\n");
130 snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/stripecount", lov_path);
131 if (read_proc_entry(tmp_path, buf, sizeof(buf)) < 0)
133 def_stripe_count = (short)atoi(buf);
135 snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/numobd", lov_path);
136 if (read_proc_entry(tmp_path, buf, sizeof(buf)) < 0)
138 ost_count = atoi(buf);
140 if (lum_dir == NULL) {
141 stripe_count = def_stripe_count;
142 min_stripe_count = -1;
144 stripe_count = (short)lum_dir->lmm_stripe_count;
145 printf("dir stripe %d, ", stripe_count);
146 min_stripe_count = 1;
149 printf("default stripe %d, ost count %d\n",
150 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(LLAPI_MSG_ERROR, "file1 stripe count %d != dir %d\n",
164 lum_file1->lmm_stripe_count, stripe_count);
168 if (lum_file1->lmm_stripe_count < stripe_count)
169 llapi_err(LLAPI_MSG_WARN, "warning: file1 used fewer stripes "
170 "%d < dir %d (likely due to bug 4900)\n",
171 lum_file1->lmm_stripe_count, stripe_count);
174 stripe_size = (int)lum_dir->lmm_stripe_size;
175 if (stripe_size == 0) {
176 snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/stripesize",
178 if (read_proc_entry(tmp_path, buf, sizeof(buf)) < 0)
181 stripe_size = atoi(buf);
184 if (lum_file1->lmm_stripe_size != stripe_size) {
185 llapi_err(LLAPI_MSG_ERROR, "file1 stripe size %d != dir %d\n",
186 lum_file1->lmm_stripe_size, stripe_size);
191 stripe_offset = (short int)lum_dir->lmm_stripe_offset;
192 if (stripe_offset != -1) {
193 for (i = 0; i < stripe_count; i++)
194 if (lum_file1->lmm_objects[i].l_ost_idx !=
195 (stripe_offset + i) % ost_count) {
196 llapi_err(LLAPI_MSG_WARN,
197 "warning: file1 non-sequential "
198 "stripe[%d] %d != %d\n", i,
199 lum_file1->lmm_objects[i].l_ost_idx,
200 (stripe_offset + i) % ost_count);
202 } else if (lum_file2 != NULL) {
203 int next, idx, stripe = stripe_count - 1;
204 next = (lum_file1->lmm_objects[stripe].l_ost_idx + 1) %
206 idx = lum_file2->lmm_objects[0].l_ost_idx;
208 llapi_err(LLAPI_MSG_WARN, "warning: non-sequential "
209 "file1 stripe[%d] %d != file2 stripe[0] %d\n",
211 lum_file1->lmm_objects[stripe].l_ost_idx,
219 int main(int argc, char **argv)
222 struct lov_user_md *lum_dir, *lum_file1 = NULL, *lum_file2 = NULL;
227 llapi_err(LLAPI_MSG_ERROR,
228 "Usage: %s <dirname> <filename1> [filename2]\n",
233 dir = opendir(argv[1]);
236 llapi_err(LLAPI_MSG_ERROR,
237 "error: %s opendir failed\n", argv[1]);
241 lum_size = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC);
242 if ((lum_dir = (struct lov_user_md *)malloc(lum_size)) == NULL) {
244 llapi_err(LLAPI_MSG_ERROR, "error: can't allocate %d bytes "
245 "for dir EA", lum_size);
249 rc = llapi_file_get_stripe(argv[1], lum_dir);
251 if (errno == ENODATA) {
256 llapi_err(LLAPI_MSG_ERROR,
257 "error: can't get EA for %s\n", argv[1]);
262 /* XXX should be llapi_lov_getname() */
263 rc = llapi_file_get_lov_uuid(argv[1], &lov_uuid);
266 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name for %s\n",
271 if ((lum_file1 = (struct lov_user_md *)malloc(lum_size)) == NULL) {
273 llapi_err(LLAPI_MSG_ERROR,
274 "error: can't allocate %d bytes for EA\n", lum_size);
278 rc = llapi_file_get_stripe(argv[2], lum_file1);
281 llapi_err(LLAPI_MSG_ERROR,
282 "error: unable to get EA for %s\n", argv[2]);
287 lum_file2 = (struct lov_user_md *)malloc(lum_size);
288 if (lum_file2 == NULL) {
290 llapi_err(LLAPI_MSG_ERROR, "error: can't allocate %d "
291 "bytes for file2 EA\n", lum_size);
295 rc = llapi_file_get_stripe(argv[3], lum_file2);
298 llapi_err(LLAPI_MSG_ERROR,
299 "error: can't get EA for %s\n", argv[3]);
304 rc = compare(lum_dir, lum_file1, lum_file2);
310 if (lum_file1 != NULL)
312 if (lum_file2 != NULL)