Whamcloud - gitweb
We touched everything, but it's not as scary as it looks.
[fs/lustre-release.git] / lustre / utils / obdctl.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  *   Author: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24
25 #include <stdlib.h>
26 #include <sys/ioctl.h>
27 #include <fcntl.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <stdio.h>
32 #include <signal.h>
33 #define printk printf
34
35 #include <linux/lustre_lib.h>
36 #include <linux/lustre_idl.h>
37 #include <linux/lustre_dlm.h>
38
39 #include <unistd.h>
40 #include <sys/un.h>
41 #include <time.h>
42 #include <sys/time.h>
43 #include <netinet/in.h>
44 #include <errno.h>
45 #include <string.h>
46
47 #define __KERNEL__
48 #include <linux/list.h>
49 #undef __KERNEL__
50
51 #include "parser.h"
52 #include <stdio.h>
53
54 int fd = -1;
55 int connid = -1;
56 char rawbuf[8192];
57 char *buf = rawbuf;
58 int max = 8192;
59
60 #define IOCINIT(data)                                                   \
61 do {                                                                    \
62         memset(&data, 0, sizeof(data));                                 \
63         data.ioc_version = OBD_IOCTL_VERSION;                           \
64         data.ioc_conn1 = connid;                                        \
65         data.ioc_len = sizeof(data);                                    \
66         if (fd < 0) {                                                   \
67                 fprintf(stderr, "No device open, use device\n");        \
68                 return 1;                                               \
69         }                                                               \
70 } while (0)
71
72 /*
73     pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L", 
74     $obdo->{id}, 0, 
75     $obdo->{gr}, 0, 
76     $obdo->{atime}, 0, 
77     $obdo->{mtime}, 0 ,
78     $obdo->{ctime}, 0, 
79     $obdo->{size}, 0, 
80     $obdo->{blocks}, 0, 
81     $obdo->{blksize},
82     $obdo->{mode},
83     $obdo->{uid},
84     $obdo->{gid},
85     $obdo->{flags},
86     $obdo->{obdflags},
87     $obdo->{nlink},     
88     $obdo->{generation},        
89     $obdo->{valid},     
90     $obdo->{inline},
91     $obdo->{obdmd},
92     0, 0, # struct list_head 
93     0;  #  struct obd_ops 
94 }
95
96 */
97
98 char * obdo_print(struct obdo *obd)
99 {
100         char buf[1024];
101
102         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",
103                 obd->o_id,
104                 obd->o_gr,
105                 obd->o_atime,
106                 obd->o_mtime,
107                 obd->o_ctime,
108                 obd->o_size,
109                 obd->o_blocks,
110                 obd->o_blksize,
111                 obd->o_mode,
112                 obd->o_uid,
113                 obd->o_gid,
114                 obd->o_flags,
115                 obd->o_obdflags,
116                 obd->o_nlink,
117                 obd->o_valid);
118         return strdup(buf);
119 }
120
121 static int do_disconnect()
122 {
123         struct obd_ioctl_data data;
124         int rc;
125
126         if (connid == -1)
127                 return 0;
128
129         IOCINIT(data);
130
131         rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
132         if (rc < 0) {
133                 printf("Device: %x %s\n", OBD_IOC_DISCONNECT, strerror(errno));
134                 return 0;
135         } else {
136                 printf("Disconnected connid %d\n", connid);
137         }
138         connid = -1;
139
140         return 0;
141 }
142
143 static int jt_device(int argc, char **argv)
144 {
145         struct obd_ioctl_data data;
146         int rc;
147
148         do_disconnect();
149
150         memset(&data, 0, sizeof(data));
151         if ( argc != 2 ) {
152                 fprintf(stderr, "Usage: %s devno\n", argv[0]);
153                 return 1;
154         }
155
156         data.ioc_dev = atoi(argv[1]);
157
158         if (obd_ioctl_pack(&data, &buf, max)) { 
159                 printf("invalid ioctl\n"); 
160                 return 1;
161         }
162
163         if (fd == -1) 
164                 fd = open("/dev/obd", O_RDWR);
165         if (fd == -1) {
166                 printf("Opening /dev/obd: %s\n", strerror(errno));
167                 return 1;
168         }
169
170         rc = ioctl(fd, OBD_IOC_DEVICE , buf);
171         if (rc < 0) {
172                 printf("Device: %x %s\n", OBD_IOC_DEVICE, strerror(errno));
173                 return 1;
174         }
175
176         return 0;
177 }
178
179 static int jt_connect(int argc, char **argv)
180 {
181         struct obd_ioctl_data data;
182         int rc;
183
184         IOCINIT(data);
185
186         do_disconnect();
187
188         if ( argc != 1 ) {
189                 fprintf(stderr, "Usage: %s\n", argv[0]);
190                 return 1;
191         }
192
193         rc = ioctl(fd, OBD_IOC_CONNECT , &data);
194         if (rc < 0) {
195                 printf("Device: %x %s\n", OBD_IOC_CONNECT, strerror(errno));
196                 return 1;
197         }
198         connid = data.ioc_conn1;
199
200         return 0;
201 }
202
203 static int jt_disconnect(int argc, char **argv)
204 {
205         struct obd_ioctl_data data;
206         int rc;
207
208         IOCINIT(data);
209
210         if ( argc != 1 ) {
211                 fprintf(stderr, "Usage: %s\n", argv[0]);
212                 return 1;
213         }
214
215         rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
216         if (rc < 0) {
217                 printf("Device: %x %s\n", OBD_IOC_DISCONNECT, strerror(errno));
218                 return 1;
219         }
220         connid = -1;
221
222         return 0;
223 }
224
225
226 static int jt_detach(int argc, char **argv)
227 {
228         struct obd_ioctl_data data;
229         int rc;
230
231         IOCINIT(data);
232
233         if ( argc != 1 ) {
234                 fprintf(stderr, "Usage: %s\n", argv[0]);
235                 return 1;
236         }
237
238         if (obd_ioctl_pack(&data, &buf, max)) { 
239                 printf("invalid ioctl\n"); 
240                 return 1;
241         }
242
243         rc = ioctl(fd, OBD_IOC_DETACH , buf);
244         if (rc < 0) {
245                 printf("Detach: %s\n", strerror(errno));
246                 return 1;
247         }
248         return 0;
249 }
250
251 static int jt_cleanup(int argc, char **argv)
252 {
253         struct obd_ioctl_data data;
254         int rc;
255
256         IOCINIT(data);
257
258         if ( argc != 1 ) {
259                 fprintf(stderr, "Usage: %s\n", argv[0]);
260                 return 1;
261         }
262
263         rc = ioctl(fd, OBD_IOC_CLEANUP , &data);
264         if (rc < 0) {
265                 printf("Detach: %s\n", strerror(errno));
266                 return 1;
267         }
268         return 0;
269 }
270
271 static int jt_attach(int argc, char **argv)
272 {
273         struct obd_ioctl_data data;
274         int rc;
275
276         IOCINIT(data);
277
278         if ( argc != 2 && argc != 3  ) {
279                 fprintf(stderr, "Usage: %s type [data]\n", argv[0]);
280                 return 1;
281         }
282
283         data.ioc_inllen1 =  strlen(argv[1]) + 1;
284         data.ioc_inlbuf1 = argv[1];
285         if ( argc == 3 ) { 
286                 data.ioc_inllen2 = strlen(argv[2]) + 1;
287                 data.ioc_inlbuf2 = argv[2];
288         }
289
290         printf("attach len %d addr %p type %s data %s\n", data.ioc_len, buf, 
291                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
292
293         if (obd_ioctl_pack(&data, &buf, max)) { 
294                 printf("invalid ioctl\n"); 
295                 return 1;
296         }
297         printf("attach len %d addr %p raw %p type %s data %s and %s\n",
298                data.ioc_len, buf, rawbuf,
299                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2), &buf[516]);
300
301         rc = ioctl(fd, OBD_IOC_ATTACH , buf);
302         if (rc < 0) {
303                 printf("Attach: %x %s\n", OBD_IOC_ATTACH, strerror(errno));
304                 return 1;
305         }
306         return 0;
307 }
308
309 static int jt_setup(int argc, char **argv)
310 {
311         struct obd_ioctl_data data;
312         int rc;
313
314         IOCINIT(data);
315
316         if ( argc > 3  ) {
317                 fprintf(stderr, "Usage: %s [device] [fstype]\n", argv[0]);
318                 return 1;
319         }
320
321         if (argc > 1) {
322                 data.ioc_inllen1 =  strlen(argv[1]) + 1;
323                 data.ioc_inlbuf1 = argv[1];
324                 data.ioc_dev = strtoul(argv[1], NULL, 0);
325         } else {
326                 data.ioc_dev = -1;
327         }
328         if ( argc == 3 ) { 
329                 data.ioc_inllen2 = strlen(argv[2]) + 1;
330                 data.ioc_inlbuf2 = argv[2];
331         }
332
333         printf("setup len %d addr %p device %s type %s\n", data.ioc_len, buf, 
334                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
335
336         if (obd_ioctl_pack(&data, &buf, max)) { 
337                 printf("invalid ioctl\n"); 
338                 return 1;
339         }
340         printf("setup len %d addr %p raw %p device %s type %s\n", 
341                data.ioc_len, buf, rawbuf,
342                MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
343
344         rc = ioctl(fd, OBD_IOC_SETUP , buf);
345         if (rc < 0) {
346                 printf("setup: %x %s\n", OBD_IOC_SETUP, strerror(errno));
347                 return 1;
348         }
349         return 0;
350 }
351
352
353 static int jt_create(int argc, char **argv)
354 {
355         struct obd_ioctl_data data;
356         int num = 1;
357         int silent = 0;
358         int i;
359         int rc;
360
361         IOCINIT(data);
362         if (argc > 1) { 
363                 num = strtoul(argv[1], NULL, 0);
364         } else { 
365                 printf("usage %s num [mode] [silent]\n", argv[0]); 
366         }
367
368         if (argc > 2) { 
369                 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
370         } else { 
371                 data.ioc_obdo1.o_mode = 0100644;
372         }
373         data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
374
375         if (argc > 3) { 
376                 silent = strtoul(argv[3], NULL, 0);
377         }
378                 
379         printf("Creating %d obdos\n", num);
380
381         for (i = 0 ; i<num ; i++) { 
382                 rc = ioctl(fd, OBD_IOC_CREATE , &data);
383                 if (rc < 0) {
384                         printf("Create: %x %s\n", OBD_IOC_CREATE, 
385                                strerror(errno));
386                         return 1;
387                 }
388                 printf("created obdo %Ld\n", data.ioc_obdo1.o_id);
389         }
390         return 0;
391 }
392
393 static int jt_setattr(int argc, char **argv)
394 {
395         struct obd_ioctl_data data;
396         int rc;
397
398         IOCINIT(data);
399         if (argc < 2) { 
400                 printf("usage: %s id mode\n", argv[0]); 
401                 return -1;
402         }
403
404         data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
405         data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
406         data.ioc_obdo1.o_valid = OBD_MD_FLMODE; 
407
408         rc = ioctl(fd, OBD_IOC_SETATTR , &data);
409         if (rc < 0) {
410                 printf("setattr: %x %s\n", OBD_IOC_SETATTR, strerror(errno));
411         }
412         return rc;
413 }
414
415 static int jt_destroy(int argc, char **argv)
416 {
417         struct obd_ioctl_data data;
418         int rc;
419
420         IOCINIT(data);
421         if (argc < 1) { 
422                 printf("usage: %s id\n", argv[0]); 
423         }
424
425         data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
426
427         rc = ioctl(fd, OBD_IOC_DESTROY , &data);
428         if (rc < 0) {
429                 printf("setattr: %x %s\n", OBD_IOC_DESTROY, strerror(errno));
430         }
431         return rc;
432 }
433
434 static int jt_getattr(int argc, char **argv)
435 {
436         struct obd_ioctl_data data;
437         int rc;
438
439         IOCINIT(data);
440         if (argc == 2) {
441                 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
442                 data.ioc_obdo1.o_valid = 0xffffffff;
443                 printf("getting attr for %Ld\n", data.ioc_obdo1.o_id);
444         } else {
445                 fprintf(stderr, "usage: %s id\n", argv[0]);
446                 return 0;
447         }
448
449         rc = ioctl(fd, OBD_IOC_GETATTR , &data);
450         if (rc) {
451                 printf("Error: %s\n", strerror(rc));
452         } else {
453                 printf("attr obdo %Ld, mode %o\n", data.ioc_obdo1.o_id,
454                        data.ioc_obdo1.o_mode);
455         }
456         return 0;
457 }
458
459 static int jt_test_getattr(int argc, char **argv)
460 {
461         struct obd_ioctl_data data;
462         struct timeval start;
463         int count, i;
464         int silent = 0;
465         int rc;
466
467         IOCINIT(data);
468         if (argc == 2 || argc == 3) {
469                 count = strtoul(argv[1], NULL, 0);
470
471                 if (argc == 3)
472                         silent = strtoul(argv[2], NULL, 0);
473                 data.ioc_obdo1.o_valid = 0xffffffff;
474                 data.ioc_obdo1.o_id = 2;
475                 gettimeofday(&start, NULL);
476                 printf("getting %d attrs (testing only): %s", count,
477                        ctime(&start.tv_sec));
478         } else {
479                 fprintf(stderr, "usage: %s count [silent]\n", argv[0]);
480                 return 0;
481         }
482
483         for (i = 0 ; i < count; i++) {
484                 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
485                 if (rc) {
486                         printf("Error: %s on getattr #%d\n", strerror(rc), i);
487                         break;
488                 } else if (!silent) {
489                         printf("attr number %d\n", i);
490                 }
491         }
492         if (!rc) {
493                 struct timeval end;
494
495                 gettimeofday(&end, NULL);
496
497                 printf("got attrs successfully %d times (%g/sec): %s", i,
498                        (double)i / ((double)(end.tv_sec - start.tv_sec) +
499                                     (double)(end.tv_usec - start.tv_usec) /
500                                     1000000), ctime(&end.tv_sec));
501         }
502         return 0;
503 }
504
505 static int jt_test_brw(int argc, char **argv)
506 {
507         struct obd_ioctl_data data;
508         struct timeval start;
509         char *bulk;
510         int pages = 4, count;
511         int silent = 0, write = 0, rw;
512         int i;
513         int rc;
514
515         if (argc >= 2 && argc <= 5) {
516                 int len;
517
518                 count = strtoul(argv[1], NULL, 0);
519
520                 if (argc >= 3)
521                         write = strtoul(argv[2], NULL, 0);
522                 if (argc >= 4)
523                         silent = strtoul(argv[3], NULL, 0);
524                 if (argc >= 5)
525                         pages = strtoul(argv[4], NULL, 0);
526
527                 len = pages * PAGE_SIZE;
528
529                 bulk = malloc(2 * len);
530                 if (!bulk) {
531                         fprintf(stderr,
532                                 "%s: out of memory allocating 2x%d pages\n",
533                                 argv[0], pages);
534                         return 0;
535                 }
536                 IOCINIT(data);
537                 data.ioc_conn2 = connid;
538                 data.ioc_obdo1.o_id = data.ioc_obdo2.o_id = 2;
539                 data.ioc_count = len;
540                 data.ioc_offset = 0;
541                 data.ioc_plen1 = data.ioc_plen2 = len;
542                 data.ioc_pbuf1 = bulk;
543                 data.ioc_pbuf2 = bulk + len;
544
545                 gettimeofday(&start, NULL);
546                 printf("%s %d (2x%d pages) (testing only): %s",
547                        write ? "writing" : "reading", count, pages,
548                        ctime(&start.tv_sec));
549         } else {
550                 printf("usage: %s count [write [silent [pages]]]\n", argv[0]);
551                 return 0;
552         }
553
554         rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
555         for (i = 0 ; i < count; i++) {
556                 rc = ioctl(fd, rw, &data);
557                 if (rc) {
558                         printf("Error: %s on %s #%d\n", strerror(rc),
559                                write ? "write" : "read", i);
560                         break;
561                 } else if (!silent) {
562                         printf("%s number %d\n", write ? "write" : "read", i);
563                 }
564         }
565
566         free(bulk);
567
568         if (!rc) {
569                 struct timeval end;
570
571                 gettimeofday(&end, NULL);
572
573                 printf("%s 2x%d pages successfully %d times (%g/sec): %s",
574                        write ? "wrote" : "read", pages, i,
575                        2.0 * i * pages / ((double)(end.tv_sec - start.tv_sec) +
576                                     (double)(end.tv_usec - start.tv_usec) /
577                                     1000000), ctime(&end.tv_sec));
578         }
579         return 0;
580 }
581
582 static int jt_test_ldlm(int argc, char **argv)
583 {
584         struct obd_ioctl_data data;
585         int rc;
586
587         IOCINIT(data);
588         if (argc != 1) {
589                 fprintf(stderr, "usage: %s\n", argv[0]);
590                 return 0;
591         }
592
593         rc = ioctl(fd, IOC_LDLM_TEST, &data);
594         if (rc)
595                 fprintf(stderr, "LDLM test failed: %s\n", strerror(errno));
596         return rc;
597 }
598
599 command_t list[] = {
600         {"device", jt_device, 0, "set current device (args device no)"},
601         {"attach", jt_attach, 0, "name the typed of device (args: type data"},
602         {"setup", jt_setup, 0, "setup device (args: blkdev, data"},
603         {"detach", jt_detach, 0, "detach the current device (arg: )"},
604         {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
605         {"create", jt_create, 0, "create [count [mode [silent]]]"},
606         {"destroy", jt_destroy, 0, "destroy id"},
607         {"getattr", jt_getattr, 0, "getattr id"},
608         {"setattr", jt_setattr, 0, "setattr id mode"},
609         {"connect", jt_connect, 0, "connect - get a connection to device"},
610         {"disconnect", jt_disconnect, 0,
611                 "disconnect - break connection to device"},
612         {"test_getattr", jt_test_getattr, 0, "test_getattr count [silent]"},
613         {"test_brw", jt_test_brw, 0, "test_brw count [write [silent]]"},
614         {"test_ldlm", jt_test_ldlm, 0, "test lock manager (no args)"},
615         {"help", Parser_help, 0, "help"},
616         {"exit", Parser_quit, 0, "quit"},
617         {"quit", Parser_quit, 0, "quit"},
618         { 0, 0, 0, NULL }
619 };
620
621
622 static void signal_server(int sig)
623 {
624         if (sig == SIGINT) { 
625                 fprintf(stderr,"\nDisconnecting any existing connections...\n");
626                 do_disconnect();
627                 exit(1);
628         }
629 }
630
631 int main(int argc, char **argv)
632 {
633         struct sigaction sigact;
634         int rc = 0;
635
636         sigact.sa_handler = signal_server;
637         sigfillset(&sigact.sa_mask);
638         sigact.sa_flags = SA_RESTART;
639         sigaction(SIGINT, &sigact, NULL);
640
641
642         if (argc > 1) { 
643                 rc = Parser_execarg(argc - 1, &argv[1], list);
644         } else {
645                 Parser_init("obdctl > ", list);
646                 Parser_commands();
647         }
648
649         do_disconnect();
650         return rc;
651 }
652