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/tests/iam_ut.c
39 * Author: Nikita Danilov <nikita@clusterfs.com>
50 #include <sys/types.h>
58 * Maximal format name length.
63 struct iam_uapi_info {
68 char iui_fmt_name[DX_FMT_NAME_LEN];
77 struct iam_uapi_op iui_op;
82 IAM_IOC_INIT = _IOW('i', 1, struct iam_uapi_info),
83 IAM_IOC_GETINFO = _IOR('i', 2, struct iam_uapi_info),
84 IAM_IOC_INSERT = _IOR('i', 3, struct iam_uapi_op),
85 IAM_IOC_LOOKUP = _IOWR('i', 4, struct iam_uapi_op),
86 IAM_IOC_DELETE = _IOR('i', 5, struct iam_uapi_op),
87 IAM_IOC_IT_START = _IOR('i', 6, struct iam_uapi_it),
88 IAM_IOC_IT_NEXT = _IOW('i', 7, struct iam_uapi_it),
89 IAM_IOC_IT_STOP = _IOR('i', 8, struct iam_uapi_it),
91 IAM_IOC_POLYMORPH = _IOR('i', 9, unsigned long)
94 static void usage(void)
96 printf("usage: iam_ut [-v] [-h] file\n");
99 static int doop(int fd, const void *key, const void *rec,
100 int cmd, const char *name)
104 struct iam_uapi_op op = {
108 result = ioctl(fd, cmd, &op);
110 fprintf(stderr, "ioctl(%s): %i/%i (%m)\n", name, result, errno);
114 static int doit(int fd, const void *key, const void *rec,
115 int cmd, const char *name)
119 struct iam_uapi_it it = {
127 assert((void *)&it == (void *)&it.iui_op);
129 result = ioctl(fd, cmd, &it);
131 fprintf(stderr, "ioctl(%s): %i/%i (%m)\n", name, result, errno);
133 result = it.iui_state;
137 static int insert(int fd, const void *key, const void *rec)
139 return doop(fd, key, rec, IAM_IOC_INSERT, "IAM_IOC_INSERT");
142 static int lookup(int fd, const void *key, void *rec)
144 return doop(fd, key, rec, IAM_IOC_LOOKUP, "IAM_IOC_LOOKUP");
147 static int delete(int fd, const void *key, void *rec)
149 return doop(fd, key, rec, IAM_IOC_DELETE, "IAM_IOC_DELETE");
152 static int rec_is_nul_term(int recsize)
154 return recsize == 255;
157 static void print_rec(const unsigned char *rec, int nr)
161 for (i = 0; i < nr; ++i) {
162 printf("%c", rec[i]);
163 if (rec_is_nul_term(nr) && rec[i] == 0)
167 for (i = 0; i < nr; ++i) {
168 printf("%x", rec[i]);
169 if (rec_is_nul_term(nr) && rec[i] == 0)
185 unsigned char hex2dec(unsigned char hex)
187 if ('0' <= hex && hex <= '9') {
189 } else if ('a' <= hex && hex <= 'f') {
190 return hex - 'a' + 10;
191 } else if ('A' <= hex && hex <= 'F') {
192 return hex - 'A' + 10;
194 fprintf(stderr, "Wrong hex digit '%c'\n", hex);
199 unsigned char *packdigit(unsigned char *number)
204 area = calloc(strlen(number) / 2 + 2, sizeof area[0]);
206 for (scan = area; *number; number += 2, scan++)
207 *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
212 int main(int argc, char **argv)
225 void *(*copier)(void *, void *, size_t);
235 struct iam_uapi_info ua;
237 setbuf(stdout, NULL);
238 setbuf(stderr, NULL);
246 opt = getopt(argc, argv, "vilk:K:N:r:R:dsSnP:");
253 key_opt = packdigit(optarg);
263 rec_opt = packdigit(optarg);
292 mode = strtoul(optarg, NULL, 0);
293 rc = ioctl(0, IAM_IOC_POLYMORPH, mode);
295 perror("IAM_IOC_POLYMORPH");
300 fprintf(stderr, "Unable to parse options.");
308 rc = ioctl(0, IAM_IOC_INIT, &ua);
310 fprintf(stderr, "ioctl(IAM_IOC_INIT): %i (%m)\n", rc);
314 rc = ioctl(0, IAM_IOC_GETINFO, &ua);
316 fprintf(stderr, "ioctl(IAM_IOC_GETATTR): %i (%m)\n", rc);
320 keysize = ua.iui_keysize;
321 recsize = ua.iui_recsize;
323 printf("keysize: %i, recsize: %i, ptrsize: %i, "
324 "height: %i, name: %s\n",
325 keysize, recsize, ua.iui_ptrsize,
326 ua.iui_height, ua.iui_fmt_name);
328 key = calloc(keysize + 1, sizeof key[0]);
329 rec = calloc(recsize + 1, sizeof rec[0]);
331 if (key == NULL || rec == NULL) {
332 fprintf(stderr, "cannot allocate memory\n");
337 copier = keynul ? &strncpy : &memcpy;
338 copier(key, key_opt ? : "RIVERRUN", keysize + 1);
343 copier = recnul ? &strncpy : &memcpy;
344 copier(rec, rec_opt ? : "PALEFIRE", recsize + 1);
350 if (op == OP_INSERT) {
351 rc = doop(0, key, rec, IAM_IOC_INSERT, "IAM_IOC_INSERT");
353 } else if (op == OP_DELETE) {
354 rc = doop(0, key, rec, IAM_IOC_DELETE, "IAM_IOC_DELETE");
356 } else if (op == OP_LOOKUP) {
357 rc = doop(0, key, rec, IAM_IOC_LOOKUP, "IAM_IOC_LOOKUP");
359 print_rec(rec, recsize);
361 } else if (op == OP_IT_START) {
362 rc = doop(0, key, rec, IAM_IOC_IT_START, "IAM_IOC_IT_START");
364 print_rec(key, keysize);
365 print_rec(rec, recsize);
368 } else if (op == OP_IT_STOP) {
369 rc = doop(0, key, rec, IAM_IOC_IT_STOP, "IAM_IOC_IT_STOP");
371 } else if (op == OP_IT_NEXT) {
372 rc = doop(0, key, rec, IAM_IOC_IT_NEXT, "IAM_IOC_IT_NEXT");
374 print_rec(key, keysize);
375 print_rec(rec, recsize);
380 rc = insert(0, key, rec);
386 rc = insert(0, "DAEDALUS", "FINNEGAN");
392 rc = insert(0, "DAEDALUS", "FINNEGAN");
393 if (errno != EEXIST) {
395 fprintf(stderr, "Duplicate key not detected!\n");
402 rc = lookup(0, "RIVERRUN", rec);
408 print_rec(rec, recsize);
410 for (i = 0; i < N; ++i) {
411 memset(key, 0, keysize + 1);
412 memset(rec, 0, recsize + 1);
413 snprintf(key, keysize + 1, "y-%x-x", i);
414 snprintf(rec, recsize + 1, "p-%x-q", 1000 - i);
415 rc = insert(0, key, rec);
421 printf("key %#x inserted\n", i);