Whamcloud - gitweb
Numerous changes:
[fs/lustre-release.git] / lustre / utils / obdctl.c
1 #include <stdlib.h>
2 #include <sys/ioctl.h>
3 #include <fcntl.h>
4 #include <sys/socket.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <stdio.h>
8 #define printk printf
9 #include <linux/obd_class.h>
10 #include <unistd.h>
11 #include <sys/un.h>
12 #include <sys/time.h>
13 #include <netinet/in.h>
14 #include <errno.h>
15 #include <string.h>
16
17 #define __KERNEL__
18 #include <linux/list.h>
19 #undef __KERNEL__
20
21 #include "parser.h"
22 #include <stdio.h>
23
24 int fd = -1;
25 int connid = -1;
26 char rawbuf[8192];
27 char *buf = rawbuf;
28 int max = 8192;
29
30 #define IOCINIT(data) do { memset(&data, 0, sizeof(data)); data.ioc_version = OBD_IOCTL_VERSION; data.ioc_conn1 = connid; data.ioc_len = sizeof(data); if (fd < 0) { printf("No device open, use device\n"); return 1;}} while (0)
31
32 /*
33     pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L", 
34     $obdo->{id}, 0, 
35     $obdo->{gr}, 0, 
36     $obdo->{atime}, 0, 
37     $obdo->{mtime}, 0 ,
38     $obdo->{ctime}, 0, 
39     $obdo->{size}, 0, 
40     $obdo->{blocks}, 0, 
41     $obdo->{blksize},
42     $obdo->{mode},
43     $obdo->{uid},
44     $obdo->{gid},
45     $obdo->{flags},
46     $obdo->{obdflags},
47     $obdo->{nlink},     
48     $obdo->{generation},        
49     $obdo->{valid},     
50     $obdo->{inline},
51     $obdo->{obdmd},
52     0, 0, # struct list_head 
53     0;  #  struct obd_ops 
54 }
55
56 */
57
58 char * obdo_print(struct obdo *obd)
59 {
60         char buf[1024];
61
62         sprintf(buf, "id: %Ld\ngrp: %Ld\natime: %Ld\nmtime: %Ld\nctime: %Ld\nsize: %Ld\nblocks: %Ld\nblksize: %d\nmode: %o\nuid: %d\ngid: %d\nflags: %x\nobdflags: %x\nnlink: %d,\nvalid %x\n",
63                 obd->o_id,
64                 obd->o_gr,
65                 obd->o_atime,
66                 obd->o_mtime,
67                 obd->o_ctime,
68                 obd->o_size,
69                 obd->o_blocks,
70                 obd->o_blksize,
71                 obd->o_mode,
72                 obd->o_uid,
73                 obd->o_gid,
74                 obd->o_flags,
75                 obd->o_obdflags,
76                 obd->o_nlink,
77                 obd->o_valid);
78         return strdup(buf);
79 }
80
81
82 static int jt_device(int argc, char **argv)
83 {
84         struct obd_ioctl_data data;
85         int rc;
86
87         memset(&data, 0, sizeof(data));
88         if ( argc != 2 ) {
89                 fprintf(stderr, "Usage: %s devno\n", argv[0]);
90                 return 1;
91         }
92
93         data.ioc_dev = atoi(argv[1]);
94
95         if (obd_ioctl_pack(&data, &buf, max)) { 
96                 printf("invalid ioctl\n"); 
97                 return 1;
98         }
99
100         if (fd == -1) 
101                 fd = open("/dev/obd", O_RDWR);
102         if (fd == -1) {
103                 printf("Opening /dev/obd: %s\n", strerror(errno));
104                 return 1;
105         }
106
107         rc = ioctl(fd, OBD_IOC_DEVICE , buf);
108         if (rc < 0) {
109                 printf("Device: %x %s\n", OBD_IOC_DEVICE, strerror(errno));
110                 return 1;
111         }
112
113         return 0;
114 }
115
116 static int jt_connect(int argc, char **argv)
117 {
118         struct obd_ioctl_data data;
119         int rc;
120
121         IOCINIT(data);
122
123         if ( argc != 1 ) {
124                 fprintf(stderr, "Usage: %s\n", argv[0]);
125                 return 1;
126         }
127
128         rc = ioctl(fd, OBD_IOC_CONNECT , &data);
129         if (rc < 0) {
130                 printf("Device: %x %s\n", OBD_IOC_CONNECT, strerror(errno));
131                 return 1;
132         }
133         connid = data.ioc_conn1;
134
135         return 0;
136 }
137
138
139
140 static int jt_disconnect(int argc, char **argv)
141 {
142         struct obd_ioctl_data data;
143         int rc;
144
145         IOCINIT(data);
146
147         if ( argc != 1 ) {
148                 fprintf(stderr, "Usage: %s\n", argv[0]);
149                 return 1;
150         }
151
152         rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
153         if (rc < 0) {
154                 printf("Device: %x %s\n", OBD_IOC_DISCONNECT, strerror(errno));
155                 return 1;
156         }
157         connid = -1;
158
159         return 0;
160 }
161
162
163 static int jt_detach(int argc, char **argv)
164 {
165         struct obd_ioctl_data data;
166         int rc;
167
168         IOCINIT(data);
169
170         if ( argc != 1 ) {
171                 fprintf(stderr, "Usage: %s\n", argv[0]);
172                 return 1;
173         }
174
175         if (obd_ioctl_pack(&data, &buf, max)) { 
176                 printf("invalid ioctl\n"); 
177                 return 1;
178         }
179
180         rc = ioctl(fd, OBD_IOC_DETACH , buf);
181         if (rc < 0) {
182                 printf("Detach: %s\n", strerror(errno));
183                 return 1;
184         }
185         return 0;
186 }
187
188 static int jt_cleanup(int argc, char **argv)
189 {
190         struct obd_ioctl_data data;
191         int rc;
192
193         IOCINIT(data);
194
195         if ( argc != 1 ) {
196                 fprintf(stderr, "Usage: %s\n", argv[0]);
197                 return 1;
198         }
199
200         rc = ioctl(fd, OBD_IOC_CLEANUP , &data);
201         if (rc < 0) {
202                 printf("Detach: %s\n", strerror(errno));
203                 return 1;
204         }
205         return 0;
206 }
207
208 static int jt_attach(int argc, char **argv)
209 {
210         struct obd_ioctl_data data;
211         int rc;
212
213         IOCINIT(data);
214
215         if ( argc != 2 && argc != 3  ) {
216                 fprintf(stderr, "Usage: %s type [data]\n", argv[0]);
217                 return 1;
218         }
219
220         data.ioc_inllen1 =  strlen(argv[1]) + 1;
221         data.ioc_inlbuf1 = argv[1];
222         if ( argc == 3 ) { 
223                 data.ioc_inllen2 = strlen(argv[2]) + 1;
224                 data.ioc_inlbuf2 = argv[2];
225         }
226
227         printf("attach len %d addr %p type %s data %s\n", data.ioc_len, buf, 
228                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
229
230         if (obd_ioctl_pack(&data, &buf, max)) { 
231                 printf("invalid ioctl\n"); 
232                 return 1;
233         }
234         printf("attach len %d addr %p raw %p type %s data %s and %s\n", data.ioc_len, buf, rawbuf,
235                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2), &buf[516]);
236
237         rc = ioctl(fd, OBD_IOC_ATTACH , buf);
238         if (rc < 0) {
239                 printf("Attach: %x %s\n", OBD_IOC_ATTACH, strerror(errno));
240                 return 1;
241         }
242         return 0;
243 }
244
245 static int jt_setup(int argc, char **argv)
246 {
247         struct obd_ioctl_data data;
248         int rc;
249
250         IOCINIT(data);
251
252         if ( argc != 2 && argc != 3  ) {
253                 fprintf(stderr, "Usage: %s device [fstype]\n", argv[0]);
254                 return 1;
255         }
256
257         data.ioc_inllen1 =  strlen(argv[1]) + 1;
258         data.ioc_inlbuf1 = argv[1];
259         if ( argc == 3 ) { 
260                 data.ioc_inllen2 = strlen(argv[2]) + 1;
261                 data.ioc_inlbuf2 = argv[2];
262         }
263
264         printf("attach len %d addr %p type %s data %s\n", data.ioc_len, buf, 
265                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
266
267         if (obd_ioctl_pack(&data, &buf, max)) { 
268                 printf("invalid ioctl\n"); 
269                 return 1;
270         }
271         printf("attach len %d addr %p raw %p type %s data %s and %s\n", data.ioc_len, buf, rawbuf,
272                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2), &buf[516]);
273
274         rc = ioctl(fd, OBD_IOC_SETUP , buf);
275         if (rc < 0) {
276                 printf("Attach: %x %s\n", OBD_IOC_SETUP, strerror(errno));
277                 return 1;
278         }
279         return 0;
280 }
281
282
283 static int jt_create(int argc, char **argv)
284 {
285         struct obd_ioctl_data data;
286         int num = 1;
287         int silent = 0;
288         int i;
289         int rc;
290
291         IOCINIT(data);
292         if (argc > 1) { 
293                 num = strtoul(argv[1], NULL, 0);
294         } else
295
296         if (argc > 2) { 
297                 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
298         } else { 
299                 data.ioc_obdo1.o_mode = 0100644;
300         }
301         data.ioc_obdo1.o_mode = OBD_MD_FLMODE;
302
303         if (argc > 3) { 
304                 silent = strtoul(argv[3], NULL, 0);
305         }
306                 
307         printf("Creating %d obdos\n", num);
308
309         for (i = 0 ; i<num ; i++) { 
310                 rc = ioctl(fd, OBD_IOC_CREATE , &data);
311                 if (rc < 0) {
312                         printf("Create: %x %s\n", OBD_IOC_CREATE, 
313                                strerror(errno));
314                         return 1;
315                 }
316                 printf("created obdo %Ld\n", data.ioc_obdo1.o_id);
317         }
318         return 0;
319 }
320
321 command_t list[] = {
322         {"device", jt_device, 0, "set current device (args device no)"},
323     {"attach", jt_attach, 0, "name the typed of device (args: type data"},
324     {"setup", jt_setup, 0, "setup device (args: blkdev, data"},
325     {"detach", jt_detach, 0, "detach the current device (arg: )"},
326     {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
327     {"create", jt_create, 0, "create [count [mode [silent]]]"},
328     {"connect", jt_connect, 0, "connect - get a connection to device"},
329     {"disconnect", jt_disconnect, 0, "disconnect - break connection to device"},
330     {"help", Parser_help, 0, "help"},
331     {"exit", Parser_quit, 0, "quit"},
332     {"quit", Parser_quit, 0, "quit"},
333     { 0, 0, 0, NULL }
334 };
335
336 int main(int argc, char **argv)
337 {
338
339         if (argc > 1) { 
340                 return Parser_execarg(argc - 1, &argv[1], list);
341         }
342
343         Parser_init("obdctl > ", list);
344         Parser_commands();
345
346         return 0;
347 }
348