Whamcloud - gitweb
Remove ancient Perl cruft from obd.c.
[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 rc = 0, i;
775         char *end;
776
777         IOCINIT(data);
778         if (argc < 2 || argc > 4)
779                 return CMD_HELP;
780
781         count = strtoul(argv[1], &end, 0);
782         if (*end) {
783                 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
784                         cmdname(argv[0]), argv[1]);
785                 return CMD_HELP;
786         }
787
788         if (argc > 2) {
789                 data.ioc_obdo1.o_mode = strtoul(argv[2], &end, 0);
790                 if (*end) {
791                         fprintf(stderr, "error: %s: invalid mode '%s'\n",
792                                 cmdname(argv[0]), argv[2]);
793                         return CMD_HELP;
794                 }
795         } else
796                 data.ioc_obdo1.o_mode = 0100644;
797         data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
798
799         if (argc > 3) {
800                 verbose = get_verbose(argv[0], argv[3]);
801                 if (verbose == BAD_VERBOSE)
802                         return CMD_HELP;
803         }
804
805         printf("%s: %d objects\n", cmdname(argv[0]), count);
806         gettimeofday(&next_time, NULL);
807         next_time.tv_sec -= verbose;
808
809         for (i = 1, next_count = verbose; i <= count; i++) {
810                 rc = ioctl(fd, OBD_IOC_CREATE, &data);
811                 SHMEM_BUMP();
812                 if (rc < 0) {
813                         fprintf(stderr, "error: %s: #%d - %s\n",
814                                 cmdname(argv[0]), i, strerror(rc = errno));
815                         break;
816                 }
817                 if (be_verbose(verbose, &next_time, i, &next_count, count))
818                         printf("%s: #%d is object id %Ld\n", cmdname(argv[0]),
819                                i, data.ioc_obdo1.o_id);
820         }
821         return rc;
822 }
823
824 int jt_obd_setattr(int argc, char **argv)
825 {
826         struct obd_ioctl_data data;
827         char *end;
828         int rc;
829
830         IOCINIT(data);
831         if (argc != 2)
832                 return CMD_HELP;
833
834         data.ioc_obdo1.o_id = strtoul(argv[1], &end, 0);
835         if (*end) {
836                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
837                         cmdname(argv[0]), argv[1]);
838                 return CMD_HELP;
839         }
840         data.ioc_obdo1.o_mode = S_IFREG | strtoul(argv[2], &end, 0);
841         if (*end) {
842                 fprintf(stderr, "error: %s: invalid mode '%s'\n",
843                         cmdname(argv[0]), argv[2]);
844                 return CMD_HELP;
845         }
846         data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
847
848         rc = ioctl(fd, OBD_IOC_SETATTR, &data);
849         if (rc < 0)
850                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
851                         strerror(rc = errno));
852
853         return rc;
854 }
855
856 int jt_obd_destroy(int argc, char **argv)
857 {
858         struct obd_ioctl_data data;
859         char *end;
860         int rc;
861
862         IOCINIT(data);
863         if (argc != 2)
864                 return CMD_HELP;
865
866         data.ioc_obdo1.o_id = strtoul(argv[1], &end, 0);
867         if (*end) {
868                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
869                         cmdname(argv[0]), argv[1]);
870                 return CMD_HELP;
871         }
872         data.ioc_obdo1.o_mode = S_IFREG | 0644;
873
874         rc = ioctl(fd, OBD_IOC_DESTROY, &data);
875         if (rc < 0)
876                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
877                         strerror(rc = errno));
878
879         return rc;
880 }
881
882 int jt_obd_getattr(int argc, char **argv)
883 {
884         struct obd_ioctl_data data;
885         char *end;
886         int rc;
887
888         if (argc != 2)
889                 return CMD_HELP;
890
891         IOCINIT(data);
892         data.ioc_obdo1.o_id = strtoul(argv[1], &end, 0);
893         if (*end) {
894                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
895                         cmdname(argv[0]), argv[1]);
896                 return CMD_HELP;
897         }
898         /* to help obd filter */
899         data.ioc_obdo1.o_mode = 0100644;
900         data.ioc_obdo1.o_valid = 0xffffffff;
901         printf("%s: object id %Ld\n", cmdname(argv[0]), data.ioc_obdo1.o_id);
902
903         rc = ioctl(fd, OBD_IOC_GETATTR, &data);
904         if (rc) {
905                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
906                         strerror(rc = errno));
907         } else {
908                 printf("%s: object id %Ld, mode %o\n", cmdname(argv[0]),
909                        data.ioc_obdo1.o_id, data.ioc_obdo1.o_mode);
910         }
911         return rc;
912 }
913
914 int jt_obd_test_getattr(int argc, char **argv)
915 {
916         struct obd_ioctl_data data;
917         struct timeval start, next_time;
918         int i, count, next_count;
919         int verbose;
920         char *end;
921         int rc = 0;
922
923         if (argc != 2 && argc != 3)
924                 return CMD_HELP;
925
926         IOCINIT(data);
927         count = strtoul(argv[1], &end, 0);
928         if (*end) {
929                 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
930                         cmdname(argv[0]), argv[1]);
931                 return CMD_HELP;
932         }
933
934         if (argc == 3) {
935                 verbose = get_verbose(argv[0], argv[2]);
936                 if (verbose == BAD_VERBOSE)
937                         return CMD_HELP;
938         } else
939                 verbose = 1;
940
941         data.ioc_obdo1.o_valid = 0xffffffff;
942         data.ioc_obdo1.o_id = 2;
943         gettimeofday(&start, NULL);
944         next_time.tv_sec = start.tv_sec - verbose;
945         next_time.tv_usec = start.tv_usec;
946         if (verbose != 0)
947                 printf("%s: getting %d attrs (testing only): %s",
948                        cmdname(argv[0]), count, ctime(&start.tv_sec));
949
950         for (i = 1, next_count = verbose; i <= count; i++) {
951                 rc = ioctl(fd, OBD_IOC_GETATTR, &data);
952                 SHMEM_BUMP();
953                 if (rc < 0) {
954                         fprintf(stderr, "error: %s: #%d - %s\n",
955                                 cmdname(argv[0]), i, strerror(rc = errno));
956                         break;
957                 } else {
958                         if (be_verbose
959                             (verbose, &next_time, i, &next_count, count))
960                                 printf("%s: got attr #%d\n", cmdname(argv[0]),
961                                        i);
962                 }
963         }
964
965         if (!rc) {
966                 struct timeval end;
967                 double diff;
968
969                 gettimeofday(&end, NULL);
970
971                 diff = difftime(&end, &start);
972
973                 --i;
974                 if (verbose != 0)
975                         printf("%s: %d attrs in %.4gs (%.4g attr/s): %s",
976                                cmdname(argv[0]), i, diff, (double)i / diff,
977                                ctime(&end.tv_sec));
978         }
979         return rc;
980 }
981
982 int jt_obd_test_brw(int argc, char **argv)
983 {
984         struct obd_ioctl_data data;
985         struct timeval start, next_time;
986         int pages = 1, objid = 3, count, next_count;
987         int verbose = 1, write = 0, rw;
988         long long offset;
989         char *end;
990         int i;
991         int len;
992         int rc = 0;
993
994         if (argc < 2 || argc > 6) {
995                 fprintf(stderr, "error: %s: bad number of arguments: %d\n",
996                         cmdname(argv[0]), argc);
997                 return CMD_HELP;
998         }
999
1000         count = strtoul(argv[1], &end, 0);
1001         if (*end) {
1002                 fprintf(stderr, "error: %s: bad iteration count '%s'\n",
1003                         cmdname(argv[0]), argv[1]);
1004                 return CMD_HELP;
1005         }
1006
1007         if (argc >= 3) {
1008                 if (argv[2][0] == 'w' || argv[2][0] == '1')
1009                         write = 1;
1010                 else if (argv[2][0] == 'r' || argv[2][0] == '0')
1011                         write = 0;
1012
1013                 verbose = get_verbose(argv[0], argv[3]);
1014                 if (verbose == BAD_VERBOSE)
1015                         return CMD_HELP;
1016         }
1017
1018         if (argc >= 5) {
1019                 pages = strtoul(argv[4], &end, 0);
1020                 if (*end) {
1021                         fprintf(stderr, "error: %s: bad page count '%s'\n",
1022                                 cmdname(argv[0]), argv[4]);
1023                         return CMD_HELP;
1024                 }
1025         }
1026         if (argc >= 6) {
1027                 objid = strtoul(argv[5], &end, 0);
1028                 if (*end) {
1029                         fprintf(stderr, "error: %s: bad objid '%s'\n",
1030                                 cmdname(argv[0]), argv[5]);
1031                         return CMD_HELP;
1032                 }
1033         }
1034
1035         len = pages * PAGE_SIZE;
1036
1037         IOCINIT(data);
1038         data.ioc_obdo1.o_id = objid;
1039         data.ioc_count = len;
1040         data.ioc_offset = 0;
1041
1042         gettimeofday(&start, NULL);
1043         next_time.tv_sec = start.tv_sec - verbose;
1044         next_time.tv_usec = start.tv_usec;
1045
1046         if (verbose != 0)
1047                 printf("%s: %s %dx%d pages (testing only): %s",
1048                        cmdname(argv[0]), write ? "writing" : "reading",
1049                        count, pages, ctime(&start.tv_sec));
1050
1051         rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
1052         for (i = 1, next_count = verbose, offset = 0; i <= count; i++) {
1053                 rc = ioctl(fd, rw, &data);
1054                 SHMEM_BUMP();
1055                 if (rc) {
1056                         fprintf(stderr, "error: %s: #%d - %s on %s\n",
1057                                 cmdname(argv[0]), i, strerror(rc = errno),
1058                                 write ? "write" : "read");
1059                         break;
1060                 } else if (be_verbose
1061                            (verbose, &next_time, i, &next_count, count))
1062                         printf("%s: %s number %d\n", cmdname(argv[0]),
1063                                write ? "write" : "read", i);
1064
1065                 data.ioc_offset += len;
1066         }
1067
1068         if (!rc) {
1069                 struct timeval end;
1070                 double diff;
1071
1072                 gettimeofday(&end, NULL);
1073
1074                 diff = difftime(&end, &start);
1075
1076                 --i;
1077                 if (verbose != 0)
1078                         printf("%s: %s %dx%d pages in %.4gs (%.4g pg/s): %s",
1079                                cmdname(argv[0]), write ? "wrote" : "read",
1080                                i, pages, diff, (double)i * pages / diff,
1081                                ctime(&end.tv_sec));
1082         }
1083         return rc;
1084 }
1085
1086 int jt_obd_lov_config(int argc, char **argv)
1087 {
1088         struct obd_ioctl_data data;
1089         struct lov_desc desc;
1090         uuid_t *uuidarray;
1091         int rc, size, i;
1092         char *end;
1093
1094         IOCINIT(data);
1095
1096         if (argc <= 6)
1097                 return CMD_HELP;
1098
1099         if (strlen(argv[1]) > sizeof(*uuidarray) - 1) {
1100                 fprintf(stderr, "error: %s: no %dB memory for uuid's\n",
1101                         cmdname(argv[0]), strlen(argv[1]));
1102                 return -ENOMEM;
1103         }
1104
1105         memset(&desc, 0, sizeof(desc));
1106         strncpy(desc.ld_uuid, argv[1], sizeof(*uuidarray) - 1);
1107         desc.ld_default_stripe_count = strtoul(argv[2], &end, 0);
1108         if (*end) {
1109                 fprintf(stderr, "error: %s: bad default stripe count '%s'\n",
1110                         cmdname(argv[0]), argv[2]);
1111                 return CMD_HELP;
1112         }
1113         desc.ld_default_stripe_size = strtoul(argv[3], &end, 0);
1114         if (*end) {
1115                 fprintf(stderr, "error: %s: bad default stripe size '%s'\n",
1116                         cmdname(argv[0]), argv[3]);
1117                 return CMD_HELP;
1118         }
1119         if (desc.ld_default_stripe_size < 4096) {
1120                 fprintf(stderr, "error: %s: stripe size %ld too small\n",
1121                         cmdname(argv[0]), (long)desc.ld_default_stripe_size);
1122                 return -EINVAL;
1123         }
1124         desc.ld_default_stripe_offset = (__u64) strtoul(argv[4], &end, 0);
1125         if (*end) {
1126                 fprintf(stderr, "error: %s: bad default stripe offset '%s'\n",
1127                         cmdname(argv[0]), argv[4]);
1128                 return CMD_HELP;
1129         }
1130         desc.ld_pattern = strtoul(argv[5], &end, 0);
1131         if (*end) {
1132                 fprintf(stderr, "error: %s: bad stripe pattern '%s'\n",
1133                         cmdname(argv[0]), argv[5]);
1134                 return CMD_HELP;
1135         }
1136         desc.ld_tgt_count = argc - 6;
1137
1138         size = desc.ld_tgt_count * sizeof(*uuidarray);
1139         uuidarray = malloc(size);
1140         if (!uuidarray) {
1141                 fprintf(stderr, "error: %s: no %dB memory for uuid's\n",
1142                         cmdname(argv[0]), size);
1143                 return -ENOMEM;
1144         }
1145         memset(uuidarray, 0, size);
1146         for (i = 6; i < argc; i++) {
1147                 char *buf = (char *)(uuidarray + i - 6);
1148                 if (strlen(argv[i]) >= sizeof(*uuidarray)) {
1149                         fprintf(stderr, "error: %s: arg %d (%s) too long\n",
1150                                 cmdname(argv[0]), i, argv[i]);
1151                         free(uuidarray);
1152                         return -EINVAL;
1153                 }
1154                 strcpy(buf, argv[i]);
1155         }
1156
1157         data.ioc_inllen1 = sizeof(desc);
1158         data.ioc_inlbuf1 = (char *)&desc;
1159         data.ioc_inllen2 = size;
1160         data.ioc_inlbuf2 = (char *)uuidarray;
1161
1162         if (obd_ioctl_pack(&data, &buf, max)) {
1163                 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1164                 return -EINVAL;
1165         }
1166
1167         rc = ioctl(fd, OBD_IOC_LOV_CONFIG, buf);
1168         if (rc)
1169                 fprintf(stderr, "lov_config: error: %s: %s\n",
1170                         cmdname(argv[0]), strerror(rc = errno));
1171         free(uuidarray);
1172         return rc;
1173 }
1174
1175 int jt_obd_test_ldlm(int argc, char **argv)
1176 {
1177         struct obd_ioctl_data data;
1178         int rc;
1179
1180         IOCINIT(data);
1181         if (argc != 1)
1182                 return CMD_HELP;
1183
1184         rc = ioctl(fd, IOC_LDLM_TEST, &data);
1185         if (rc)
1186                 fprintf(stderr, "error: %s: test failed: %s\n",
1187                         cmdname(argv[0]), strerror(rc = errno));
1188         return rc;
1189 }
1190
1191 int jt_obd_dump_ldlm(int argc, char **argv)
1192 {
1193         struct obd_ioctl_data data;
1194         int rc;
1195
1196         IOCINIT(data);
1197         if (argc != 1)
1198                 return CMD_HELP;
1199
1200         rc = ioctl(fd, IOC_LDLM_DUMP, &data);
1201         if (rc)
1202                 fprintf(stderr, "error: %s failed: %s\n",
1203                         cmdname(argv[0]), strerror(rc = errno));
1204         return rc;
1205 }
1206
1207 int jt_obd_ldlm_regress_start(int argc, char **argv)
1208 {
1209         int rc;
1210         struct obd_ioctl_data data;
1211         char argstring[200];
1212         int i, count = sizeof(argstring) - 1;
1213
1214         IOCINIT(data);
1215         if (argc > 5)
1216                 return CMD_HELP;
1217
1218         argstring[0] = '\0';
1219         for (i = 1; i < argc; i++) {
1220                 strncat(argstring, " ", count);
1221                 count--;
1222                 strncat(argstring, argv[i], count);
1223                 count -= strlen(argv[i]);
1224         }
1225
1226         if (strlen(argstring)) {
1227                 data.ioc_inlbuf1 = argstring;
1228                 data.ioc_inllen1 = strlen(argstring) + 1;
1229         }
1230
1231         if (obd_ioctl_pack(&data, &buf, max)) {
1232                 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1233                 return -2;
1234         }
1235
1236         rc = ioctl(fd, IOC_LDLM_REGRESS_START, buf);
1237
1238         if (rc)
1239                 fprintf(stderr, "error: %s: test failed: %s\n",
1240                         cmdname(argv[0]), strerror(rc = errno));
1241
1242         return rc;
1243 }
1244
1245 int jt_obd_ldlm_regress_stop(int argc, char **argv)
1246 {
1247         int rc;
1248         struct obd_ioctl_data data;
1249         IOCINIT(data);
1250
1251         if (argc != 1)
1252                 return CMD_HELP;
1253
1254         rc = ioctl(fd, IOC_LDLM_REGRESS_STOP, &data);
1255
1256         if (rc)
1257                 fprintf(stderr, "error: %s: test failed: %s\n",
1258                         cmdname(argv[0]), strerror(rc = errno));
1259         return rc;
1260 }
1261
1262 int jt_obd_newconn(int argc, char **argv)
1263 {
1264         int rc;
1265         struct obd_ioctl_data data;
1266
1267         IOCINIT(data);
1268         if (argc != 1)
1269                 return CMD_HELP;
1270
1271         rc = ioctl(fd, OBD_IOC_RECOVD_NEWCONN, &data);
1272         if (rc < 0)
1273                 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1274                         strerror(rc = errno));
1275
1276         return rc;
1277 }
1278
1279 static void signal_server(int sig)
1280 {
1281         if (sig == SIGINT) {
1282                 do_disconnect("sigint", 1);
1283                 exit(1);
1284         } else
1285                 fprintf(stderr, "%s: got signal %d\n", cmdname("sigint"), sig);
1286 }
1287
1288 int obd_initialize(int argc, char **argv)
1289 {
1290         SHMEM_SETUP();
1291         return 0;
1292 }
1293
1294
1295 void obd_cleanup(int argc, char **argv)
1296 {
1297         struct sigaction sigact;
1298
1299         sigact.sa_handler = signal_server;
1300         sigfillset(&sigact.sa_mask);
1301         sigact.sa_flags = SA_RESTART;
1302         sigaction(SIGINT, &sigact, NULL);
1303
1304         do_disconnect(argv[0], 1);
1305 }