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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2014, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/utils/libiam.c
34 * iam user level library
36 * Author: Wang Di <wangdi@clusterfs.com>
37 * Author: Nikita Danilov <nikita@clusterfs.com>
38 * Author: Fan Yong <fanyong@clusterfs.com>
49 #include <sys/ioctl.h>
50 #include <sys/types.h>
52 #include <libcfs/util/string.h>
53 #include <lustre/libiam.h>
55 typedef __u32 lvar_hash_t;
58 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
59 IAM_LVAR_ROOT_MAGIC = 0xb01dface
62 struct iam_lfix_root {
64 u_int16_t ilr_keysize;
65 u_int16_t ilr_recsize;
66 u_int16_t ilr_ptrsize;
67 u_int16_t ilr_indirect_levels;
71 IAM_LEAF_HEADER_MAGIC = 0x1976,
72 IAM_LVAR_LEAF_MAGIC = 0x1973
75 struct iam_leaf_head {
80 struct dx_countlimit {
85 struct lvar_leaf_header {
86 u_int16_t vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
87 u_int16_t vlh_used; /* used bytes, including header */
94 u_int8_t vr_indirect_levels;
96 u_int16_t vr_padding1;
99 struct lvar_leaf_entry {
101 u_int16_t vle_keysize;
107 LVAR_ROUND = LVAR_PAD - 1
111 * Stores \a val at \a dst, where the latter is possibly unaligned. Uses
112 * memcpy(). This macro is needed to avoid dependency of user level tools on
113 * the kernel headers.
115 #define STORE_UNALIGNED(val, dst) \
117 typeof(*(dst)) __val = (val); \
119 memcpy(dst, &__val, sizeof *(dst)); \
122 static int root_limit(int rootgap, int blocksize, int size)
127 limit = (blocksize - rootgap) / size;
128 nlimit = blocksize / size;
134 static int lfix_root_limit(int blocksize, int size)
136 return root_limit(sizeof(struct iam_lfix_root), blocksize, size);
139 static void lfix_root(void *buf,
140 int blocksize, int keysize, int ptrsize, int recsize)
142 struct iam_lfix_root *root;
143 struct dx_countlimit *limit;
147 *root = (typeof(*root)) {
148 .ilr_magic = htole64(IAM_LFIX_ROOT_MAGIC),
149 .ilr_keysize = htole16(keysize),
150 .ilr_recsize = htole16(recsize),
151 .ilr_ptrsize = htole16(ptrsize),
152 .ilr_indirect_levels = 0
155 limit = (void *)(root + 1);
156 *limit = (typeof(*limit)){
158 * limit itself + one pointer to the leaf.
161 .limit = lfix_root_limit(blocksize, keysize + ptrsize)
168 entry += keysize + ptrsize;
171 * Entry format is <key> followed by <ptr>. In the minimal tree
172 * consisting of a root and single node, <key> is a minimal possible
175 * XXX: this key is hard-coded to be a sequence of 0's.
178 /* now @entry points to <ptr> */
180 STORE_UNALIGNED(htole32(1), (u_int32_t *)entry);
182 STORE_UNALIGNED(htole64(1), (u_int64_t *)entry);
185 static void lfix_leaf(void *buf,
186 int blocksize, int keysize, int ptrsize, int recsize)
188 struct iam_leaf_head *head;
192 *head = (typeof(*head)) {
193 .ill_magic = htole16(IAM_LEAF_HEADER_MAGIC),
195 * Leaf contains an entry with the smallest possible key
196 * (created by zeroing).
198 .ill_count = htole16(1),
202 static int lvar_root_limit(int blocksize, int size)
204 return root_limit(sizeof(struct lvar_root), blocksize, size);
207 static void lvar_root(void *buf,
208 int blocksize, int keysize, int ptrsize, int recsize)
210 struct lvar_root *root;
211 struct dx_countlimit *limit;
215 isize = sizeof(lvar_hash_t) + ptrsize;
217 *root = (typeof(*root)) {
218 .vr_magic = htole32(IAM_LVAR_ROOT_MAGIC),
219 .vr_recsize = htole16(recsize),
220 .vr_ptrsize = htole16(ptrsize),
221 .vr_indirect_levels = 0
224 limit = (void *)(root + 1);
225 *limit = (typeof(*limit)) {
227 * limit itself + one pointer to the leaf.
230 .limit = lvar_root_limit(blocksize, keysize + ptrsize)
240 * Entry format is <key> followed by <ptr>. In the minimal tree
241 * consisting of a root and single node, <key> is a minimal possible
244 * XXX: this key is hard-coded to be a sequence of 0's.
246 entry += sizeof(lvar_hash_t);
247 /* now @entry points to <ptr> */
249 STORE_UNALIGNED(htole32(1), (u_int32_t *)entry);
251 STORE_UNALIGNED(htole64(1), (u_int64_t *)entry);
254 static int lvar_esize(int namelen, int recsize)
256 return (offsetof(struct lvar_leaf_entry, vle_key) +
257 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
260 static void lvar_leaf(void *buf,
261 int blocksize, int keysize, int ptrsize, int recsize)
263 struct lvar_leaf_header *head;
268 *head = (typeof(*head)) {
269 .vlh_magic = htole16(IAM_LVAR_LEAF_MAGIC),
270 .vlh_used = htole16(sizeof *head + lvar_esize(0, recsize))
272 rec = (void *)(head + 1);
273 rec[offsetof(struct lvar_leaf_entry, vle_key)] = recsize;
283 struct iam_uapi_op iui_op;
288 IAM_IOC_INIT = _IOW('i', 1, struct iam_uapi_info),
289 IAM_IOC_GETINFO = _IOR('i', 2, struct iam_uapi_info),
290 IAM_IOC_INSERT = _IOR('i', 3, struct iam_uapi_op),
291 IAM_IOC_LOOKUP = _IOWR('i', 4, struct iam_uapi_op),
292 IAM_IOC_DELETE = _IOR('i', 5, struct iam_uapi_op),
293 IAM_IOC_IT_START = _IOR('i', 6, struct iam_uapi_it),
294 IAM_IOC_IT_NEXT = _IOW('i', 7, struct iam_uapi_it),
295 IAM_IOC_IT_STOP = _IOR('i', 8, struct iam_uapi_it),
296 IAM_IOC_POLYMORPH = _IOR('i', 9, unsigned long)
299 static unsigned char hex2dec(unsigned char hex)
301 if (('0' <= hex) && (hex <= '9'))
303 else if (('a' <= hex) && (hex <= 'f'))
304 return hex - 'a' + 10;
305 else if (('A' <= hex) && (hex <= 'F'))
306 return hex - 'A' + 10;
311 static unsigned char *packdigit(unsigned char *number)
316 area = calloc(strlen((char *)number) / 2 + 2, sizeof(char));
318 for (scan = area; *number; number += 2, scan++)
319 *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
324 static char *iam_convert(int size, int need_convert, char *source)
332 ptr = calloc(size + 1, sizeof(char));
337 opt = packdigit((unsigned char*)source);
342 memcpy(ptr, opt, size + 1);
346 strlcpy(ptr, source, size + 1);
352 static int iam_doop(int fd, struct iam_uapi_info *ua, int cmd,
353 int key_need_convert, char *key_buf,
354 int *keysize, char *save_key,
355 int rec_need_convert, char *rec_buf,
356 int *recsize, char *save_rec)
361 struct iam_uapi_op op;
363 key = iam_convert(ua->iui_keysize, key_need_convert, key_buf);
367 rec = iam_convert(ua->iui_recsize, rec_need_convert, rec_buf);
375 ret = ioctl(fd, cmd, &op);
377 if ((keysize != NULL) && (*keysize > 0) && (save_key != NULL)) {
378 if (*keysize > ua->iui_keysize)
379 *keysize = ua->iui_keysize;
380 memcpy(save_key, key, *keysize);
382 if ((recsize != NULL) && (*recsize > 0) && (save_rec != NULL)) {
383 if (*recsize > ua->iui_recsize)
384 *recsize = ua->iui_recsize;
385 memcpy(save_rec, rec, *recsize);
395 * Creat an iam file, but do NOT open it.
396 * Return 0 if success, else -1.
398 int iam_creat(char *filename, enum iam_fmt_t fmt,
399 int blocksize, int keysize, int recsize, int ptrsize)
404 if (filename == NULL) {
409 if ((fmt != FMT_LFIX) && (fmt != FMT_LVAR)) {
414 if (blocksize <= 100) {
429 if (ptrsize != 4 && ptrsize != 8) {
434 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
439 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0600);
444 buf = malloc(blocksize);
450 memset(buf, 0, blocksize);
452 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
454 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
456 if (write(fd, buf, blocksize) != blocksize) {
462 memset(buf, 0, blocksize);
464 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
466 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
468 if (write(fd, buf, blocksize) != blocksize) {
480 * Open an iam file, but do NOT creat it if the file doesn't exist.
481 * Please use iam_creat for creating the file before use iam_open.
482 * Return file id (fd) if success, else -1.
484 int iam_open(char *filename, struct iam_uapi_info *ua)
488 if (filename == NULL) {
498 fd = open(filename, O_RDONLY);
503 if (ioctl(fd, IAM_IOC_INIT, ua) != 0) {
508 if (ioctl(fd, IAM_IOC_GETINFO, ua) != 0) {
517 * Close file opened by iam_open.
519 int iam_close(int fd)
525 * Please use iam_open before use this function.
527 int iam_insert(int fd, struct iam_uapi_info *ua,
528 int key_need_convert, char *key_buf,
529 int rec_need_convert, char *rec_buf)
531 return iam_doop(fd, ua, IAM_IOC_INSERT,
532 key_need_convert, key_buf, NULL, NULL,
533 rec_need_convert, rec_buf, NULL, NULL);
537 * Please use iam_open before use this function.
539 int iam_lookup(int fd, struct iam_uapi_info *ua,
540 int key_need_convert, char *key_buf,
541 int *keysize, char *save_key,
542 int rec_need_convert, char *rec_buf,
543 int *recsize, char *save_rec)
545 return iam_doop(fd, ua, IAM_IOC_LOOKUP,
546 key_need_convert, key_buf, keysize, save_key,
547 rec_need_convert, rec_buf, recsize, save_rec);
551 * Please use iam_open before use this function.
553 int iam_delete(int fd, struct iam_uapi_info *ua,
554 int key_need_convert, char *key_buf,
555 int rec_need_convert, char *rec_buf)
557 return iam_doop(fd, ua, IAM_IOC_DELETE,
558 key_need_convert, key_buf, NULL, NULL,
559 rec_need_convert, rec_buf, NULL, NULL);
563 * Please use iam_open before use this function.
565 int iam_it_start(int fd, struct iam_uapi_info *ua,
566 int key_need_convert, char *key_buf,
567 int *keysize, char *save_key,
568 int rec_need_convert, char *rec_buf,
569 int *recsize, char *save_rec)
571 return iam_doop(fd, ua, IAM_IOC_IT_START,
572 key_need_convert, key_buf, keysize, save_key,
573 rec_need_convert, rec_buf, recsize, save_rec);
577 * Please use iam_open before use this function.
579 int iam_it_next(int fd, struct iam_uapi_info *ua,
580 int key_need_convert, char *key_buf,
581 int *keysize, char *save_key,
582 int rec_need_convert, char *rec_buf,
583 int *recsize, char *save_rec)
585 return iam_doop(fd, ua, IAM_IOC_IT_NEXT,
586 key_need_convert, key_buf, keysize, save_key,
587 rec_need_convert, rec_buf, recsize, save_rec);
591 * Please use iam_open before use this function.
593 int iam_it_stop(int fd, struct iam_uapi_info *ua,
594 int key_need_convert, char *key_buf,
595 int rec_need_convert, char *rec_buf)
597 return iam_doop(fd, ua, IAM_IOC_IT_STOP,
598 key_need_convert, key_buf, NULL, NULL,
599 rec_need_convert, rec_buf, NULL, NULL);
603 * Change iam file mode.
605 int iam_polymorph(char *filename, unsigned long mode)
610 if (filename == NULL) {
615 fd = open(filename, O_RDONLY);
620 ret = ioctl(fd, IAM_IOC_POLYMORPH, mode);