Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / utils / create_iam.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  create_iam.c
5  *  User-level tool for creation of iam files.
6  *
7  *  Copyright (c) 2006 Cluster File Systems, Inc.
8  *   Author: Wang Di <wangdi@clusterfs.com>
9  *   Author: Nikita Danilov <nikita@clusterfs.com>
10  *
11  *   This file is part of the Lustre file system, http://www.lustre.org
12  *   Lustre is a trademark of Cluster File Systems, Inc.
13  *
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.
18  *
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.
23  *
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.
28  */
29
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <errno.h>
36
37 #include <sys/types.h>
38
39 #ifdef HAVE_ENDIAN_H
40 #include <endian.h>
41 #endif
42
43 #include <libcfs/libcfs.h>
44
45 void usage(void)
46 {
47         printf("usage: create_iam "
48                "[-h] [-k <keysize>] [-r recsize] [-b <blocksize] [-p <ptrsize>] [-v]\n");
49 }
50
51 enum {
52         IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
53         IAM_LVAR_ROOT_MAGIC = 0xb01dface
54 };
55
56 struct iam_lfix_root {
57         u_int64_t  ilr_magic;
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;
62 };
63
64 enum {
65         IAM_LEAF_HEADER_MAGIC = 0x1976,
66         IAM_LVAR_LEAF_MAGIC   = 0x1973
67 };
68
69 struct iam_leaf_head {
70         u_int16_t ill_magic;
71         u_int16_t ill_count;
72 };
73
74 struct dx_countlimit {
75         u_int16_t limit;
76         u_int16_t count;
77 };
78
79 typedef __u32 lvar_hash_t;
80
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 */
84 };
85
86 struct lvar_root {
87         u_int32_t vr_magic;
88         u_int16_t vr_recsize;
89         u_int16_t vr_ptrsize;
90         u_int8_t  vr_indirect_levels;
91         u_int8_t  vr_padding0;
92         u_int16_t vr_padding1;
93 };
94
95 struct lvar_leaf_entry {
96         u_int32_t vle_hash;
97         u_int16_t vle_keysize;
98         u_int8_t  vle_key[0];
99 };
100
101 enum {
102         LVAR_PAD   = 4,
103         LVAR_ROUND = LVAR_PAD - 1
104 };
105
106 static void lfix_root(void *buf,
107                       int blocksize, int keysize, int ptrsize, int recsize)
108 {
109         struct iam_lfix_root *root;
110         struct dx_countlimit *limit;
111         void                 *entry;
112
113         root = buf;
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
120         };
121
122         limit = (void *)(root + 1);
123         *limit = (typeof(*limit)){
124                 /*
125                  * limit itself + one pointer to the leaf.
126                  */
127                 .count = cpu_to_le16(2),
128                 .limit = (blocksize - sizeof *root) / (keysize + ptrsize)
129         };
130
131         entry = root + 1;
132         /*
133          * Skip over @limit.
134          */
135         entry += keysize + ptrsize;
136
137         /*
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
140          * key.
141          *
142          * XXX: this key is hard-coded to be a sequence of 0's.
143          */
144         entry += keysize;
145         /* now @entry points to <ptr> */
146         if (ptrsize == 4)
147                 *(u_int32_t *)entry = cpu_to_le32(1);
148         else
149                 *(u_int64_t *)entry = cpu_to_le64(1);
150 }
151
152 static void lfix_leaf(void *buf,
153                       int blocksize, int keysize, int ptrsize, int recsize)
154 {
155         struct iam_leaf_head *head;
156
157         /* form leaf */
158         head = buf;
159         *head = (struct iam_leaf_head) {
160                 .ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC),
161                 /*
162                  * Leaf contains an entry with the smallest possible key
163                  * (created by zeroing).
164                  */
165                 .ill_count = cpu_to_le16(1),
166         };
167 }
168
169 static void lvar_root(void *buf,
170                       int blocksize, int keysize, int ptrsize, int recsize)
171 {
172         struct lvar_root *root;
173         struct dx_countlimit *limit;
174         void                 *entry;
175         int isize;
176
177         isize = sizeof(lvar_hash_t) + ptrsize;
178         root = buf;
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
184         };
185
186         limit = (void *)(root + 1);
187         *limit = (typeof(*limit)){
188                 /*
189                  * limit itself + one pointer to the leaf.
190                  */
191                 .count = cpu_to_le16(2),
192                 .limit = (blocksize - sizeof *root) / isize
193         };
194
195         entry = root + 1;
196         /*
197          * Skip over @limit.
198          */
199         entry += isize;
200
201         /*
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
204          * key.
205          *
206          * XXX: this key is hard-coded to be a sequence of 0's.
207          */
208         entry += sizeof(lvar_hash_t);
209         /* now @entry points to <ptr> */
210         if (ptrsize == 4)
211                 *(u_int32_t *)entry = cpu_to_le32(1);
212         else
213                 *(u_int64_t *)entry = cpu_to_le64(1);
214 }
215
216 static int lvar_esize(int namelen, int recsize)
217 {
218         return (offsetof(struct lvar_leaf_entry, vle_key) +
219                 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
220 }
221
222 static void lvar_leaf(void *buf,
223                       int blocksize, int keysize, int ptrsize, int recsize)
224 {
225         struct lvar_leaf_header *head;
226
227         /* form leaf */
228         head = buf;
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))
232         };
233 }
234
235 enum iam_fmt_t {
236         FMT_LFIX,
237         FMT_LVAR
238 };
239
240 int main(int argc, char **argv)
241 {
242         int rc;
243         int opt;
244         int blocksize = 4096;
245         int keysize   = 8;
246         int recsize   = 8;
247         int ptrsize   = 4;
248         int verbose   = 0;
249         void *buf;
250         char *fmtstr = "lfix";
251         enum iam_fmt_t fmt;
252
253         do {
254                 opt = getopt(argc, argv, "hb:k:r:p:vf:");
255                 switch (opt) {
256                 case 'v':
257                         verbose++;
258                 case -1:
259                         break;
260                 case 'b':
261                         blocksize = atoi(optarg);
262                         break;
263                 case 'k':
264                         keysize = atoi(optarg);
265                         break;
266                 case 'r':
267                         recsize = atoi(optarg);
268                         break;
269                 case 'p':
270                         ptrsize = atoi(optarg);
271                         break;
272                 case 'f':
273                         fmtstr = optarg;
274                         break;
275                 case '?':
276                 default:
277                         fprintf(stderr, "Unable to parse options.");
278                 case 'h':
279                         usage();
280                         return 0;
281                 }
282         } while (opt != -1);
283
284         if (ptrsize != 4 && ptrsize != 8) {
285                 fprintf(stderr, "Invalid ptrsize (%i). "
286                         "Only 4 and 8 are supported\n", ptrsize);
287                 return 1;
288         }
289
290         if (blocksize <= 100 || keysize < 1 || recsize < 0) {
291                 fprintf(stderr, "Too small record, key or block block\n");
292                 return 1;
293         }
294
295         if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
296                 fprintf(stderr, "Too large (record, key) or too small block\n");
297                 return 1;
298         }
299
300         if (!strcmp(fmtstr, "lfix"))
301                 fmt = FMT_LFIX;
302         else if (!strcmp(fmtstr, "lvar"))
303                 fmt = FMT_LVAR;
304         else {
305                 fprintf(stderr, "Wrong format `%s'\n", fmtstr);
306                 return 1;
307         }
308
309         if (verbose > 0) {
310                 fprintf(stderr,
311                         "fmt: %s, key: %i, rec: %i, ptr: %i, block: %i\n",
312                         fmtstr, keysize, recsize, ptrsize, blocksize);
313         }
314         buf = malloc(blocksize);
315         if (buf == NULL) {
316                 fprintf(stderr, "Unable to allocate %i bytes\n", blocksize);
317                 return 1;
318         }
319
320         memset(buf, 0, blocksize);
321
322         if (fmt == FMT_LFIX)
323                 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
324         else
325                 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
326
327         rc = write(1, buf, blocksize);
328         if (rc != blocksize) {
329                 fprintf(stderr, "Unable to write root node: %m (%i)\n", rc);
330                 free(buf);
331                 return 1;
332         }
333
334         /* form leaf */
335         memset(buf, 0, blocksize);
336
337         if (fmt == FMT_LFIX)
338                 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
339         else
340                 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
341
342         rc = write(1, buf, blocksize);
343         free(buf);
344         if (rc != blocksize) {
345                 fprintf(stderr, "Unable to write leaf node: %m (%i)\n", rc);
346                 return 1;
347         }
348         if (verbose > 0)
349                 fprintf(stderr, "Don't forget to umount/mount "
350                         "before accessing iam from the kernel!\n");
351         return 0;
352 }