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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
27 * This file is part of Lustre, http://www.lustre.org/
29 * lustre/utils/create_iam.c
31 * User-level tool for creation of iam files.
33 * Author: Wang Di <wangdi@clusterfs.com>
34 * Author: Nikita Danilov <nikita@clusterfs.com>
45 #include <sys/types.h>
50 "usage: create_iam [-h] [-k <keysize>] [-r recsize] [-b <blocksize] [-p <ptrsize>] [-v]\n");
54 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
55 IAM_LVAR_ROOT_MAGIC = 0xb01dface
58 struct iam_lfix_root {
60 u_int16_t ilr_keysize;
61 u_int16_t ilr_recsize;
62 u_int16_t ilr_ptrsize;
63 u_int16_t ilr_indirect_levels;
67 IAM_LEAF_HEADER_MAGIC = 0x1976,
68 IAM_LVAR_LEAF_MAGIC = 0x1973
71 struct iam_leaf_head {
76 struct dx_countlimit {
81 typedef __u32 lvar_hash_t;
83 struct lvar_leaf_header {
84 u_int16_t vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
85 u_int16_t vlh_used; /* used bytes, including header */
92 u_int8_t vr_indirect_levels;
94 u_int16_t vr_padding1;
97 struct lvar_leaf_entry {
99 u_int16_t vle_keysize;
105 LVAR_ROUND = LVAR_PAD - 1
109 * Stores \a val at \a dst, where the latter is possibly unaligned. Uses
110 * memcpy(). This macro is needed to avoid dependency of user level tools on
111 * the kernel headers.
113 #define STORE_UNALIGNED(val, dst) \
115 typeof(val) __val = (val); \
117 BUILD_BUG_ON(sizeof(val) != sizeof(*(dst))); \
118 memcpy(dst, &__val, sizeof(*(dst))); \
121 static void lfix_root(void *buf,
122 int blocksize, int keysize, int ptrsize, int recsize)
124 struct iam_lfix_root *root;
125 struct dx_countlimit *limit;
129 *root = (typeof(*root)) {
130 .ilr_magic = cpu_to_le64(IAM_LFIX_ROOT_MAGIC),
131 .ilr_keysize = cpu_to_le16(keysize),
132 .ilr_recsize = cpu_to_le16(recsize),
133 .ilr_ptrsize = cpu_to_le16(ptrsize),
134 .ilr_indirect_levels = 0
137 limit = (void *)(root + 1);
138 *limit = (typeof(*limit)){
140 * limit itself + one pointer to the leaf.
142 .count = cpu_to_le16(2),
143 .limit = (blocksize - sizeof(*root)) / (keysize + ptrsize)
150 entry += keysize + ptrsize;
153 * Entry format is <key> followed by <ptr>. In the minimal tree
154 * consisting of a root and single node, <key> is a minimal possible
157 * XXX: this key is hard-coded to be a sequence of 0's.
160 /* now @entry points to <ptr> */
162 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
164 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
167 static void lfix_leaf(void *buf,
168 int blocksize, int keysize, int ptrsize, int recsize)
170 struct iam_leaf_head *head;
174 *head = (struct iam_leaf_head) {
175 .ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC),
177 * Leaf contains an entry with the smallest possible key
178 * (created by zeroing).
180 .ill_count = cpu_to_le16(1),
184 static void lvar_root(void *buf,
185 int blocksize, int keysize, int ptrsize, int recsize)
187 struct lvar_root *root;
188 struct dx_countlimit *limit;
192 isize = sizeof(lvar_hash_t) + ptrsize;
194 *root = (typeof(*root)) {
195 .vr_magic = cpu_to_le32(IAM_LVAR_ROOT_MAGIC),
196 .vr_recsize = cpu_to_le16(recsize),
197 .vr_ptrsize = cpu_to_le16(ptrsize),
198 .vr_indirect_levels = 0
201 limit = (void *)(root + 1);
202 *limit = (typeof(*limit)){
204 * limit itself + one pointer to the leaf.
206 .count = cpu_to_le16(2),
207 .limit = (blocksize - sizeof(*root)) / isize
217 * Entry format is <key> followed by <ptr>. In the minimal tree
218 * consisting of a root and single node, <key> is a minimal possible
221 * XXX: this key is hard-coded to be a sequence of 0's.
223 entry += sizeof(lvar_hash_t);
224 /* now @entry points to <ptr> */
226 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
228 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
231 static int lvar_esize(int namelen, int recsize)
233 return (offsetof(struct lvar_leaf_entry, vle_key) +
234 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
237 static void lvar_leaf(void *buf,
238 int blocksize, int keysize, int ptrsize, int recsize)
240 struct lvar_leaf_header *head;
244 *head = (typeof(*head)) {
245 .vlh_magic = cpu_to_le16(IAM_LVAR_LEAF_MAGIC),
246 .vlh_used = cpu_to_le16(sizeof(*head) + lvar_esize(0, recsize))
255 int main(int argc, char **argv)
259 int blocksize = 4096;
265 char *fmtstr = "lfix";
269 opt = getopt(argc, argv, "hb:k:r:p:vf:");
276 blocksize = atoi(optarg);
279 keysize = atoi(optarg);
282 recsize = atoi(optarg);
285 ptrsize = atoi(optarg);
292 fprintf(stderr, "Unable to parse options.");
299 if (ptrsize != 4 && ptrsize != 8) {
301 "Invalid ptrsize (%i). Only 4 and 8 are supported\n",
306 if (blocksize <= 100 || keysize < 1 || recsize < 0) {
307 fprintf(stderr, "Too small record, key or block block\n");
311 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
312 fprintf(stderr, "Too large (record, key) or too small block\n");
316 if (!strcmp(fmtstr, "lfix")) {
318 } else if (!strcmp(fmtstr, "lvar")) {
321 fprintf(stderr, "Wrong format `%s'\n", fmtstr);
327 "fmt: %s, key: %i, rec: %i, ptr: %i, block: %i\n",
328 fmtstr, keysize, recsize, ptrsize, blocksize);
330 buf = malloc(blocksize);
332 fprintf(stderr, "Unable to allocate %i bytes\n", blocksize);
336 memset(buf, 0, blocksize);
339 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
341 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
343 rc = write(1, buf, blocksize);
344 if (rc != blocksize) {
345 fprintf(stderr, "Unable to write root node: %m (%i)\n", rc);
351 memset(buf, 0, blocksize);
354 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
356 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
358 rc = write(1, buf, blocksize);
360 if (rc != blocksize) {
361 fprintf(stderr, "Unable to write leaf node: %m (%i)\n", rc);
366 "Don't forget to umount/mount before accessing iam from the kernel!\n");