Whamcloud - gitweb
land b_md onto HEAD:
[fs/lustre-release.git] / lustre / tests / testreq.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <signal.h>
29 #include <unistd.h>
30 #include <sys/ioctl.h>
31
32 #define _GNU_SOURCE
33 #include <getopt.h>
34 #undef _GNU_SOURCE
35
36 #include <linux/lustre_mds.h>
37
38 static void usage(char *argv0, int status)
39 {
40         printf(
41 "Usage: %s [OPTION...]\n\
42 \n\
43 --getattr <directory>\n\
44 --setattr <directory>\n\
45 --readpage <directory>\n\
46 --open <directory>\n\
47 --close <directory handle (returned by open)>\n\
48 --create <new name>\n", argv0);
49
50         exit(status);
51 }
52
53 int main(int argc, char **argv)
54 {
55         int fd = 0;
56         int rc = 0;
57         int c  = 0;
58         long cmd = 0;
59         unsigned long arg;
60         char *short_opts = "h", *name = argv[0];
61         static struct option long_opts[] = {
62 #define OPT_GETATTR -2
63                 {"getattr", no_argument, NULL, OPT_GETATTR},
64 #define OPT_READPAGE -3
65                 {"readpage", no_argument, NULL, OPT_READPAGE},
66 #define OPT_SETATTR -4
67                 {"setattr", no_argument, NULL, OPT_SETATTR},
68 #define OPT_CREATE -5
69                 {"create", no_argument, NULL, OPT_CREATE},
70 #define OPT_OPEN -6
71                 {"open", no_argument, NULL, OPT_OPEN},
72 #define OPT_CLOSE -7
73                 {"close", required_argument, NULL, OPT_CLOSE},
74 #define OPT_HELP 'h'
75                 {"help", no_argument, NULL, OPT_HELP},
76                 {0}
77         };
78
79         do {
80                 c = getopt_long(argc, argv, short_opts, long_opts, NULL);
81
82                 switch (c) {
83                 case OPT_HELP:
84                         usage(argv[0], 0);
85                         break;
86                 case OPT_GETATTR:
87                         cmd = IOC_REQUEST_GETATTR;
88                         name = "getattr";
89                         arg = 2;
90                         break;
91                 case OPT_SETATTR:
92                         cmd = IOC_REQUEST_SETATTR;
93                         name = "setattr";
94                         arg = 2;
95                         break;
96                 case OPT_READPAGE:
97                         cmd = IOC_REQUEST_READPAGE;
98                         name = "readpage";
99                         arg = 2;
100                         break;
101                 case OPT_CREATE:
102                         cmd = IOC_REQUEST_CREATE;
103                         name ="create";
104                         arg = 2;
105                         break;
106                 case OPT_OPEN:
107                         cmd = IOC_REQUEST_OPEN;
108                         name = "open";
109                         arg = 2;
110                         break;
111                 case OPT_CLOSE:
112                         cmd = IOC_REQUEST_CLOSE;
113                         name = "close";
114                         arg = strtoul(optarg, NULL, 0);
115                         break;
116                 case '?':
117                         usage(argv[0], 1);
118                 }
119         } while (c != -1);
120
121         if (cmd == 0)
122                 usage(argv[0], 1);
123
124         fd = open("/dev/request", O_RDONLY);
125         if (fd == -1) {
126                 fprintf(stderr, "error opening /dev/request: %s\n",
127                         strerror(errno));
128                 exit(1);
129         }
130
131         fprintf(stderr, "Executing %s test (arg=%lu)...\n", name, arg);
132         if (cmd == IOC_REQUEST_OPEN) {
133                 rc = ioctl(fd, cmd, &arg);
134                 printf("%lu\n", arg);
135         } else
136                 rc = ioctl(fd, cmd, arg);
137         fprintf(stderr, "result code: %d\n", rc);
138
139         return 0;
140 }