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>
57 printf("usage: create_iam "
58 "[-h] [-k <keysize>] [-r recsize] [-b <blocksize] [-p <ptrsize>] [-v]\n");
62 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
63 IAM_LVAR_ROOT_MAGIC = 0xb01dface
66 struct iam_lfix_root {
68 u_int16_t ilr_keysize;
69 u_int16_t ilr_recsize;
70 u_int16_t ilr_ptrsize;
71 u_int16_t ilr_indirect_levels;
75 IAM_LEAF_HEADER_MAGIC = 0x1976,
76 IAM_LVAR_LEAF_MAGIC = 0x1973
79 struct iam_leaf_head {
84 struct dx_countlimit {
89 typedef __u32 lvar_hash_t;
91 struct lvar_leaf_header {
92 u_int16_t vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
93 u_int16_t vlh_used; /* used bytes, including header */
100 u_int8_t vr_indirect_levels;
101 u_int8_t vr_padding0;
102 u_int16_t vr_padding1;
105 struct lvar_leaf_entry {
107 u_int16_t vle_keysize;
113 LVAR_ROUND = LVAR_PAD - 1
117 * Stores \a val at \a dst, where the latter is possibly unaligned. Uses
118 * memcpy(). This macro is needed to avoid dependency of user level tools on
119 * the kernel headers.
121 #define STORE_UNALIGNED(val, dst) \
123 typeof(val) __val = (val); \
125 CLASSERT(sizeof(val) == sizeof(*(dst))); \
126 memcpy(dst, &__val, sizeof(*(dst))); \
129 static void lfix_root(void *buf,
130 int blocksize, int keysize, int ptrsize, int recsize)
132 struct iam_lfix_root *root;
133 struct dx_countlimit *limit;
137 *root = (typeof(*root)) {
138 .ilr_magic = cpu_to_le64(IAM_LFIX_ROOT_MAGIC),
139 .ilr_keysize = cpu_to_le16(keysize),
140 .ilr_recsize = cpu_to_le16(recsize),
141 .ilr_ptrsize = cpu_to_le16(ptrsize),
142 .ilr_indirect_levels = 0
145 limit = (void *)(root + 1);
146 *limit = (typeof(*limit)){
148 * limit itself + one pointer to the leaf.
150 .count = cpu_to_le16(2),
151 .limit = (blocksize - sizeof *root) / (keysize + ptrsize)
158 entry += keysize + ptrsize;
161 * Entry format is <key> followed by <ptr>. In the minimal tree
162 * consisting of a root and single node, <key> is a minimal possible
165 * XXX: this key is hard-coded to be a sequence of 0's.
168 /* now @entry points to <ptr> */
170 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
172 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
175 static void lfix_leaf(void *buf,
176 int blocksize, int keysize, int ptrsize, int recsize)
178 struct iam_leaf_head *head;
182 *head = (struct iam_leaf_head) {
183 .ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC),
185 * Leaf contains an entry with the smallest possible key
186 * (created by zeroing).
188 .ill_count = cpu_to_le16(1),
192 static void lvar_root(void *buf,
193 int blocksize, int keysize, int ptrsize, int recsize)
195 struct lvar_root *root;
196 struct dx_countlimit *limit;
200 isize = sizeof(lvar_hash_t) + ptrsize;
202 *root = (typeof(*root)) {
203 .vr_magic = cpu_to_le32(IAM_LVAR_ROOT_MAGIC),
204 .vr_recsize = cpu_to_le16(recsize),
205 .vr_ptrsize = cpu_to_le16(ptrsize),
206 .vr_indirect_levels = 0
209 limit = (void *)(root + 1);
210 *limit = (typeof(*limit)){
212 * limit itself + one pointer to the leaf.
214 .count = cpu_to_le16(2),
215 .limit = (blocksize - sizeof *root) / isize
225 * Entry format is <key> followed by <ptr>. In the minimal tree
226 * consisting of a root and single node, <key> is a minimal possible
229 * XXX: this key is hard-coded to be a sequence of 0's.
231 entry += sizeof(lvar_hash_t);
232 /* now @entry points to <ptr> */
234 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
236 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
239 static int lvar_esize(int namelen, int recsize)
241 return (offsetof(struct lvar_leaf_entry, vle_key) +
242 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
245 static void lvar_leaf(void *buf,
246 int blocksize, int keysize, int ptrsize, int recsize)
248 struct lvar_leaf_header *head;
252 *head = (typeof(*head)) {
253 .vlh_magic = cpu_to_le16(IAM_LVAR_LEAF_MAGIC),
254 .vlh_used = cpu_to_le16(sizeof *head + lvar_esize(0, recsize))
263 int main(int argc, char **argv)
267 int blocksize = 4096;
273 char *fmtstr = "lfix";
277 opt = getopt(argc, argv, "hb:k:r:p:vf:");
284 blocksize = atoi(optarg);
287 keysize = atoi(optarg);
290 recsize = atoi(optarg);
293 ptrsize = atoi(optarg);
300 fprintf(stderr, "Unable to parse options.");
307 if (ptrsize != 4 && ptrsize != 8) {
308 fprintf(stderr, "Invalid ptrsize (%i). "
309 "Only 4 and 8 are supported\n", ptrsize);
313 if (blocksize <= 100 || keysize < 1 || recsize < 0) {
314 fprintf(stderr, "Too small record, key or block block\n");
318 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
319 fprintf(stderr, "Too large (record, key) or too small block\n");
323 if (!strcmp(fmtstr, "lfix"))
325 else if (!strcmp(fmtstr, "lvar"))
328 fprintf(stderr, "Wrong format `%s'\n", fmtstr);
334 "fmt: %s, key: %i, rec: %i, ptr: %i, block: %i\n",
335 fmtstr, keysize, recsize, ptrsize, blocksize);
337 buf = malloc(blocksize);
339 fprintf(stderr, "Unable to allocate %i bytes\n", blocksize);
343 memset(buf, 0, blocksize);
346 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
348 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
350 rc = write(1, buf, blocksize);
351 if (rc != blocksize) {
352 fprintf(stderr, "Unable to write root node: %m (%i)\n", rc);
358 memset(buf, 0, blocksize);
361 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
363 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
365 rc = write(1, buf, blocksize);
367 if (rc != blocksize) {
368 fprintf(stderr, "Unable to write leaf node: %m (%i)\n", rc);
372 fprintf(stderr, "Don't forget to umount/mount "
373 "before accessing iam from the kernel!\n");