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