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.
30 * Copyright (c) 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/libiam.c
38 * iam user level library
40 * Author: Wang Di <wangdi@clusterfs.com>
41 * Author: Nikita Danilov <nikita@clusterfs.com>
42 * Author: Fan Yong <fanyong@clusterfs.com>
53 #include <sys/types.h>
59 #include <libcfs/libcfs.h>
60 #include <lustre/libiam.h>
62 typedef __u32 lvar_hash_t;
65 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
66 IAM_LVAR_ROOT_MAGIC = 0xb01dface
69 struct iam_lfix_root {
71 u_int16_t ilr_keysize;
72 u_int16_t ilr_recsize;
73 u_int16_t ilr_ptrsize;
74 u_int16_t ilr_indirect_levels;
78 IAM_LEAF_HEADER_MAGIC = 0x1976,
79 IAM_LVAR_LEAF_MAGIC = 0x1973
82 struct iam_leaf_head {
87 struct dx_countlimit {
92 struct lvar_leaf_header {
93 u_int16_t vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
94 u_int16_t vlh_used; /* used bytes, including header */
100 u_int16_t vr_ptrsize;
101 u_int8_t vr_indirect_levels;
102 u_int8_t vr_padding0;
103 u_int16_t vr_padding1;
106 struct lvar_leaf_entry {
108 u_int16_t vle_keysize;
114 LVAR_ROUND = LVAR_PAD - 1
118 * Stores \a val at \a dst, where the latter is possibly unaligned. Uses
119 * memcpy(). This macro is needed to avoid dependency of user level tools on
120 * the kernel headers.
122 #define STORE_UNALIGNED(val, dst) \
124 typeof(*(dst)) __val = (val); \
126 memcpy(dst, &__val, sizeof *(dst)); \
129 static int root_limit(int rootgap, int blocksize, int size)
134 limit = (blocksize - rootgap) / size;
135 nlimit = blocksize / size;
141 static int lfix_root_limit(int blocksize, int size)
143 return root_limit(sizeof(struct iam_lfix_root), blocksize, size);
146 static void lfix_root(void *buf,
147 int blocksize, int keysize, int ptrsize, int recsize)
149 struct iam_lfix_root *root;
150 struct dx_countlimit *limit;
154 *root = (typeof(*root)) {
155 .ilr_magic = cpu_to_le64(IAM_LFIX_ROOT_MAGIC),
156 .ilr_keysize = cpu_to_le16(keysize),
157 .ilr_recsize = cpu_to_le16(recsize),
158 .ilr_ptrsize = cpu_to_le16(ptrsize),
159 .ilr_indirect_levels = 0
162 limit = (void *)(root + 1);
163 *limit = (typeof(*limit)){
165 * limit itself + one pointer to the leaf.
167 .count = cpu_to_le16(2),
168 .limit = lfix_root_limit(blocksize, keysize + ptrsize)
175 entry += keysize + ptrsize;
178 * Entry format is <key> followed by <ptr>. In the minimal tree
179 * consisting of a root and single node, <key> is a minimal possible
182 * XXX: this key is hard-coded to be a sequence of 0's.
185 /* now @entry points to <ptr> */
187 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
189 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
192 static void lfix_leaf(void *buf,
193 int blocksize, int keysize, int ptrsize, int recsize)
195 struct iam_leaf_head *head;
199 *head = (typeof(*head)) {
200 .ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC),
202 * Leaf contains an entry with the smallest possible key
203 * (created by zeroing).
205 .ill_count = cpu_to_le16(1),
209 static int lvar_root_limit(int blocksize, int size)
211 return root_limit(sizeof(struct lvar_root), blocksize, size);
214 static void lvar_root(void *buf,
215 int blocksize, int keysize, int ptrsize, int recsize)
217 struct lvar_root *root;
218 struct dx_countlimit *limit;
222 isize = sizeof(lvar_hash_t) + ptrsize;
224 *root = (typeof(*root)) {
225 .vr_magic = cpu_to_le32(IAM_LVAR_ROOT_MAGIC),
226 .vr_recsize = cpu_to_le16(recsize),
227 .vr_ptrsize = cpu_to_le16(ptrsize),
228 .vr_indirect_levels = 0
231 limit = (void *)(root + 1);
232 *limit = (typeof(*limit)) {
234 * limit itself + one pointer to the leaf.
236 .count = cpu_to_le16(2),
237 .limit = lvar_root_limit(blocksize, keysize + ptrsize)
247 * Entry format is <key> followed by <ptr>. In the minimal tree
248 * consisting of a root and single node, <key> is a minimal possible
251 * XXX: this key is hard-coded to be a sequence of 0's.
253 entry += sizeof(lvar_hash_t);
254 /* now @entry points to <ptr> */
256 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
258 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
261 static int lvar_esize(int namelen, int recsize)
263 return (offsetof(struct lvar_leaf_entry, vle_key) +
264 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
267 static void lvar_leaf(void *buf,
268 int blocksize, int keysize, int ptrsize, int recsize)
270 struct lvar_leaf_header *head;
275 *head = (typeof(*head)) {
276 .vlh_magic = cpu_to_le16(IAM_LVAR_LEAF_MAGIC),
277 .vlh_used = cpu_to_le16(sizeof *head + lvar_esize(0, recsize))
279 rec = (void *)(head + 1);
280 rec[offsetof(struct lvar_leaf_entry, vle_key)] = recsize;
290 struct iam_uapi_op iui_op;
295 IAM_IOC_INIT = _IOW('i', 1, struct iam_uapi_info),
296 IAM_IOC_GETINFO = _IOR('i', 2, struct iam_uapi_info),
297 IAM_IOC_INSERT = _IOR('i', 3, struct iam_uapi_op),
298 IAM_IOC_LOOKUP = _IOWR('i', 4, struct iam_uapi_op),
299 IAM_IOC_DELETE = _IOR('i', 5, struct iam_uapi_op),
300 IAM_IOC_IT_START = _IOR('i', 6, struct iam_uapi_it),
301 IAM_IOC_IT_NEXT = _IOW('i', 7, struct iam_uapi_it),
302 IAM_IOC_IT_STOP = _IOR('i', 8, struct iam_uapi_it),
303 IAM_IOC_POLYMORPH = _IOR('i', 9, unsigned long)
306 static unsigned char hex2dec(unsigned char hex)
308 if (('0' <= hex) && (hex <= '9'))
310 else if (('a' <= hex) && (hex <= 'f'))
311 return hex - 'a' + 10;
312 else if (('A' <= hex) && (hex <= 'F'))
313 return hex - 'A' + 10;
318 static unsigned char *packdigit(unsigned char *number)
323 area = calloc(strlen((char *)number) / 2 + 2, sizeof(char));
325 for (scan = area; *number; number += 2, scan++)
326 *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
331 static char *iam_convert(int size, int need_convert, char *source)
339 ptr = calloc(size + 1, sizeof(char));
344 opt = packdigit((unsigned char*)source);
349 memcpy(ptr, opt, size + 1);
353 strlcpy(ptr, source, size + 1);
359 static int iam_doop(int fd, struct iam_uapi_info *ua, int cmd,
360 int key_need_convert, char *key_buf,
361 int *keysize, char *save_key,
362 int rec_need_convert, char *rec_buf,
363 int *recsize, char *save_rec)
368 struct iam_uapi_op op;
370 key = iam_convert(ua->iui_keysize, key_need_convert, key_buf);
374 rec = iam_convert(ua->iui_recsize, rec_need_convert, rec_buf);
382 ret = ioctl(fd, cmd, &op);
384 if ((keysize != NULL) && (*keysize > 0) && (save_key != NULL)) {
385 if (*keysize > ua->iui_keysize)
386 *keysize = ua->iui_keysize;
387 memcpy(save_key, key, *keysize);
389 if ((recsize != NULL) && (*recsize > 0) && (save_rec != NULL)) {
390 if (*recsize > ua->iui_recsize)
391 *recsize = ua->iui_recsize;
392 memcpy(save_rec, rec, *recsize);
402 * Creat an iam file, but do NOT open it.
403 * Return 0 if success, else -1.
405 int iam_creat(char *filename, enum iam_fmt_t fmt,
406 int blocksize, int keysize, int recsize, int ptrsize)
411 if (filename == NULL) {
416 if ((fmt != FMT_LFIX) && (fmt != FMT_LVAR)) {
421 if (blocksize <= 100) {
436 if (ptrsize != 4 && ptrsize != 8) {
441 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
446 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0600);
451 buf = malloc(blocksize);
457 memset(buf, 0, blocksize);
459 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
461 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
463 if (write(fd, buf, blocksize) != blocksize) {
469 memset(buf, 0, blocksize);
471 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
473 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
475 if (write(fd, buf, blocksize) != blocksize) {
487 * Open an iam file, but do NOT creat it if the file doesn't exist.
488 * Please use iam_creat for creating the file before use iam_open.
489 * Return file id (fd) if success, else -1.
491 int iam_open(char *filename, struct iam_uapi_info *ua)
495 if (filename == NULL) {
505 fd = open(filename, O_RDONLY);
510 if (ioctl(fd, IAM_IOC_INIT, ua) != 0) {
515 if (ioctl(fd, IAM_IOC_GETINFO, ua) != 0) {
524 * Close file opened by iam_open.
526 int iam_close(int fd)
532 * Please use iam_open before use this function.
534 int iam_insert(int fd, struct iam_uapi_info *ua,
535 int key_need_convert, char *key_buf,
536 int rec_need_convert, char *rec_buf)
538 return iam_doop(fd, ua, IAM_IOC_INSERT,
539 key_need_convert, key_buf, NULL, NULL,
540 rec_need_convert, rec_buf, NULL, NULL);
544 * Please use iam_open before use this function.
546 int iam_lookup(int fd, struct iam_uapi_info *ua,
547 int key_need_convert, char *key_buf,
548 int *keysize, char *save_key,
549 int rec_need_convert, char *rec_buf,
550 int *recsize, char *save_rec)
552 return iam_doop(fd, ua, IAM_IOC_LOOKUP,
553 key_need_convert, key_buf, keysize, save_key,
554 rec_need_convert, rec_buf, recsize, save_rec);
558 * Please use iam_open before use this function.
560 int iam_delete(int fd, struct iam_uapi_info *ua,
561 int key_need_convert, char *key_buf,
562 int rec_need_convert, char *rec_buf)
564 return iam_doop(fd, ua, IAM_IOC_DELETE,
565 key_need_convert, key_buf, NULL, NULL,
566 rec_need_convert, rec_buf, NULL, NULL);
570 * Please use iam_open before use this function.
572 int iam_it_start(int fd, struct iam_uapi_info *ua,
573 int key_need_convert, char *key_buf,
574 int *keysize, char *save_key,
575 int rec_need_convert, char *rec_buf,
576 int *recsize, char *save_rec)
578 return iam_doop(fd, ua, IAM_IOC_IT_START,
579 key_need_convert, key_buf, keysize, save_key,
580 rec_need_convert, rec_buf, recsize, save_rec);
584 * Please use iam_open before use this function.
586 int iam_it_next(int fd, struct iam_uapi_info *ua,
587 int key_need_convert, char *key_buf,
588 int *keysize, char *save_key,
589 int rec_need_convert, char *rec_buf,
590 int *recsize, char *save_rec)
592 return iam_doop(fd, ua, IAM_IOC_IT_NEXT,
593 key_need_convert, key_buf, keysize, save_key,
594 rec_need_convert, rec_buf, recsize, save_rec);
598 * Please use iam_open before use this function.
600 int iam_it_stop(int fd, struct iam_uapi_info *ua,
601 int key_need_convert, char *key_buf,
602 int rec_need_convert, char *rec_buf)
604 return iam_doop(fd, ua, IAM_IOC_IT_STOP,
605 key_need_convert, key_buf, NULL, NULL,
606 rec_need_convert, rec_buf, NULL, NULL);
610 * Change iam file mode.
612 int iam_polymorph(char *filename, unsigned long mode)
617 if (filename == NULL) {
622 fd = open(filename, O_RDONLY);
627 ret = ioctl(fd, IAM_IOC_POLYMORPH, mode);