Whamcloud - gitweb
- debug.c was only used by obdclass, so I moved it there
[fs/lustre-release.git] / lustre / utils / obd.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  *   Author: Andreas Dilger <adilger@clusterfs.com>
8  *   Author: Robert Read <rread@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
12  *   Lustre is free software; you can redistribute it and/or
13  *   modify it under the terms of version 2 of the GNU General Public
14  *   License as published by the Free Software Foundation.
15  *
16  *   Lustre is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with Lustre; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  */
26
27
28 #include <stdlib.h>
29 #include <sys/ioctl.h>
30 #include <fcntl.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34 #include <sys/stat.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <signal.h>
38 #define printk printf
39
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_dlm.h>
43
44 #include <unistd.h>
45 #include <sys/un.h>
46 #include <time.h>
47 #include <sys/time.h>
48 #include <netinet/in.h>
49 #include <errno.h>
50 #include <string.h>
51
52 #include <asm/page.h>           /* needed for PAGE_SIZE - rread */
53
54 #define __KERNEL__
55 #include <linux/list.h>
56 #undef __KERNEL__
57
58 #include "obdctl.h"
59 #include "parser.h"
60 #include <stdio.h>
61
62 #define SHMEM_STATS 1
63 #if SHMEM_STATS
64 # include <sys/ipc.h>
65 # include <sys/shm.h>
66
67 # define MAX_SHMEM_COUNT 1024
68 static long long *shared_counters;
69 static long long counter_snapshot[2][MAX_SHMEM_COUNT];
70 struct timeval prev_time;
71 #endif
72
73 int fd = -1;
74 uint64_t conn_addr = -1;
75 uint64_t conn_cookie;
76 char rawbuf[8192];
77 char *buf = rawbuf;
78 int max = 8192;
79
80 static int thread;
81
82 static int getfd(char *func);
83 static char *cmdname(char *func);
84
85 #define IOCINIT(data)                                                   \
86 do {                                                                    \
87         memset(&data, 0, sizeof(data));                                 \
88         data.ioc_version = OBD_IOCTL_VERSION;                           \
89         data.ioc_addr = conn_addr;                                      \
90         data.ioc_cookie = conn_cookie;                                  \
91         data.ioc_len = sizeof(data);                                    \
92         if (fd < 0) {                                                   \
93                 fprintf(stderr, "No device open, use device\n");        \
94                 return 1;                                               \
95         }                                                               \
96 } while (0)
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\n"
103                 "size: %Ld\nblocks: %Ld\nblksize: %d\nmode: %o\nuid: %d\n"
104                 "gid: %d\nflags: %x\nobdflags: %x\nnlink: %d,\nvalid %x\n",
105                 obd->o_id,
106                 obd->o_gr,
107                 obd->o_atime,
108                 obd->o_mtime,
109                 obd->o_ctime,
110                 obd->o_size,
111                 obd->o_blocks,
112                 obd->o_blksize,
113                 obd->o_mode,
114                 obd->o_uid,
115                 obd->o_gid,
116                 obd->o_flags, obd->o_obdflags, obd->o_nlink, obd->o_valid);
117         return strdup(buf);
118 }
119
120
121 #define BAD_VERBOSE (-999999999)
122
123 #define N2D_OFF 0x100      /* So we can tell between error codes and devices */
124
125 static int do_name2dev(char *func, char *name)
126 {
127         struct obd_ioctl_data data;
128         int rc;
129
130         if (getfd(func))
131                 return -1;
132
133         IOCINIT(data);
134
135         data.ioc_inllen1 = strlen(name) + 1;
136         data.ioc_inlbuf1 = name;
137
138         if (obd_ioctl_pack(&data, &buf, max)) {
139                 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(func));
140                 return -2;
141         }
142         rc = ioctl(fd, OBD_IOC_NAME2DEV, buf);
143         if (rc < 0) {
144                 fprintf(stderr, "error: %s: %s - %s\n", cmdname(func),
145                         name, strerror(rc = errno));
146                 return rc;
147         }
148
149         memcpy((char *)(&data), buf, sizeof(data));
150
151         return data.ioc_dev + N2D_OFF;
152 }
153
154 /*
155  * resolve a device name to a device number.
156  * supports a number or name.
157  * FIXME: support UUID
158  */
159 static int parse_devname(char *func, char *name)
160 {
161         int rc;
162         int ret = -1;
163
164         if (!name)
165                 return ret;
166         if (name[0] == '$') {
167                 rc = do_name2dev(func, name + 1);
168                 if (rc >= N2D_OFF) {
169                         ret = rc - N2D_OFF;
170                         printf("%s is device %d\n", name, ret);
171                 } else {
172                         fprintf(stderr, "error: %s: %s: %s\n", cmdname(func),
173                                 name, "device not found");
174                 }
175         } else
176                 ret = strtoul(name, NULL, 0);
177
178         return ret;
179 }
180
181 static char *cmdname(char *func)
182 {
183         static char buf[512];
184
185         if (thread) {
186                 sprintf(buf, "%s-%d", func, thread);
187                 return buf;
188         }
189
190         return func;
191 }
192
193 static int getfd(char *func)
194 {
195         if (fd == -1)
196                 fd = open("/dev/obd", O_RDWR);
197         if (fd == -1) {
198                 fprintf(stderr, "error: %s: opening /dev/obd: %s\n"
199                         "hint: lustre kernel modules may not be loaded.\n",
200                         cmdname(func), strerror(errno));
201                 return -1;
202         }
203         return 0;
204 }
205
206 #define difftime(a, b)                                          \
207         ((double)(a)->tv_sec - (b)->tv_sec +                    \
208          ((double)((a)->tv_usec - (b)->tv_usec) / 1000000))
209
210 static int be_verbose(int verbose, struct timeval *next_time,
211                       int num, int *next_num, int num_total)
212 {
213         struct timeval now;
214
215         if (!verbose)
216                 return 0;
217
218         if (next_time != NULL)
219                 gettimeofday(&now, NULL);
220
221         /* A positive verbosity means to print every X iterations */
222         if (verbose > 0 &&
223             (next_num == NULL || num >= *next_num || num >= num_total)) {
224                 *next_num += verbose;
225                 if (next_time) {
226                         next_time->tv_sec = now.tv_sec - verbose;
227                         next_time->tv_usec = now.tv_usec;
228                 }
229                 return 1;
230         }
231
232         /* A negative verbosity means to print at most each X seconds */
233         if (verbose < 0 && next_time != NULL && difftime(&now, next_time) >= 0){
234                 next_time->tv_sec = now.tv_sec - verbose;
235                 next_time->tv_usec = now.tv_usec;
236                 if (next_num)
237                         *next_num = num;
238                 return 1;
239         }
240
241         return 0;
242 }
243
244 static int get_verbose(char *func, const char *arg)
245 {
246         int verbose;
247         char *end;
248
249         if (!arg || arg[0] == 'v')
250                 verbose = 1;
251         else if (arg[0] == 's' || arg[0] == 'q')
252                 verbose = 0;
253         else {
254                 verbose = (int)strtoul(arg, &end, 0);
255                 if (*end) {
256                         fprintf(stderr, "error: %s: bad verbose option '%s'\n",
257                                 cmdname(func), arg);
258                         return BAD_VERBOSE;
259                 }
260         }
261
262         if (verbose < 0)
263                 printf("Print status every %d seconds\n", -verbose);
264         else if (verbose == 1)
265                 printf("Print status every operation\n");
266         else if (verbose > 1)
267                 printf("Print status every %d operations\n", verbose);
268
269         return verbose;
270 }
271
272 int do_disconnect(char *func, int verbose)
273 {
274         int rc;
275         struct obd_ioctl_data data;
276
277         if (conn_addr == -1)
278                 return 0;
279
280         IOCINIT(data);
281
282         rc = ioctl(fd, OBD_IOC_DISCONNECT, &data);
283         if (rc < 0) {
284                 fprintf(stderr, "error: %s: %x %s\n", cmdname(func),
285                         OBD_IOC_DISCONNECT, strerror(errno));
286         } else {
287                 if (verbose)
288                         printf("%s: disconnected conn %Lx\n", cmdname(func),
289                                conn_addr);
290                 conn_addr = -1;
291         }
292
293         return rc;
294 }
295
296 #if SHMEM_STATS
297 static void shmem_setup(void)
298 {
299         int shmid = shmget(IPC_PRIVATE, sizeof(counter_snapshot[0]), 0600);
300
301         if (shmid == -1) {
302                 fprintf(stderr, "Can't create shared memory counters: %s\n",
303                         strerror(errno));
304                 return;
305         }
306
307         shared_counters = (long long *)shmat(shmid, NULL, 0);
308
309         if (shared_counters == (long long *)(-1)) {
310                 fprintf(stderr, "Can't attach shared memory counters: %s\n",
311                         strerror(errno));
312                 shared_counters = NULL;
313                 return;
314         }
315 }
316
317 static inline void shmem_reset(void)
318 {
319         if (shared_counters == NULL)
320                 return;
321
322         memset(shared_counters, 0, sizeof(counter_snapshot[0]));
323         memset(counter_snapshot, 0, sizeof(counter_snapshot));
324         gettimeofday(&prev_time, NULL);
325 }
326
327 static inline void shmem_bump(void)
328 {
329         if (shared_counters == NULL || thread <= 0 || thread > MAX_SHMEM_COUNT)
330                 return;
331
332         shared_counters[thread - 1]++;
333 }
334
335 static void shmem_snap(int n)
336 {
337         struct timeval this_time;
338         int non_zero = 0;
339         long long total = 0;
340         double secs;
341         int i;
342
343         if (shared_counters == NULL || n > MAX_SHMEM_COUNT)
344                 return;
345
346         memcpy(counter_snapshot[1], counter_snapshot[0],
347                n * sizeof(counter_snapshot[0][0]));
348         memcpy(counter_snapshot[0], shared_counters,
349                n * sizeof(counter_snapshot[0][0]));
350         gettimeofday(&this_time, NULL);
351
352         for (i = 0; i < n; i++) {
353                 long long this_count =
354                         counter_snapshot[0][i] - counter_snapshot[1][i];
355
356                 if (this_count != 0) {
357                         non_zero++;
358                         total += this_count;
359                 }
360         }
361
362         secs = (this_time.tv_sec + this_time.tv_usec / 1000000.0) -
363                 (prev_time.tv_sec + prev_time.tv_usec / 1000000.0);
364
365         printf("%d/%d Total: %f/second\n", non_zero, n, total / secs);
366
367         prev_time = this_time;
368 }
369
370 #define SHMEM_SETUP()   shmem_setup()
371 #define SHMEM_RESET()   shmem_reset()
372 #define SHMEM_BUMP()    shmem_bump()
373 #define SHMEM_SNAP(n)   shmem_snap(n)
374 #else
375 #define SHMEM_SETUP()
376 #define SHMEM_RESET()
377 #define SHMEM_BUMP()
378 #define SHMEM_SNAP(n)
379 #endif
380
381 extern command_t cmdlist[];
382
383 static int do_device(char *func, int dev)
384 {
385         struct obd_ioctl_data data;
386
387         memset(&data, 0, sizeof(data));
388
389         data.ioc_dev = dev;
390
391         if (getfd(func))
392                 return -1;
393
394         if (obd_ioctl_pack(&data, &buf, max)) {
395                 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(func));
396                 return -2;
397         }
398
399         return ioctl(fd, OBD_IOC_DEVICE, buf);
400 }
401
402 int jt_obd_device(int argc, char **argv)
403 {
404         int rc, dev;
405         do_disconnect(argv[0], 1);
406
407         if (argc != 2)
408                 return CMD_HELP;
409
410         dev = parse_devname(argv[0], argv[1]);
411         if (dev < 0)
412                 return -1;
413
414         rc = do_device(argv[0], dev);
415         if (rc < 0)
416                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
417                         strerror(rc = errno));
418
419         return rc;
420 }
421
422 int jt_obd_connect(int argc, char **argv)
423 {
424         struct obd_ioctl_data data;
425         int rc;
426
427         IOCINIT(data);
428
429         do_disconnect(argv[0], 1);
430
431 #warning TODO: implement timeout per lctl usage for probe
432         if (argc != 1)
433                 return CMD_HELP;
434
435         rc = ioctl(fd, OBD_IOC_CONNECT, &data);
436         if (rc < 0)
437                 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
438                         OBD_IOC_CONNECT, strerror(rc = errno));
439         else {
440                 conn_addr = data.ioc_addr;
441                 conn_cookie = data.ioc_cookie;
442         }
443         return rc;
444 }
445
446 int jt_obd_disconnect(int argc, char **argv)
447 {
448         if (argc != 1)
449                 return CMD_HELP;
450
451         if (conn_addr == -1)
452                 return 0;
453
454         return do_disconnect(argv[0], 0);
455 }
456
457 int jt_opt_device(int argc, char **argv)
458 {
459         char *arg2[3];
460         int ret;
461         int rc;
462
463         if (argc < 3)
464                 return CMD_HELP;
465
466         rc = do_device("device", parse_devname(argv[0], argv[1]));
467
468         if (!rc) {
469                 arg2[0] = "connect";
470                 arg2[1] = NULL;
471                 rc = jt_obd_connect(1, arg2);
472         }
473
474         if (!rc)
475                 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
476
477         ret = do_disconnect(argv[0], 0);
478         if (!rc)
479                 rc = ret;
480
481         return rc;
482 }
483
484 int jt_opt_threads(int argc, char **argv)
485 {
486         int threads, next_thread;
487         int verbose;
488         int rc = 0;
489         char *end;
490         int i;
491
492         if (argc < 5)
493                 return CMD_HELP;
494
495         threads = strtoul(argv[1], &end, 0);
496         if (*end) {
497                 fprintf(stderr, "error: %s: invalid page count '%s'\n",
498                         cmdname(argv[0]), argv[1]);
499                 return CMD_HELP;
500         }
501
502         verbose = get_verbose(argv[0], argv[2]);
503         if (verbose == BAD_VERBOSE)
504                 return CMD_HELP;
505
506         if (verbose != 0)
507                 printf("%s: starting %d threads on device %s running %s\n",
508                        argv[0], threads, argv[3], argv[4]);
509
510         SHMEM_RESET();
511
512         for (i = 1, next_thread = verbose; i <= threads; i++) {
513                 rc = fork();
514                 if (rc < 0) {
515                         fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
516                                 strerror(rc = errno));
517                         break;
518                 } else if (rc == 0) {
519                         thread = i;
520                         argv[2] = "--device";
521                         return jt_opt_device(argc - 2, argv + 2);
522                 } else if (be_verbose(verbose, NULL, i, &next_thread, threads))
523                         printf("%s: thread #%d (PID %d) started\n",
524                                argv[0], i, rc);
525                 rc = 0;
526         }
527
528         if (!thread) {          /* parent process */
529                 int live_threads = threads;
530
531                 while (live_threads > 0) {
532                         int status;
533                         pid_t ret;
534
535                         ret = waitpid(0, &status, verbose < 0 ? WNOHANG : 0);
536                         if (ret == 0) {
537                                 if (verbose >= 0)
538                                         abort();
539
540                                 sleep(-verbose);
541                                 SHMEM_SNAP(threads);
542                                 continue;
543                         }
544
545                         if (ret < 0) {
546                                 fprintf(stderr, "error: %s: wait - %s\n",
547                                         argv[0], strerror(errno));
548                                 if (!rc)
549                                         rc = errno;
550                         } else {
551                                 /*
552                                  * This is a hack.  We _should_ be able to use
553                                  * WIFEXITED(status) to see if there was an
554                                  * error, but it appears to be broken and it
555                                  * always returns 1 (OK).  See wait(2).
556                                  */
557                                 int err = WEXITSTATUS(status);
558                                 if (err || WIFSIGNALED(status))
559                                         fprintf(stderr,
560                                                 "%s: PID %d had rc=%d\n",
561                                                 argv[0], ret, err);
562                                 if (!rc)
563                                         rc = err;
564
565                                 live_threads--;
566                         }
567                 }
568         }
569
570         return rc;
571 }
572
573 int jt_obd_detach(int argc, char **argv)
574 {
575         struct obd_ioctl_data data;
576         int rc;
577
578         IOCINIT(data);
579
580         if (argc != 1)
581                 return CMD_HELP;
582
583         if (obd_ioctl_pack(&data, &buf, max)) {
584                 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
585                 return -2;
586         }
587
588         rc = ioctl(fd, OBD_IOC_DETACH, buf);
589         if (rc < 0)
590                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
591                         strerror(rc = errno));
592
593         return rc;
594 }
595
596 int jt_obd_cleanup(int argc, char **argv)
597 {
598         struct obd_ioctl_data data;
599         int rc;
600
601         IOCINIT(data);
602
603         if (argc != 1)
604                 return CMD_HELP;
605
606         rc = ioctl(fd, OBD_IOC_CLEANUP, &data);
607         if (rc < 0)
608                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
609                         strerror(rc = errno));
610
611         return rc;
612 }
613
614 int jt_obd_newdev(int argc, char **argv)
615 {
616         int rc;
617         struct obd_ioctl_data data;
618
619         if (getfd(argv[0]))
620                 return -1;
621
622         IOCINIT(data);
623
624         if (argc != 1)
625                 return CMD_HELP;
626
627         rc = ioctl(fd, OBD_IOC_NEWDEV, &data);
628         if (rc < 0)
629                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
630                         strerror(rc = errno));
631         else {
632                 printf("Current device set to %d\n", data.ioc_dev);
633         }
634
635         return rc;
636 }
637
638 int jt_obd_list(int argc, char **argv)
639 {
640         int rc;
641         char buf[1024];
642         struct obd_ioctl_data *data = (struct obd_ioctl_data *)buf;
643
644         if (getfd(argv[0]))
645                 return -1;
646
647         memset(buf, 0, sizeof(buf));
648         data->ioc_version = OBD_IOCTL_VERSION;
649         data->ioc_addr = conn_addr;
650         data->ioc_cookie = conn_addr;
651         data->ioc_len = sizeof(buf);
652         data->ioc_inllen1 = sizeof(buf) - size_round(sizeof(*data));
653
654         if (argc != 1)
655                 return CMD_HELP;
656
657         rc = ioctl(fd, OBD_IOC_LIST, data);
658         if (rc < 0)
659                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
660                         strerror(rc = errno));
661         else {
662                 printf("%s", data->ioc_bulk);
663         }
664
665         return rc;
666 }
667
668 int jt_obd_attach(int argc, char **argv)
669 {
670         struct obd_ioctl_data data;
671         int rc;
672
673         IOCINIT(data);
674
675         if (argc != 2 && argc != 3 && argc != 4)
676                 return CMD_HELP;
677
678         data.ioc_inllen1 = strlen(argv[1]) + 1;
679         data.ioc_inlbuf1 = argv[1];
680         if (argc >= 3) {
681                 data.ioc_inllen2 = strlen(argv[2]) + 1;
682                 data.ioc_inlbuf2 = argv[2];
683         }
684
685         if (argc == 4) {
686                 data.ioc_inllen3 = strlen(argv[3]) + 1;
687                 data.ioc_inlbuf3 = argv[3];
688         }
689
690         if (obd_ioctl_pack(&data, &buf, max)) {
691                 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
692                 return -2;
693         }
694
695         rc = ioctl(fd, OBD_IOC_ATTACH, buf);
696         if (rc < 0)
697                 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
698                         OBD_IOC_ATTACH, strerror(rc = errno));
699         else if (argc == 3) {
700                 char name[1024];
701                 if (strlen(argv[2]) > 128) {
702                         printf("Name too long to set environment\n");
703                         return -EINVAL;
704                 }
705                 snprintf(name, 512, "LUSTRE_DEV_%s", argv[2]);
706                 rc = setenv(name, argv[1], 1);
707                 if (rc) {
708                         printf("error setting env variable %s\n", name);
709                 }
710         }
711
712         return rc;
713 }
714
715 int jt_obd_name2dev(int argc, char **argv)
716 {
717         int rc;
718
719         if (argc != 2)
720                 return CMD_HELP;
721
722         rc = do_name2dev(argv[0], argv[1]);
723         if (rc >= N2D_OFF) {
724                 int dev = rc - N2D_OFF;
725                 rc = do_device(argv[0], dev);
726                 if (rc == 0)
727                         printf("%d\n", dev);
728         }
729         return rc;
730 }
731
732 int jt_obd_setup(int argc, char **argv)
733 {
734         struct obd_ioctl_data data;
735         int rc;
736
737         IOCINIT(data);
738
739         if (argc > 3)
740                 return CMD_HELP;
741
742         data.ioc_dev = -1;
743         if (argc > 1) {
744                 data.ioc_dev = parse_devname(argv[0], argv[1]);
745                 if (data.ioc_dev < 0)
746                         return -1;
747                 data.ioc_inllen1 = strlen(argv[1]) + 1;
748                 data.ioc_inlbuf1 = argv[1];
749         }
750         if (argc == 3) {
751                 data.ioc_inllen2 = strlen(argv[2]) + 1;
752                 data.ioc_inlbuf2 = argv[2];
753         }
754
755         if (obd_ioctl_pack(&data, &buf, max)) {
756                 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
757                 return -2;
758         }
759         rc = ioctl(fd, OBD_IOC_SETUP, buf);
760         if (rc < 0)
761                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
762                         strerror(rc = errno));
763
764         return rc;
765 }
766
767
768 int jt_obd_create(int argc, char **argv)
769 {
770         struct obd_ioctl_data data;
771         struct timeval next_time;
772         int count = 1, next_count;
773         int verbose = 1;
774         int mode = 0100644;
775         int rc = 0, i;
776         char *end;
777
778         IOCINIT(data);
779         if (argc < 2 || argc > 4)
780                 return CMD_HELP;
781
782         count = strtoul(argv[1], &end, 0);
783         if (*end) {
784                 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
785                         cmdname(argv[0]), argv[1]);
786                 return CMD_HELP;
787         }
788
789         if (argc > 2) {
790                 mode = strtoul(argv[2], &end, 0);
791                 if (*end) {
792                         fprintf(stderr, "error: %s: invalid mode '%s'\n",
793                                 cmdname(argv[0]), argv[2]);
794                         return CMD_HELP;
795                 }
796         }
797
798         if (argc > 3) {
799                 verbose = get_verbose(argv[0], argv[3]);
800                 if (verbose == BAD_VERBOSE)
801                         return CMD_HELP;
802         }
803
804         printf("%s: %d objects\n", cmdname(argv[0]), count);
805         gettimeofday(&next_time, NULL);
806         next_time.tv_sec -= verbose;
807
808         for (i = 1, next_count = verbose; i <= count; i++) {
809                 data.ioc_obdo1.o_mode = mode;
810                 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
811
812                 rc = ioctl(fd, OBD_IOC_CREATE, &data);
813                 SHMEM_BUMP();
814                 if (rc < 0) {
815                         fprintf(stderr, "error: %s: #%d - %s\n",
816                                 cmdname(argv[0]), i, strerror(rc = errno));
817                         break;
818                 }
819                 if (!(data.ioc_obdo1.o_valid & OBD_MD_FLID)) {
820                         fprintf(stderr, "error: %s: objid not valid #%d:%08x\n",
821                                 cmdname(argv[0]), i, data.ioc_obdo1.o_valid);
822                         rc = EINVAL;
823                         break;
824                 }
825
826                 if (be_verbose(verbose, &next_time, i, &next_count, count))
827                         printf("%s: #%d is object id %Ld\n", cmdname(argv[0]),
828                                i, (long long)data.ioc_obdo1.o_id);
829         }
830         return rc;
831 }
832
833 int jt_obd_setattr(int argc, char **argv)
834 {
835         struct obd_ioctl_data data;
836         char *end;
837         int rc;
838
839         IOCINIT(data);
840         if (argc != 2)
841                 return CMD_HELP;
842
843         data.ioc_obdo1.o_id = strtoull(argv[1], &end, 0);
844         if (*end) {
845                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
846                         cmdname(argv[0]), argv[1]);
847                 return CMD_HELP;
848         }
849         data.ioc_obdo1.o_mode = S_IFREG | strtoul(argv[2], &end, 0);
850         if (*end) {
851                 fprintf(stderr, "error: %s: invalid mode '%s'\n",
852                         cmdname(argv[0]), argv[2]);
853                 return CMD_HELP;
854         }
855         data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLMODE;
856
857         rc = ioctl(fd, OBD_IOC_SETATTR, &data);
858         if (rc < 0)
859                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
860                         strerror(rc = errno));
861
862         return rc;
863 }
864
865 int jt_obd_destroy(int argc, char **argv)
866 {
867         struct obd_ioctl_data data;
868         char *end;
869         int rc;
870
871         IOCINIT(data);
872         if (argc != 2)
873                 return CMD_HELP;
874
875         data.ioc_obdo1.o_id = strtoull(argv[1], &end, 0);
876         if (*end) {
877                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
878                         cmdname(argv[0]), argv[1]);
879                 return CMD_HELP;
880         }
881         data.ioc_obdo1.o_mode = S_IFREG | 0644;
882
883         rc = ioctl(fd, OBD_IOC_DESTROY, &data);
884         if (rc < 0)
885                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
886                         strerror(rc = errno));
887
888         return rc;
889 }
890
891 int jt_obd_getattr(int argc, char **argv)
892 {
893         struct obd_ioctl_data data;
894         char *end;
895         int rc;
896
897         if (argc != 2)
898                 return CMD_HELP;
899
900         IOCINIT(data);
901         data.ioc_obdo1.o_id = strtoul(argv[1], &end, 0);
902         if (*end) {
903                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
904                         cmdname(argv[0]), argv[1]);
905                 return CMD_HELP;
906         }
907         /* to help obd filter */
908         data.ioc_obdo1.o_mode = 0100644;
909         data.ioc_obdo1.o_valid = 0xffffffff;
910         printf("%s: object id %Ld\n", cmdname(argv[0]), data.ioc_obdo1.o_id);
911
912         rc = ioctl(fd, OBD_IOC_GETATTR, &data);
913         if (rc) {
914                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
915                         strerror(rc = errno));
916         } else {
917                 printf("%s: object id %Ld, mode %o\n", cmdname(argv[0]),
918                        data.ioc_obdo1.o_id, data.ioc_obdo1.o_mode);
919         }
920         return rc;
921 }
922
923 int jt_obd_test_getattr(int argc, char **argv)
924 {
925         struct obd_ioctl_data data;
926         struct timeval start, next_time;
927         int i, count, next_count;
928         int verbose = 1;
929         obd_id id = 3;
930         char *end;
931         int rc = 0;
932
933         if (argc < 2 && argc > 4)
934                 return CMD_HELP;
935
936         IOCINIT(data);
937         count = strtoul(argv[1], &end, 0);
938         if (*end) {
939                 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
940                         cmdname(argv[0]), argv[1]);
941                 return CMD_HELP;
942         }
943
944         if (argc >= 3) {
945                 verbose = get_verbose(argv[0], argv[2]);
946                 if (verbose == BAD_VERBOSE)
947                         return CMD_HELP;
948         }
949
950         if (argc >= 4) {
951                 id = strtoull(argv[3], &end, 0);
952                 if (*end) {
953                         fprintf(stderr, "error: %s: invalid objid '%s'\n",
954                                 cmdname(argv[0]), argv[3]);
955                         return CMD_HELP;
956                 }
957         }
958
959         gettimeofday(&start, NULL);
960         next_time.tv_sec = start.tv_sec - verbose;
961         next_time.tv_usec = start.tv_usec;
962         if (verbose != 0)
963                 printf("%s: getting %d attrs (testing only): %s",
964                        cmdname(argv[0]), count, ctime(&start.tv_sec));
965
966         for (i = 1, next_count = verbose; i <= count; i++) {
967                 data.ioc_obdo1.o_id = id;
968                 data.ioc_obdo1.o_valid = 0xffffffff;
969                 rc = ioctl(fd, OBD_IOC_GETATTR, &data);
970                 SHMEM_BUMP();
971                 if (rc < 0) {
972                         fprintf(stderr, "error: %s: #%d - %s\n",
973                                 cmdname(argv[0]), i, strerror(rc = errno));
974                         break;
975                 } else {
976                         if (be_verbose
977                             (verbose, &next_time, i, &next_count, count))
978                                 printf("%s: got attr #%d\n", cmdname(argv[0]),
979                                        i);
980                 }
981         }
982
983         if (!rc) {
984                 struct timeval end;
985                 double diff;
986
987                 gettimeofday(&end, NULL);
988
989                 diff = difftime(&end, &start);
990
991                 --i;
992                 if (verbose != 0)
993                         printf("%s: %d attrs in %.4gs (%.4g attr/s): %s",
994                                cmdname(argv[0]), i, diff, (double)i / diff,
995                                ctime(&end.tv_sec));
996         }
997         return rc;
998 }
999
1000 int jt_obd_test_brw(int argc, char **argv)
1001 {
1002         struct obd_ioctl_data data;
1003         struct timeval start, next_time;
1004         int pages = 1, objid = 3, count, next_count;
1005         int verbose = 1, write = 0, rw;
1006         char *end;
1007         int i;
1008         int len;
1009         int rc = 0;
1010
1011         if (argc < 2 || argc > 6) {
1012                 fprintf(stderr, "error: %s: bad number of arguments: %d\n",
1013                         cmdname(argv[0]), argc);
1014                 return CMD_HELP;
1015         }
1016
1017         count = strtoul(argv[1], &end, 0);
1018         if (*end) {
1019                 fprintf(stderr, "error: %s: bad iteration count '%s'\n",
1020                         cmdname(argv[0]), argv[1]);
1021                 return CMD_HELP;
1022         }
1023
1024         if (argc >= 3) {
1025                 if (argv[2][0] == 'w' || argv[2][0] == '1')
1026                         write = 1;
1027                 else if (argv[2][0] == 'r' || argv[2][0] == '0')
1028                         write = 0;
1029         }
1030
1031         if (argc >= 4) {
1032                 verbose = get_verbose(argv[0], argv[3]);
1033                 if (verbose == BAD_VERBOSE)
1034                         return CMD_HELP;
1035         }
1036
1037         if (argc >= 5) {
1038                 pages = strtoul(argv[4], &end, 0);
1039                 if (*end) {
1040                         fprintf(stderr, "error: %s: bad page count '%s'\n",
1041                                 cmdname(argv[0]), argv[4]);
1042                         return CMD_HELP;
1043                 }
1044         }
1045         if (argc >= 6) {
1046                 if (argv[5][0] == 't')
1047                         objid = strtoul(argv[5] + 1, &end, 0) + thread;
1048                 else
1049                         objid = strtoull(argv[5], &end, 0);
1050                 if (*end) {
1051                         fprintf(stderr, "error: %s: bad objid '%s'\n",
1052                                 cmdname(argv[0]), argv[5]);
1053                         return CMD_HELP;
1054                 }
1055         }
1056
1057         len = pages * PAGE_SIZE;
1058
1059         IOCINIT(data);
1060         data.ioc_obdo1.o_id = objid;
1061         data.ioc_count = len;
1062         data.ioc_offset = 0;
1063
1064         gettimeofday(&start, NULL);
1065         next_time.tv_sec = start.tv_sec - verbose;
1066         next_time.tv_usec = start.tv_usec;
1067
1068         if (verbose != 0)
1069                 printf("%s: %s %dx%d pages (testing only): %s",
1070                        cmdname(argv[0]), write ? "writing" : "reading",
1071                        count, pages, ctime(&start.tv_sec));
1072
1073         rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
1074         for (i = 1, next_count = verbose; i <= count; i++) {
1075                 rc = ioctl(fd, rw, &data);
1076                 SHMEM_BUMP();
1077                 if (rc) {
1078                         fprintf(stderr, "error: %s: #%d - %s on %s\n",
1079                                 cmdname(argv[0]), i, strerror(rc = errno),
1080                                 write ? "write" : "read");
1081                         break;
1082                 } else if (be_verbose
1083                            (verbose, &next_time, i, &next_count, count))
1084                         printf("%s: %s number %d\n", cmdname(argv[0]),
1085                                write ? "write" : "read", i);
1086
1087                 data.ioc_offset += len;
1088         }
1089
1090         if (!rc) {
1091                 struct timeval end;
1092                 double diff;
1093
1094                 gettimeofday(&end, NULL);
1095
1096                 diff = difftime(&end, &start);
1097
1098                 --i;
1099                 if (verbose != 0)
1100                         printf("%s: %s %dx%d pages in %.4gs (%.4g pg/s): %s",
1101                                cmdname(argv[0]), write ? "wrote" : "read",
1102                                i, pages, diff, (double)i * pages / diff,
1103                                ctime(&end.tv_sec));
1104         }
1105         return rc;
1106 }
1107
1108 int jt_obd_lov_config(int argc, char **argv)
1109 {
1110         struct obd_ioctl_data data;
1111         struct lov_desc desc;
1112         uuid_t *uuidarray;
1113         int rc, size, i;
1114         char *end;
1115
1116         IOCINIT(data);
1117
1118         if (argc <= 6)
1119                 return CMD_HELP;
1120
1121         if (strlen(argv[1]) > sizeof(*uuidarray) - 1) {
1122                 fprintf(stderr, "error: %s: no %dB memory for uuid's\n",
1123                         cmdname(argv[0]), strlen(argv[1]));
1124                 return -ENOMEM;
1125         }
1126
1127         memset(&desc, 0, sizeof(desc));
1128         strncpy(desc.ld_uuid, argv[1], sizeof(*uuidarray) - 1);
1129         desc.ld_default_stripe_count = strtoul(argv[2], &end, 0);
1130         if (*end) {
1131                 fprintf(stderr, "error: %s: bad default stripe count '%s'\n",
1132                         cmdname(argv[0]), argv[2]);
1133                 return CMD_HELP;
1134         }
1135         desc.ld_default_stripe_size = strtoul(argv[3], &end, 0);
1136         if (*end) {
1137                 fprintf(stderr, "error: %s: bad default stripe size '%s'\n",
1138                         cmdname(argv[0]), argv[3]);
1139                 return CMD_HELP;
1140         }
1141         if (desc.ld_default_stripe_size < 4096) {
1142                 fprintf(stderr, "error: %s: stripe size %ld too small\n",
1143                         cmdname(argv[0]), (long)desc.ld_default_stripe_size);
1144                 return -EINVAL;
1145         }
1146         desc.ld_default_stripe_offset = (__u64) strtoul(argv[4], &end, 0);
1147         if (*end) {
1148                 fprintf(stderr, "error: %s: bad default stripe offset '%s'\n",
1149                         cmdname(argv[0]), argv[4]);
1150                 return CMD_HELP;
1151         }
1152         desc.ld_pattern = strtoul(argv[5], &end, 0);
1153         if (*end) {
1154                 fprintf(stderr, "error: %s: bad stripe pattern '%s'\n",
1155                         cmdname(argv[0]), argv[5]);
1156                 return CMD_HELP;
1157         }
1158         desc.ld_tgt_count = argc - 6;
1159
1160         size = desc.ld_tgt_count * sizeof(*uuidarray);
1161         uuidarray = malloc(size);
1162         if (!uuidarray) {
1163                 fprintf(stderr, "error: %s: no %dB memory for uuid's\n",
1164                         cmdname(argv[0]), size);
1165                 return -ENOMEM;
1166         }
1167         memset(uuidarray, 0, size);
1168         for (i = 6; i < argc; i++) {
1169                 char *buf = (char *)(uuidarray + i - 6);
1170                 if (strlen(argv[i]) >= sizeof(*uuidarray)) {
1171                         fprintf(stderr, "error: %s: arg %d (%s) too long\n",
1172                                 cmdname(argv[0]), i, argv[i]);
1173                         free(uuidarray);
1174                         return -EINVAL;
1175                 }
1176                 strcpy(buf, argv[i]);
1177         }
1178
1179         data.ioc_inllen1 = sizeof(desc);
1180         data.ioc_inlbuf1 = (char *)&desc;
1181         data.ioc_inllen2 = size;
1182         data.ioc_inlbuf2 = (char *)uuidarray;
1183
1184         if (obd_ioctl_pack(&data, &buf, max)) {
1185                 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1186                 return -EINVAL;
1187         }
1188
1189         rc = ioctl(fd, OBD_IOC_LOV_CONFIG, buf);
1190         if (rc)
1191                 fprintf(stderr, "lov_config: error: %s: %s\n",
1192                         cmdname(argv[0]), strerror(rc = errno));
1193         free(uuidarray);
1194         return rc;
1195 }
1196
1197 int jt_obd_test_ldlm(int argc, char **argv)
1198 {
1199         struct obd_ioctl_data data;
1200         int rc;
1201
1202         IOCINIT(data);
1203         if (argc != 1)
1204                 return CMD_HELP;
1205
1206         rc = ioctl(fd, IOC_LDLM_TEST, &data);
1207         if (rc)
1208                 fprintf(stderr, "error: %s: test failed: %s\n",
1209                         cmdname(argv[0]), strerror(rc = errno));
1210         return rc;
1211 }
1212
1213 int jt_obd_dump_ldlm(int argc, char **argv)
1214 {
1215         struct obd_ioctl_data data;
1216         int rc;
1217
1218         IOCINIT(data);
1219         if (argc != 1)
1220                 return CMD_HELP;
1221
1222         rc = ioctl(fd, IOC_LDLM_DUMP, &data);
1223         if (rc)
1224                 fprintf(stderr, "error: %s failed: %s\n",
1225                         cmdname(argv[0]), strerror(rc = errno));
1226         return rc;
1227 }
1228
1229 int jt_obd_ldlm_regress_start(int argc, char **argv)
1230 {
1231         int rc;
1232         struct obd_ioctl_data data;
1233         char argstring[200];
1234         int i, count = sizeof(argstring) - 1;
1235
1236         IOCINIT(data);
1237         if (argc > 5)
1238                 return CMD_HELP;
1239
1240         argstring[0] = '\0';
1241         for (i = 1; i < argc; i++) {
1242                 strncat(argstring, " ", count);
1243                 count--;
1244                 strncat(argstring, argv[i], count);
1245                 count -= strlen(argv[i]);
1246         }
1247
1248         if (strlen(argstring)) {
1249                 data.ioc_inlbuf1 = argstring;
1250                 data.ioc_inllen1 = strlen(argstring) + 1;
1251         }
1252
1253         if (obd_ioctl_pack(&data, &buf, max)) {
1254                 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1255                 return -2;
1256         }
1257
1258         rc = ioctl(fd, IOC_LDLM_REGRESS_START, buf);
1259
1260         if (rc)
1261                 fprintf(stderr, "error: %s: test failed: %s\n",
1262                         cmdname(argv[0]), strerror(rc = errno));
1263
1264         return rc;
1265 }
1266
1267 int jt_obd_ldlm_regress_stop(int argc, char **argv)
1268 {
1269         int rc;
1270         struct obd_ioctl_data data;
1271         IOCINIT(data);
1272
1273         if (argc != 1)
1274                 return CMD_HELP;
1275
1276         rc = ioctl(fd, IOC_LDLM_REGRESS_STOP, &data);
1277
1278         if (rc)
1279                 fprintf(stderr, "error: %s: test failed: %s\n",
1280                         cmdname(argv[0]), strerror(rc = errno));
1281         return rc;
1282 }
1283
1284 int jt_obd_newconn(int argc, char **argv)
1285 {
1286         int rc;
1287         struct obd_ioctl_data data;
1288
1289         IOCINIT(data);
1290         if (argc < 2 || argc > 3)
1291                 return CMD_HELP;
1292
1293         data.ioc_inllen1 = strlen(argv[1]) + 1;
1294         data.ioc_inlbuf1 = argv[1];
1295
1296         if (argc == 3) {
1297                 data.ioc_inllen2 = strlen(argv[2]) + 1;
1298                 data.ioc_inlbuf2 = argv[2];
1299         }
1300
1301         if (obd_ioctl_pack(&data, &buf, max)) {
1302                 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1303                 return -2;
1304         }
1305
1306         rc = ioctl(fd, OBD_IOC_RECOVD_NEWCONN, buf);
1307         if (rc < 0)
1308                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1309                         strerror(rc = errno));
1310
1311         return rc;
1312 }
1313
1314 static void signal_server(int sig)
1315 {
1316         if (sig == SIGINT) {
1317                 do_disconnect("sigint", 1);
1318                 exit(1);
1319         } else
1320                 fprintf(stderr, "%s: got signal %d\n", cmdname("sigint"), sig);
1321 }
1322
1323 int obd_initialize(int argc, char **argv)
1324 {
1325         SHMEM_SETUP();
1326         return 0;
1327 }
1328
1329
1330 void obd_cleanup(int argc, char **argv)
1331 {
1332         struct sigaction sigact;
1333
1334         sigact.sa_handler = signal_server;
1335         sigfillset(&sigact.sa_mask);
1336         sigact.sa_flags = SA_RESTART;
1337         sigaction(SIGINT, &sigact, NULL);
1338
1339         do_disconnect(argv[0], 1);
1340 }