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, 2015, 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>
52 #include <sys/ioctl.h>
53 #include <sys/types.h>
55 #include <libcfs/byteorder.h>
56 #include <libcfs/util/string.h>
57 #include <lustre/libiam.h>
59 typedef __u32 lvar_hash_t;
62 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
63 IAM_LVAR_ROOT_MAGIC = 0xb01dface
66 struct iam_lfix_root {
68 u_int16_t ilr_keysize;
69 u_int16_t ilr_recsize;
70 u_int16_t ilr_ptrsize;
71 u_int16_t ilr_indirect_levels;
75 IAM_LEAF_HEADER_MAGIC = 0x1976,
76 IAM_LVAR_LEAF_MAGIC = 0x1973
79 struct iam_leaf_head {
84 struct dx_countlimit {
89 struct lvar_leaf_header {
90 u_int16_t vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
91 u_int16_t vlh_used; /* used bytes, including header */
98 u_int8_t vr_indirect_levels;
100 u_int16_t vr_padding1;
103 struct lvar_leaf_entry {
105 u_int16_t vle_keysize;
111 LVAR_ROUND = LVAR_PAD - 1
115 * Stores \a val at \a dst, where the latter is possibly unaligned. Uses
116 * memcpy(). This macro is needed to avoid dependency of user level tools on
117 * the kernel headers.
119 #define STORE_UNALIGNED(val, dst) \
121 typeof(*(dst)) __val = (val); \
123 memcpy(dst, &__val, sizeof *(dst)); \
126 static int root_limit(int rootgap, int blocksize, int size)
131 limit = (blocksize - rootgap) / size;
132 nlimit = blocksize / size;
138 static int lfix_root_limit(int blocksize, int size)
140 return root_limit(sizeof(struct iam_lfix_root), blocksize, size);
143 static void lfix_root(void *buf,
144 int blocksize, int keysize, int ptrsize, int recsize)
146 struct iam_lfix_root *root;
147 struct dx_countlimit *limit;
151 *root = (typeof(*root)) {
152 .ilr_magic = cpu_to_le64(IAM_LFIX_ROOT_MAGIC),
153 .ilr_keysize = cpu_to_le16(keysize),
154 .ilr_recsize = cpu_to_le16(recsize),
155 .ilr_ptrsize = cpu_to_le16(ptrsize),
156 .ilr_indirect_levels = 0
159 limit = (void *)(root + 1);
160 *limit = (typeof(*limit)){
162 * limit itself + one pointer to the leaf.
164 .count = cpu_to_le16(2),
165 .limit = lfix_root_limit(blocksize, keysize + ptrsize)
172 entry += keysize + ptrsize;
175 * Entry format is <key> followed by <ptr>. In the minimal tree
176 * consisting of a root and single node, <key> is a minimal possible
179 * XXX: this key is hard-coded to be a sequence of 0's.
182 /* now @entry points to <ptr> */
184 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
186 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
189 static void lfix_leaf(void *buf,
190 int blocksize, int keysize, int ptrsize, int recsize)
192 struct iam_leaf_head *head;
196 *head = (typeof(*head)) {
197 .ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC),
199 * Leaf contains an entry with the smallest possible key
200 * (created by zeroing).
202 .ill_count = cpu_to_le16(1),
206 static int lvar_root_limit(int blocksize, int size)
208 return root_limit(sizeof(struct lvar_root), blocksize, size);
211 static void lvar_root(void *buf,
212 int blocksize, int keysize, int ptrsize, int recsize)
214 struct lvar_root *root;
215 struct dx_countlimit *limit;
219 isize = sizeof(lvar_hash_t) + ptrsize;
221 *root = (typeof(*root)) {
222 .vr_magic = cpu_to_le32(IAM_LVAR_ROOT_MAGIC),
223 .vr_recsize = cpu_to_le16(recsize),
224 .vr_ptrsize = cpu_to_le16(ptrsize),
225 .vr_indirect_levels = 0
228 limit = (void *)(root + 1);
229 *limit = (typeof(*limit)) {
231 * limit itself + one pointer to the leaf.
233 .count = cpu_to_le16(2),
234 .limit = lvar_root_limit(blocksize, keysize + ptrsize)
244 * Entry format is <key> followed by <ptr>. In the minimal tree
245 * consisting of a root and single node, <key> is a minimal possible
248 * XXX: this key is hard-coded to be a sequence of 0's.
250 entry += sizeof(lvar_hash_t);
251 /* now @entry points to <ptr> */
253 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
255 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
258 static int lvar_esize(int namelen, int recsize)
260 return (offsetof(struct lvar_leaf_entry, vle_key) +
261 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
264 static void lvar_leaf(void *buf,
265 int blocksize, int keysize, int ptrsize, int recsize)
267 struct lvar_leaf_header *head;
272 *head = (typeof(*head)) {
273 .vlh_magic = cpu_to_le16(IAM_LVAR_LEAF_MAGIC),
274 .vlh_used = cpu_to_le16(sizeof *head + lvar_esize(0, recsize))
276 rec = (void *)(head + 1);
277 rec[offsetof(struct lvar_leaf_entry, vle_key)] = recsize;
287 struct iam_uapi_op iui_op;
292 IAM_IOC_INIT = _IOW('i', 1, struct iam_uapi_info),
293 IAM_IOC_GETINFO = _IOR('i', 2, struct iam_uapi_info),
294 IAM_IOC_INSERT = _IOR('i', 3, struct iam_uapi_op),
295 IAM_IOC_LOOKUP = _IOWR('i', 4, struct iam_uapi_op),
296 IAM_IOC_DELETE = _IOR('i', 5, struct iam_uapi_op),
297 IAM_IOC_IT_START = _IOR('i', 6, struct iam_uapi_it),
298 IAM_IOC_IT_NEXT = _IOW('i', 7, struct iam_uapi_it),
299 IAM_IOC_IT_STOP = _IOR('i', 8, struct iam_uapi_it),
300 IAM_IOC_POLYMORPH = _IOR('i', 9, unsigned long)
303 static unsigned char hex2dec(unsigned char hex)
305 if (('0' <= hex) && (hex <= '9'))
307 else if (('a' <= hex) && (hex <= 'f'))
308 return hex - 'a' + 10;
309 else if (('A' <= hex) && (hex <= 'F'))
310 return hex - 'A' + 10;
315 static unsigned char *packdigit(unsigned char *number)
320 area = calloc(strlen((char *)number) / 2 + 2, sizeof(char));
322 for (scan = area; *number; number += 2, scan++)
323 *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
328 static char *iam_convert(int size, int need_convert, char *source)
336 ptr = calloc(size + 1, sizeof(char));
341 opt = packdigit((unsigned char*)source);
346 memcpy(ptr, opt, size + 1);
350 strlcpy(ptr, source, size + 1);
356 static int iam_doop(int fd, struct iam_uapi_info *ua, int cmd,
357 int key_need_convert, char *key_buf,
358 int *keysize, char *save_key,
359 int rec_need_convert, char *rec_buf,
360 int *recsize, char *save_rec)
365 struct iam_uapi_op op;
367 key = iam_convert(ua->iui_keysize, key_need_convert, key_buf);
371 rec = iam_convert(ua->iui_recsize, rec_need_convert, rec_buf);
379 ret = ioctl(fd, cmd, &op);
381 if ((keysize != NULL) && (*keysize > 0) && (save_key != NULL)) {
382 if (*keysize > ua->iui_keysize)
383 *keysize = ua->iui_keysize;
384 memcpy(save_key, key, *keysize);
386 if ((recsize != NULL) && (*recsize > 0) && (save_rec != NULL)) {
387 if (*recsize > ua->iui_recsize)
388 *recsize = ua->iui_recsize;
389 memcpy(save_rec, rec, *recsize);
399 * Creat an iam file, but do NOT open it.
400 * Return 0 if success, else -1.
402 int iam_creat(char *filename, enum iam_fmt_t fmt,
403 int blocksize, int keysize, int recsize, int ptrsize)
408 if (filename == NULL) {
413 if ((fmt != FMT_LFIX) && (fmt != FMT_LVAR)) {
418 if (blocksize <= 100) {
433 if (ptrsize != 4 && ptrsize != 8) {
438 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
443 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0600);
448 buf = malloc(blocksize);
454 memset(buf, 0, blocksize);
456 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
458 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
460 if (write(fd, buf, blocksize) != blocksize) {
466 memset(buf, 0, blocksize);
468 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
470 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
472 if (write(fd, buf, blocksize) != blocksize) {
484 * Open an iam file, but do NOT creat it if the file doesn't exist.
485 * Please use iam_creat for creating the file before use iam_open.
486 * Return file id (fd) if success, else -1.
488 int iam_open(char *filename, struct iam_uapi_info *ua)
492 if (filename == NULL) {
502 fd = open(filename, O_RDONLY);
507 if (ioctl(fd, IAM_IOC_INIT, ua) != 0) {
512 if (ioctl(fd, IAM_IOC_GETINFO, ua) != 0) {
521 * Close file opened by iam_open.
523 int iam_close(int fd)
529 * Please use iam_open before use this function.
531 int iam_insert(int fd, struct iam_uapi_info *ua,
532 int key_need_convert, char *key_buf,
533 int rec_need_convert, char *rec_buf)
535 return iam_doop(fd, ua, IAM_IOC_INSERT,
536 key_need_convert, key_buf, NULL, NULL,
537 rec_need_convert, rec_buf, NULL, NULL);
541 * Please use iam_open before use this function.
543 int iam_lookup(int fd, struct iam_uapi_info *ua,
544 int key_need_convert, char *key_buf,
545 int *keysize, char *save_key,
546 int rec_need_convert, char *rec_buf,
547 int *recsize, char *save_rec)
549 return iam_doop(fd, ua, IAM_IOC_LOOKUP,
550 key_need_convert, key_buf, keysize, save_key,
551 rec_need_convert, rec_buf, recsize, save_rec);
555 * Please use iam_open before use this function.
557 int iam_delete(int fd, struct iam_uapi_info *ua,
558 int key_need_convert, char *key_buf,
559 int rec_need_convert, char *rec_buf)
561 return iam_doop(fd, ua, IAM_IOC_DELETE,
562 key_need_convert, key_buf, NULL, NULL,
563 rec_need_convert, rec_buf, NULL, NULL);
567 * Please use iam_open before use this function.
569 int iam_it_start(int fd, struct iam_uapi_info *ua,
570 int key_need_convert, char *key_buf,
571 int *keysize, char *save_key,
572 int rec_need_convert, char *rec_buf,
573 int *recsize, char *save_rec)
575 return iam_doop(fd, ua, IAM_IOC_IT_START,
576 key_need_convert, key_buf, keysize, save_key,
577 rec_need_convert, rec_buf, recsize, save_rec);
581 * Please use iam_open before use this function.
583 int iam_it_next(int fd, struct iam_uapi_info *ua,
584 int key_need_convert, char *key_buf,
585 int *keysize, char *save_key,
586 int rec_need_convert, char *rec_buf,
587 int *recsize, char *save_rec)
589 return iam_doop(fd, ua, IAM_IOC_IT_NEXT,
590 key_need_convert, key_buf, keysize, save_key,
591 rec_need_convert, rec_buf, recsize, save_rec);
595 * Please use iam_open before use this function.
597 int iam_it_stop(int fd, struct iam_uapi_info *ua,
598 int key_need_convert, char *key_buf,
599 int rec_need_convert, char *rec_buf)
601 return iam_doop(fd, ua, IAM_IOC_IT_STOP,
602 key_need_convert, key_buf, NULL, NULL,
603 rec_need_convert, rec_buf, NULL, NULL);
607 * Change iam file mode.
609 int iam_polymorph(char *filename, unsigned long mode)
614 if (filename == NULL) {
619 fd = open(filename, O_RDONLY);
624 ret = ioctl(fd, IAM_IOC_POLYMORPH, mode);