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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/utils/create_iam.c
36 * User-level tool for creation of iam files.
38 * Author: Wang Di <wangdi@clusterfs.com>
39 * Author: Nikita Danilov <nikita@clusterfs.com>
49 #include <sys/types.h>
55 #include <libcfs/libcfs.h>
59 printf("usage: create_iam "
60 "[-h] [-k <keysize>] [-r recsize] [-b <blocksize] [-p <ptrsize>] [-v]\n");
64 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
65 IAM_LVAR_ROOT_MAGIC = 0xb01dface
68 struct iam_lfix_root {
70 u_int16_t ilr_keysize;
71 u_int16_t ilr_recsize;
72 u_int16_t ilr_ptrsize;
73 u_int16_t ilr_indirect_levels;
77 IAM_LEAF_HEADER_MAGIC = 0x1976,
78 IAM_LVAR_LEAF_MAGIC = 0x1973
81 struct iam_leaf_head {
86 struct dx_countlimit {
91 typedef __u32 lvar_hash_t;
93 struct lvar_leaf_header {
94 u_int16_t vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
95 u_int16_t vlh_used; /* used bytes, including header */
100 u_int16_t vr_recsize;
101 u_int16_t vr_ptrsize;
102 u_int8_t vr_indirect_levels;
103 u_int8_t vr_padding0;
104 u_int16_t vr_padding1;
107 struct lvar_leaf_entry {
109 u_int16_t vle_keysize;
115 LVAR_ROUND = LVAR_PAD - 1
119 * Stores \a val at \a dst, where the latter is possibly unaligned. Uses
120 * memcpy(). This macro is needed to avoid dependency of user level tools on
121 * the kernel headers.
123 #define STORE_UNALIGNED(val, dst) \
125 typeof(val) __val = (val); \
127 CLASSERT(sizeof(val) == sizeof(*(dst))); \
128 memcpy(dst, &__val, sizeof(*(dst))); \
131 static void lfix_root(void *buf,
132 int blocksize, int keysize, int ptrsize, int recsize)
134 struct iam_lfix_root *root;
135 struct dx_countlimit *limit;
139 *root = (typeof(*root)) {
140 .ilr_magic = cpu_to_le64(IAM_LFIX_ROOT_MAGIC),
141 .ilr_keysize = cpu_to_le16(keysize),
142 .ilr_recsize = cpu_to_le16(recsize),
143 .ilr_ptrsize = cpu_to_le16(ptrsize),
144 .ilr_indirect_levels = 0
147 limit = (void *)(root + 1);
148 *limit = (typeof(*limit)){
150 * limit itself + one pointer to the leaf.
152 .count = cpu_to_le16(2),
153 .limit = (blocksize - sizeof *root) / (keysize + ptrsize)
160 entry += keysize + ptrsize;
163 * Entry format is <key> followed by <ptr>. In the minimal tree
164 * consisting of a root and single node, <key> is a minimal possible
167 * XXX: this key is hard-coded to be a sequence of 0's.
170 /* now @entry points to <ptr> */
172 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
174 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
177 static void lfix_leaf(void *buf,
178 int blocksize, int keysize, int ptrsize, int recsize)
180 struct iam_leaf_head *head;
184 *head = (struct iam_leaf_head) {
185 .ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC),
187 * Leaf contains an entry with the smallest possible key
188 * (created by zeroing).
190 .ill_count = cpu_to_le16(1),
194 static void lvar_root(void *buf,
195 int blocksize, int keysize, int ptrsize, int recsize)
197 struct lvar_root *root;
198 struct dx_countlimit *limit;
202 isize = sizeof(lvar_hash_t) + ptrsize;
204 *root = (typeof(*root)) {
205 .vr_magic = cpu_to_le32(IAM_LVAR_ROOT_MAGIC),
206 .vr_recsize = cpu_to_le16(recsize),
207 .vr_ptrsize = cpu_to_le16(ptrsize),
208 .vr_indirect_levels = 0
211 limit = (void *)(root + 1);
212 *limit = (typeof(*limit)){
214 * limit itself + one pointer to the leaf.
216 .count = cpu_to_le16(2),
217 .limit = (blocksize - sizeof *root) / isize
227 * Entry format is <key> followed by <ptr>. In the minimal tree
228 * consisting of a root and single node, <key> is a minimal possible
231 * XXX: this key is hard-coded to be a sequence of 0's.
233 entry += sizeof(lvar_hash_t);
234 /* now @entry points to <ptr> */
236 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
238 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
241 static int lvar_esize(int namelen, int recsize)
243 return (offsetof(struct lvar_leaf_entry, vle_key) +
244 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
247 static void lvar_leaf(void *buf,
248 int blocksize, int keysize, int ptrsize, int recsize)
250 struct lvar_leaf_header *head;
254 *head = (typeof(*head)) {
255 .vlh_magic = cpu_to_le16(IAM_LVAR_LEAF_MAGIC),
256 .vlh_used = cpu_to_le16(sizeof *head + lvar_esize(0, recsize))
265 int main(int argc, char **argv)
269 int blocksize = 4096;
275 char *fmtstr = "lfix";
279 opt = getopt(argc, argv, "hb:k:r:p:vf:");
286 blocksize = atoi(optarg);
289 keysize = atoi(optarg);
292 recsize = atoi(optarg);
295 ptrsize = atoi(optarg);
302 fprintf(stderr, "Unable to parse options.");
309 if (ptrsize != 4 && ptrsize != 8) {
310 fprintf(stderr, "Invalid ptrsize (%i). "
311 "Only 4 and 8 are supported\n", ptrsize);
315 if (blocksize <= 100 || keysize < 1 || recsize < 0) {
316 fprintf(stderr, "Too small record, key or block block\n");
320 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
321 fprintf(stderr, "Too large (record, key) or too small block\n");
325 if (!strcmp(fmtstr, "lfix"))
327 else if (!strcmp(fmtstr, "lvar"))
330 fprintf(stderr, "Wrong format `%s'\n", fmtstr);
336 "fmt: %s, key: %i, rec: %i, ptr: %i, block: %i\n",
337 fmtstr, keysize, recsize, ptrsize, blocksize);
339 buf = malloc(blocksize);
341 fprintf(stderr, "Unable to allocate %i bytes\n", blocksize);
345 memset(buf, 0, blocksize);
348 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
350 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
352 rc = write(1, buf, blocksize);
353 if (rc != blocksize) {
354 fprintf(stderr, "Unable to write root node: %m (%i)\n", rc);
360 memset(buf, 0, blocksize);
363 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
365 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
367 rc = write(1, buf, blocksize);
369 if (rc != blocksize) {
370 fprintf(stderr, "Unable to write leaf node: %m (%i)\n", rc);
374 fprintf(stderr, "Don't forget to umount/mount "
375 "before accessing iam from the kernel!\n");