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/libiam.c
36 * iam user level library
38 * Author: Wang Di <wangdi@clusterfs.com>
39 * Author: Nikita Danilov <nikita@clusterfs.com>
40 * Author: Fan Yong <fanyong@clusterfs.com>
51 #include <sys/types.h>
57 #include <libcfs/libcfs.h>
58 #include <lustre/libiam.h>
60 typedef __u32 lvar_hash_t;
63 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
64 IAM_LVAR_ROOT_MAGIC = 0xb01dface
67 struct iam_lfix_root {
69 u_int16_t ilr_keysize;
70 u_int16_t ilr_recsize;
71 u_int16_t ilr_ptrsize;
72 u_int16_t ilr_indirect_levels;
76 IAM_LEAF_HEADER_MAGIC = 0x1976,
77 IAM_LVAR_LEAF_MAGIC = 0x1973
80 struct iam_leaf_head {
85 struct dx_countlimit {
90 struct lvar_leaf_header {
91 u_int16_t vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
92 u_int16_t vlh_used; /* used bytes, including header */
99 u_int8_t vr_indirect_levels;
100 u_int8_t vr_padding0;
101 u_int16_t vr_padding1;
104 struct lvar_leaf_entry {
106 u_int16_t vle_keysize;
112 LVAR_ROUND = LVAR_PAD - 1
116 * Stores \a val at \a dst, where the latter is possibly unaligned. Uses
117 * memcpy(). This macro is needed to avoid dependency of user level tools on
118 * the kernel headers.
120 #define STORE_UNALIGNED(val, dst) \
122 typeof(*(dst)) __val = (val); \
124 memcpy(dst, &__val, sizeof *(dst)); \
127 static int root_limit(int rootgap, int blocksize, int size)
132 limit = (blocksize - rootgap) / size;
133 nlimit = blocksize / size;
139 static int lfix_root_limit(int blocksize, int size)
141 return root_limit(sizeof(struct iam_lfix_root), blocksize, size);
144 static void lfix_root(void *buf,
145 int blocksize, int keysize, int ptrsize, int recsize)
147 struct iam_lfix_root *root;
148 struct dx_countlimit *limit;
152 *root = (typeof(*root)) {
153 .ilr_magic = cpu_to_le64(IAM_LFIX_ROOT_MAGIC),
154 .ilr_keysize = cpu_to_le16(keysize),
155 .ilr_recsize = cpu_to_le16(recsize),
156 .ilr_ptrsize = cpu_to_le16(ptrsize),
157 .ilr_indirect_levels = 0
160 limit = (void *)(root + 1);
161 *limit = (typeof(*limit)){
163 * limit itself + one pointer to the leaf.
165 .count = cpu_to_le16(2),
166 .limit = lfix_root_limit(blocksize, keysize + ptrsize)
173 entry += keysize + ptrsize;
176 * Entry format is <key> followed by <ptr>. In the minimal tree
177 * consisting of a root and single node, <key> is a minimal possible
180 * XXX: this key is hard-coded to be a sequence of 0's.
183 /* now @entry points to <ptr> */
185 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
187 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
190 static void lfix_leaf(void *buf,
191 int blocksize, int keysize, int ptrsize, int recsize)
193 struct iam_leaf_head *head;
197 *head = (typeof(*head)) {
198 .ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC),
200 * Leaf contains an entry with the smallest possible key
201 * (created by zeroing).
203 .ill_count = cpu_to_le16(1),
207 static int lvar_root_limit(int blocksize, int size)
209 return root_limit(sizeof(struct lvar_root), blocksize, size);
212 static void lvar_root(void *buf,
213 int blocksize, int keysize, int ptrsize, int recsize)
215 struct lvar_root *root;
216 struct dx_countlimit *limit;
220 isize = sizeof(lvar_hash_t) + ptrsize;
222 *root = (typeof(*root)) {
223 .vr_magic = cpu_to_le32(IAM_LVAR_ROOT_MAGIC),
224 .vr_recsize = cpu_to_le16(recsize),
225 .vr_ptrsize = cpu_to_le16(ptrsize),
226 .vr_indirect_levels = 0
229 limit = (void *)(root + 1);
230 *limit = (typeof(*limit)) {
232 * limit itself + one pointer to the leaf.
234 .count = cpu_to_le16(2),
235 .limit = lvar_root_limit(blocksize, keysize + ptrsize)
245 * Entry format is <key> followed by <ptr>. In the minimal tree
246 * consisting of a root and single node, <key> is a minimal possible
249 * XXX: this key is hard-coded to be a sequence of 0's.
251 entry += sizeof(lvar_hash_t);
252 /* now @entry points to <ptr> */
254 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
256 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
259 static int lvar_esize(int namelen, int recsize)
261 return (offsetof(struct lvar_leaf_entry, vle_key) +
262 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
265 static void lvar_leaf(void *buf,
266 int blocksize, int keysize, int ptrsize, int recsize)
268 struct lvar_leaf_header *head;
273 *head = (typeof(*head)) {
274 .vlh_magic = cpu_to_le16(IAM_LVAR_LEAF_MAGIC),
275 .vlh_used = cpu_to_le16(sizeof *head + lvar_esize(0, recsize))
277 rec = (void *)(head + 1);
278 rec[offsetof(struct lvar_leaf_entry, vle_key)] = recsize;
288 struct iam_uapi_op iui_op;
293 IAM_IOC_INIT = _IOW('i', 1, struct iam_uapi_info),
294 IAM_IOC_GETINFO = _IOR('i', 2, struct iam_uapi_info),
295 IAM_IOC_INSERT = _IOR('i', 3, struct iam_uapi_op),
296 IAM_IOC_LOOKUP = _IOWR('i', 4, struct iam_uapi_op),
297 IAM_IOC_DELETE = _IOR('i', 5, struct iam_uapi_op),
298 IAM_IOC_IT_START = _IOR('i', 6, struct iam_uapi_it),
299 IAM_IOC_IT_NEXT = _IOW('i', 7, struct iam_uapi_it),
300 IAM_IOC_IT_STOP = _IOR('i', 8, struct iam_uapi_it),
301 IAM_IOC_POLYMORPH = _IOR('i', 9, unsigned long)
304 static unsigned char hex2dec(unsigned char hex)
306 if (('0' <= hex) && (hex <= '9'))
308 else if (('a' <= hex) && (hex <= 'f'))
309 return hex - 'a' + 10;
310 else if (('A' <= hex) && (hex <= 'F'))
311 return hex - 'A' + 10;
316 static unsigned char *packdigit(unsigned char *number)
321 area = calloc(strlen((char *)number) / 2 + 2, sizeof(char));
323 for (scan = area; *number; number += 2, scan++)
324 *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
329 static char *iam_convert(int size, int need_convert, char *source)
337 ptr = calloc(size + 1, sizeof(char));
342 opt = packdigit((unsigned char*)source);
347 memcpy(ptr, opt, size + 1);
351 strlcpy(ptr, source, size + 1);
357 static int iam_doop(int fd, struct iam_uapi_info *ua, int cmd,
358 int key_need_convert, char *key_buf,
359 int *keysize, char *save_key,
360 int rec_need_convert, char *rec_buf,
361 int *recsize, char *save_rec)
366 struct iam_uapi_op op;
368 key = iam_convert(ua->iui_keysize, key_need_convert, key_buf);
372 rec = iam_convert(ua->iui_recsize, rec_need_convert, rec_buf);
380 ret = ioctl(fd, cmd, &op);
382 if ((keysize != NULL) && (*keysize > 0) && (save_key != NULL)) {
383 if (*keysize > ua->iui_keysize)
384 *keysize = ua->iui_keysize;
385 memcpy(save_key, key, *keysize);
387 if ((recsize != NULL) && (*recsize > 0) && (save_rec != NULL)) {
388 if (*recsize > ua->iui_recsize)
389 *recsize = ua->iui_recsize;
390 memcpy(save_rec, rec, *recsize);
400 * Creat an iam file, but do NOT open it.
401 * Return 0 if success, else -1.
403 int iam_creat(char *filename, enum iam_fmt_t fmt,
404 int blocksize, int keysize, int recsize, int ptrsize)
409 if (filename == NULL) {
414 if ((fmt != FMT_LFIX) && (fmt != FMT_LVAR)) {
419 if (blocksize <= 100) {
434 if (ptrsize != 4 && ptrsize != 8) {
439 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
444 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0600);
449 buf = malloc(blocksize);
455 memset(buf, 0, blocksize);
457 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
459 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
461 if (write(fd, buf, blocksize) != blocksize) {
467 memset(buf, 0, blocksize);
469 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
471 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
473 if (write(fd, buf, blocksize) != blocksize) {
485 * Open an iam file, but do NOT creat it if the file doesn't exist.
486 * Please use iam_creat for creating the file before use iam_open.
487 * Return file id (fd) if success, else -1.
489 int iam_open(char *filename, struct iam_uapi_info *ua)
493 if (filename == NULL) {
503 fd = open(filename, O_RDONLY);
508 if (ioctl(fd, IAM_IOC_INIT, ua) != 0) {
513 if (ioctl(fd, IAM_IOC_GETINFO, ua) != 0) {
522 * Close file opened by iam_open.
524 int iam_close(int fd)
530 * Please use iam_open before use this function.
532 int iam_insert(int fd, struct iam_uapi_info *ua,
533 int key_need_convert, char *key_buf,
534 int rec_need_convert, char *rec_buf)
536 return iam_doop(fd, ua, IAM_IOC_INSERT,
537 key_need_convert, key_buf, NULL, NULL,
538 rec_need_convert, rec_buf, NULL, NULL);
542 * Please use iam_open before use this function.
544 int iam_lookup(int fd, struct iam_uapi_info *ua,
545 int key_need_convert, char *key_buf,
546 int *keysize, char *save_key,
547 int rec_need_convert, char *rec_buf,
548 int *recsize, char *save_rec)
550 return iam_doop(fd, ua, IAM_IOC_LOOKUP,
551 key_need_convert, key_buf, keysize, save_key,
552 rec_need_convert, rec_buf, recsize, save_rec);
556 * Please use iam_open before use this function.
558 int iam_delete(int fd, struct iam_uapi_info *ua,
559 int key_need_convert, char *key_buf,
560 int rec_need_convert, char *rec_buf)
562 return iam_doop(fd, ua, IAM_IOC_DELETE,
563 key_need_convert, key_buf, NULL, NULL,
564 rec_need_convert, rec_buf, NULL, NULL);
568 * Please use iam_open before use this function.
570 int iam_it_start(int fd, struct iam_uapi_info *ua,
571 int key_need_convert, char *key_buf,
572 int *keysize, char *save_key,
573 int rec_need_convert, char *rec_buf,
574 int *recsize, char *save_rec)
576 return iam_doop(fd, ua, IAM_IOC_IT_START,
577 key_need_convert, key_buf, keysize, save_key,
578 rec_need_convert, rec_buf, recsize, save_rec);
582 * Please use iam_open before use this function.
584 int iam_it_next(int fd, struct iam_uapi_info *ua,
585 int key_need_convert, char *key_buf,
586 int *keysize, char *save_key,
587 int rec_need_convert, char *rec_buf,
588 int *recsize, char *save_rec)
590 return iam_doop(fd, ua, IAM_IOC_IT_NEXT,
591 key_need_convert, key_buf, keysize, save_key,
592 rec_need_convert, rec_buf, recsize, save_rec);
596 * Please use iam_open before use this function.
598 int iam_it_stop(int fd, struct iam_uapi_info *ua,
599 int key_need_convert, char *key_buf,
600 int rec_need_convert, char *rec_buf)
602 return iam_doop(fd, ua, IAM_IOC_IT_STOP,
603 key_need_convert, key_buf, NULL, NULL,
604 rec_need_convert, rec_buf, NULL, NULL);
608 * Change iam file mode.
610 int iam_polymorph(char *filename, unsigned long mode)
615 if (filename == NULL) {
620 fd = open(filename, O_RDONLY);
625 ret = ioctl(fd, IAM_IOC_POLYMORPH, mode);