1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * iam user level library
7 * Copyright (c) 2006 Cluster File Systems, Inc.
8 * Author: Wang Di <wangdi@clusterfs.com>
9 * Author: Nikita Danilov <nikita@clusterfs.com>
10 * Author: Fan Yong <fanyong@clusterfs.com>
12 * This file is part of the Lustre file system, http://www.lustre.org
13 * Lustre is a trademark of Cluster File Systems, Inc.
15 * You may have signed or agreed to another license before downloading
16 * this software. If so, you are bound by the terms and conditions
17 * of that agreement, and the following does not apply to you. See the
18 * LICENSE file included with this distribution for more information.
20 * If you did not agree to a different license, then this copy of Lustre
21 * is open source software; you can redistribute it and/or modify it
22 * under the terms of version 2 of the GNU General Public License as
23 * published by the Free Software Foundation.
25 * In either case, Lustre is distributed in the hope that it will be
26 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
27 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * license text for more details.
39 #include <sys/types.h>
45 #include <libcfs/libcfs.h>
46 #include <liblustre.h>
47 #include <lustre/libiam.h>
49 typedef __u32 lvar_hash_t;
52 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL,
53 IAM_LVAR_ROOT_MAGIC = 0xb01dface
56 struct iam_lfix_root {
58 u_int16_t ilr_keysize;
59 u_int16_t ilr_recsize;
60 u_int16_t ilr_ptrsize;
61 u_int16_t ilr_indirect_levels;
65 IAM_LEAF_HEADER_MAGIC = 0x1976,
66 IAM_LVAR_LEAF_MAGIC = 0x1973
69 struct iam_leaf_head {
74 struct dx_countlimit {
79 struct lvar_leaf_header {
80 u_int16_t vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
81 u_int16_t vlh_used; /* used bytes, including header */
88 u_int8_t vr_indirect_levels;
90 u_int16_t vr_padding1;
93 struct lvar_leaf_entry {
95 u_int16_t vle_keysize;
101 LVAR_ROUND = LVAR_PAD - 1
104 static int root_limit(int rootgap, int blocksize, int size)
109 limit = (blocksize - rootgap) / size;
110 nlimit = blocksize / size;
116 static int lfix_root_limit(int blocksize, int size)
118 return root_limit(sizeof(struct iam_lfix_root), blocksize, size);
121 static void lfix_root(void *buf,
122 int blocksize, int keysize, int ptrsize, int recsize)
124 struct iam_lfix_root *root;
125 struct dx_countlimit *limit;
129 *root = (typeof(*root)) {
130 .ilr_magic = cpu_to_le64(IAM_LFIX_ROOT_MAGIC),
131 .ilr_keysize = cpu_to_le16(keysize),
132 .ilr_recsize = cpu_to_le16(recsize),
133 .ilr_ptrsize = cpu_to_le16(ptrsize),
134 .ilr_indirect_levels = 0
137 limit = (void *)(root + 1);
138 *limit = (typeof(*limit)){
140 * limit itself + one pointer to the leaf.
142 .count = cpu_to_le16(2),
143 .limit = lfix_root_limit(blocksize, keysize + ptrsize)
150 entry += keysize + ptrsize;
153 * Entry format is <key> followed by <ptr>. In the minimal tree
154 * consisting of a root and single node, <key> is a minimal possible
157 * XXX: this key is hard-coded to be a sequence of 0's.
160 /* now @entry points to <ptr> */
162 *(u_int32_t *)entry = cpu_to_le32(1);
164 *(u_int64_t *)entry = cpu_to_le64(1);
167 static void lfix_leaf(void *buf,
168 int blocksize, int keysize, int ptrsize, int recsize)
170 struct iam_leaf_head *head;
174 *head = (typeof(*head)) {
175 .ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC),
177 * Leaf contains an entry with the smallest possible key
178 * (created by zeroing).
180 .ill_count = cpu_to_le16(1),
184 static int lvar_root_limit(int blocksize, int size)
186 return root_limit(sizeof(struct lvar_root), blocksize, size);
189 static void lvar_root(void *buf,
190 int blocksize, int keysize, int ptrsize, int recsize)
192 struct lvar_root *root;
193 struct dx_countlimit *limit;
197 isize = sizeof(lvar_hash_t) + ptrsize;
199 *root = (typeof(*root)) {
200 .vr_magic = cpu_to_le32(IAM_LVAR_ROOT_MAGIC),
201 .vr_recsize = cpu_to_le16(recsize),
202 .vr_ptrsize = cpu_to_le16(ptrsize),
203 .vr_indirect_levels = 0
206 limit = (void *)(root + 1);
207 *limit = (typeof(*limit)) {
209 * limit itself + one pointer to the leaf.
211 .count = cpu_to_le16(2),
212 .limit = lvar_root_limit(blocksize, keysize + ptrsize)
222 * Entry format is <key> followed by <ptr>. In the minimal tree
223 * consisting of a root and single node, <key> is a minimal possible
226 * XXX: this key is hard-coded to be a sequence of 0's.
228 entry += sizeof(lvar_hash_t);
229 /* now @entry points to <ptr> */
231 *(u_int32_t *)entry = cpu_to_le32(1);
233 *(u_int64_t *)entry = cpu_to_le64(1);
236 static int lvar_esize(int namelen, int recsize)
238 return (offsetof(struct lvar_leaf_entry, vle_key) +
239 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
242 static void lvar_leaf(void *buf,
243 int blocksize, int keysize, int ptrsize, int recsize)
245 struct lvar_leaf_header *head;
250 *head = (typeof(*head)) {
251 .vlh_magic = cpu_to_le16(IAM_LVAR_LEAF_MAGIC),
252 .vlh_used = cpu_to_le16(sizeof *head + lvar_esize(0, recsize))
254 rec = (void *)(head + 1);
255 rec[offsetof(struct lvar_leaf_entry, vle_key)] = recsize;
265 struct iam_uapi_op iui_op;
270 IAM_IOC_INIT = _IOW('i', 1, struct iam_uapi_info),
271 IAM_IOC_GETINFO = _IOR('i', 2, struct iam_uapi_info),
272 IAM_IOC_INSERT = _IOR('i', 3, struct iam_uapi_op),
273 IAM_IOC_LOOKUP = _IOWR('i', 4, struct iam_uapi_op),
274 IAM_IOC_DELETE = _IOR('i', 5, struct iam_uapi_op),
275 IAM_IOC_IT_START = _IOR('i', 6, struct iam_uapi_it),
276 IAM_IOC_IT_NEXT = _IOW('i', 7, struct iam_uapi_it),
277 IAM_IOC_IT_STOP = _IOR('i', 8, struct iam_uapi_it),
278 IAM_IOC_POLYMORPH = _IOR('i', 9, unsigned long)
281 static unsigned char hex2dec(unsigned char hex)
283 if (('0' <= hex) && (hex <= '9'))
285 else if (('a' <= hex) && (hex <= 'f'))
286 return hex - 'a' + 10;
287 else if (('A' <= hex) && (hex <= 'F'))
288 return hex - 'A' + 10;
293 static unsigned char *packdigit(unsigned char *number)
298 area = calloc(strlen(number) / 2 + 2, sizeof(char));
300 for (scan = area; *number; number += 2, scan++)
301 *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
306 static char *iam_convert(int size, int need_convert, char *source)
314 ptr = calloc(size + 1, sizeof(char));
319 opt = packdigit(source);
324 memcpy(ptr, opt, size + 1);
328 strncpy(ptr, source, size + 1);
334 static int iam_doop(int fd, struct iam_uapi_info *ua, int cmd,
335 int key_need_convert, char *key_buf,
336 int *keysize, char *save_key,
337 int rec_need_convert, char *rec_buf,
338 int *recsize, char *save_rec)
343 struct iam_uapi_op op;
345 key = iam_convert(ua->iui_keysize, key_need_convert, key_buf);
349 rec = iam_convert(ua->iui_recsize, rec_need_convert, rec_buf);
357 ret = ioctl(fd, cmd, &op);
359 if ((keysize != NULL) && (*keysize > 0) && (save_key != NULL)) {
360 if (*keysize > ua->iui_keysize)
361 *keysize = ua->iui_keysize;
362 memcpy(save_key, key, *keysize);
364 if ((recsize != NULL) && (*recsize > 0) && (save_rec != NULL)) {
365 if (*recsize > ua->iui_recsize)
366 *recsize = ua->iui_recsize;
367 memcpy(save_rec, rec, *recsize);
377 * Creat an iam file, but do NOT open it.
378 * Return 0 if success, else -1.
380 int iam_creat(char *filename, enum iam_fmt_t fmt,
381 int blocksize, int keysize, int recsize, int ptrsize)
386 if (filename == NULL) {
391 if ((fmt != FMT_LFIX) && (fmt != FMT_LVAR)) {
396 if (blocksize <= 100) {
411 if (ptrsize != 4 && ptrsize != 8) {
416 if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
421 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0600);
426 buf = malloc(blocksize);
432 memset(buf, 0, blocksize);
434 lfix_root(buf, blocksize, keysize, ptrsize, recsize);
436 lvar_root(buf, blocksize, keysize, ptrsize, recsize);
438 if (write(fd, buf, blocksize) != blocksize) {
444 memset(buf, 0, blocksize);
446 lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
448 lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
450 if (write(fd, buf, blocksize) != blocksize) {
462 * Open an iam file, but do NOT creat it if the file doesn't exist.
463 * Please use iam_creat for creating the file before use iam_open.
464 * Return file id (fd) if success, else -1.
466 int iam_open(char *filename, struct iam_uapi_info *ua)
470 if (filename == NULL) {
480 fd = open(filename, O_RDONLY);
485 if (ioctl(fd, IAM_IOC_INIT, ua) != 0) {
490 if (ioctl(fd, IAM_IOC_GETINFO, ua) != 0) {
499 * Close file opened by iam_open.
501 int iam_close(int fd)
507 * Please use iam_open before use this function.
509 int iam_insert(int fd, struct iam_uapi_info *ua,
510 int key_need_convert, char *key_buf,
511 int rec_need_convert, char *rec_buf)
513 return iam_doop(fd, ua, IAM_IOC_INSERT,
514 key_need_convert, key_buf, NULL, NULL,
515 rec_need_convert, rec_buf, NULL, NULL);
519 * Please use iam_open before use this function.
521 int iam_lookup(int fd, struct iam_uapi_info *ua,
522 int key_need_convert, char *key_buf,
523 int *keysize, char *save_key,
524 int rec_need_convert, char *rec_buf,
525 int *recsize, char *save_rec)
527 return iam_doop(fd, ua, IAM_IOC_LOOKUP,
528 key_need_convert, key_buf, keysize, save_key,
529 rec_need_convert, rec_buf, recsize, save_rec);
533 * Please use iam_open before use this function.
535 int iam_delete(int fd, struct iam_uapi_info *ua,
536 int key_need_convert, char *key_buf,
537 int rec_need_convert, char *rec_buf)
539 return iam_doop(fd, ua, IAM_IOC_DELETE,
540 key_need_convert, key_buf, NULL, NULL,
541 rec_need_convert, rec_buf, NULL, NULL);
545 * Please use iam_open before use this function.
547 int iam_it_start(int fd, struct iam_uapi_info *ua,
548 int key_need_convert, char *key_buf,
549 int *keysize, char *save_key,
550 int rec_need_convert, char *rec_buf,
551 int *recsize, char *save_rec)
553 return iam_doop(fd, ua, IAM_IOC_IT_START,
554 key_need_convert, key_buf, keysize, save_key,
555 rec_need_convert, rec_buf, recsize, save_rec);
559 * Please use iam_open before use this function.
561 int iam_it_next(int fd, struct iam_uapi_info *ua,
562 int key_need_convert, char *key_buf,
563 int *keysize, char *save_key,
564 int rec_need_convert, char *rec_buf,
565 int *recsize, char *save_rec)
567 return iam_doop(fd, ua, IAM_IOC_IT_NEXT,
568 key_need_convert, key_buf, keysize, save_key,
569 rec_need_convert, rec_buf, recsize, save_rec);
573 * Please use iam_open before use this function.
575 int iam_it_stop(int fd, struct iam_uapi_info *ua,
576 int key_need_convert, char *key_buf,
577 int rec_need_convert, char *rec_buf)
579 return iam_doop(fd, ua, IAM_IOC_IT_STOP,
580 key_need_convert, key_buf, NULL, NULL,
581 rec_need_convert, rec_buf, NULL, NULL);
585 * Change iam file mode.
587 int iam_polymorph(char *filename, unsigned long mode)
592 if (filename == NULL) {
597 fd = open(filename, O_RDONLY);
602 ret = ioctl(fd, IAM_IOC_POLYMORPH, mode);