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>
56 #include <libcfs/libcfs.h>
60 * Maximal format name length.
65 struct iam_uapi_info {
70 char iui_fmt_name[DX_FMT_NAME_LEN];
79 struct iam_uapi_op iui_op;
84 IAM_IOC_INIT = _IOW('i', 1, struct iam_uapi_info),
85 IAM_IOC_GETINFO = _IOR('i', 2, struct iam_uapi_info),
86 IAM_IOC_INSERT = _IOR('i', 3, struct iam_uapi_op),
87 IAM_IOC_LOOKUP = _IOWR('i', 4, struct iam_uapi_op),
88 IAM_IOC_DELETE = _IOR('i', 5, struct iam_uapi_op),
89 IAM_IOC_IT_START = _IOR('i', 6, struct iam_uapi_it),
90 IAM_IOC_IT_NEXT = _IOW('i', 7, struct iam_uapi_it),
91 IAM_IOC_IT_STOP = _IOR('i', 8, struct iam_uapi_it),
93 IAM_IOC_POLYMORPH = _IOR('i', 9, unsigned long)
96 static void usage(void)
98 printf("usage: iam_ut [-v] [-h] file\n");
101 static int doop(int fd, const void *key, const void *rec,
102 int cmd, const char *name)
106 struct iam_uapi_op op = {
110 result = ioctl(fd, cmd, &op);
112 fprintf(stderr, "ioctl(%s): %i/%i (%m)\n", name, result, errno);
116 static int doit(int fd, const void *key, const void *rec,
117 int cmd, const char *name)
121 struct iam_uapi_it it = {
129 assert((void *)&it == (void *)&it.iui_op);
131 result = ioctl(fd, cmd, &it);
133 fprintf(stderr, "ioctl(%s): %i/%i (%m)\n", name, result, errno);
135 result = it.iui_state;
139 static int insert(int fd, const void *key, const void *rec)
141 return doop(fd, key, rec, IAM_IOC_INSERT, "IAM_IOC_INSERT");
144 static int lookup(int fd, const void *key, void *rec)
146 return doop(fd, key, rec, IAM_IOC_LOOKUP, "IAM_IOC_LOOKUP");
149 static int delete(int fd, const void *key, void *rec)
151 return doop(fd, key, rec, IAM_IOC_DELETE, "IAM_IOC_DELETE");
154 static int rec_is_nul_term(int recsize)
156 return recsize == 255;
159 static void print_rec(const unsigned char *rec, int nr)
163 for (i = 0; i < nr; ++i) {
164 printf("%c", rec[i]);
165 if (rec_is_nul_term(nr) && rec[i] == 0)
169 for (i = 0; i < nr; ++i) {
170 printf("%x", rec[i]);
171 if (rec_is_nul_term(nr) && rec[i] == 0)
187 unsigned char hex2dec(unsigned char hex)
189 if ('0' <= hex && hex <= '9') {
191 } else if ('a' <= hex && hex <= 'f') {
192 return hex - 'a' + 10;
193 } else if ('A' <= hex && hex <= 'F') {
194 return hex - 'A' + 10;
196 fprintf(stderr, "Wrong hex digit '%c'\n", hex);
201 unsigned char *packdigit(unsigned char *number)
206 area = calloc(strlen(number) / 2 + 2, sizeof area[0]);
208 for (scan = area; *number; number += 2, scan++)
209 *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
214 int main(int argc, char **argv)
227 void *(*copier)(void *, void *, size_t);
237 struct iam_uapi_info ua;
239 setbuf(stdout, NULL);
240 setbuf(stderr, NULL);
248 opt = getopt(argc, argv, "vilk:K:N:r:R:dsSnP:");
255 key_opt = packdigit(optarg);
265 rec_opt = packdigit(optarg);
294 mode = strtoul(optarg, NULL, 0);
295 rc = ioctl(0, IAM_IOC_POLYMORPH, mode);
297 perror("IAM_IOC_POLYMORPH");
302 fprintf(stderr, "Unable to parse options.");
310 rc = ioctl(0, IAM_IOC_INIT, &ua);
312 fprintf(stderr, "ioctl(IAM_IOC_INIT): %i (%m)\n", rc);
316 rc = ioctl(0, IAM_IOC_GETINFO, &ua);
318 fprintf(stderr, "ioctl(IAM_IOC_GETATTR): %i (%m)\n", rc);
322 keysize = ua.iui_keysize;
323 recsize = ua.iui_recsize;
325 printf("keysize: %i, recsize: %i, ptrsize: %i, "
326 "height: %i, name: %s\n",
327 keysize, recsize, ua.iui_ptrsize,
328 ua.iui_height, ua.iui_fmt_name);
330 key = calloc(keysize + 1, sizeof key[0]);
331 rec = calloc(recsize + 1, sizeof rec[0]);
333 if (key == NULL || rec == NULL) {
334 fprintf(stderr, "cannot allocate memory\n");
339 copier = keynul ? &strncpy : &memcpy;
340 copier(key, key_opt ? : "RIVERRUN", keysize + 1);
345 copier = recnul ? &strncpy : &memcpy;
346 copier(rec, rec_opt ? : "PALEFIRE", recsize + 1);
352 if (op == OP_INSERT) {
353 rc = doop(0, key, rec, IAM_IOC_INSERT, "IAM_IOC_INSERT");
355 } else if (op == OP_DELETE) {
356 rc = doop(0, key, rec, IAM_IOC_DELETE, "IAM_IOC_DELETE");
358 } else if (op == OP_LOOKUP) {
359 rc = doop(0, key, rec, IAM_IOC_LOOKUP, "IAM_IOC_LOOKUP");
361 print_rec(rec, recsize);
363 } else if (op == OP_IT_START) {
364 rc = doop(0, key, rec, IAM_IOC_IT_START, "IAM_IOC_IT_START");
366 print_rec(key, keysize);
367 print_rec(rec, recsize);
370 } else if (op == OP_IT_STOP) {
371 rc = doop(0, key, rec, IAM_IOC_IT_STOP, "IAM_IOC_IT_STOP");
373 } else if (op == OP_IT_NEXT) {
374 rc = doop(0, key, rec, IAM_IOC_IT_NEXT, "IAM_IOC_IT_NEXT");
376 print_rec(key, keysize);
377 print_rec(rec, recsize);
382 rc = insert(0, key, rec);
388 rc = insert(0, "DAEDALUS", "FINNEGAN");
394 rc = insert(0, "DAEDALUS", "FINNEGAN");
395 if (errno != EEXIST) {
397 fprintf(stderr, "Duplicate key not detected!\n");
404 rc = lookup(0, "RIVERRUN", rec);
410 print_rec(rec, recsize);
412 for (i = 0; i < N; ++i) {
413 memset(key, 0, keysize + 1);
414 memset(rec, 0, recsize + 1);
415 snprintf(key, keysize + 1, "y-%x-x", i);
416 snprintf(rec, recsize + 1, "p-%x-q", 1000 - i);
417 rc = insert(0, key, rec);
423 printf("key %#x inserted\n", i);