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;
281 struct iam_uapi_op iui_op;
286 IAM_IOC_INIT = _IOW('i', 1, struct iam_uapi_info),
287 IAM_IOC_GETINFO = _IOR('i', 2, struct iam_uapi_info),
288 IAM_IOC_INSERT = _IOR('i', 3, struct iam_uapi_op),
289 IAM_IOC_LOOKUP = _IOWR('i', 4, struct iam_uapi_op),
290 IAM_IOC_DELETE = _IOR('i', 5, struct iam_uapi_op),
291 IAM_IOC_IT_START = _IOR('i', 6, struct iam_uapi_it),
292 IAM_IOC_IT_NEXT = _IOW('i', 7, struct iam_uapi_it),
293 IAM_IOC_IT_STOP = _IOR('i', 8, struct iam_uapi_it),
294 IAM_IOC_POLYMORPH = _IOR('i', 9, unsigned long)
297 static unsigned char hex2dec(unsigned char hex)
299 if (('0' <= hex) && (hex <= '9'))
301 else if (('a' <= hex) && (hex <= 'f'))
302 return hex - 'a' + 10;
303 else if (('A' <= hex) && (hex <= 'F'))
304 return hex - 'A' + 10;
308 static unsigned char *packdigit(unsigned char *number)
313 area = calloc(strlen((char *)number) / 2 + 2, sizeof(char));
315 for (scan = area; *number; number += 2, scan++)
316 *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
321 static char *iam_convert(int size, int need_convert, char *source)
330 ptr = calloc(size + 1, sizeof(char));
334 opt = packdigit((unsigned char *)source);
339 memcpy(ptr, opt, size + 1);
342 ptr = strdup(source);
348 static int iam_doop(int fd, struct iam_uapi_info *ua, int cmd,
349 int key_need_convert, char *key_buf,
350 int *keysize, char *save_key,
351 int rec_need_convert, char *rec_buf,
352 int *recsize, char *save_rec)
357 struct iam_uapi_op op;
359 key = iam_convert(ua->iui_keysize, key_need_convert, key_buf);
363 rec = iam_convert(ua->iui_recsize, rec_need_convert, rec_buf);
371 ret = ioctl(fd, cmd, &op);
373 if ((keysize) && (*keysize > 0) && (save_key)) {
374 if (*keysize > ua->iui_keysize)
375 *keysize = ua->iui_keysize;
376 memcpy(save_key, key, *keysize);
378 if ((recsize) && (*recsize > 0) && (save_rec)) {
379 if (*recsize > ua->iui_recsize)
380 *recsize = ua->iui_recsize;
381 memcpy(save_rec, rec, *recsize);
390 * Creat an iam file, but do NOT open it.
391 * Return 0 if success, else -1.
393 int iam_creat(char *filename, enum iam_fmt_t fmt,
394 int blocksize, int keysize, int recsize, int ptrsize)
404 if ((fmt != FMT_LFIX) && (fmt != FMT_LVAR)) {
409 if (blocksize <= 100) {
424 if (ptrsize != 4 && ptrsize != 8) {
429 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
434 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0600);
438 buf = malloc(blocksize);
444 memset(buf, 0, blocksize);
446 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
448 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
450 if (write(fd, buf, blocksize) != blocksize) {
456 memset(buf, 0, blocksize);
458 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
460 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
462 if (write(fd, buf, blocksize) != blocksize) {
474 * Open an iam file, but do NOT creat it if the file doesn't exist.
475 * Please use iam_creat for creating the file before use iam_open.
476 * Return file id (fd) if success, else -1.
478 int iam_open(char *filename, struct iam_uapi_info *ua)
492 fd = open(filename, O_RDONLY);
496 if (ioctl(fd, IAM_IOC_INIT, ua) != 0) {
501 if (ioctl(fd, IAM_IOC_GETINFO, ua) != 0) {
510 * Close file opened by iam_open.
512 int iam_close(int fd)
518 * Please use iam_open before use this function.
520 int iam_insert(int fd, struct iam_uapi_info *ua,
521 int key_need_convert, char *key_buf,
522 int rec_need_convert, char *rec_buf)
524 return iam_doop(fd, ua, IAM_IOC_INSERT,
525 key_need_convert, key_buf, NULL, NULL,
526 rec_need_convert, rec_buf, NULL, NULL);
530 * Please use iam_open before use this function.
532 int iam_lookup(int fd, struct iam_uapi_info *ua,
533 int key_need_convert, char *key_buf,
534 int *keysize, char *save_key,
535 int rec_need_convert, char *rec_buf,
536 int *recsize, char *save_rec)
538 return iam_doop(fd, ua, IAM_IOC_LOOKUP,
539 key_need_convert, key_buf, keysize, save_key,
540 rec_need_convert, rec_buf, recsize, save_rec);
544 * Please use iam_open before use this function.
546 int iam_delete(int fd, struct iam_uapi_info *ua,
547 int key_need_convert, char *key_buf,
548 int rec_need_convert, char *rec_buf)
550 return iam_doop(fd, ua, IAM_IOC_DELETE,
551 key_need_convert, key_buf, NULL, NULL,
552 rec_need_convert, rec_buf, NULL, NULL);
556 * Please use iam_open before use this function.
558 int iam_it_start(int fd, struct iam_uapi_info *ua,
559 int key_need_convert, char *key_buf,
560 int *keysize, char *save_key,
561 int rec_need_convert, char *rec_buf,
562 int *recsize, char *save_rec)
564 return iam_doop(fd, ua, IAM_IOC_IT_START,
565 key_need_convert, key_buf, keysize, save_key,
566 rec_need_convert, rec_buf, recsize, save_rec);
570 * Please use iam_open before use this function.
572 int iam_it_next(int fd, struct iam_uapi_info *ua,
573 int key_need_convert, char *key_buf,
574 int *keysize, char *save_key,
575 int rec_need_convert, char *rec_buf,
576 int *recsize, char *save_rec)
578 return iam_doop(fd, ua, IAM_IOC_IT_NEXT,
579 key_need_convert, key_buf, keysize, save_key,
580 rec_need_convert, rec_buf, recsize, save_rec);
584 * Please use iam_open before use this function.
586 int iam_it_stop(int fd, struct iam_uapi_info *ua,
587 int key_need_convert, char *key_buf,
588 int rec_need_convert, char *rec_buf)
590 return iam_doop(fd, ua, IAM_IOC_IT_STOP,
591 key_need_convert, key_buf, NULL, NULL,
592 rec_need_convert, rec_buf, NULL, NULL);
596 * Change iam file mode.
598 int iam_polymorph(char *filename, unsigned long mode)
608 fd = open(filename, O_RDONLY);
612 ret = ioctl(fd, IAM_IOC_POLYMORPH, mode);