Whamcloud - gitweb
A solid part of the MDS request processing infrastructure.
[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 != 2 && argc != 3  ) {
254                 fprintf(stderr, "Usage: %s device [fstype]\n", argv[0]);
255                 return 1;
256         }
257
258         data.ioc_inllen1 =  strlen(argv[1]) + 1;
259         data.ioc_inlbuf1 = argv[1];
260         if ( argc == 3 ) { 
261                 data.ioc_inllen2 = strlen(argv[2]) + 1;
262                 data.ioc_inlbuf2 = argv[2];
263         }
264
265         printf("setup len %d addr %p device %s type %s\n", data.ioc_len, buf, 
266                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
267
268         if (obd_ioctl_pack(&data, &buf, max)) { 
269                 printf("invalid ioctl\n"); 
270                 return 1;
271         }
272         printf("setup len %d addr %p raw %p device %s type %s\n", 
273                data.ioc_len, buf, rawbuf,
274                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
275
276         rc = ioctl(fd, OBD_IOC_SETUP , buf);
277         if (rc < 0) {
278                 printf("setup: %x %s\n", OBD_IOC_SETUP, strerror(errno));
279                 return 1;
280         }
281         return 0;
282 }
283
284
285 static int jt_create(int argc, char **argv)
286 {
287         struct obd_ioctl_data data;
288         int num = 1;
289         int silent = 0;
290         int i;
291         int rc;
292
293         IOCINIT(data);
294         if (argc > 1) { 
295                 num = strtoul(argv[1], NULL, 0);
296         } else
297
298         if (argc > 2) { 
299                 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
300         } else { 
301                 data.ioc_obdo1.o_mode = 0100644;
302         }
303         data.ioc_obdo1.o_mode = OBD_MD_FLMODE;
304
305         if (argc > 3) { 
306                 silent = strtoul(argv[3], NULL, 0);
307         }
308                 
309         printf("Creating %d obdos\n", num);
310
311         for (i = 0 ; i<num ; i++) { 
312                 rc = ioctl(fd, OBD_IOC_CREATE , &data);
313                 if (rc < 0) {
314                         printf("Create: %x %s\n", OBD_IOC_CREATE, 
315                                strerror(errno));
316                         return 1;
317                 }
318                 printf("created obdo %Ld\n", data.ioc_obdo1.o_id);
319         }
320         return 0;
321 }
322
323 command_t list[] = {
324         {"device", jt_device, 0, "set current device (args device no)"},
325     {"attach", jt_attach, 0, "name the typed of device (args: type data"},
326     {"setup", jt_setup, 0, "setup device (args: blkdev, data"},
327     {"detach", jt_detach, 0, "detach the current device (arg: )"},
328     {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
329     {"create", jt_create, 0, "create [count [mode [silent]]]"},
330     {"connect", jt_connect, 0, "connect - get a connection to device"},
331     {"disconnect", jt_disconnect, 0, "disconnect - break connection to device"},
332     {"help", Parser_help, 0, "help"},
333     {"exit", Parser_quit, 0, "quit"},
334     {"quit", Parser_quit, 0, "quit"},
335     { 0, 0, 0, NULL }
336 };
337
338 int main(int argc, char **argv)
339 {
340
341         if (argc > 1) { 
342                 return Parser_execarg(argc - 1, &argv[1], list);
343         }
344
345         Parser_init("obdctl > ", list);
346         Parser_commands();
347
348         return 0;
349 }
350