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.
27 * This file is part of Lustre, http://www.lustre.org/
28 * Lustre is a trademark of Sun Microsystems, Inc.
30 * lustre/tests/iam_ut.c
35 * Author: Nikita Danilov <nikita@clusterfs.com>
46 #include <sys/types.h>
54 * Maximal format name length.
59 struct iam_uapi_info {
64 char iui_fmt_name[DX_FMT_NAME_LEN];
73 struct iam_uapi_op iui_op;
78 IAM_IOC_INIT = _IOW('i', 1, struct iam_uapi_info),
79 IAM_IOC_GETINFO = _IOR('i', 2, struct iam_uapi_info),
80 IAM_IOC_INSERT = _IOR('i', 3, struct iam_uapi_op),
81 IAM_IOC_LOOKUP = _IOWR('i', 4, struct iam_uapi_op),
82 IAM_IOC_DELETE = _IOR('i', 5, struct iam_uapi_op),
83 IAM_IOC_IT_START = _IOR('i', 6, struct iam_uapi_it),
84 IAM_IOC_IT_NEXT = _IOW('i', 7, struct iam_uapi_it),
85 IAM_IOC_IT_STOP = _IOR('i', 8, struct iam_uapi_it),
87 IAM_IOC_POLYMORPH = _IOR('i', 9, unsigned long)
90 static void usage(void)
92 printf("usage: iam_ut [-v] [-h] file\n");
95 static int doop(int fd, const void *key, const void *rec,
96 int cmd, const char *name)
100 struct iam_uapi_op op = {
104 result = ioctl(fd, cmd, &op);
106 fprintf(stderr, "ioctl(%s): %i/%i (%m)\n", name, result, errno);
110 static int doit(int fd, const void *key, const void *rec,
111 int cmd, const char *name)
115 struct iam_uapi_it it = {
123 assert((void *)&it == (void *)&it.iui_op);
125 result = ioctl(fd, cmd, &it);
127 fprintf(stderr, "ioctl(%s): %i/%i (%m)\n", name, result, errno);
129 result = it.iui_state;
133 static int insert(int fd, const void *key, const void *rec)
135 return doop(fd, key, rec, IAM_IOC_INSERT, "IAM_IOC_INSERT");
138 static int lookup(int fd, const void *key, void *rec)
140 return doop(fd, key, rec, IAM_IOC_LOOKUP, "IAM_IOC_LOOKUP");
143 static int delete(int fd, const void *key, void *rec)
145 return doop(fd, key, rec, IAM_IOC_DELETE, "IAM_IOC_DELETE");
148 static int rec_is_nul_term(int recsize)
150 return recsize == 255;
153 static void print_rec(const unsigned char *rec, int nr)
157 for (i = 0; i < nr; ++i) {
158 printf("%c", rec[i]);
159 if (rec_is_nul_term(nr) && rec[i] == 0)
163 for (i = 0; i < nr; ++i) {
164 printf("%x", rec[i]);
165 if (rec_is_nul_term(nr) && rec[i] == 0)
181 unsigned char hex2dec(unsigned char hex)
183 if ('0' <= hex && hex <= '9') {
185 } else if ('a' <= hex && hex <= 'f') {
186 return hex - 'a' + 10;
187 } else if ('A' <= hex && hex <= 'F') {
188 return hex - 'A' + 10;
190 fprintf(stderr, "Wrong hex digit '%c'\n", hex);
195 unsigned char *packdigit(unsigned char *number)
200 area = calloc(strlen(number) / 2 + 2, sizeof area[0]);
202 for (scan = area; *number; number += 2, scan++)
203 *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
208 int main(int argc, char **argv)
221 void *(*copier)(void *, void *, size_t);
231 struct iam_uapi_info ua;
233 setbuf(stdout, NULL);
234 setbuf(stderr, NULL);
242 opt = getopt(argc, argv, "vilk:K:N:r:R:dsSnP:");
249 key_opt = packdigit(optarg);
259 rec_opt = packdigit(optarg);
288 mode = strtoul(optarg, NULL, 0);
289 rc = ioctl(0, IAM_IOC_POLYMORPH, mode);
291 perror("IAM_IOC_POLYMORPH");
296 fprintf(stderr, "Unable to parse options.");
304 rc = ioctl(0, IAM_IOC_INIT, &ua);
306 fprintf(stderr, "ioctl(IAM_IOC_INIT): %i (%m)\n", rc);
310 rc = ioctl(0, IAM_IOC_GETINFO, &ua);
312 fprintf(stderr, "ioctl(IAM_IOC_GETATTR): %i (%m)\n", rc);
316 keysize = ua.iui_keysize;
317 recsize = ua.iui_recsize;
319 printf("keysize: %i, recsize: %i, ptrsize: %i, "
320 "height: %i, name: %s\n",
321 keysize, recsize, ua.iui_ptrsize,
322 ua.iui_height, ua.iui_fmt_name);
324 key = calloc(keysize + 1, sizeof key[0]);
325 rec = calloc(recsize + 1, sizeof rec[0]);
327 if (key == NULL || rec == NULL) {
328 fprintf(stderr, "cannot allocate memory\n");
333 copier = keynul ? &strncpy : &memcpy;
334 copier(key, key_opt ? : "RIVERRUN", keysize + 1);
339 copier = recnul ? &strncpy : &memcpy;
340 copier(rec, rec_opt ? : "PALEFIRE", recsize + 1);
346 if (op == OP_INSERT) {
347 rc = doop(0, key, rec, IAM_IOC_INSERT, "IAM_IOC_INSERT");
349 } else if (op == OP_DELETE) {
350 rc = doop(0, key, rec, IAM_IOC_DELETE, "IAM_IOC_DELETE");
352 } else if (op == OP_LOOKUP) {
353 rc = doop(0, key, rec, IAM_IOC_LOOKUP, "IAM_IOC_LOOKUP");
355 print_rec(rec, recsize);
357 } else if (op == OP_IT_START) {
358 rc = doop(0, key, rec, IAM_IOC_IT_START, "IAM_IOC_IT_START");
360 print_rec(key, keysize);
361 print_rec(rec, recsize);
364 } else if (op == OP_IT_STOP) {
365 rc = doop(0, key, rec, IAM_IOC_IT_STOP, "IAM_IOC_IT_STOP");
367 } else if (op == OP_IT_NEXT) {
368 rc = doop(0, key, rec, IAM_IOC_IT_NEXT, "IAM_IOC_IT_NEXT");
370 print_rec(key, keysize);
371 print_rec(rec, recsize);
376 rc = insert(0, key, rec);
382 rc = insert(0, "DAEDALUS", "FINNEGAN");
388 rc = insert(0, "DAEDALUS", "FINNEGAN");
389 if (errno != EEXIST) {
391 fprintf(stderr, "Duplicate key not detected!\n");
398 rc = lookup(0, "RIVERRUN", rec);
404 print_rec(rec, recsize);
406 for (i = 0; i < N; ++i) {
407 memset(key, 0, keysize + 1);
408 memset(rec, 0, recsize + 1);
409 snprintf(key, keysize + 1, "y-%x-x", i);
410 snprintf(rec, recsize + 1, "p-%x-q", 1000 - i);
411 rc = insert(0, key, rec);
417 printf("key %#x inserted\n", i);