1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * User-level tool for creation of iam files.
7 * Copyright (c) 2006 Cluster File Systems, Inc.
8 * Author: Wang Di <wangdi@clusterfs.com>
9 * Author: Nikita Danilov <nikita@clusterfs.com>
11 * This file is part of the Lustre file system, http://www.lustre.org
12 * Lustre is a trademark of Cluster File Systems, Inc.
14 * You may have signed or agreed to another license before downloading
15 * this software. If so, you are bound by the terms and conditions
16 * of that agreement, and the following does not apply to you. See the
17 * LICENSE file included with this distribution for more information.
19 * If you did not agree to a different license, then this copy of Lustre
20 * is open source software; you can redistribute it and/or modify it
21 * under the terms of version 2 of the GNU General Public License as
22 * published by the Free Software Foundation.
24 * In either case, Lustre is distributed in the hope that it will be
25 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * license text for more details.
37 #include <sys/types.h>
43 #include <libcfs/libcfs.h>
47 printf("usage: create_iam "
48 "[-h] [-k <keysize>] [-r recsize] [-b <blocksize] [-p <ptrsize>] [-v]\n");
52 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
53 IAM_LVAR_ROOT_MAGIC = 0xb01dface
56 struct iam_lfix_root {
58 u_int16_t ilr_keysize;
59 u_int16_t ilr_recsize;
60 u_int16_t ilr_ptrsize;
61 u_int16_t ilr_indirect_levels;
65 IAM_LEAF_HEADER_MAGIC = 0x1976,
66 IAM_LVAR_LEAF_MAGIC = 0x1973
69 struct iam_leaf_head {
74 struct dx_countlimit {
79 typedef __u32 lvar_hash_t;
81 struct lvar_leaf_header {
82 u_int16_t vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
83 u_int16_t vlh_used; /* used bytes, including header */
90 u_int8_t vr_indirect_levels;
92 u_int16_t vr_padding1;
95 struct lvar_leaf_entry {
97 u_int16_t vle_keysize;
103 LVAR_ROUND = LVAR_PAD - 1
106 static void lfix_root(void *buf,
107 int blocksize, int keysize, int ptrsize, int recsize)
109 struct iam_lfix_root *root;
110 struct dx_countlimit *limit;
114 *root = (typeof(*root)) {
115 .ilr_magic = cpu_to_le64(IAM_LFIX_ROOT_MAGIC),
116 .ilr_keysize = cpu_to_le16(keysize),
117 .ilr_recsize = cpu_to_le16(recsize),
118 .ilr_ptrsize = cpu_to_le16(ptrsize),
119 .ilr_indirect_levels = 0
122 limit = (void *)(root + 1);
123 *limit = (typeof(*limit)){
125 * limit itself + one pointer to the leaf.
127 .count = cpu_to_le16(2),
128 .limit = (blocksize - sizeof *root) / (keysize + ptrsize)
135 entry += keysize + ptrsize;
138 * Entry format is <key> followed by <ptr>. In the minimal tree
139 * consisting of a root and single node, <key> is a minimal possible
142 * XXX: this key is hard-coded to be a sequence of 0's.
145 /* now @entry points to <ptr> */
147 *(u_int32_t *)entry = cpu_to_le32(1);
149 *(u_int64_t *)entry = cpu_to_le64(1);
152 static void lfix_leaf(void *buf,
153 int blocksize, int keysize, int ptrsize, int recsize)
155 struct iam_leaf_head *head;
159 *head = (struct iam_leaf_head) {
160 .ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC),
162 * Leaf contains an entry with the smallest possible key
163 * (created by zeroing).
165 .ill_count = cpu_to_le16(1),
169 static void lvar_root(void *buf,
170 int blocksize, int keysize, int ptrsize, int recsize)
172 struct lvar_root *root;
173 struct dx_countlimit *limit;
177 isize = sizeof(lvar_hash_t) + ptrsize;
179 *root = (typeof(*root)) {
180 .vr_magic = cpu_to_le32(IAM_LVAR_ROOT_MAGIC),
181 .vr_recsize = cpu_to_le16(recsize),
182 .vr_ptrsize = cpu_to_le16(ptrsize),
183 .vr_indirect_levels = 0
186 limit = (void *)(root + 1);
187 *limit = (typeof(*limit)){
189 * limit itself + one pointer to the leaf.
191 .count = cpu_to_le16(2),
192 .limit = (blocksize - sizeof *root) / isize
202 * Entry format is <key> followed by <ptr>. In the minimal tree
203 * consisting of a root and single node, <key> is a minimal possible
206 * XXX: this key is hard-coded to be a sequence of 0's.
208 entry += sizeof(lvar_hash_t);
209 /* now @entry points to <ptr> */
211 *(u_int32_t *)entry = cpu_to_le32(1);
213 *(u_int64_t *)entry = cpu_to_le64(1);
216 static int lvar_esize(int namelen, int recsize)
218 return (offsetof(struct lvar_leaf_entry, vle_key) +
219 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
222 static void lvar_leaf(void *buf,
223 int blocksize, int keysize, int ptrsize, int recsize)
225 struct lvar_leaf_header *head;
229 *head = (typeof(*head)) {
230 .vlh_magic = cpu_to_le16(IAM_LVAR_LEAF_MAGIC),
231 .vlh_used = cpu_to_le16(sizeof *head + lvar_esize(0, recsize))
240 int main(int argc, char **argv)
244 int blocksize = 4096;
250 char *fmtstr = "lfix";
254 opt = getopt(argc, argv, "hb:k:r:p:vf:");
261 blocksize = atoi(optarg);
264 keysize = atoi(optarg);
267 recsize = atoi(optarg);
270 ptrsize = atoi(optarg);
277 fprintf(stderr, "Unable to parse options.");
284 if (ptrsize != 4 && ptrsize != 8) {
285 fprintf(stderr, "Invalid ptrsize (%i). "
286 "Only 4 and 8 are supported\n", ptrsize);
290 if (blocksize <= 100 || keysize < 1 || recsize < 0) {
291 fprintf(stderr, "Too small record, key or block block\n");
295 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
296 fprintf(stderr, "Too large (record, key) or too small block\n");
300 if (!strcmp(fmtstr, "lfix"))
302 else if (!strcmp(fmtstr, "lvar"))
305 fprintf(stderr, "Wrong format `%s'\n", fmtstr);
311 "fmt: %s, key: %i, rec: %i, ptr: %i, block: %i\n",
312 fmtstr, keysize, recsize, ptrsize, blocksize);
314 buf = malloc(blocksize);
316 fprintf(stderr, "Unable to allocate %i bytes\n", blocksize);
320 memset(buf, 0, blocksize);
323 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
325 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
327 rc = write(1, buf, blocksize);
328 if (rc != blocksize) {
329 fprintf(stderr, "Unable to write root node: %m (%i)\n", rc);
335 memset(buf, 0, blocksize);
338 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
340 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
342 rc = write(1, buf, blocksize);
344 if (rc != blocksize) {
345 fprintf(stderr, "Unable to write leaf node: %m (%i)\n", rc);
349 fprintf(stderr, "Don't forget to umount/mount "
350 "before accessing iam from the kernel!\n");