Whamcloud - gitweb
class/class_obd.c: small OBD_ATTACHED sanity cleanup; OBD_SET_UP fix.
[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/lustre_lib.h>
10 #include <linux/lustre_idl.h>
11 #include <unistd.h>
12 #include <sys/un.h>
13 #include <sys/time.h>
14 #include <netinet/in.h>
15 #include <errno.h>
16 #include <string.h>
17
18 #define __KERNEL__
19 #include <linux/list.h>
20 #undef __KERNEL__
21
22 #include "parser.h"
23 #include <stdio.h>
24
25 int fd = -1;
26 int connid = -1;
27 char rawbuf[8192];
28 char *buf = rawbuf;
29 int max = 8192;
30
31 #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)
32
33 /*
34     pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L", 
35     $obdo->{id}, 0, 
36     $obdo->{gr}, 0, 
37     $obdo->{atime}, 0, 
38     $obdo->{mtime}, 0 ,
39     $obdo->{ctime}, 0, 
40     $obdo->{size}, 0, 
41     $obdo->{blocks}, 0, 
42     $obdo->{blksize},
43     $obdo->{mode},
44     $obdo->{uid},
45     $obdo->{gid},
46     $obdo->{flags},
47     $obdo->{obdflags},
48     $obdo->{nlink},     
49     $obdo->{generation},        
50     $obdo->{valid},     
51     $obdo->{inline},
52     $obdo->{obdmd},
53     0, 0, # struct list_head 
54     0;  #  struct obd_ops 
55 }
56
57 */
58
59 char * obdo_print(struct obdo *obd)
60 {
61         char buf[1024];
62
63         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",
64                 obd->o_id,
65                 obd->o_gr,
66                 obd->o_atime,
67                 obd->o_mtime,
68                 obd->o_ctime,
69                 obd->o_size,
70                 obd->o_blocks,
71                 obd->o_blksize,
72                 obd->o_mode,
73                 obd->o_uid,
74                 obd->o_gid,
75                 obd->o_flags,
76                 obd->o_obdflags,
77                 obd->o_nlink,
78                 obd->o_valid);
79         return strdup(buf);
80 }
81
82
83 static int jt_device(int argc, char **argv)
84 {
85         struct obd_ioctl_data data;
86         int rc;
87
88         memset(&data, 0, sizeof(data));
89         if ( argc != 2 ) {
90                 fprintf(stderr, "Usage: %s devno\n", argv[0]);
91                 return 1;
92         }
93
94         data.ioc_dev = atoi(argv[1]);
95
96         if (obd_ioctl_pack(&data, &buf, max)) { 
97                 printf("invalid ioctl\n"); 
98                 return 1;
99         }
100
101         if (fd == -1) 
102                 fd = open("/dev/obd", O_RDWR);
103         if (fd == -1) {
104                 printf("Opening /dev/obd: %s\n", strerror(errno));
105                 return 1;
106         }
107
108         rc = ioctl(fd, OBD_IOC_DEVICE , buf);
109         if (rc < 0) {
110                 printf("Device: %x %s\n", OBD_IOC_DEVICE, strerror(errno));
111                 return 1;
112         }
113
114         return 0;
115 }
116
117 static int jt_connect(int argc, char **argv)
118 {
119         struct obd_ioctl_data data;
120         int rc;
121
122         IOCINIT(data);
123
124         if ( argc != 1 ) {
125                 fprintf(stderr, "Usage: %s\n", argv[0]);
126                 return 1;
127         }
128
129         rc = ioctl(fd, OBD_IOC_CONNECT , &data);
130         if (rc < 0) {
131                 printf("Device: %x %s\n", OBD_IOC_CONNECT, strerror(errno));
132                 return 1;
133         }
134         connid = data.ioc_conn1;
135
136         return 0;
137 }
138
139
140
141 static int jt_disconnect(int argc, char **argv)
142 {
143         struct obd_ioctl_data data;
144         int rc;
145
146         IOCINIT(data);
147
148         if ( argc != 1 ) {
149                 fprintf(stderr, "Usage: %s\n", argv[0]);
150                 return 1;
151         }
152
153         rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
154         if (rc < 0) {
155                 printf("Device: %x %s\n", OBD_IOC_DISCONNECT, strerror(errno));
156                 return 1;
157         }
158         connid = -1;
159
160         return 0;
161 }
162
163
164 static int jt_detach(int argc, char **argv)
165 {
166         struct obd_ioctl_data data;
167         int rc;
168
169         IOCINIT(data);
170
171         if ( argc != 1 ) {
172                 fprintf(stderr, "Usage: %s\n", argv[0]);
173                 return 1;
174         }
175
176         if (obd_ioctl_pack(&data, &buf, max)) { 
177                 printf("invalid ioctl\n"); 
178                 return 1;
179         }
180
181         rc = ioctl(fd, OBD_IOC_DETACH , buf);
182         if (rc < 0) {
183                 printf("Detach: %s\n", strerror(errno));
184                 return 1;
185         }
186         return 0;
187 }
188
189 static int jt_cleanup(int argc, char **argv)
190 {
191         struct obd_ioctl_data data;
192         int rc;
193
194         IOCINIT(data);
195
196         if ( argc != 1 ) {
197                 fprintf(stderr, "Usage: %s\n", argv[0]);
198                 return 1;
199         }
200
201         rc = ioctl(fd, OBD_IOC_CLEANUP , &data);
202         if (rc < 0) {
203                 printf("Detach: %s\n", strerror(errno));
204                 return 1;
205         }
206         return 0;
207 }
208
209 static int jt_attach(int argc, char **argv)
210 {
211         struct obd_ioctl_data data;
212         int rc;
213
214         IOCINIT(data);
215
216         if ( argc != 2 && argc != 3  ) {
217                 fprintf(stderr, "Usage: %s type [data]\n", argv[0]);
218                 return 1;
219         }
220
221         data.ioc_inllen1 =  strlen(argv[1]) + 1;
222         data.ioc_inlbuf1 = argv[1];
223         if ( argc == 3 ) { 
224                 data.ioc_inllen2 = strlen(argv[2]) + 1;
225                 data.ioc_inlbuf2 = argv[2];
226         }
227
228         printf("attach len %d addr %p type %s data %s\n", data.ioc_len, buf, 
229                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
230
231         if (obd_ioctl_pack(&data, &buf, max)) { 
232                 printf("invalid ioctl\n"); 
233                 return 1;
234         }
235         printf("attach len %d addr %p raw %p type %s data %s and %s\n", data.ioc_len, buf, rawbuf,
236                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2), &buf[516]);
237
238         rc = ioctl(fd, OBD_IOC_ATTACH , buf);
239         if (rc < 0) {
240                 printf("Attach: %x %s\n", OBD_IOC_ATTACH, strerror(errno));
241                 return 1;
242         }
243         return 0;
244 }
245
246 static int jt_setup(int argc, char **argv)
247 {
248         struct obd_ioctl_data data;
249         int rc;
250
251         IOCINIT(data);
252
253         if ( argc > 3  ) {
254                 fprintf(stderr, "Usage: %s [device] [fstype]\n", argv[0]);
255                 return 1;
256         }
257
258         if (argc > 1) {
259                 data.ioc_inllen1 =  strlen(argv[1]) + 1;
260                 data.ioc_inlbuf1 = argv[1];
261                 data.ioc_dev = strtoul(argv[1], NULL, 0);
262         } else {
263                 data.ioc_dev = -1;
264         }
265         if ( argc == 3 ) { 
266                 data.ioc_inllen2 = strlen(argv[2]) + 1;
267                 data.ioc_inlbuf2 = argv[2];
268         }
269
270         printf("setup len %d addr %p device %s type %s\n", data.ioc_len, buf, 
271                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
272
273         if (obd_ioctl_pack(&data, &buf, max)) { 
274                 printf("invalid ioctl\n"); 
275                 return 1;
276         }
277         printf("setup len %d addr %p raw %p device %s type %s\n", 
278                data.ioc_len, buf, rawbuf,
279                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
280
281         rc = ioctl(fd, OBD_IOC_SETUP , buf);
282         if (rc < 0) {
283                 printf("setup: %x %s\n", OBD_IOC_SETUP, strerror(errno));
284                 return 1;
285         }
286         return 0;
287 }
288
289
290 static int jt_create(int argc, char **argv)
291 {
292         struct obd_ioctl_data data;
293         int num = 1;
294         int silent = 0;
295         int i;
296         int rc;
297
298         IOCINIT(data);
299         if (argc > 1) { 
300                 num = strtoul(argv[1], NULL, 0);
301         } else { 
302                 printf("usage %s num [mode] [silent]\n", argv[0]); 
303         }
304
305         if (argc > 2) { 
306                 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
307         } else { 
308                 data.ioc_obdo1.o_mode = 0100644;
309         }
310         data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
311
312         if (argc > 3) { 
313                 silent = strtoul(argv[3], NULL, 0);
314         }
315                 
316         printf("Creating %d obdos\n", num);
317
318         for (i = 0 ; i<num ; i++) { 
319                 rc = ioctl(fd, OBD_IOC_CREATE , &data);
320                 if (rc < 0) {
321                         printf("Create: %x %s\n", OBD_IOC_CREATE, 
322                                strerror(errno));
323                         return 1;
324                 }
325                 printf("created obdo %Ld\n", data.ioc_obdo1.o_id);
326         }
327         return 0;
328 }
329
330 static int jt_setattr(int argc, char **argv)
331 {
332         struct obd_ioctl_data data;
333         int rc;
334
335         IOCINIT(data);
336         if (argc < 2) { 
337                 printf("usage: %s id mode\n", argv[0]); 
338                 return -1;
339         }
340
341         data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
342         data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
343         data.ioc_obdo1.o_valid = OBD_MD_FLMODE; 
344
345         rc = ioctl(fd, OBD_IOC_SETATTR , &data);
346         if (rc < 0) {
347                 printf("setattr: %x %s\n", OBD_IOC_SETATTR, strerror(errno));
348         }
349         return rc;
350 }
351
352 static int jt_destroy(int argc, char **argv)
353 {
354         struct obd_ioctl_data data;
355         int rc;
356
357         IOCINIT(data);
358         if (argc < 1) { 
359                 printf("usage %s id\n", argv[0]); 
360         }
361
362         data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
363
364         rc = ioctl(fd, OBD_IOC_DESTROY , &data);
365         if (rc < 0) {
366                 printf("setattr: %x %s\n", OBD_IOC_DESTROY, strerror(errno));
367         }
368         return rc;
369 }
370
371 static int jt_getattr(int argc, char **argv)
372 {
373         struct obd_ioctl_data data;
374         int rc;
375
376         IOCINIT(data);
377         if (argc != 1) { 
378                 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
379                 data.ioc_obdo1.o_valid = 0xffffffff;
380                 printf("getting attr for %Ld\n", data.ioc_obdo1.o_id);
381         } else { 
382                 printf("usage %s id\n", argv[0]); 
383                 return 0;
384         }
385
386         rc = ioctl(fd, OBD_IOC_GETATTR , &data);
387         if (rc) { 
388                 printf("Error: %s\n", strerror(rc)); 
389         } else { 
390                 printf("attr obdo %Ld, mode %o\n", data.ioc_obdo1.o_id, 
391                        data.ioc_obdo1.o_mode);
392         }
393         return 0;
394 }
395
396 command_t list[] = {
397         {"device", jt_device, 0, "set current device (args device no)"},
398     {"attach", jt_attach, 0, "name the typed of device (args: type data"},
399     {"setup", jt_setup, 0, "setup device (args: blkdev, data"},
400     {"detach", jt_detach, 0, "detach the current device (arg: )"},
401     {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
402     {"create", jt_create, 0, "create [count [mode [silent]]]"},
403     {"destroy", jt_destroy, 0, "destroy id"},
404     {"getattr", jt_getattr, 0, "getattr id"},
405     {"setattr", jt_setattr, 0, "setattr id mode"},
406     {"connect", jt_connect, 0, "connect - get a connection to device"},
407     {"disconnect", jt_disconnect, 0, "disconnect - break connection to device"},
408     {"help", Parser_help, 0, "help"},
409     {"exit", Parser_quit, 0, "quit"},
410     {"quit", Parser_quit, 0, "quit"},
411     { 0, 0, 0, NULL }
412 };
413
414 int main(int argc, char **argv)
415 {
416
417         if (argc > 1) { 
418                 return Parser_execarg(argc - 1, &argv[1], list);
419         }
420
421         Parser_init("obdctl > ", list);
422         Parser_commands();
423
424         return 0;
425 }
426