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>
50 #include <sys/types.h>
54 printf("usage: create_iam "
55 "[-h] [-k <keysize>] [-r recsize] [-b <blocksize] [-p <ptrsize>] [-v]\n");
59 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
60 IAM_LVAR_ROOT_MAGIC = 0xb01dface
63 struct iam_lfix_root {
65 u_int16_t ilr_keysize;
66 u_int16_t ilr_recsize;
67 u_int16_t ilr_ptrsize;
68 u_int16_t ilr_indirect_levels;
72 IAM_LEAF_HEADER_MAGIC = 0x1976,
73 IAM_LVAR_LEAF_MAGIC = 0x1973
76 struct iam_leaf_head {
81 struct dx_countlimit {
86 typedef __u32 lvar_hash_t;
88 struct lvar_leaf_header {
89 u_int16_t vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
90 u_int16_t vlh_used; /* used bytes, including header */
97 u_int8_t vr_indirect_levels;
99 u_int16_t vr_padding1;
102 struct lvar_leaf_entry {
104 u_int16_t vle_keysize;
110 LVAR_ROUND = LVAR_PAD - 1
114 * Stores \a val at \a dst, where the latter is possibly unaligned. Uses
115 * memcpy(). This macro is needed to avoid dependency of user level tools on
116 * the kernel headers.
118 #define STORE_UNALIGNED(val, dst) \
120 typeof(val) __val = (val); \
122 CLASSERT(sizeof(val) == sizeof(*(dst))); \
123 memcpy(dst, &__val, sizeof(*(dst))); \
126 static void lfix_root(void *buf,
127 int blocksize, int keysize, int ptrsize, int recsize)
129 struct iam_lfix_root *root;
130 struct dx_countlimit *limit;
134 *root = (typeof(*root)) {
135 .ilr_magic = cpu_to_le64(IAM_LFIX_ROOT_MAGIC),
136 .ilr_keysize = cpu_to_le16(keysize),
137 .ilr_recsize = cpu_to_le16(recsize),
138 .ilr_ptrsize = cpu_to_le16(ptrsize),
139 .ilr_indirect_levels = 0
142 limit = (void *)(root + 1);
143 *limit = (typeof(*limit)){
145 * limit itself + one pointer to the leaf.
147 .count = cpu_to_le16(2),
148 .limit = (blocksize - sizeof *root) / (keysize + ptrsize)
155 entry += keysize + ptrsize;
158 * Entry format is <key> followed by <ptr>. In the minimal tree
159 * consisting of a root and single node, <key> is a minimal possible
162 * XXX: this key is hard-coded to be a sequence of 0's.
165 /* now @entry points to <ptr> */
167 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
169 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
172 static void lfix_leaf(void *buf,
173 int blocksize, int keysize, int ptrsize, int recsize)
175 struct iam_leaf_head *head;
179 *head = (struct iam_leaf_head) {
180 .ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC),
182 * Leaf contains an entry with the smallest possible key
183 * (created by zeroing).
185 .ill_count = cpu_to_le16(1),
189 static void lvar_root(void *buf,
190 int blocksize, int keysize, int ptrsize, int recsize)
192 struct lvar_root *root;
193 struct dx_countlimit *limit;
197 isize = sizeof(lvar_hash_t) + ptrsize;
199 *root = (typeof(*root)) {
200 .vr_magic = cpu_to_le32(IAM_LVAR_ROOT_MAGIC),
201 .vr_recsize = cpu_to_le16(recsize),
202 .vr_ptrsize = cpu_to_le16(ptrsize),
203 .vr_indirect_levels = 0
206 limit = (void *)(root + 1);
207 *limit = (typeof(*limit)){
209 * limit itself + one pointer to the leaf.
211 .count = cpu_to_le16(2),
212 .limit = (blocksize - sizeof *root) / isize
222 * Entry format is <key> followed by <ptr>. In the minimal tree
223 * consisting of a root and single node, <key> is a minimal possible
226 * XXX: this key is hard-coded to be a sequence of 0's.
228 entry += sizeof(lvar_hash_t);
229 /* now @entry points to <ptr> */
231 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
233 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
236 static int lvar_esize(int namelen, int recsize)
238 return (offsetof(struct lvar_leaf_entry, vle_key) +
239 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
242 static void lvar_leaf(void *buf,
243 int blocksize, int keysize, int ptrsize, int recsize)
245 struct lvar_leaf_header *head;
249 *head = (typeof(*head)) {
250 .vlh_magic = cpu_to_le16(IAM_LVAR_LEAF_MAGIC),
251 .vlh_used = cpu_to_le16(sizeof *head + lvar_esize(0, recsize))
260 int main(int argc, char **argv)
264 int blocksize = 4096;
270 char *fmtstr = "lfix";
274 opt = getopt(argc, argv, "hb:k:r:p:vf:");
281 blocksize = atoi(optarg);
284 keysize = atoi(optarg);
287 recsize = atoi(optarg);
290 ptrsize = atoi(optarg);
297 fprintf(stderr, "Unable to parse options.");
304 if (ptrsize != 4 && ptrsize != 8) {
305 fprintf(stderr, "Invalid ptrsize (%i). "
306 "Only 4 and 8 are supported\n", ptrsize);
310 if (blocksize <= 100 || keysize < 1 || recsize < 0) {
311 fprintf(stderr, "Too small record, key or block block\n");
315 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
316 fprintf(stderr, "Too large (record, key) or too small block\n");
320 if (!strcmp(fmtstr, "lfix"))
322 else if (!strcmp(fmtstr, "lvar"))
325 fprintf(stderr, "Wrong format `%s'\n", fmtstr);
331 "fmt: %s, key: %i, rec: %i, ptr: %i, block: %i\n",
332 fmtstr, keysize, recsize, ptrsize, blocksize);
334 buf = malloc(blocksize);
336 fprintf(stderr, "Unable to allocate %i bytes\n", blocksize);
340 memset(buf, 0, blocksize);
343 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
345 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
347 rc = write(1, buf, blocksize);
348 if (rc != blocksize) {
349 fprintf(stderr, "Unable to write root node: %m (%i)\n", rc);
355 memset(buf, 0, blocksize);
358 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
360 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
362 rc = write(1, buf, blocksize);
364 if (rc != blocksize) {
365 fprintf(stderr, "Unable to write leaf node: %m (%i)\n", rc);
369 fprintf(stderr, "Don't forget to umount/mount "
370 "before accessing iam from the kernel!\n");