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/
31 * lustre/utils/libiam.c
33 * iam user level library
35 * Author: Wang Di <wangdi@clusterfs.com>
36 * Author: Nikita Danilov <nikita@clusterfs.com>
37 * Author: Fan Yong <fanyong@clusterfs.com>
47 #include <sys/ioctl.h>
48 #include <sys/types.h>
50 #include <libcfs/util/string.h>
51 #include <lustre/libiam.h>
53 typedef __u32 lvar_hash_t;
56 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
57 IAM_LVAR_ROOT_MAGIC = 0xb01dface
60 struct iam_lfix_root {
62 u_int16_t ilr_keysize;
63 u_int16_t ilr_recsize;
64 u_int16_t ilr_ptrsize;
65 u_int16_t ilr_indirect_levels;
69 IAM_LEAF_HEADER_MAGIC = 0x1976,
70 IAM_LVAR_LEAF_MAGIC = 0x1973
73 struct iam_leaf_head {
78 struct dx_countlimit {
83 struct lvar_leaf_header {
84 u_int16_t vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
85 u_int16_t vlh_used; /* used bytes, including header */
92 u_int8_t vr_indirect_levels;
94 u_int16_t vr_padding1;
97 struct lvar_leaf_entry {
99 u_int16_t vle_keysize;
105 LVAR_ROUND = LVAR_PAD - 1
109 * Stores \a val at \a dst, where the latter is possibly unaligned. Uses
110 * memcpy(). This macro is needed to avoid dependency of user level tools on
111 * the kernel headers.
113 #define STORE_UNALIGNED(val, dst) \
115 typeof(*(dst)) __val = (val); \
117 memcpy(dst, &__val, sizeof *(dst)); \
120 static int root_limit(int rootgap, int blocksize, int size)
125 limit = (blocksize - rootgap) / size;
126 nlimit = blocksize / size;
132 static int lfix_root_limit(int blocksize, int size)
134 return root_limit(sizeof(struct iam_lfix_root), blocksize, size);
137 static void lfix_root(void *buf,
138 int blocksize, int keysize, int ptrsize, int recsize)
140 struct iam_lfix_root *root;
141 struct dx_countlimit *limit;
145 *root = (typeof(*root)) {
146 .ilr_magic = htole64(IAM_LFIX_ROOT_MAGIC),
147 .ilr_keysize = htole16(keysize),
148 .ilr_recsize = htole16(recsize),
149 .ilr_ptrsize = htole16(ptrsize),
150 .ilr_indirect_levels = 0
153 limit = (void *)(root + 1);
154 *limit = (typeof(*limit)){
156 * limit itself + one pointer to the leaf.
159 .limit = lfix_root_limit(blocksize, keysize + ptrsize)
166 entry += keysize + ptrsize;
169 * Entry format is <key> followed by <ptr>. In the minimal tree
170 * consisting of a root and single node, <key> is a minimal possible
173 * XXX: this key is hard-coded to be a sequence of 0's.
176 /* now @entry points to <ptr> */
178 STORE_UNALIGNED(htole32(1), (u_int32_t *)entry);
180 STORE_UNALIGNED(htole64(1), (u_int64_t *)entry);
183 static void lfix_leaf(void *buf,
184 int blocksize, int keysize, int ptrsize, int recsize)
186 struct iam_leaf_head *head;
190 *head = (typeof(*head)) {
191 .ill_magic = htole16(IAM_LEAF_HEADER_MAGIC),
193 * Leaf contains an entry with the smallest possible key
194 * (created by zeroing).
196 .ill_count = htole16(1),
200 static int lvar_root_limit(int blocksize, int size)
202 return root_limit(sizeof(struct lvar_root), blocksize, size);
205 static void lvar_root(void *buf,
206 int blocksize, int keysize, int ptrsize, int recsize)
208 struct lvar_root *root;
209 struct dx_countlimit *limit;
213 isize = sizeof(lvar_hash_t) + ptrsize;
215 *root = (typeof(*root)) {
216 .vr_magic = htole32(IAM_LVAR_ROOT_MAGIC),
217 .vr_recsize = htole16(recsize),
218 .vr_ptrsize = htole16(ptrsize),
219 .vr_indirect_levels = 0
222 limit = (void *)(root + 1);
223 *limit = (typeof(*limit)) {
225 * limit itself + one pointer to the leaf.
228 .limit = lvar_root_limit(blocksize, keysize + ptrsize)
238 * Entry format is <key> followed by <ptr>. In the minimal tree
239 * consisting of a root and single node, <key> is a minimal possible
242 * XXX: this key is hard-coded to be a sequence of 0's.
244 entry += sizeof(lvar_hash_t);
245 /* now @entry points to <ptr> */
247 STORE_UNALIGNED(htole32(1), (u_int32_t *)entry);
249 STORE_UNALIGNED(htole64(1), (u_int64_t *)entry);
252 static int lvar_esize(int namelen, int recsize)
254 return (offsetof(struct lvar_leaf_entry, vle_key) +
255 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
258 static void lvar_leaf(void *buf,
259 int blocksize, int keysize, int ptrsize, int recsize)
261 struct lvar_leaf_header *head;
266 *head = (typeof(*head)) {
267 .vlh_magic = htole16(IAM_LVAR_LEAF_MAGIC),
268 .vlh_used = htole16(sizeof(*head) + lvar_esize(0, recsize))
270 rec = (void *)(head + 1);
271 rec[offsetof(struct lvar_leaf_entry, vle_key)] = recsize;
280 struct iam_uapi_op iui_op;
285 IAM_IOC_INIT = _IOW('i', 1, struct iam_uapi_info),
286 IAM_IOC_GETINFO = _IOR('i', 2, struct iam_uapi_info),
287 IAM_IOC_INSERT = _IOR('i', 3, struct iam_uapi_op),
288 IAM_IOC_LOOKUP = _IOWR('i', 4, struct iam_uapi_op),
289 IAM_IOC_DELETE = _IOR('i', 5, struct iam_uapi_op),
290 IAM_IOC_IT_START = _IOR('i', 6, struct iam_uapi_it),
291 IAM_IOC_IT_NEXT = _IOW('i', 7, struct iam_uapi_it),
292 IAM_IOC_IT_STOP = _IOR('i', 8, struct iam_uapi_it),
293 IAM_IOC_POLYMORPH = _IOR('i', 9, unsigned long)
296 static unsigned char hex2dec(unsigned char hex)
298 if (('0' <= hex) && (hex <= '9'))
300 else if (('a' <= hex) && (hex <= 'f'))
301 return hex - 'a' + 10;
302 else if (('A' <= hex) && (hex <= 'F'))
303 return hex - 'A' + 10;
307 static unsigned char *packdigit(unsigned char *number)
312 area = calloc(strlen((char *)number) / 2 + 2, sizeof(char));
314 for (scan = area; *number; number += 2, scan++)
315 *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
320 static char *iam_convert(int size, int need_convert, char *source)
329 ptr = calloc(size + 1, sizeof(char));
333 opt = packdigit((unsigned char *)source);
338 memcpy(ptr, opt, size + 1);
341 ptr = strdup(source);
347 static int iam_doop(int fd, struct iam_uapi_info *ua, int cmd,
348 int key_need_convert, char *key_buf,
349 int *keysize, char *save_key,
350 int rec_need_convert, char *rec_buf,
351 int *recsize, char *save_rec)
356 struct iam_uapi_op op;
358 key = iam_convert(ua->iui_keysize, key_need_convert, key_buf);
362 rec = iam_convert(ua->iui_recsize, rec_need_convert, rec_buf);
370 ret = ioctl(fd, cmd, &op);
372 if ((keysize) && (*keysize > 0) && (save_key)) {
373 if (*keysize > ua->iui_keysize)
374 *keysize = ua->iui_keysize;
375 memcpy(save_key, key, *keysize);
377 if ((recsize) && (*recsize > 0) && (save_rec)) {
378 if (*recsize > ua->iui_recsize)
379 *recsize = ua->iui_recsize;
380 memcpy(save_rec, rec, *recsize);
389 * Creat an iam file, but do NOT open it.
390 * Return 0 if success, else -1.
392 int iam_creat(char *filename, enum iam_fmt_t fmt,
393 int blocksize, int keysize, int recsize, int ptrsize)
403 if ((fmt != FMT_LFIX) && (fmt != FMT_LVAR)) {
408 if (blocksize <= 100) {
423 if (ptrsize != 4 && ptrsize != 8) {
428 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
433 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0600);
437 buf = malloc(blocksize);
443 memset(buf, 0, blocksize);
445 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
447 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
449 if (write(fd, buf, blocksize) != blocksize) {
455 memset(buf, 0, blocksize);
457 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
459 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
461 if (write(fd, buf, blocksize) != blocksize) {
473 * Open an iam file, but do NOT creat it if the file doesn't exist.
474 * Please use iam_creat for creating the file before use iam_open.
475 * Return file id (fd) if success, else -1.
477 int iam_open(char *filename, struct iam_uapi_info *ua)
491 fd = open(filename, O_RDONLY);
495 if (ioctl(fd, IAM_IOC_INIT, ua) != 0) {
500 if (ioctl(fd, IAM_IOC_GETINFO, ua) != 0) {
509 * Close file opened by iam_open.
511 int iam_close(int fd)
517 * Please use iam_open before use this function.
519 int iam_insert(int fd, struct iam_uapi_info *ua,
520 int key_need_convert, char *key_buf,
521 int rec_need_convert, char *rec_buf)
523 return iam_doop(fd, ua, IAM_IOC_INSERT,
524 key_need_convert, key_buf, NULL, NULL,
525 rec_need_convert, rec_buf, NULL, NULL);
529 * Please use iam_open before use this function.
531 int iam_lookup(int fd, struct iam_uapi_info *ua,
532 int key_need_convert, char *key_buf,
533 int *keysize, char *save_key,
534 int rec_need_convert, char *rec_buf,
535 int *recsize, char *save_rec)
537 return iam_doop(fd, ua, IAM_IOC_LOOKUP,
538 key_need_convert, key_buf, keysize, save_key,
539 rec_need_convert, rec_buf, recsize, save_rec);
543 * Please use iam_open before use this function.
545 int iam_delete(int fd, struct iam_uapi_info *ua,
546 int key_need_convert, char *key_buf,
547 int rec_need_convert, char *rec_buf)
549 return iam_doop(fd, ua, IAM_IOC_DELETE,
550 key_need_convert, key_buf, NULL, NULL,
551 rec_need_convert, rec_buf, NULL, NULL);
555 * Please use iam_open before use this function.
557 int iam_it_start(int fd, struct iam_uapi_info *ua,
558 int key_need_convert, char *key_buf,
559 int *keysize, char *save_key,
560 int rec_need_convert, char *rec_buf,
561 int *recsize, char *save_rec)
563 return iam_doop(fd, ua, IAM_IOC_IT_START,
564 key_need_convert, key_buf, keysize, save_key,
565 rec_need_convert, rec_buf, recsize, save_rec);
569 * Please use iam_open before use this function.
571 int iam_it_next(int fd, struct iam_uapi_info *ua,
572 int key_need_convert, char *key_buf,
573 int *keysize, char *save_key,
574 int rec_need_convert, char *rec_buf,
575 int *recsize, char *save_rec)
577 return iam_doop(fd, ua, IAM_IOC_IT_NEXT,
578 key_need_convert, key_buf, keysize, save_key,
579 rec_need_convert, rec_buf, recsize, save_rec);
583 * Please use iam_open before use this function.
585 int iam_it_stop(int fd, struct iam_uapi_info *ua,
586 int key_need_convert, char *key_buf,
587 int rec_need_convert, char *rec_buf)
589 return iam_doop(fd, ua, IAM_IOC_IT_STOP,
590 key_need_convert, key_buf, NULL, NULL,
591 rec_need_convert, rec_buf, NULL, NULL);
595 * Change iam file mode.
597 int iam_polymorph(char *filename, unsigned long mode)
607 fd = open(filename, O_RDONLY);
611 ret = ioctl(fd, IAM_IOC_POLYMORPH, mode);