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>
48 #include <sys/ioctl.h>
49 #include <sys/types.h>
51 #include <libcfs/util/string.h>
52 #include <lustre/libiam.h>
54 typedef __u32 lvar_hash_t;
57 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
58 IAM_LVAR_ROOT_MAGIC = 0xb01dface
61 struct iam_lfix_root {
63 u_int16_t ilr_keysize;
64 u_int16_t ilr_recsize;
65 u_int16_t ilr_ptrsize;
66 u_int16_t ilr_indirect_levels;
70 IAM_LEAF_HEADER_MAGIC = 0x1976,
71 IAM_LVAR_LEAF_MAGIC = 0x1973
74 struct iam_leaf_head {
79 struct dx_countlimit {
84 struct lvar_leaf_header {
85 u_int16_t vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
86 u_int16_t vlh_used; /* used bytes, including header */
93 u_int8_t vr_indirect_levels;
95 u_int16_t vr_padding1;
98 struct lvar_leaf_entry {
100 u_int16_t vle_keysize;
106 LVAR_ROUND = LVAR_PAD - 1
110 * Stores \a val at \a dst, where the latter is possibly unaligned. Uses
111 * memcpy(). This macro is needed to avoid dependency of user level tools on
112 * the kernel headers.
114 #define STORE_UNALIGNED(val, dst) \
116 typeof(*(dst)) __val = (val); \
118 memcpy(dst, &__val, sizeof *(dst)); \
121 static int root_limit(int rootgap, int blocksize, int size)
126 limit = (blocksize - rootgap) / size;
127 nlimit = blocksize / size;
133 static int lfix_root_limit(int blocksize, int size)
135 return root_limit(sizeof(struct iam_lfix_root), blocksize, size);
138 static void lfix_root(void *buf,
139 int blocksize, int keysize, int ptrsize, int recsize)
141 struct iam_lfix_root *root;
142 struct dx_countlimit *limit;
146 *root = (typeof(*root)) {
147 .ilr_magic = htole64(IAM_LFIX_ROOT_MAGIC),
148 .ilr_keysize = htole16(keysize),
149 .ilr_recsize = htole16(recsize),
150 .ilr_ptrsize = htole16(ptrsize),
151 .ilr_indirect_levels = 0
154 limit = (void *)(root + 1);
155 *limit = (typeof(*limit)){
157 * limit itself + one pointer to the leaf.
160 .limit = lfix_root_limit(blocksize, keysize + ptrsize)
167 entry += keysize + ptrsize;
170 * Entry format is <key> followed by <ptr>. In the minimal tree
171 * consisting of a root and single node, <key> is a minimal possible
174 * XXX: this key is hard-coded to be a sequence of 0's.
177 /* now @entry points to <ptr> */
179 STORE_UNALIGNED(htole32(1), (u_int32_t *)entry);
181 STORE_UNALIGNED(htole64(1), (u_int64_t *)entry);
184 static void lfix_leaf(void *buf,
185 int blocksize, int keysize, int ptrsize, int recsize)
187 struct iam_leaf_head *head;
191 *head = (typeof(*head)) {
192 .ill_magic = htole16(IAM_LEAF_HEADER_MAGIC),
194 * Leaf contains an entry with the smallest possible key
195 * (created by zeroing).
197 .ill_count = htole16(1),
201 static int lvar_root_limit(int blocksize, int size)
203 return root_limit(sizeof(struct lvar_root), blocksize, size);
206 static void lvar_root(void *buf,
207 int blocksize, int keysize, int ptrsize, int recsize)
209 struct lvar_root *root;
210 struct dx_countlimit *limit;
214 isize = sizeof(lvar_hash_t) + ptrsize;
216 *root = (typeof(*root)) {
217 .vr_magic = htole32(IAM_LVAR_ROOT_MAGIC),
218 .vr_recsize = htole16(recsize),
219 .vr_ptrsize = htole16(ptrsize),
220 .vr_indirect_levels = 0
223 limit = (void *)(root + 1);
224 *limit = (typeof(*limit)) {
226 * limit itself + one pointer to the leaf.
229 .limit = lvar_root_limit(blocksize, keysize + ptrsize)
239 * Entry format is <key> followed by <ptr>. In the minimal tree
240 * consisting of a root and single node, <key> is a minimal possible
243 * XXX: this key is hard-coded to be a sequence of 0's.
245 entry += sizeof(lvar_hash_t);
246 /* now @entry points to <ptr> */
248 STORE_UNALIGNED(htole32(1), (u_int32_t *)entry);
250 STORE_UNALIGNED(htole64(1), (u_int64_t *)entry);
253 static int lvar_esize(int namelen, int recsize)
255 return (offsetof(struct lvar_leaf_entry, vle_key) +
256 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
259 static void lvar_leaf(void *buf,
260 int blocksize, int keysize, int ptrsize, int recsize)
262 struct lvar_leaf_header *head;
267 *head = (typeof(*head)) {
268 .vlh_magic = htole16(IAM_LVAR_LEAF_MAGIC),
269 .vlh_used = htole16(sizeof *head + lvar_esize(0, recsize))
271 rec = (void *)(head + 1);
272 rec[offsetof(struct lvar_leaf_entry, vle_key)] = recsize;
282 struct iam_uapi_op iui_op;
287 IAM_IOC_INIT = _IOW('i', 1, struct iam_uapi_info),
288 IAM_IOC_GETINFO = _IOR('i', 2, struct iam_uapi_info),
289 IAM_IOC_INSERT = _IOR('i', 3, struct iam_uapi_op),
290 IAM_IOC_LOOKUP = _IOWR('i', 4, struct iam_uapi_op),
291 IAM_IOC_DELETE = _IOR('i', 5, struct iam_uapi_op),
292 IAM_IOC_IT_START = _IOR('i', 6, struct iam_uapi_it),
293 IAM_IOC_IT_NEXT = _IOW('i', 7, struct iam_uapi_it),
294 IAM_IOC_IT_STOP = _IOR('i', 8, struct iam_uapi_it),
295 IAM_IOC_POLYMORPH = _IOR('i', 9, unsigned long)
298 static unsigned char hex2dec(unsigned char hex)
300 if (('0' <= hex) && (hex <= '9'))
302 else if (('a' <= hex) && (hex <= 'f'))
303 return hex - 'a' + 10;
304 else if (('A' <= hex) && (hex <= 'F'))
305 return hex - 'A' + 10;
310 static unsigned char *packdigit(unsigned char *number)
315 area = calloc(strlen((char *)number) / 2 + 2, sizeof(char));
317 for (scan = area; *number; number += 2, scan++)
318 *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
323 static char *iam_convert(int size, int need_convert, char *source)
332 ptr = calloc(size + 1, sizeof(char));
336 opt = packdigit((unsigned char*)source);
341 memcpy(ptr, opt, size + 1);
345 ptr = strdup(source);
351 static int iam_doop(int fd, struct iam_uapi_info *ua, int cmd,
352 int key_need_convert, char *key_buf,
353 int *keysize, char *save_key,
354 int rec_need_convert, char *rec_buf,
355 int *recsize, char *save_rec)
360 struct iam_uapi_op op;
362 key = iam_convert(ua->iui_keysize, key_need_convert, key_buf);
366 rec = iam_convert(ua->iui_recsize, rec_need_convert, rec_buf);
374 ret = ioctl(fd, cmd, &op);
376 if ((keysize != NULL) && (*keysize > 0) && (save_key != NULL)) {
377 if (*keysize > ua->iui_keysize)
378 *keysize = ua->iui_keysize;
379 memcpy(save_key, key, *keysize);
381 if ((recsize != NULL) && (*recsize > 0) && (save_rec != NULL)) {
382 if (*recsize > ua->iui_recsize)
383 *recsize = ua->iui_recsize;
384 memcpy(save_rec, rec, *recsize);
394 * Creat an iam file, but do NOT open it.
395 * Return 0 if success, else -1.
397 int iam_creat(char *filename, enum iam_fmt_t fmt,
398 int blocksize, int keysize, int recsize, int ptrsize)
403 if (filename == NULL) {
408 if ((fmt != FMT_LFIX) && (fmt != FMT_LVAR)) {
413 if (blocksize <= 100) {
428 if (ptrsize != 4 && ptrsize != 8) {
433 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
438 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0600);
443 buf = malloc(blocksize);
449 memset(buf, 0, blocksize);
451 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
453 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
455 if (write(fd, buf, blocksize) != blocksize) {
461 memset(buf, 0, blocksize);
463 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
465 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
467 if (write(fd, buf, blocksize) != blocksize) {
479 * Open an iam file, but do NOT creat it if the file doesn't exist.
480 * Please use iam_creat for creating the file before use iam_open.
481 * Return file id (fd) if success, else -1.
483 int iam_open(char *filename, struct iam_uapi_info *ua)
487 if (filename == NULL) {
497 fd = open(filename, O_RDONLY);
502 if (ioctl(fd, IAM_IOC_INIT, ua) != 0) {
507 if (ioctl(fd, IAM_IOC_GETINFO, ua) != 0) {
516 * Close file opened by iam_open.
518 int iam_close(int fd)
524 * Please use iam_open before use this function.
526 int iam_insert(int fd, struct iam_uapi_info *ua,
527 int key_need_convert, char *key_buf,
528 int rec_need_convert, char *rec_buf)
530 return iam_doop(fd, ua, IAM_IOC_INSERT,
531 key_need_convert, key_buf, NULL, NULL,
532 rec_need_convert, rec_buf, NULL, NULL);
536 * Please use iam_open before use this function.
538 int iam_lookup(int fd, struct iam_uapi_info *ua,
539 int key_need_convert, char *key_buf,
540 int *keysize, char *save_key,
541 int rec_need_convert, char *rec_buf,
542 int *recsize, char *save_rec)
544 return iam_doop(fd, ua, IAM_IOC_LOOKUP,
545 key_need_convert, key_buf, keysize, save_key,
546 rec_need_convert, rec_buf, recsize, save_rec);
550 * Please use iam_open before use this function.
552 int iam_delete(int fd, struct iam_uapi_info *ua,
553 int key_need_convert, char *key_buf,
554 int rec_need_convert, char *rec_buf)
556 return iam_doop(fd, ua, IAM_IOC_DELETE,
557 key_need_convert, key_buf, NULL, NULL,
558 rec_need_convert, rec_buf, NULL, NULL);
562 * Please use iam_open before use this function.
564 int iam_it_start(int fd, struct iam_uapi_info *ua,
565 int key_need_convert, char *key_buf,
566 int *keysize, char *save_key,
567 int rec_need_convert, char *rec_buf,
568 int *recsize, char *save_rec)
570 return iam_doop(fd, ua, IAM_IOC_IT_START,
571 key_need_convert, key_buf, keysize, save_key,
572 rec_need_convert, rec_buf, recsize, save_rec);
576 * Please use iam_open before use this function.
578 int iam_it_next(int fd, struct iam_uapi_info *ua,
579 int key_need_convert, char *key_buf,
580 int *keysize, char *save_key,
581 int rec_need_convert, char *rec_buf,
582 int *recsize, char *save_rec)
584 return iam_doop(fd, ua, IAM_IOC_IT_NEXT,
585 key_need_convert, key_buf, keysize, save_key,
586 rec_need_convert, rec_buf, recsize, save_rec);
590 * Please use iam_open before use this function.
592 int iam_it_stop(int fd, struct iam_uapi_info *ua,
593 int key_need_convert, char *key_buf,
594 int rec_need_convert, char *rec_buf)
596 return iam_doop(fd, ua, IAM_IOC_IT_STOP,
597 key_need_convert, key_buf, NULL, NULL,
598 rec_need_convert, rec_buf, NULL, NULL);
602 * Change iam file mode.
604 int iam_polymorph(char *filename, unsigned long mode)
609 if (filename == NULL) {
614 fd = open(filename, O_RDONLY);
619 ret = ioctl(fd, IAM_IOC_POLYMORPH, mode);