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.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/utils/libiam.c
36 * iam user level library
38 * Author: Wang Di <wangdi@clusterfs.com>
39 * Author: Nikita Danilov <nikita@clusterfs.com>
40 * Author: Fan Yong <fanyong@clusterfs.com>
51 #include <sys/types.h>
57 #include <libcfs/libcfs.h>
58 #include <liblustre.h>
59 #include <lustre/libiam.h>
61 typedef __u32 lvar_hash_t;
64 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
65 IAM_LVAR_ROOT_MAGIC = 0xb01dface
68 struct iam_lfix_root {
70 u_int16_t ilr_keysize;
71 u_int16_t ilr_recsize;
72 u_int16_t ilr_ptrsize;
73 u_int16_t ilr_indirect_levels;
77 IAM_LEAF_HEADER_MAGIC = 0x1976,
78 IAM_LVAR_LEAF_MAGIC = 0x1973
81 struct iam_leaf_head {
86 struct dx_countlimit {
91 struct lvar_leaf_header {
92 u_int16_t vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
93 u_int16_t vlh_used; /* used bytes, including header */
100 u_int8_t vr_indirect_levels;
101 u_int8_t vr_padding0;
102 u_int16_t vr_padding1;
105 struct lvar_leaf_entry {
107 u_int16_t vle_keysize;
113 LVAR_ROUND = LVAR_PAD - 1
117 * Stores \a val at \a dst, where the latter is possibly unaligned. Uses
118 * memcpy(). This macro is needed to avoid dependency of user level tools on
119 * the kernel headers.
121 #define STORE_UNALIGNED(val, dst) \
123 typeof(*(dst)) __val = (val); \
125 memcpy(dst, &__val, sizeof *(dst)); \
128 static int root_limit(int rootgap, int blocksize, int size)
133 limit = (blocksize - rootgap) / size;
134 nlimit = blocksize / size;
140 static int lfix_root_limit(int blocksize, int size)
142 return root_limit(sizeof(struct iam_lfix_root), blocksize, size);
145 static void lfix_root(void *buf,
146 int blocksize, int keysize, int ptrsize, int recsize)
148 struct iam_lfix_root *root;
149 struct dx_countlimit *limit;
153 *root = (typeof(*root)) {
154 .ilr_magic = cpu_to_le64(IAM_LFIX_ROOT_MAGIC),
155 .ilr_keysize = cpu_to_le16(keysize),
156 .ilr_recsize = cpu_to_le16(recsize),
157 .ilr_ptrsize = cpu_to_le16(ptrsize),
158 .ilr_indirect_levels = 0
161 limit = (void *)(root + 1);
162 *limit = (typeof(*limit)){
164 * limit itself + one pointer to the leaf.
166 .count = cpu_to_le16(2),
167 .limit = lfix_root_limit(blocksize, keysize + ptrsize)
174 entry += keysize + ptrsize;
177 * Entry format is <key> followed by <ptr>. In the minimal tree
178 * consisting of a root and single node, <key> is a minimal possible
181 * XXX: this key is hard-coded to be a sequence of 0's.
184 /* now @entry points to <ptr> */
186 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
188 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
191 static void lfix_leaf(void *buf,
192 int blocksize, int keysize, int ptrsize, int recsize)
194 struct iam_leaf_head *head;
198 *head = (typeof(*head)) {
199 .ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC),
201 * Leaf contains an entry with the smallest possible key
202 * (created by zeroing).
204 .ill_count = cpu_to_le16(1),
208 static int lvar_root_limit(int blocksize, int size)
210 return root_limit(sizeof(struct lvar_root), blocksize, size);
213 static void lvar_root(void *buf,
214 int blocksize, int keysize, int ptrsize, int recsize)
216 struct lvar_root *root;
217 struct dx_countlimit *limit;
221 isize = sizeof(lvar_hash_t) + ptrsize;
223 *root = (typeof(*root)) {
224 .vr_magic = cpu_to_le32(IAM_LVAR_ROOT_MAGIC),
225 .vr_recsize = cpu_to_le16(recsize),
226 .vr_ptrsize = cpu_to_le16(ptrsize),
227 .vr_indirect_levels = 0
230 limit = (void *)(root + 1);
231 *limit = (typeof(*limit)) {
233 * limit itself + one pointer to the leaf.
235 .count = cpu_to_le16(2),
236 .limit = lvar_root_limit(blocksize, keysize + ptrsize)
246 * Entry format is <key> followed by <ptr>. In the minimal tree
247 * consisting of a root and single node, <key> is a minimal possible
250 * XXX: this key is hard-coded to be a sequence of 0's.
252 entry += sizeof(lvar_hash_t);
253 /* now @entry points to <ptr> */
255 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
257 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
260 static int lvar_esize(int namelen, int recsize)
262 return (offsetof(struct lvar_leaf_entry, vle_key) +
263 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
266 static void lvar_leaf(void *buf,
267 int blocksize, int keysize, int ptrsize, int recsize)
269 struct lvar_leaf_header *head;
274 *head = (typeof(*head)) {
275 .vlh_magic = cpu_to_le16(IAM_LVAR_LEAF_MAGIC),
276 .vlh_used = cpu_to_le16(sizeof *head + lvar_esize(0, recsize))
278 rec = (void *)(head + 1);
279 rec[offsetof(struct lvar_leaf_entry, vle_key)] = recsize;
289 struct iam_uapi_op iui_op;
294 IAM_IOC_INIT = _IOW('i', 1, struct iam_uapi_info),
295 IAM_IOC_GETINFO = _IOR('i', 2, struct iam_uapi_info),
296 IAM_IOC_INSERT = _IOR('i', 3, struct iam_uapi_op),
297 IAM_IOC_LOOKUP = _IOWR('i', 4, struct iam_uapi_op),
298 IAM_IOC_DELETE = _IOR('i', 5, struct iam_uapi_op),
299 IAM_IOC_IT_START = _IOR('i', 6, struct iam_uapi_it),
300 IAM_IOC_IT_NEXT = _IOW('i', 7, struct iam_uapi_it),
301 IAM_IOC_IT_STOP = _IOR('i', 8, struct iam_uapi_it),
302 IAM_IOC_POLYMORPH = _IOR('i', 9, unsigned long)
305 static unsigned char hex2dec(unsigned char hex)
307 if (('0' <= hex) && (hex <= '9'))
309 else if (('a' <= hex) && (hex <= 'f'))
310 return hex - 'a' + 10;
311 else if (('A' <= hex) && (hex <= 'F'))
312 return hex - 'A' + 10;
317 static unsigned char *packdigit(unsigned char *number)
322 area = calloc(strlen((char *)number) / 2 + 2, sizeof(char));
324 for (scan = area; *number; number += 2, scan++)
325 *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
330 static char *iam_convert(int size, int need_convert, char *source)
338 ptr = calloc(size + 1, sizeof(char));
343 opt = packdigit((unsigned char*)source);
348 memcpy(ptr, opt, size + 1);
352 strncpy(ptr, source, size + 1);
358 static int iam_doop(int fd, struct iam_uapi_info *ua, int cmd,
359 int key_need_convert, char *key_buf,
360 int *keysize, char *save_key,
361 int rec_need_convert, char *rec_buf,
362 int *recsize, char *save_rec)
367 struct iam_uapi_op op;
369 key = iam_convert(ua->iui_keysize, key_need_convert, key_buf);
373 rec = iam_convert(ua->iui_recsize, rec_need_convert, rec_buf);
381 ret = ioctl(fd, cmd, &op);
383 if ((keysize != NULL) && (*keysize > 0) && (save_key != NULL)) {
384 if (*keysize > ua->iui_keysize)
385 *keysize = ua->iui_keysize;
386 memcpy(save_key, key, *keysize);
388 if ((recsize != NULL) && (*recsize > 0) && (save_rec != NULL)) {
389 if (*recsize > ua->iui_recsize)
390 *recsize = ua->iui_recsize;
391 memcpy(save_rec, rec, *recsize);
401 * Creat an iam file, but do NOT open it.
402 * Return 0 if success, else -1.
404 int iam_creat(char *filename, enum iam_fmt_t fmt,
405 int blocksize, int keysize, int recsize, int ptrsize)
410 if (filename == NULL) {
415 if ((fmt != FMT_LFIX) && (fmt != FMT_LVAR)) {
420 if (blocksize <= 100) {
435 if (ptrsize != 4 && ptrsize != 8) {
440 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
445 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0600);
450 buf = malloc(blocksize);
456 memset(buf, 0, blocksize);
458 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
460 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
462 if (write(fd, buf, blocksize) != blocksize) {
468 memset(buf, 0, blocksize);
470 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
472 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
474 if (write(fd, buf, blocksize) != blocksize) {
486 * Open an iam file, but do NOT creat it if the file doesn't exist.
487 * Please use iam_creat for creating the file before use iam_open.
488 * Return file id (fd) if success, else -1.
490 int iam_open(char *filename, struct iam_uapi_info *ua)
494 if (filename == NULL) {
504 fd = open(filename, O_RDONLY);
509 if (ioctl(fd, IAM_IOC_INIT, ua) != 0) {
514 if (ioctl(fd, IAM_IOC_GETINFO, ua) != 0) {
523 * Close file opened by iam_open.
525 int iam_close(int fd)
531 * Please use iam_open before use this function.
533 int iam_insert(int fd, struct iam_uapi_info *ua,
534 int key_need_convert, char *key_buf,
535 int rec_need_convert, char *rec_buf)
537 return iam_doop(fd, ua, IAM_IOC_INSERT,
538 key_need_convert, key_buf, NULL, NULL,
539 rec_need_convert, rec_buf, NULL, NULL);
543 * Please use iam_open before use this function.
545 int iam_lookup(int fd, struct iam_uapi_info *ua,
546 int key_need_convert, char *key_buf,
547 int *keysize, char *save_key,
548 int rec_need_convert, char *rec_buf,
549 int *recsize, char *save_rec)
551 return iam_doop(fd, ua, IAM_IOC_LOOKUP,
552 key_need_convert, key_buf, keysize, save_key,
553 rec_need_convert, rec_buf, recsize, save_rec);
557 * Please use iam_open before use this function.
559 int iam_delete(int fd, struct iam_uapi_info *ua,
560 int key_need_convert, char *key_buf,
561 int rec_need_convert, char *rec_buf)
563 return iam_doop(fd, ua, IAM_IOC_DELETE,
564 key_need_convert, key_buf, NULL, NULL,
565 rec_need_convert, rec_buf, NULL, NULL);
569 * Please use iam_open before use this function.
571 int iam_it_start(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_START,
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_next(int fd, struct iam_uapi_info *ua,
586 int key_need_convert, char *key_buf,
587 int *keysize, char *save_key,
588 int rec_need_convert, char *rec_buf,
589 int *recsize, char *save_rec)
591 return iam_doop(fd, ua, IAM_IOC_IT_NEXT,
592 key_need_convert, key_buf, keysize, save_key,
593 rec_need_convert, rec_buf, recsize, save_rec);
597 * Please use iam_open before use this function.
599 int iam_it_stop(int fd, struct iam_uapi_info *ua,
600 int key_need_convert, char *key_buf,
601 int rec_need_convert, char *rec_buf)
603 return iam_doop(fd, ua, IAM_IOC_IT_STOP,
604 key_need_convert, key_buf, NULL, NULL,
605 rec_need_convert, rec_buf, NULL, NULL);
609 * Change iam file mode.
611 int iam_polymorph(char *filename, unsigned long mode)
616 if (filename == NULL) {
621 fd = open(filename, O_RDONLY);
626 ret = ioctl(fd, IAM_IOC_POLYMORPH, mode);