Whamcloud - gitweb
LU-1889 build: fix false 'uninitialized scalar variable' errs
[fs/lustre-release.git] / lustre / utils / obd.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/utils/obd.c
37  *
38  * Author: Peter J. Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  * Author: Andreas Dilger <adilger@clusterfs.com>
41  * Author: Robert Read <rread@clusterfs.com>
42  */
43
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48 #include <sys/types.h>
49 #include <sys/un.h>
50 #include <sys/wait.h>
51
52 #include <ctype.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <glob.h>
56 #include <signal.h>
57 #include <stdarg.h>
58 #include <stdbool.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <time.h>
63 #include <unistd.h>
64
65 #include "obdctl.h"
66
67 #include <obd.h>          /* for struct lov_stripe_md */
68 #include <lustre/lustre_build_version.h>
69
70 #include <lnet/lnetctl.h>
71 #include <libcfs/libcfsutil.h>
72 #include <lustre/lustreapi.h>
73
74 #define MAX_STRING_SIZE 128
75 #define DEVICES_LIST "/proc/fs/lustre/devices"
76
77 #if HAVE_LIBPTHREAD
78 #include <sys/ipc.h>
79 #include <sys/shm.h>
80 #include <pthread.h>
81
82 #define MAX_THREADS 4096
83 #define MAX_BASE_ID 0xffffffff
84 struct shared_data {
85         l_mutex_t mutex;
86         l_cond_t  cond;
87         int       stopping;
88         struct {
89                 __u64 counters[MAX_THREADS];
90                 __u64 offsets[MAX_THREADS];
91                 int   thr_running;
92                 int   start_barrier;
93                 int   stop_barrier;
94                 struct timeval start_time;
95                 struct timeval end_time;
96         } body;
97 };
98
99 static struct shared_data *shared_data;
100 static __u64 counter_snapshot[2][MAX_THREADS];
101 static int prev_valid;
102 static struct timeval prev_time;
103 static int thread;
104 static int nthreads;
105 #else
106 const int thread = 0;
107 const int nthreads = 1;
108 #endif
109
110 static int cur_device = -1;
111
112 struct lov_oinfo lov_oinfos[LOV_MAX_STRIPE_COUNT];
113
114 struct lsm_buffer {
115         struct lov_stripe_md lsm;
116         struct lov_oinfo *ptrs[LOV_MAX_STRIPE_COUNT];
117 } lsm_buffer;
118
119 static int l2_ioctl(int dev_id, int opc, void *buf)
120 {
121         return l_ioctl(dev_id, opc, buf);
122 }
123
124 int lcfg_ioctl(char * func, int dev_id, struct lustre_cfg *lcfg)
125 {
126         struct obd_ioctl_data data;
127         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
128         int rc;
129
130         memset(&data, 0, sizeof(data));
131         data.ioc_dev = cur_device;
132         data.ioc_type = LUSTRE_CFG_TYPE;
133         data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
134                                         lcfg->lcfg_buflens);
135         data.ioc_pbuf1 = (void *)lcfg;
136         memset(buf, 0, sizeof(rawbuf));
137         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
138         if (rc) {
139                 fprintf(stderr, "error: %s: invalid ioctl\n",
140                         jt_cmdname(func));
141                 return rc;
142         }
143
144         rc =  l_ioctl(dev_id, OBD_IOC_PROCESS_CFG, buf);
145
146         return rc;
147 }
148
149 static int do_device(char *func, char *devname);
150
151 static int get_mgs_device()
152 {
153         char mgs[] = "$MGS";
154         static int mgs_device = -1;
155
156         if (mgs_device == -1) {
157                 int rc;
158                 do_disconnect(NULL, 1);
159                 rc = do_device("mgsioc", mgs);
160                 if (rc) {
161                         fprintf(stderr,
162                                 "This command must be run on the MGS.\n");
163                         errno = ENODEV;
164                         return -1;
165                 }
166                 mgs_device = cur_device;
167         }
168         return mgs_device;
169 }
170
171 /* Returns -1 on error with errno set */
172 int lcfg_mgs_ioctl(char *func, int dev_id, struct lustre_cfg *lcfg)
173 {
174         struct obd_ioctl_data data;
175         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
176         int rc;
177
178         memset(&data, 0, sizeof(data));
179         rc = data.ioc_dev = get_mgs_device();
180         if (rc < 0)
181                 goto out;
182         data.ioc_type = LUSTRE_CFG_TYPE;
183         data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
184                                         lcfg->lcfg_buflens);
185         data.ioc_pbuf1 = (void *)lcfg;
186         memset(buf, 0, sizeof(rawbuf));
187         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
188         if (rc) {
189                 fprintf(stderr, "error: %s: invalid ioctl\n",
190                         jt_cmdname(func));
191                 return rc;
192         }
193
194         rc = l_ioctl(dev_id, OBD_IOC_PARAM, buf);
195 out:
196         if (rc) {
197                 if (errno == ENOSYS)
198                         fprintf(stderr, "Make sure cfg_device is set first.\n");
199         }
200         return rc;
201 }
202
203 char *obdo_print(struct obdo *obd)
204 {
205         char buf[1024];
206
207         sprintf(buf, "id: "LPX64"\ngrp: "LPX64"\natime: "LPU64"\nmtime: "LPU64
208                 "\nctime: "LPU64"\nsize: "LPU64"\nblocks: "LPU64
209                 "\nblksize: %u\nmode: %o\nuid: %d\ngid: %d\nflags: %x\n"
210                 "misc: %x\nnlink: %d,\nvalid "LPX64"\n",
211                 obd->o_id, obd->o_seq, obd->o_atime, obd->o_mtime, obd->o_ctime,
212                 obd->o_size, obd->o_blocks, obd->o_blksize, obd->o_mode,
213                 obd->o_uid, obd->o_gid, obd->o_flags, obd->o_misc,
214                 obd->o_nlink, obd->o_valid);
215         return strdup(buf);
216 }
217
218
219 #define BAD_VERBOSE (-999999999)
220
221 #define N2D_OFF 0x100      /* So we can tell between error codes and devices */
222
223 static int do_name2dev(char *func, char *name)
224 {
225         struct obd_ioctl_data data;
226         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
227         int rc;
228
229         memset(&data, 0, sizeof(data));
230         data.ioc_dev = cur_device;
231         data.ioc_inllen1 = strlen(name) + 1;
232         data.ioc_inlbuf1 = name;
233
234         memset(buf, 0, sizeof(rawbuf));
235         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
236         if (rc) {
237                 fprintf(stderr, "error: %s: invalid ioctl\n",
238                         jt_cmdname(func));
239                 return rc;
240         }
241         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_NAME2DEV, buf);
242         if (rc < 0)
243                 return errno;
244         rc = obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
245         if (rc) {
246                 fprintf(stderr, "error: %s: invalid reply\n",
247                         jt_cmdname(func));
248                 return rc;
249         }
250
251         return data.ioc_dev + N2D_OFF;
252 }
253
254 /*
255  * resolve a device name to a device number.
256  * supports a number, $name or %uuid.
257  */
258 int parse_devname(char *func, char *name)
259 {
260         int rc;
261         int ret = -1;
262
263         if (!name)
264                 return ret;
265         if (isdigit(name[0])) {
266                 ret = strtoul(name, NULL, 0);
267         } else {
268                 if (name[0] == '$' || name[0] == '%')
269                         name++;
270                 rc = do_name2dev(func, name);
271                 if (rc >= N2D_OFF) {
272                         ret = rc - N2D_OFF;
273                         // printf("Name %s is device %d\n", name, ret);
274                 } else {
275                         fprintf(stderr, "No device found for name %s: %s\n",
276                                 name, strerror(rc));
277                 }
278         }
279         return ret;
280 }
281
282 static void
283 reset_lsmb (struct lsm_buffer *lsmb)
284 {
285         memset (&lsmb->lsm, 0, sizeof (lsmb->lsm));
286         memset(lov_oinfos, 0, sizeof(lov_oinfos));
287         lsmb->lsm.lsm_magic = LOV_MAGIC;
288 }
289
290 static int
291 parse_lsm (struct lsm_buffer *lsmb, char *string)
292 {
293         struct lov_stripe_md *lsm = &lsmb->lsm;
294         char                 *end;
295         int                   i;
296
297         /*
298          * object_id[=size#count[@offset:id]*]
299          */
300
301         reset_lsmb (lsmb);
302
303         lsm->lsm_object_id = strtoull (string, &end, 0);
304         if (end == string)
305                 return (-1);
306         string = end;
307
308         if (*string == 0)
309                 return (0);
310
311         if (*string != '=')
312                 return (-1);
313         string++;
314
315         lsm->lsm_stripe_size = strtoul (string, &end, 0);
316         if (end == string)
317                 return (-1);
318         string = end;
319
320         if (*string != '#')
321                 return (-1);
322         string++;
323
324         lsm->lsm_stripe_count = strtoul (string, &end, 0);
325         if (end == string)
326                 return (-1);
327         string = end;
328
329         if (*string == 0)               /* don't have to specify obj ids */
330                 return (0);
331
332         for (i = 0; i < lsm->lsm_stripe_count; i++) {
333                 if (*string != '@')
334                         return (-1);
335                 string++;
336                 lsm->lsm_oinfo[i]->loi_ost_idx = strtoul(string, &end, 0);
337                 if (*end != ':')
338                         return (-1);
339                 string = end + 1;
340                 lsm->lsm_oinfo[i]->loi_id = strtoull(string, &end, 0);
341                 string = end;
342         }
343
344         if (*string != 0)
345                 return (-1);
346
347         return (0);
348 }
349
350 char *jt_cmdname(char *func)
351 {
352         static char buf[512];
353
354         if (thread) {
355                 sprintf(buf, "%s-%d", func, thread);
356                 return buf;
357         }
358
359         return func;
360 }
361
362 #define difftime(a, b)                                  \
363         ((a)->tv_sec - (b)->tv_sec +                    \
364          ((a)->tv_usec - (b)->tv_usec) / 1000000.0)
365
366 static int be_verbose(int verbose, struct timeval *next_time,
367                       __u64 num, __u64 *next_num, int num_total)
368 {
369         struct timeval now;
370
371         if (!verbose)
372                 return 0;
373
374         if (next_time != NULL)
375                 gettimeofday(&now, NULL);
376
377         /* A positive verbosity means to print every X iterations */
378         if (verbose > 0 && (num >= *next_num || num >= num_total)) {
379                 *next_num += verbose;
380                 if (next_time) {
381                         next_time->tv_sec = now.tv_sec - verbose;
382                         next_time->tv_usec = now.tv_usec;
383                 }
384                 return 1;
385         }
386
387         /* A negative verbosity means to print at most each X seconds */
388         if (verbose < 0 && next_time != NULL &&
389             difftime(&now, next_time) >= 0.0){
390                 next_time->tv_sec = now.tv_sec - verbose;
391                 next_time->tv_usec = now.tv_usec;
392                 *next_num = num;
393                 return 1;
394         }
395
396         return 0;
397 }
398
399 static int get_verbose(char *func, const char *arg)
400 {
401         int verbose;
402         char *end;
403
404         if (!arg || arg[0] == 'v')
405                 verbose = 1;
406         else if (arg[0] == 's' || arg[0] == 'q')
407                 verbose = 0;
408         else {
409                 verbose = (int)strtoul(arg, &end, 0);
410                 if (*end) {
411                         fprintf(stderr, "error: %s: bad verbose option '%s'\n",
412                                 jt_cmdname(func), arg);
413                         return BAD_VERBOSE;
414                 }
415         }
416
417         if (verbose < 0)
418                 printf("Print status every %d seconds\n", -verbose);
419         else if (verbose == 1)
420                 printf("Print status every operation\n");
421         else if (verbose > 1)
422                 printf("Print status every %d operations\n", verbose);
423
424         return verbose;
425 }
426
427 int do_disconnect(char *func, int verbose)
428 {
429         lcfg_set_devname(NULL);
430         cur_device = -1;
431         return 0;
432 }
433
434 #ifdef MAX_THREADS
435 static int shmem_setup(void)
436 {
437         pthread_mutexattr_t mattr;
438         pthread_condattr_t  cattr;
439         int                 rc;
440         int                 shmid;
441
442         /* Create new segment */
443         shmid = shmget(IPC_PRIVATE, sizeof(*shared_data), 0600);
444         if (shmid == -1) {
445                 fprintf(stderr, "Can't create shared data: %s\n",
446                         strerror(errno));
447                 return errno;
448         }
449
450         /* Attatch to new segment */
451         shared_data = (struct shared_data *)shmat(shmid, NULL, 0);
452
453         if (shared_data == (struct shared_data *)(-1)) {
454                 fprintf(stderr, "Can't attach shared data: %s\n",
455                         strerror(errno));
456                 shared_data = NULL;
457                 return errno;
458         }
459
460         /* Mark segment as destroyed, so it will disappear when we exit.
461          * Forks will inherit attached segments, so we should be OK.
462          */
463         if (shmctl(shmid, IPC_RMID, NULL) == -1) {
464                 fprintf(stderr, "Can't destroy shared data: %s\n",
465                         strerror(errno));
466                 return errno;
467         }
468
469         pthread_mutexattr_init(&mattr);
470         pthread_condattr_init(&cattr);
471
472         rc = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
473         if (rc != 0) {
474                 fprintf(stderr, "Can't set shared mutex attr\n");
475                 return rc;
476         }
477
478         rc = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
479         if (rc != 0) {
480                 fprintf(stderr, "Can't set shared cond attr\n");
481                 return rc;
482         }
483
484         pthread_mutex_init(&shared_data->mutex, &mattr);
485         pthread_cond_init(&shared_data->cond, &cattr);
486
487         pthread_mutexattr_destroy(&mattr);
488         pthread_condattr_destroy(&cattr);
489
490         return 0;
491 }
492
493 static inline void shmem_lock(void)
494 {
495         l_mutex_lock(&shared_data->mutex);
496 }
497
498 static inline void shmem_unlock(void)
499 {
500         l_mutex_unlock(&shared_data->mutex);
501 }
502
503 static inline void shmem_wait(void)
504 {
505         l_cond_wait(&shared_data->cond, &shared_data->mutex);
506 }
507
508 static inline void shmem_wakeup_all(void)
509 {
510         l_cond_broadcast(&shared_data->cond);
511 }
512
513 static inline void shmem_reset(int total_threads)
514 {
515         if (shared_data == NULL)
516                 return;
517
518         memset(&shared_data->body, 0, sizeof(shared_data->body));
519         memset(counter_snapshot, 0, sizeof(counter_snapshot));
520         prev_valid = 0;
521         shared_data->stopping = 0;
522         shared_data->body.start_barrier = total_threads;
523         shared_data->body.stop_barrier = total_threads;
524 }
525
526 static inline void shmem_bump(__u32 counter)
527 {
528         static bool running_not_bumped = true;
529
530         if (shared_data == NULL || thread <= 0 || thread > MAX_THREADS)
531                 return;
532
533         shmem_lock();
534         shared_data->body.counters[thread - 1] += counter;
535         if (running_not_bumped) {
536                 shared_data->body.thr_running++;
537                 running_not_bumped = false;
538         }
539         shmem_unlock();
540 }
541
542 static void shmem_total(int total_threads)
543 {
544         __u64 total = 0;
545         double secs;
546         int i;
547
548         if (shared_data == NULL || total_threads > MAX_THREADS)
549                 return;
550
551         shmem_lock();
552         for (i = 0; i < total_threads; i++)
553                 total += shared_data->body.counters[i];
554
555         secs = difftime(&shared_data->body.end_time,
556                         &shared_data->body.start_time);
557         shmem_unlock();
558
559         printf("Total: total "LPU64" threads %d sec %f %f/second\n",
560                total, total_threads, secs, total / secs);
561
562         return;
563 }
564
565 static void shmem_snap(int total_threads, int live_threads)
566 {
567         struct timeval this_time;
568         int non_zero = 0;
569         __u64 total = 0;
570         double secs;
571         int running;
572         int i;
573
574         if (shared_data == NULL || total_threads > MAX_THREADS)
575                 return;
576
577         shmem_lock();
578         memcpy(counter_snapshot[0], shared_data->body.counters,
579                total_threads * sizeof(counter_snapshot[0][0]));
580         running = shared_data->body.thr_running;
581         shmem_unlock();
582
583         gettimeofday(&this_time, NULL);
584
585         for (i = 0; i < total_threads; i++) {
586                 long long this_count =
587                         counter_snapshot[0][i] - counter_snapshot[1][i];
588
589                 if (this_count != 0) {
590                         non_zero++;
591                         total += this_count;
592                 }
593         }
594
595         secs = difftime(&this_time, &prev_time);
596         if (prev_valid && secs > 1.0)    /* someone screwed with the time? */
597                 printf("%d/%d Total: %f/second\n", non_zero, total_threads,
598                        total / secs);
599
600         memcpy(counter_snapshot[1], counter_snapshot[0],
601                total_threads * sizeof(counter_snapshot[0][0]));
602         prev_time = this_time;
603         if (!prev_valid &&
604             running == total_threads)
605                 prev_valid = 1;
606 }
607
608 static void shmem_stop(void)
609 {
610         if (shared_data == NULL)
611                 return;
612
613         shared_data->stopping = 1;
614 }
615
616 static void shmem_cleanup(void)
617 {
618         if (shared_data == NULL)
619                 return;
620
621         shmem_stop();
622
623         pthread_mutex_destroy(&shared_data->mutex);
624         pthread_cond_destroy(&shared_data->cond);
625 }
626
627 static int shmem_running(void)
628 {
629         return (shared_data == NULL || !shared_data->stopping);
630 }
631
632 static void shmem_end_time_locked(void)
633 {
634         shared_data->body.stop_barrier--;
635         if (shared_data->body.stop_barrier == 0)
636                 gettimeofday(&shared_data->body.end_time, NULL);
637 }
638
639 static void shmem_start_time_locked(void)
640 {
641         shared_data->body.start_barrier--;
642         if (shared_data->body.start_barrier == 0) {
643                 shmem_wakeup_all();
644                 gettimeofday(&shared_data->body.start_time, NULL);
645         } else {
646                 shmem_wait();
647         }
648 }
649
650 #else
651 static int shmem_setup(void)
652 {
653         return 0;
654 }
655
656 static inline void shmem_reset(int total_threads)
657 {
658 }
659
660 static inline void shmem_bump(__u32 counters)
661 {
662 }
663
664 static void shmem_lock()
665 {
666 }
667
668 static void shmem_unlock()
669 {
670 }
671
672 static void shmem_cleanup(void)
673 {
674 }
675
676 static int shmem_running(void)
677 {
678         return 1;
679 }
680 #endif
681
682 extern command_t cmdlist[];
683
684 static int do_device(char *func, char *devname)
685 {
686         int dev;
687
688         dev = parse_devname(func, devname);
689         if (dev < 0)
690                 return -1;
691
692         lcfg_set_devname(devname);
693         cur_device = dev;
694         return 0;
695 }
696
697 int jt_obd_get_device()
698 {
699         return cur_device;
700 }
701
702 int jt_obd_device(int argc, char **argv)
703 {
704         int rc;
705
706         if (argc > 2)
707                 return CMD_HELP;
708
709         if (argc == 1) {
710                 printf("current device is %d - %s\n",
711                        cur_device, lcfg_get_devname() ? : "not set");
712                 return 0;
713         }
714         rc = do_device("device", argv[1]);
715         return rc;
716 }
717
718 int jt_opt_device(int argc, char **argv)
719 {
720         int ret;
721         int rc;
722
723         if (argc < 3)
724                 return CMD_HELP;
725
726         rc = do_device("device", argv[1]);
727
728         if (!rc)
729                 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
730
731         ret = do_disconnect(argv[0], 0);
732         if (!rc)
733                 rc = ret;
734
735         return rc;
736 }
737
738 #ifdef MAX_THREADS
739 static void parent_sighandler (int sig)
740 {
741         return;
742 }
743
744 int jt_opt_threads(int argc, char **argv)
745 {
746         static char      cmdstr[128];
747         sigset_t         saveset;
748         sigset_t         sigset;
749         struct sigaction sigact;
750         struct sigaction saveact1;
751         struct sigaction saveact2;
752         unsigned long    threads;
753         __u64            next_thread;
754         int verbose;
755         int rc = 0;
756         int report_count = -1;
757         char *end;
758         int i;
759
760         if (argc < 5)
761                 return CMD_HELP;
762
763         threads = strtoul(argv[1], &end, 0);
764
765         if (*end == '.')
766                 report_count = strtoul(end + 1, &end, 0);
767
768         if (*end || threads > MAX_THREADS) {
769                 fprintf(stderr, "error: %s: invalid thread count '%s'\n",
770                         jt_cmdname(argv[0]), argv[1]);
771                 return CMD_HELP;
772         }
773
774         verbose = get_verbose(argv[0], argv[2]);
775         if (verbose == BAD_VERBOSE)
776                 return CMD_HELP;
777
778         if (verbose != 0) {
779                 snprintf(cmdstr, sizeof(cmdstr), "%s", argv[4]);
780                 for (i = 5; i < argc; i++)
781                         snprintf(cmdstr + strlen(cmdstr), sizeof(cmdstr),
782                                  " %s", argv[i]);
783
784                 printf("%s: starting %ld threads on device %s running %s\n",
785                        argv[0], threads, argv[3], cmdstr);
786         }
787
788         shmem_reset(threads);
789
790         sigemptyset(&sigset);
791         sigaddset(&sigset, SIGALRM);
792         sigaddset(&sigset, SIGCHLD);
793         sigprocmask(SIG_BLOCK, &sigset, &saveset);
794
795         nthreads = threads;
796
797         for (i = 1, next_thread = verbose; i <= threads; i++) {
798                 rc = fork();
799                 if (rc < 0) {
800                         fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
801                                 strerror(rc = errno));
802                         break;
803                 } else if (rc == 0) {
804                         sigprocmask(SIG_SETMASK, &saveset, NULL);
805
806                         thread = i;
807                         argv[2] = "--device";
808                         exit(jt_opt_device(argc - 2, argv + 2));
809                 } else if (be_verbose(verbose, NULL, i, &next_thread, threads))
810                         printf("%s: thread #%d (PID %d) started\n",
811                                argv[0], i, rc);
812                 rc = 0;
813         }
814
815         if (!thread) {          /* parent process */
816                 int live_threads = threads;
817
818                 sigemptyset(&sigset);
819                 sigemptyset(&sigact.sa_mask);
820                 sigact.sa_handler = parent_sighandler;
821                 sigact.sa_flags = 0;
822
823                 sigaction(SIGALRM, &sigact, &saveact1);
824                 sigaction(SIGCHLD, &sigact, &saveact2);
825
826                 while (live_threads > 0) {
827                         int status;
828                         pid_t ret;
829
830                         if (verbose < 0)        /* periodic stats */
831                                 alarm(-verbose);
832
833                         sigsuspend(&sigset);
834                         alarm(0);
835
836                         while (live_threads > 0) {
837                                 ret = waitpid(0, &status, WNOHANG);
838                                 if (ret == 0)
839                                         break;
840
841                                 if (ret < 0) {
842                                         fprintf(stderr, "error: %s: wait - %s\n",
843                                                 argv[0], strerror(errno));
844                                         if (!rc)
845                                                 rc = errno;
846                                         continue;
847                                 } else {
848                                         /*
849                                          * This is a hack.  We _should_ be able
850                                          * to use WIFEXITED(status) to see if
851                                          * there was an error, but it appears
852                                          * to be broken and it always returns 1
853                                          * (OK).  See wait(2).
854                                          */
855                                         int err = WEXITSTATUS(status);
856                                         if (err || WIFSIGNALED(status))
857                                                 fprintf(stderr,
858                                                         "%s: PID %d had rc=%d\n",
859                                                         argv[0], ret, err);
860                                         if (!rc)
861                                                 rc = err;
862
863                                         live_threads--;
864                                 }
865                         }
866
867                         /* Show stats while all threads running */
868                         if (verbose < 0) {
869                                 shmem_snap(threads, live_threads);
870                                 if (report_count > 0 && --report_count == 0)
871                                         shmem_stop();
872                         }
873                 }
874                 sigaction(SIGCHLD, &saveact2, NULL);
875                 sigaction(SIGALRM, &saveact1, NULL);
876         }
877
878         shmem_total(threads);
879         sigprocmask(SIG_SETMASK, &saveset, NULL);
880
881         return rc;
882 }
883 #else
884 int jt_opt_threads(int argc, char **argv)
885 {
886         fprintf(stderr, "%s not-supported in a single-threaded runtime\n",
887                 jt_cmdname(argv[0]));
888         return CMD_HELP;
889 }
890 #endif
891
892 int jt_opt_net(int argc, char **argv)
893 {
894         char *arg2[3];
895         int rc;
896
897         if (argc < 3)
898                 return CMD_HELP;
899
900         arg2[0] = argv[0];
901         arg2[1] = argv[1];
902         arg2[2] = NULL;
903         rc = jt_ptl_network (2, arg2);
904
905         if (!rc)
906                 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
907
908         return rc;
909 }
910
911 int jt_obd_no_transno(int argc, char **argv)
912 {
913         struct obd_ioctl_data data;
914         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
915         int rc;
916
917         memset(&data, 0, sizeof(data));
918         data.ioc_dev = cur_device;
919
920         if (argc != 1)
921                 return CMD_HELP;
922
923         memset(buf, 0, sizeof(rawbuf));
924         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
925         if (rc) {
926                 fprintf(stderr, "error: %s: invalid ioctl\n",
927                         jt_cmdname(argv[0]));
928                 return rc;
929         }
930         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_NO_TRANSNO, buf);
931         if (rc < 0)
932                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
933                         strerror(rc = errno));
934
935         return rc;
936 }
937
938 int jt_obd_set_readonly(int argc, char **argv)
939 {
940         struct obd_ioctl_data data;
941         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
942         int rc;
943
944         memset(&data, 0, sizeof(data));
945         data.ioc_dev = cur_device;
946
947         if (argc != 1)
948                 return CMD_HELP;
949
950         memset(buf, 0, sizeof(rawbuf));
951         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
952         if (rc) {
953                 fprintf(stderr, "error: %s: invalid ioctl\n",
954                         jt_cmdname(argv[0]));
955                 return rc;
956         }
957         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_SET_READONLY, buf);
958         if (rc < 0)
959                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
960                         strerror(rc = errno));
961
962         return rc;
963 }
964
965 int jt_obd_abort_recovery(int argc, char **argv)
966 {
967         struct obd_ioctl_data data;
968         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
969         int rc;
970
971         memset(&data, 0, sizeof(data));
972         data.ioc_dev = cur_device;
973
974         if (argc != 1)
975                 return CMD_HELP;
976
977         memset(buf, 0, sizeof(rawbuf));
978         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
979         if (rc) {
980                 fprintf(stderr, "error: %s: invalid ioctl\n",
981                         jt_cmdname(argv[0]));
982                 return rc;
983         }
984         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_ABORT_RECOVERY, buf);
985         if (rc < 0)
986                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
987                         strerror(rc = errno));
988
989         return rc;
990 }
991
992 int jt_get_version(int argc, char **argv)
993 {
994         int rc;
995         char rawbuf[MAX_IOC_BUFLEN];
996         char *version;
997
998         if (argc != 1)
999                 return CMD_HELP;
1000
1001         rc = llapi_get_version(rawbuf, MAX_IOC_BUFLEN, &version);
1002         if (rc)
1003                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1004                         strerror(-rc));
1005         else
1006                 printf("Lustre version: %s\n", version);
1007
1008         printf("lctl   version: %s\n", BUILD_VERSION);
1009         return rc;
1010 }
1011
1012 /*
1013  * Print an obd device line with the ost_conn_uuid inserted, if the obd
1014  * is an osc.
1015  */
1016 static void print_obd_line(char *s)
1017 {
1018         char buf[MAX_STRING_SIZE];
1019         char obd_name[MAX_OBD_NAME];
1020         FILE *fp = NULL;
1021         char *ptr;
1022
1023         if (sscanf(s, " %*d %*s osc %s %*s %*d ", obd_name) == 0)
1024                 goto try_mdc;
1025         snprintf(buf, sizeof(buf),
1026                  "/proc/fs/lustre/osc/%s/ost_conn_uuid", obd_name);
1027         if ((fp = fopen(buf, "r")) == NULL)
1028                 goto try_mdc;
1029         goto got_one;
1030
1031 try_mdc:
1032         if (sscanf(s, " %*d %*s mdc %s %*s %*d ", obd_name) == 0)
1033                 goto fail;
1034         snprintf(buf, sizeof(buf),
1035                  "/proc/fs/lustre/mdc/%s/mds_conn_uuid", obd_name);
1036         if ((fp = fopen(buf, "r")) == NULL)
1037                 goto fail;
1038
1039 got_one:
1040         /* should not ignore fgets(3)'s return value */
1041         if (!fgets(buf, sizeof(buf), fp)) {
1042                 fprintf(stderr, "reading from %s: %s", buf, strerror(errno));
1043                 fclose(fp);
1044                 return;
1045         }
1046         fclose(fp);
1047
1048         /* trim trailing newlines */
1049         ptr = strrchr(buf, '\n');
1050         if (ptr) *ptr = '\0';
1051         ptr = strrchr(s, '\n');
1052         if (ptr) *ptr = '\0';
1053
1054         printf("%s %s\n", s, buf);
1055         return;
1056
1057 fail:
1058         printf("%s", s);
1059         return;
1060 }
1061
1062 /* get device list by ioctl */
1063 int jt_obd_list_ioctl(int argc, char **argv)
1064 {
1065         int rc, index;
1066         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1067         struct obd_ioctl_data *data = (struct obd_ioctl_data *)buf;
1068
1069         if (argc > 2)
1070                 return CMD_HELP;
1071         /* Just ignore a -t option.  Only supported with /proc. */
1072         else if (argc == 2 && strcmp(argv[1], "-t") != 0)
1073                 return CMD_HELP;
1074
1075         for (index = 0;; index++) {
1076                 memset(buf, 0, sizeof(rawbuf));
1077                 data->ioc_version = OBD_IOCTL_VERSION;
1078                 data->ioc_inllen1 =
1079                         sizeof(rawbuf) - cfs_size_round(sizeof(*data));
1080                 data->ioc_inlbuf1 = buf + cfs_size_round(sizeof(*data));
1081                 data->ioc_len = obd_ioctl_packlen(data);
1082                 data->ioc_count = index;
1083
1084                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_GETDEVICE, buf);
1085                 if (rc != 0)
1086                         break;
1087                 printf("%s\n", (char *)data->ioc_bulk);
1088         }
1089         if (rc != 0) {
1090                 if (errno == ENOENT)
1091                         /* no device or the last device */
1092                         rc = 0;
1093                 else
1094                         fprintf(stderr, "Error getting device list: %s: "
1095                                 "check dmesg.\n", strerror(errno));
1096         }
1097         return rc;
1098 }
1099
1100 int jt_obd_list(int argc, char **argv)
1101 {
1102         int rc;
1103         char buf[MAX_STRING_SIZE];
1104         FILE *fp = NULL;
1105         int print_obd = 0;
1106
1107         if (argc > 2)
1108                 return CMD_HELP;
1109         else if (argc == 2) {
1110                 if (strcmp(argv[1], "-t") == 0)
1111                         print_obd = 1;
1112                 else
1113                         return CMD_HELP;
1114         }
1115
1116         fp = fopen(DEVICES_LIST, "r");
1117         if (fp == NULL) {
1118                 fprintf(stderr, "error: %s: %s opening "DEVICES_LIST"\n",
1119                         jt_cmdname(argv[0]), strerror(rc =  errno));
1120                 return jt_obd_list_ioctl(argc, argv);
1121         }
1122
1123         while (fgets(buf, sizeof(buf), fp) != NULL)
1124                 if (print_obd)
1125                         print_obd_line(buf);
1126                 else
1127                         printf("%s", buf);
1128
1129         fclose(fp);
1130         return 0;
1131 }
1132
1133 struct jt_fid_space {
1134         obd_seq jt_seq;
1135         obd_id  jt_id;
1136         int     jt_width;
1137 };
1138
1139 int jt_obd_alloc_fids(struct jt_fid_space *space, struct lu_fid *fid,
1140                       __u64 *count)
1141 {
1142         int rc;
1143
1144         if (space->jt_seq == 0 || space->jt_id == space->jt_width) {
1145                 struct obd_ioctl_data  data;
1146                 char rawbuf[MAX_IOC_BUFLEN];
1147                 char *buf = rawbuf;
1148                 __u64 seqnr;
1149                 int max_count;
1150
1151                 memset(&data, 0, sizeof(data));
1152                 data.ioc_dev = cur_device;
1153
1154                 data.ioc_pbuf1 = (char *)&seqnr;
1155                 data.ioc_plen1 = sizeof(seqnr);
1156
1157                 data.ioc_pbuf2 = (char *)&max_count;
1158                 data.ioc_plen2 = sizeof(max_count);
1159
1160                 memset(buf, 0, sizeof(rawbuf));
1161                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1162                 if (rc) {
1163                         fprintf(stderr, "error: invalid ioctl rc = %d\n", rc);
1164                         return rc;
1165                 }
1166
1167                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_ECHO_ALLOC_SEQ, buf);
1168                 if (rc) {
1169                         fprintf(stderr, "ioctl error: rc = %d\n", rc);
1170                         return rc;
1171                 }
1172
1173                 space->jt_seq = *(__u64 *)data.ioc_pbuf1;
1174                 space->jt_width = *(int *)data.ioc_pbuf2;
1175                 space->jt_id = 1;
1176         }
1177         fid->f_seq = space->jt_seq;
1178         fid->f_oid = space->jt_id;
1179         fid->f_ver = 0;
1180
1181         space->jt_id = min(space->jt_id + *count, space->jt_width);
1182
1183         *count = space->jt_id - fid->f_oid;
1184         return 0;
1185 }
1186
1187 #define MD_STEP_COUNT 1000
1188 int jt_obd_md_common(int argc, char **argv, int cmd)
1189 {
1190         struct obd_ioctl_data  data;
1191         struct timeval         start;
1192         struct timeval         end_time;
1193         char                   rawbuf[MAX_IOC_BUFLEN];
1194         char                  *buf = rawbuf;
1195         int                    mode = 0000644;
1196         int                    create_mode;
1197         int                    rc = 0;
1198         char                  *parent_basedir = NULL;
1199         char                   dirname[4096];
1200         int                    parent_base_id = 0;
1201         int                    parent_count = 1;
1202         __u64                  child_base_id = -1;
1203         int                    stripe_count = 0;
1204         int                    stripe_index = -1;
1205         int                    count = 0;
1206         char                  *end;
1207         __u64                  seconds = 0;
1208         double                 diff;
1209         int                    c;
1210         int                    xattr_size = 0;
1211         __u64                  total_count = 0;
1212         char                  *name = NULL;
1213         struct jt_fid_space    fid_space = {0};
1214         int                    version = 0;
1215         struct option          long_opts[] = {
1216                 {"child_base_id",     required_argument, 0, 'b'},
1217                 {"stripe_count",      required_argument, 0, 'c'},
1218                 {"parent_basedir",    required_argument, 0, 'd'},
1219                 {"parent_dircount",   required_argument, 0, 'D'},
1220                 {"stripe_index",      required_argument, 0, 'i'},
1221                 {"mode",              required_argument, 0, 'm'},
1222                 {"count",             required_argument, 0, 'n'},
1223                 {"time",              required_argument, 0, 't'},
1224                 {"version",           no_argument,       0, 'v'},
1225                 {"xattr_size",        required_argument, 0, 'x'},
1226                 {0, 0, 0, 0}
1227         };
1228
1229         optind = 0;
1230         while ((c = getopt_long(argc, argv, "b:c:d:D:m:n:t:vx:",
1231                                 long_opts, NULL)) >= 0) {
1232                 switch (c) {
1233                 case 'b':
1234                         child_base_id = strtoull(optarg, &end, 0);
1235                         if (*end) {
1236                                 fprintf(stderr, "error: %s: bad child_base_id"
1237                                         " '%s'\n", jt_cmdname(argv[0]), optarg);
1238                                 return CMD_HELP;
1239                         }
1240                         break;
1241                 case 'c':
1242                         stripe_count = strtoul(optarg, &end, 0);
1243                         if (*end) {
1244                                 fprintf(stderr, "error: %s: bad stripe count"
1245                                         " '%s'\n", jt_cmdname(argv[0]), optarg);
1246                                 return CMD_HELP;
1247                         }
1248                         break;
1249                 case 'd':
1250                         parent_basedir = optarg;
1251                         break;
1252                 case 'D':
1253                         parent_count = strtoul(optarg, &end, 0);
1254                         if (*end) {
1255                                 fprintf(stderr, "error: %s: bad parent count"
1256                                         " '%s'\n", jt_cmdname(argv[0]), optarg);
1257                                 return CMD_HELP;
1258                         }
1259                         break;
1260                 case 'i':
1261                         stripe_index = strtoul(optarg, &end, 0);
1262                         if (*end) {
1263                                 fprintf(stderr, "error: %s: bad stripe index"
1264                                         " '%s'\n", jt_cmdname(argv[0]), optarg);
1265                                 return CMD_HELP;
1266                         }
1267                         break;
1268                 case 'm':
1269                         mode = strtoul(optarg, &end, 0);
1270                         if (*end) {
1271                                 fprintf(stderr, "error: %s: bad mode '%s'\n",
1272                                         jt_cmdname(argv[0]), optarg);
1273                                 return CMD_HELP;
1274                         }
1275                         break;
1276                 case 'n':
1277                         total_count = strtoul(optarg, &end, 0);
1278                         if (*end || total_count == 0) {
1279                                 fprintf(stderr, "%s: bad child count '%s'\n",
1280                                         jt_cmdname(argv[0]), optarg);
1281                                 return CMD_HELP;
1282                         }
1283                         break;
1284                 case 't':
1285                         seconds = strtoull(optarg, &end, 0);
1286                         if (*end) {
1287                                 fprintf(stderr, "error: %s: seconds '%s'\n",
1288                                         jt_cmdname(argv[0]), optarg);
1289                                 return CMD_HELP;
1290                         }
1291                         break;
1292                 case 'v':
1293                         version = 1;
1294                         break;
1295                 case 'x':
1296                         xattr_size = strtoul(optarg, &end, 0);
1297                         if (*end) {
1298                                 fprintf(stderr, "error: %s: xattr_size '%s'\n",
1299                                         jt_cmdname(argv[0]), optarg);
1300                                 return CMD_HELP;
1301                         }
1302                         SET_BUT_UNUSED(xattr_size);
1303                         break;
1304                 default:
1305                         fprintf(stderr, "error: %s: option '%s' "
1306                                 "unrecognized\n", argv[0], argv[optind - 1]);
1307                         return CMD_HELP;
1308                 }
1309         }
1310
1311         memset(&data, 0, sizeof(data));
1312         data.ioc_dev = cur_device;
1313         if (child_base_id == -1) {
1314                 if (optind >= argc)
1315                         return CMD_HELP;
1316                 name = argv[optind];
1317                 total_count = 1;
1318         } else {
1319                 if (optind < argc) {
1320                         fprintf(stderr, "child_base_id and name can not"
1321                                         " specified at the same time\n");
1322                         return CMD_HELP;
1323                 }
1324         }
1325
1326         if (stripe_count == 0 && stripe_index != -1) {
1327                 fprintf(stderr, "If stripe_count is 0, stripe_index can not"
1328                                 "be specified\n");
1329                 return CMD_HELP;
1330         }
1331
1332         if (total_count == 0 && seconds == 0) {
1333                 fprintf(stderr, "count or seconds needs to be indicated\n");
1334                 return CMD_HELP;
1335         }
1336
1337         if (parent_count <= 0) {
1338                 fprintf(stderr, "parent count must < 0\n");
1339                 return CMD_HELP;
1340         }
1341
1342 #ifdef MAX_THREADS
1343         if (thread) {
1344                 shmem_lock();
1345                 /* threads interleave */
1346                 if (parent_base_id != -1)
1347                         parent_base_id += (thread - 1) % parent_count;
1348
1349                 if (child_base_id != -1)
1350                         child_base_id +=  (thread - 1) * \
1351                                           (MAX_BASE_ID / nthreads);
1352
1353                 shmem_start_time_locked();
1354                 shmem_unlock();
1355         }
1356 #endif
1357         /* If parent directory is not specified, try to get the directory
1358          * from name */
1359         if (parent_basedir == NULL) {
1360                 char *last_lash;
1361                 if (name == NULL) {
1362                         fprintf(stderr, "parent_basedir or name must be"
1363                                         "indicated!\n");
1364                         return CMD_HELP;
1365                 }
1366                 /*Get directory and name from name*/
1367                 last_lash = strrchr(name, '/');
1368                 if (last_lash == NULL || name[0] != '/') {
1369                         fprintf(stderr, "Can not locate %s\n", name);
1370                         return CMD_HELP;
1371                 }
1372
1373                 if (last_lash == name) {
1374                         sprintf(dirname, "%s", "/");
1375                         name++;
1376                 } else {
1377                         int namelen = (unsigned long)last_lash -
1378                                       (unsigned long)name;
1379                         snprintf(dirname, namelen, "%s", name);
1380                 }
1381
1382                 data.ioc_pbuf1 = dirname;
1383                 data.ioc_plen1 = strlen(dirname);
1384
1385                 data.ioc_pbuf2 = name;
1386                 data.ioc_plen2 = strlen(name);
1387         } else {
1388                 if (name != NULL) {
1389                         data.ioc_pbuf2 = name;
1390                         data.ioc_plen2 = strlen(name);
1391                 } else {
1392                         if (parent_base_id > 0)
1393                                 sprintf(dirname, "%s%d", parent_basedir,
1394                                         parent_base_id);
1395                         else
1396                                 sprintf(dirname, "%s", parent_basedir);
1397                 }
1398                 data.ioc_pbuf1 = dirname;
1399                 data.ioc_plen1 = strlen(dirname);
1400         }
1401
1402         if (cmd == ECHO_MD_MKDIR || cmd == ECHO_MD_RMDIR)
1403                 create_mode = S_IFDIR;
1404         else
1405                 create_mode = S_IFREG;
1406
1407         data.ioc_obdo1.o_mode = mode | S_IFDIR;
1408         data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
1409                                  OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1410         data.ioc_command = cmd;
1411
1412         gettimeofday(&start, NULL);
1413         while (shmem_running()) {
1414                 struct lu_fid fid;
1415
1416                 data.ioc_obdo2.o_id = child_base_id;
1417                 data.ioc_obdo2.o_mode = mode | create_mode;
1418                 data.ioc_obdo2.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
1419                                          OBD_MD_FLMODE | OBD_MD_FLFLAGS |
1420                                          OBD_MD_FLGROUP;
1421                 data.ioc_obdo2.o_misc = stripe_count;
1422                 data.ioc_obdo2.o_stripe_idx = stripe_index;
1423
1424                 if (total_count > 0) {
1425                         if ((total_count - count) > MD_STEP_COUNT)
1426                                 data.ioc_count = MD_STEP_COUNT;
1427                         else
1428                                 data.ioc_count = total_count - count;
1429                 } else {
1430                         data.ioc_count = MD_STEP_COUNT;
1431                 }
1432
1433                 if (cmd == ECHO_MD_CREATE || cmd == ECHO_MD_MKDIR) {
1434                         /*Allocate fids for the create */
1435                         rc = jt_obd_alloc_fids(&fid_space, &fid,
1436                                                &data.ioc_count);
1437                         if (rc) {
1438                                 fprintf(stderr, "Allocate fids error %d.\n",rc);
1439                                 return rc;
1440                         }
1441                         data.ioc_obdo1.o_seq = fid.f_seq;
1442                         data.ioc_obdo1.o_id = fid.f_oid;
1443                 }
1444
1445                 child_base_id += data.ioc_count;
1446                 count += data.ioc_count;
1447
1448                 memset(buf, 0, sizeof(rawbuf));
1449                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1450                 if (rc) {
1451                         fprintf(stderr, "error: %s: invalid ioctl %d\n",
1452                                 jt_cmdname(argv[0]), rc);
1453                         return rc;
1454                 }
1455
1456                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_ECHO_MD, buf);
1457                 if (rc) {
1458                         fprintf(stderr, "error: %s: %s\n",
1459                                 jt_cmdname(argv[0]), strerror(rc = errno));
1460                         return rc;
1461                 }
1462                 shmem_bump(data.ioc_count);
1463
1464                 gettimeofday(&end_time, NULL);
1465                 diff = difftime(&end_time, &start);
1466                 if (seconds > 0 && (__u64)diff > seconds)
1467                         break;
1468
1469                 if (count >= total_count && total_count > 0)
1470                         break;
1471         }
1472
1473         if (count > 0 && version) {
1474                 gettimeofday(&end_time, NULL);
1475                 diff = difftime(&end_time, &start);
1476                 printf("%s: %d in %.3fs (%.3f /s): %s",
1477                         jt_cmdname(argv[0]), count, diff,
1478                         (double)count/diff, ctime(&end_time.tv_sec));
1479         }
1480
1481 #ifdef MAX_THREADS
1482         if (thread) {
1483                 shmem_lock();
1484                 shmem_end_time_locked();
1485                 shmem_unlock();
1486         }
1487 #endif
1488         return rc;
1489 }
1490
1491 int jt_obd_test_create(int argc, char **argv)
1492 {
1493         return jt_obd_md_common(argc, argv, ECHO_MD_CREATE);
1494 }
1495
1496 int jt_obd_test_mkdir(int argc, char **argv)
1497 {
1498         return jt_obd_md_common(argc, argv, ECHO_MD_MKDIR);
1499 }
1500
1501 int jt_obd_test_destroy(int argc, char **argv)
1502 {
1503         return jt_obd_md_common(argc, argv, ECHO_MD_DESTROY);
1504 }
1505
1506 int jt_obd_test_rmdir(int argc, char **argv)
1507 {
1508         return jt_obd_md_common(argc, argv, ECHO_MD_RMDIR);
1509 }
1510
1511 int jt_obd_test_lookup(int argc, char **argv)
1512 {
1513         return jt_obd_md_common(argc, argv, ECHO_MD_LOOKUP);
1514 }
1515
1516 int jt_obd_test_setxattr(int argc, char **argv)
1517 {
1518         return jt_obd_md_common(argc, argv, ECHO_MD_SETATTR);
1519 }
1520
1521 int jt_obd_test_md_getattr(int argc, char **argv)
1522 {
1523         return jt_obd_md_common(argc, argv, ECHO_MD_GETATTR);
1524 }
1525
1526 /* Create one or more objects, arg[4] may describe stripe meta-data.  If
1527  * not, defaults assumed.  This echo-client instance stashes the stripe
1528  * object ids.  Use get_stripe on this node to print full lsm and
1529  * set_stripe on another node to cut/paste between nodes.
1530  */
1531 /* create <count> [<file_create_mode>] [q|v|# verbosity] [striping] */
1532 int jt_obd_create(int argc, char **argv)
1533 {
1534         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1535         struct obd_ioctl_data data;
1536         struct timeval next_time;
1537         __u64 count = 1, next_count, base_id = 0;
1538         int verbose = 1, mode = 0100644, rc = 0, i, valid_lsm = 0;
1539         char *end;
1540
1541         memset(&data, 0, sizeof(data));
1542         data.ioc_dev = cur_device;
1543         if (argc < 2 || argc > 5)
1544                 return CMD_HELP;
1545
1546         count = strtoull(argv[1], &end, 0);
1547         if (*end) {
1548                 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1549                         jt_cmdname(argv[0]), argv[1]);
1550                 return CMD_HELP;
1551         }
1552
1553         if (argc > 2) {
1554                 mode = strtoul(argv[2], &end, 0);
1555                 if (*end) {
1556                         fprintf(stderr, "error: %s: invalid mode '%s'\n",
1557                                 jt_cmdname(argv[0]), argv[2]);
1558                         return CMD_HELP;
1559                 }
1560                 if (!(mode & S_IFMT))
1561                         mode |= S_IFREG;
1562         }
1563
1564         if (argc > 3) {
1565                 verbose = get_verbose(argv[0], argv[3]);
1566                 if (verbose == BAD_VERBOSE)
1567                         return CMD_HELP;
1568         }
1569
1570         if (argc < 5) {
1571                 reset_lsmb (&lsm_buffer);       /* will set default */
1572         } else {
1573                 rc = parse_lsm (&lsm_buffer, argv[4]);
1574                 if (rc != 0) {
1575                         fprintf(stderr, "error: %s: invalid lsm '%s'\n",
1576                                 jt_cmdname(argv[0]), argv[4]);
1577                         return CMD_HELP;
1578                 }
1579                 base_id = lsm_buffer.lsm.lsm_object_id;
1580                 valid_lsm = 1;
1581         }
1582
1583         printf("%s: "LPD64" objects\n", jt_cmdname(argv[0]), count);
1584         gettimeofday(&next_time, NULL);
1585         next_time.tv_sec -= verbose;
1586
1587         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1588                 data.ioc_obdo1.o_mode = mode;
1589                 data.ioc_obdo1.o_id = base_id;
1590                 data.ioc_obdo1.o_uid = 0;
1591                 data.ioc_obdo1.o_gid = 0;
1592                 data.ioc_obdo1.o_valid = OBD_MD_FLTYPE | OBD_MD_FLMODE |
1593                         OBD_MD_FLID | OBD_MD_FLUID | OBD_MD_FLGID;
1594
1595                 if (valid_lsm) {
1596                         data.ioc_plen1 = sizeof lsm_buffer;
1597                         data.ioc_pbuf1 = (char *)&lsm_buffer;
1598                 }
1599
1600                 memset(buf, 0, sizeof(rawbuf));
1601                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1602                 if (rc) {
1603                         fprintf(stderr, "error: %s: invalid ioctl\n",
1604                                 jt_cmdname(argv[0]));
1605                         return rc;
1606                 }
1607                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_CREATE, buf);
1608                 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
1609                 shmem_bump(1);
1610                 if (rc < 0) {
1611                         fprintf(stderr, "error: %s: #%d - %s\n",
1612                                 jt_cmdname(argv[0]), i, strerror(rc = errno));
1613                         break;
1614                 }
1615                 if (!(data.ioc_obdo1.o_valid & OBD_MD_FLID)) {
1616                         fprintf(stderr,"error: %s: oid not valid #%d:"LPX64"\n",
1617                                 jt_cmdname(argv[0]), i, data.ioc_obdo1.o_valid);
1618                         rc = EINVAL;
1619                         break;
1620                 }
1621
1622                 if (be_verbose(verbose, &next_time, i, &next_count, count))
1623                         printf("%s: #%d is object id "LPX64"\n",
1624                                 jt_cmdname(argv[0]), i, data.ioc_obdo1.o_id);
1625         }
1626         return rc;
1627 }
1628
1629 int jt_obd_setattr(int argc, char **argv)
1630 {
1631         struct obd_ioctl_data data;
1632         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1633         char *end;
1634         int rc;
1635
1636         memset(&data, 0, sizeof(data));
1637         data.ioc_dev = cur_device;
1638         if (argc != 2)
1639                 return CMD_HELP;
1640
1641         data.ioc_obdo1.o_id = strtoull(argv[1], &end, 0);
1642         if (*end) {
1643                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1644                         jt_cmdname(argv[0]), argv[1]);
1645                 return CMD_HELP;
1646         }
1647         data.ioc_obdo1.o_mode = S_IFREG | strtoul(argv[2], &end, 0);
1648         if (*end) {
1649                 fprintf(stderr, "error: %s: invalid mode '%s'\n",
1650                         jt_cmdname(argv[0]), argv[2]);
1651                 return CMD_HELP;
1652         }
1653         data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
1654
1655         memset(buf, 0, sizeof(rawbuf));
1656         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1657         if (rc) {
1658                 fprintf(stderr, "error: %s: invalid ioctl\n",
1659                         jt_cmdname(argv[0]));
1660                 return rc;
1661         }
1662         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, buf);
1663         if (rc < 0)
1664                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1665                         strerror(rc = errno));
1666
1667         return rc;
1668 }
1669
1670 int jt_obd_test_setattr(int argc, char **argv)
1671 {
1672         struct obd_ioctl_data data;
1673         struct timeval start, next_time;
1674         __u64 i, count, next_count;
1675         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1676         int verbose = 1;
1677         obd_id objid = 3;
1678         char *end;
1679         int rc = 0;
1680
1681         if (argc < 2 || argc > 4)
1682                 return CMD_HELP;
1683
1684         memset(&data, 0, sizeof(data));
1685         data.ioc_dev = cur_device;
1686         count = strtoull(argv[1], &end, 0);
1687         if (*end) {
1688                 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1689                         jt_cmdname(argv[0]), argv[1]);
1690                 return CMD_HELP;
1691         }
1692
1693         if (argc >= 3) {
1694                 verbose = get_verbose(argv[0], argv[2]);
1695                 if (verbose == BAD_VERBOSE)
1696                         return CMD_HELP;
1697         }
1698
1699         if (argc >= 4) {
1700                 if (argv[3][0] == 't') {
1701                         objid = strtoull(argv[3] + 1, &end, 0);
1702                         if (thread)
1703                                 objid += thread - 1;
1704                 } else
1705                         objid = strtoull(argv[3], &end, 0);
1706                 if (*end) {
1707                         fprintf(stderr, "error: %s: invalid objid '%s'\n",
1708                                 jt_cmdname(argv[0]), argv[3]);
1709                         return CMD_HELP;
1710                 }
1711         }
1712
1713         gettimeofday(&start, NULL);
1714         next_time.tv_sec = start.tv_sec - verbose;
1715         next_time.tv_usec = start.tv_usec;
1716         if (verbose != 0)
1717                 printf("%s: setting "LPD64" attrs (objid "LPX64"): %s",
1718                        jt_cmdname(argv[0]), count, objid, ctime(&start.tv_sec));
1719
1720         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1721                 data.ioc_obdo1.o_id = objid;
1722                 data.ioc_obdo1.o_mode = S_IFREG;
1723                 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
1724                 memset(buf, 0, sizeof(rawbuf));
1725                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1726                 if (rc) {
1727                         fprintf(stderr, "error: %s: invalid ioctl\n",
1728                                 jt_cmdname(argv[0]));
1729                         return rc;
1730                 }
1731                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, &data);
1732                 shmem_bump(1);
1733                 if (rc < 0) {
1734                         fprintf(stderr, "error: %s: #"LPD64" - %d:%s\n",
1735                                 jt_cmdname(argv[0]), i, errno, strerror(rc = errno));
1736                         break;
1737                 } else {
1738                         if (be_verbose
1739                             (verbose, &next_time, i, &next_count, count))
1740                                 printf("%s: set attr #"LPD64"\n",
1741                                        jt_cmdname(argv[0]), i);
1742                 }
1743         }
1744
1745         if (!rc) {
1746                 struct timeval end;
1747                 double diff;
1748
1749                 gettimeofday(&end, NULL);
1750
1751                 diff = difftime(&end, &start);
1752
1753                 --i;
1754                 if (verbose != 0)
1755                         printf("%s: "LPD64" attrs in %.3fs (%.3f attr/s): %s",
1756                                jt_cmdname(argv[0]), i, diff, i / diff,
1757                                ctime(&end.tv_sec));
1758         }
1759         return rc;
1760 }
1761
1762 int jt_obd_destroy(int argc, char **argv)
1763 {
1764         struct obd_ioctl_data data;
1765         struct timeval next_time;
1766         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1767         __u64 count = 1, next_count;
1768         int verbose = 1;
1769         __u64 id;
1770         char *end;
1771         int rc = 0, i;
1772
1773         memset(&data, 0, sizeof(data));
1774         data.ioc_dev = cur_device;
1775         if (argc < 2 || argc > 4)
1776                 return CMD_HELP;
1777
1778         id = strtoull(argv[1], &end, 0);
1779         if (*end || id == 0 || errno != 0) {
1780                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1781                         jt_cmdname(argv[0]), argv[1]);
1782                 return CMD_HELP;
1783         }
1784         if (argc > 2) {
1785                 count = strtoull(argv[2], &end, 0);
1786                 if (*end) {
1787                         fprintf(stderr,
1788                                 "error: %s: invalid iteration count '%s'\n",
1789                                 jt_cmdname(argv[0]), argv[2]);
1790                         return CMD_HELP;
1791                 }
1792         }
1793
1794         if (argc > 3) {
1795                 verbose = get_verbose(argv[0], argv[3]);
1796                 if (verbose == BAD_VERBOSE)
1797                         return CMD_HELP;
1798         }
1799
1800         printf("%s: "LPD64" objects\n", jt_cmdname(argv[0]), count);
1801         gettimeofday(&next_time, NULL);
1802         next_time.tv_sec -= verbose;
1803
1804         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++, id++) {
1805                 data.ioc_obdo1.o_id = id;
1806                 data.ioc_obdo1.o_mode = S_IFREG | 0644;
1807                 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLMODE;
1808
1809                 memset(buf, 0, sizeof(rawbuf));
1810                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1811                 if (rc) {
1812                         fprintf(stderr, "error: %s: invalid ioctl\n",
1813                                 jt_cmdname(argv[0]));
1814                         return rc;
1815                 }
1816                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_DESTROY, buf);
1817                 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
1818                 shmem_bump(1);
1819                 if (rc < 0) {
1820                         fprintf(stderr, "error: %s: objid "LPX64": %s\n",
1821                                 jt_cmdname(argv[0]), id, strerror(rc = errno));
1822                         break;
1823                 }
1824
1825                 if (be_verbose(verbose, &next_time, i, &next_count, count))
1826                         printf("%s: #%d is object id "LPX64"\n",
1827                                jt_cmdname(argv[0]), i, id);
1828         }
1829
1830         return rc;
1831 }
1832
1833 int jt_obd_getattr(int argc, char **argv)
1834 {
1835         struct obd_ioctl_data data;
1836         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1837         char *end;
1838         int rc;
1839
1840         if (argc != 2)
1841                 return CMD_HELP;
1842
1843         memset(&data, 0, sizeof(data));
1844         data.ioc_dev = cur_device;
1845         data.ioc_obdo1.o_id = strtoull(argv[1], &end, 0);
1846         if (*end) {
1847                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1848                         jt_cmdname(argv[0]), argv[1]);
1849                 return CMD_HELP;
1850         }
1851         /* to help obd filter */
1852         data.ioc_obdo1.o_mode = 0100644;
1853         data.ioc_obdo1.o_valid = 0xffffffff;
1854         printf("%s: object id "LPX64"\n", jt_cmdname(argv[0]),data.ioc_obdo1.o_id);
1855
1856         memset(buf, 0, sizeof(rawbuf));
1857         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1858         if (rc) {
1859                 fprintf(stderr, "error: %s: invalid ioctl\n",
1860                         jt_cmdname(argv[0]));
1861                 return rc;
1862         }
1863         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, buf);
1864         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
1865         if (rc) {
1866                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1867                         strerror(rc = errno));
1868         } else {
1869                 printf("%s: object id "LPX64", mode %o\n", jt_cmdname(argv[0]),
1870                        data.ioc_obdo1.o_id, data.ioc_obdo1.o_mode);
1871         }
1872         return rc;
1873 }
1874
1875 int jt_obd_test_getattr(int argc, char **argv)
1876 {
1877         struct obd_ioctl_data data;
1878         struct timeval start, next_time;
1879         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1880         __u64 i, count, next_count;
1881         int verbose = 1;
1882         obd_id objid = 3;
1883         char *end;
1884         int rc = 0;
1885
1886         if (argc < 2 || argc > 4)
1887                 return CMD_HELP;
1888
1889         memset(&data, 0, sizeof(data));
1890         data.ioc_dev = cur_device;
1891         count = strtoull(argv[1], &end, 0);
1892         if (*end) {
1893                 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1894                         jt_cmdname(argv[0]), argv[1]);
1895                 return CMD_HELP;
1896         }
1897
1898         if (argc >= 3) {
1899                 verbose = get_verbose(argv[0], argv[2]);
1900                 if (verbose == BAD_VERBOSE)
1901                         return CMD_HELP;
1902         }
1903
1904         if (argc >= 4) {
1905                 if (argv[3][0] == 't') {
1906                         objid = strtoull(argv[3] + 1, &end, 0);
1907                         if (thread)
1908                                 objid += thread - 1;
1909                 } else
1910                         objid = strtoull(argv[3], &end, 0);
1911                 if (*end) {
1912                         fprintf(stderr, "error: %s: invalid objid '%s'\n",
1913                                 jt_cmdname(argv[0]), argv[3]);
1914                         return CMD_HELP;
1915                 }
1916         }
1917
1918         gettimeofday(&start, NULL);
1919         next_time.tv_sec = start.tv_sec - verbose;
1920         next_time.tv_usec = start.tv_usec;
1921         if (verbose != 0)
1922                 printf("%s: getting "LPD64" attrs (objid "LPX64"): %s",
1923                        jt_cmdname(argv[0]), count, objid, ctime(&start.tv_sec));
1924
1925         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1926                 data.ioc_obdo1.o_id = objid;
1927                 data.ioc_obdo1.o_mode = S_IFREG;
1928                 data.ioc_obdo1.o_valid = 0xffffffff;
1929                 memset(buf, 0, sizeof(rawbuf));
1930                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1931                 if (rc) {
1932                         fprintf(stderr, "error: %s: invalid ioctl\n",
1933                                 jt_cmdname(argv[0]));
1934                         return rc;
1935                 }
1936                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, &data);
1937                 shmem_bump(1);
1938                 if (rc < 0) {
1939                         fprintf(stderr, "error: %s: #"LPD64" - %d:%s\n",
1940                                 jt_cmdname(argv[0]), i, errno, strerror(rc = errno));
1941                         break;
1942                 } else {
1943                         if (be_verbose
1944                             (verbose, &next_time, i, &next_count, count))
1945                                 printf("%s: got attr #"LPD64"\n",
1946                                        jt_cmdname(argv[0]), i);
1947                 }
1948         }
1949
1950         if (!rc) {
1951                 struct timeval end;
1952                 double diff;
1953
1954                 gettimeofday(&end, NULL);
1955
1956                 diff = difftime(&end, &start);
1957
1958                 --i;
1959                 if (verbose != 0)
1960                         printf("%s: "LPD64" attrs in %.3fs (%.3f attr/s): %s",
1961                                jt_cmdname(argv[0]), i, diff, i / diff,
1962                                ctime(&end.tv_sec));
1963         }
1964         return rc;
1965 }
1966
1967 /* test_brw <cnt>                                               count
1968         <r|w[r(repeat)x(noverify)]>                             mode
1969         <q|v|#(print interval)>                                 verbosity
1970         <npages[+offset]>                                       blocksize
1971         <[[<interleave_threads>]t(inc obj by thread#)]obj>      object
1972         [p|g<args>]                                             batch */
1973 int jt_obd_test_brw(int argc, char **argv)
1974 {
1975         struct obd_ioctl_data data;
1976         struct timeval start, next_time;
1977         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1978         __u64 count, next_count, len, stride, thr_offset = 0, objid = 3;
1979         int write = 0, verbose = 1, cmd, i, rc = 0, pages = 1;
1980         int offset_pages = 0;
1981         long n;
1982         int repeat_offset = 0;
1983         unsigned long long ull;
1984         int  nthr_per_obj = 0;
1985         int  verify = 1;
1986         int  obj_idx = 0;
1987         char *end;
1988
1989         if (argc < 2 || argc > 7) {
1990                 fprintf(stderr, "error: %s: bad number of arguments: %d\n",
1991                         jt_cmdname(argv[0]), argc);
1992                 return CMD_HELP;
1993         }
1994
1995         count = strtoull(argv[1], &end, 0);
1996         if (*end) {
1997                 fprintf(stderr, "error: %s: bad iteration count '%s'\n",
1998                         jt_cmdname(argv[0]), argv[1]);
1999                 return CMD_HELP;
2000         }
2001
2002         if (argc >= 3) {
2003                 if (argv[2][0] == 'w' || argv[2][0] == '1')
2004                         write = 1;
2005                 /* else it's a read */
2006
2007                 if (argv[2][0] != 0)
2008                         for (i = 1; argv[2][i] != 0; i++)
2009                                 switch (argv[2][i]) {
2010                                 case 'r':
2011                                         repeat_offset = 1;
2012                                         break;
2013
2014                                 case 'x':
2015                                         verify = 0;
2016                                         break;
2017
2018                                 default:
2019                                         fprintf (stderr, "Can't parse cmd '%s'\n",
2020                                                  argv[2]);
2021                                         return CMD_HELP;
2022                                 }
2023         }
2024
2025         if (argc >= 4) {
2026                 verbose = get_verbose(argv[0], argv[3]);
2027                 if (verbose == BAD_VERBOSE)
2028                         return CMD_HELP;
2029         }
2030
2031         if (argc >= 5) {
2032                 pages = strtoul(argv[4], &end, 0);
2033
2034                 if (*end == '+')
2035                         offset_pages = strtoul(end + 1, &end, 0);
2036
2037                 if (*end != 0 ||
2038                     offset_pages < 0 || offset_pages >= pages) {
2039                         fprintf(stderr, "error: %s: bad npages[+offset] parameter '%s'\n",
2040                                 jt_cmdname(argv[0]), argv[4]);
2041                         return CMD_HELP;
2042                 }
2043         }
2044
2045         if (argc >= 6) {
2046                 if (thread &&
2047                     (n = strtol(argv[5], &end, 0)) > 0 &&
2048                     *end == 't' &&
2049                     (ull = strtoull(end + 1, &end, 0)) > 0 &&
2050                     *end == 0) {
2051                         nthr_per_obj = n;
2052                         objid = ull;
2053                 } else if (thread &&
2054                            argv[5][0] == 't') {
2055                         nthr_per_obj = 1;
2056                         objid = strtoull(argv[5] + 1, &end, 0);
2057                 } else {
2058                         nthr_per_obj = 0;
2059                         objid = strtoull(argv[5], &end, 0);
2060                 }
2061                 if (*end) {
2062                         fprintf(stderr, "error: %s: bad objid '%s'\n",
2063                                 jt_cmdname(argv[0]), argv[5]);
2064                         return CMD_HELP;
2065                 }
2066         }
2067
2068         memset(&data, 0, sizeof(data));
2069         data.ioc_dev = cur_device;
2070
2071         /* communicate the 'type' of brw test and batching to echo_client.
2072          * don't start.  we'd love to refactor this lctl->echo_client
2073          * interface */
2074         data.ioc_pbuf1 = (void *)1;
2075         data.ioc_plen1 = 1;
2076
2077         if (argc >= 7) {
2078                 switch(argv[6][0]) {
2079                         case 'g': /* plug and unplug */
2080                                 data.ioc_pbuf1 = (void *)2;
2081                                 data.ioc_plen1 = strtoull(argv[6] + 1, &end,
2082                                                           0);
2083                                 break;
2084                         case 'p': /* prep and commit */
2085                                 data.ioc_pbuf1 = (void *)3;
2086                                 data.ioc_plen1 = strtoull(argv[6] + 1, &end,
2087                                                           0);
2088                                 break;
2089                         default:
2090                                 fprintf(stderr, "error: %s: batching '%s' "
2091                                         "needs to specify 'p' or 'g'\n",
2092                                         jt_cmdname(argv[0]), argv[6]);
2093                                 return CMD_HELP;
2094                 }
2095
2096                 if (*end) {
2097                         fprintf(stderr, "error: %s: bad batching '%s'\n",
2098                                 jt_cmdname(argv[0]), argv[6]);
2099                         return CMD_HELP;
2100                 }
2101                 data.ioc_plen1 *= getpagesize();
2102         }
2103
2104         len = pages * getpagesize();
2105         thr_offset = offset_pages * getpagesize();
2106         stride = len;
2107
2108 #ifdef MAX_THREADS
2109         if (thread) {
2110                 shmem_lock ();
2111                 if (nthr_per_obj != 0) {
2112                         /* threads interleave */
2113                         obj_idx = (thread - 1)/nthr_per_obj;
2114                         objid += obj_idx;
2115                         stride *= nthr_per_obj;
2116                         if ((thread - 1) % nthr_per_obj == 0) {
2117                                 shared_data->body.offsets[obj_idx] =
2118                                         stride + thr_offset;
2119                         }
2120                         thr_offset += ((thread - 1) % nthr_per_obj) * len;
2121                 } else {
2122                         /* threads disjoint */
2123                         thr_offset += (thread - 1) * len;
2124                 }
2125
2126                 shmem_start_time_locked();
2127                 shmem_unlock ();
2128         }
2129 #endif
2130
2131         data.ioc_obdo1.o_id = objid;
2132         data.ioc_obdo1.o_mode = S_IFREG;
2133         data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLFLAGS;
2134         data.ioc_obdo1.o_flags = (verify ? OBD_FL_DEBUG_CHECK : 0);
2135         data.ioc_count = len;
2136         data.ioc_offset = (repeat_offset ? 0 : thr_offset);
2137
2138         gettimeofday(&start, NULL);
2139         next_time.tv_sec = start.tv_sec - verbose;
2140         next_time.tv_usec = start.tv_usec;
2141
2142         if (verbose != 0)
2143                 printf("%s: %s "LPU64"x%d pages (obj "LPX64", off "LPU64"): %s",
2144                        jt_cmdname(argv[0]), write ? "writing" : "reading", count,
2145                        pages, objid, data.ioc_offset, ctime(&start.tv_sec));
2146
2147         cmd = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
2148         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
2149                 data.ioc_obdo1.o_valid &= ~(OBD_MD_FLBLOCKS|OBD_MD_FLGRANT);
2150                 memset(buf, 0, sizeof(rawbuf));
2151                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2152                 if (rc) {
2153                         fprintf(stderr, "error: %s: invalid ioctl\n",
2154                                 jt_cmdname(argv[0]));
2155                         return rc;
2156                 }
2157                 rc = l2_ioctl(OBD_DEV_ID, cmd, buf);
2158                 shmem_bump(1);
2159                 if (rc) {
2160                         fprintf(stderr, "error: %s: #%d - %s on %s\n",
2161                                 jt_cmdname(argv[0]), i, strerror(rc = errno),
2162                                 write ? "write" : "read");
2163                         break;
2164                 } else if (be_verbose(verbose, &next_time,i, &next_count,count)) {
2165                         shmem_lock ();
2166                         printf("%s: %s number %d @ "LPD64":"LPU64" for %d\n",
2167                                jt_cmdname(argv[0]), write ? "write" : "read", i,
2168                                data.ioc_obdo1.o_id, data.ioc_offset,
2169                                (int)(pages * getpagesize()));
2170                         shmem_unlock ();
2171                 }
2172
2173                 if (!repeat_offset) {
2174 #ifdef MAX_THREADS
2175                         if (stride == len) {
2176                                 data.ioc_offset += stride;
2177                         } else if (i < count) {
2178                                 shmem_lock ();
2179                                 data.ioc_offset =
2180                                         shared_data->body.offsets[obj_idx];
2181                                 shared_data->body.offsets[obj_idx] += len;
2182                                 shmem_unlock ();
2183                         }
2184 #else
2185                         data.ioc_offset += len;
2186                         obj_idx = 0; /* avoids an unused var warning */
2187 #endif
2188                 }
2189         }
2190
2191         if (!rc) {
2192                 struct timeval end;
2193                 double diff;
2194
2195                 gettimeofday(&end, NULL);
2196
2197                 diff = difftime(&end, &start);
2198
2199                 --i;
2200                 if (verbose != 0)
2201                         printf("%s: %s %dx%d pages in %.3fs (%.3f MB/s): %s",
2202                                jt_cmdname(argv[0]), write ? "wrote" : "read",
2203                                i, pages, diff,
2204                                ((double)i * pages * getpagesize()) /
2205                                (diff * 1048576.0),
2206                                ctime(&end.tv_sec));
2207         }
2208
2209 #ifdef MAX_THREADS
2210         if (thread) {
2211                 shmem_lock();
2212                 shmem_end_time_locked();
2213                 shmem_unlock();
2214         }
2215 #endif
2216         return rc;
2217 }
2218
2219 int jt_obd_lov_getconfig(int argc, char **argv)
2220 {
2221         struct obd_ioctl_data data;
2222         struct lov_desc desc;
2223         struct obd_uuid *uuidarray;
2224         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2225         __u32 *obdgens;
2226         char *path;
2227         int rc, fd;
2228
2229         memset(&data, 0, sizeof(data));
2230         data.ioc_dev = cur_device;
2231
2232         if (argc != 2)
2233                 return CMD_HELP;
2234
2235         path = argv[1];
2236         fd = open(path, O_RDONLY);
2237         if (fd < 0) {
2238                 fprintf(stderr, "open \"%s\" failed: %s\n", path,
2239                         strerror(errno));
2240                 return -errno;
2241         }
2242
2243         memset(&desc, 0, sizeof(desc));
2244         obd_str2uuid(&desc.ld_uuid, argv[1]);
2245         desc.ld_tgt_count = ((OBD_MAX_IOCTL_BUFFER-sizeof(data)-sizeof(desc)) /
2246                              (sizeof(*uuidarray) + sizeof(*obdgens)));
2247
2248 repeat:
2249         uuidarray = calloc(desc.ld_tgt_count, sizeof(*uuidarray));
2250         if (!uuidarray) {
2251                 fprintf(stderr, "error: %s: no memory for %d uuid's\n",
2252                         jt_cmdname(argv[0]), desc.ld_tgt_count);
2253                 rc = -ENOMEM;
2254                 goto out;
2255         }
2256         obdgens = calloc(desc.ld_tgt_count, sizeof(*obdgens));
2257         if (!obdgens) {
2258                 fprintf(stderr, "error: %s: no memory for %d generation #'s\n",
2259                         jt_cmdname(argv[0]), desc.ld_tgt_count);
2260                 rc = -ENOMEM;
2261                 goto out_uuidarray;
2262         }
2263
2264         memset(buf, 0, sizeof(rawbuf));
2265         data.ioc_inllen1 = sizeof(desc);
2266         data.ioc_inlbuf1 = (char *)&desc;
2267         data.ioc_inllen2 = desc.ld_tgt_count * sizeof(*uuidarray);
2268         data.ioc_inlbuf2 = (char *)uuidarray;
2269         data.ioc_inllen3 = desc.ld_tgt_count * sizeof(*obdgens);
2270         data.ioc_inlbuf3 = (char *)obdgens;
2271
2272         if (obd_ioctl_pack(&data, &buf, sizeof(rawbuf))) {
2273                 fprintf(stderr, "error: %s: invalid ioctl\n",
2274                         jt_cmdname(argv[0]));
2275                 rc = -EINVAL;
2276                 goto out_obdgens;
2277         }
2278         rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
2279         if (rc == -ENOSPC) {
2280                 free(uuidarray);
2281                 free(obdgens);
2282                 goto repeat;
2283         } else if (rc) {
2284                 fprintf(stderr, "error: %s: ioctl error: %s\n",
2285                         jt_cmdname(argv[0]), strerror(rc = errno));
2286         } else {
2287                 struct obd_uuid *uuidp;
2288                 __u32 *genp;
2289                 int i;
2290
2291                 if (obd_ioctl_unpack(&data, buf, sizeof(rawbuf))) {
2292                         fprintf(stderr, "error: %s: invalid reply\n",
2293                                 jt_cmdname(argv[0]));
2294                         rc = -EINVAL;
2295                         goto out;
2296                 }
2297                 if (desc.ld_default_stripe_count == (__u32)-1)
2298                         printf("default_stripe_count: %d\n", -1);
2299                 else
2300                         printf("default_stripe_count: %u\n",
2301                                desc.ld_default_stripe_count);
2302                 printf("default_stripe_size: "LPU64"\n",
2303                        desc.ld_default_stripe_size);
2304                 printf("default_stripe_offset: "LPU64"\n",
2305                        desc.ld_default_stripe_offset);
2306                 printf("default_stripe_pattern: %u\n", desc.ld_pattern);
2307                 printf("obd_count: %u\n", desc.ld_tgt_count);
2308                 printf("OBDS:\tobdidx\t\tobdgen\t\t obduuid\n");
2309                 uuidp = uuidarray;
2310                 genp = obdgens;
2311                 for (i = 0; i < desc.ld_tgt_count; i++, uuidp++, genp++)
2312                         printf("\t%6u\t%14u\t\t %s\n", i, *genp, (char *)uuidp);
2313         }
2314 out_obdgens:
2315         free(obdgens);
2316 out_uuidarray:
2317         free(uuidarray);
2318 out:
2319         close(fd);
2320         return rc;
2321 }
2322
2323 int jt_obd_ldlm_regress_start(int argc, char **argv)
2324 {
2325         int rc;
2326         struct obd_ioctl_data data;
2327         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2328         char argstring[200];
2329         int i, count = sizeof(argstring) - 1;
2330
2331         memset(&data, 0, sizeof(data));
2332         data.ioc_dev = cur_device;
2333         if (argc > 5)
2334                 return CMD_HELP;
2335
2336         argstring[0] = '\0';
2337         for (i = 1; i < argc; i++) {
2338                 strncat(argstring, " ", count);
2339                 count--;
2340                 strncat(argstring, argv[i], count);
2341                 count -= strlen(argv[i]);
2342         }
2343
2344         if (strlen(argstring)) {
2345                 data.ioc_inlbuf1 = argstring;
2346                 data.ioc_inllen1 = strlen(argstring) + 1;
2347         }
2348
2349         memset(buf, 0, sizeof(rawbuf));
2350         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2351         if (rc) {
2352                 fprintf(stderr, "error: %s: invalid ioctl\n",
2353                         jt_cmdname(argv[0]));
2354                 return rc;
2355         }
2356         rc = l2_ioctl(OBD_DEV_ID, IOC_LDLM_REGRESS_START, buf);
2357         if (rc)
2358                 fprintf(stderr, "error: %s: test failed: %s\n",
2359                         jt_cmdname(argv[0]), strerror(rc = errno));
2360
2361         return rc;
2362 }
2363
2364 int jt_obd_ldlm_regress_stop(int argc, char **argv)
2365 {
2366         int rc;
2367         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2368         struct obd_ioctl_data data;
2369
2370         memset(&data, 0, sizeof(data));
2371         data.ioc_dev = cur_device;
2372
2373         if (argc != 1)
2374                 return CMD_HELP;
2375
2376         memset(buf, 0, sizeof(rawbuf));
2377         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2378         if (rc) {
2379                 fprintf(stderr, "error: %s: invalid ioctl\n",
2380                         jt_cmdname(argv[0]));
2381                 return rc;
2382         }
2383         rc = l2_ioctl(OBD_DEV_ID, IOC_LDLM_REGRESS_STOP, buf);
2384
2385         if (rc)
2386                 fprintf(stderr, "error: %s: test failed: %s\n",
2387                         jt_cmdname(argv[0]), strerror(rc = errno));
2388         return rc;
2389 }
2390
2391 static int do_activate(int argc, char **argv, int flag)
2392 {
2393         struct obd_ioctl_data data;
2394         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2395         int rc;
2396
2397         memset(&data, 0, sizeof(data));
2398         data.ioc_dev = cur_device;
2399         if (argc != 1)
2400                 return CMD_HELP;
2401
2402         /* reuse offset for 'active' */
2403         data.ioc_offset = flag;
2404
2405         memset(buf, 0, sizeof(rawbuf));
2406         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2407         if (rc) {
2408                 fprintf(stderr, "error: %s: invalid ioctl\n",
2409                         jt_cmdname(argv[0]));
2410                 return rc;
2411         }
2412         rc = l2_ioctl(OBD_DEV_ID, IOC_OSC_SET_ACTIVE, buf);
2413         if (rc)
2414                 fprintf(stderr, "error: %s: failed: %s\n",
2415                         jt_cmdname(argv[0]), strerror(rc = errno));
2416
2417         return rc;
2418 }
2419
2420 int jt_obd_deactivate(int argc, char **argv)
2421 {
2422         return do_activate(argc, argv, 0);
2423 }
2424
2425 int jt_obd_activate(int argc, char **argv)
2426 {
2427         return do_activate(argc, argv, 1);
2428 }
2429
2430 int jt_obd_recover(int argc, char **argv)
2431 {
2432         int rc;
2433         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2434         struct obd_ioctl_data data;
2435
2436         memset(&data, 0, sizeof(data));
2437         data.ioc_dev = cur_device;
2438         if (argc > 2)
2439                 return CMD_HELP;
2440
2441         if (argc == 2) {
2442                 data.ioc_inllen1 = strlen(argv[1]) + 1;
2443                 data.ioc_inlbuf1 = argv[1];
2444         }
2445
2446         memset(buf, 0, sizeof(rawbuf));
2447         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2448         if (rc) {
2449                 fprintf(stderr, "error: %s: invalid ioctl\n",
2450                         jt_cmdname(argv[0]));
2451                 return rc;
2452         }
2453         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_CLIENT_RECOVER, buf);
2454         if (rc < 0) {
2455                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
2456                         strerror(rc = errno));
2457         }
2458
2459         return rc;
2460 }
2461
2462 int jt_obd_mdc_lookup(int argc, char **argv)
2463 {
2464         struct obd_ioctl_data data;
2465         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2466         char *parent, *child;
2467         int rc, fd, verbose = 1;
2468
2469         if (argc < 3 || argc > 4)
2470                 return CMD_HELP;
2471
2472         parent = argv[1];
2473         child = argv[2];
2474         if (argc == 4)
2475                 verbose = get_verbose(argv[0], argv[3]);
2476
2477         memset(&data, 0, sizeof(data));
2478         data.ioc_dev = cur_device;
2479
2480         data.ioc_inllen1 = strlen(child) + 1;
2481         data.ioc_inlbuf1 = child;
2482
2483         memset(buf, 0, sizeof(rawbuf));
2484         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2485         if (rc) {
2486                 fprintf(stderr, "error: %s: invalid ioctl\n",
2487                         jt_cmdname(argv[0]));
2488                 return rc;
2489         }
2490
2491         fd = open(parent, O_RDONLY);
2492         if (fd < 0) {
2493                 fprintf(stderr, "open \"%s\" failed: %s\n", parent,
2494                         strerror(errno));
2495                 return -1;
2496         }
2497
2498         rc = ioctl(fd, IOC_MDC_LOOKUP, buf);
2499         if (rc < 0) {
2500                 fprintf(stderr, "error: %s: ioctl error: %s\n",
2501                         jt_cmdname(argv[0]), strerror(rc = errno));
2502         }
2503         close(fd);
2504
2505         if (verbose) {
2506                 rc = obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
2507                 if (rc) {
2508                         fprintf(stderr, "error: %s: invalid reply\n",
2509                                 jt_cmdname(argv[0]));
2510                         return rc;
2511                 }
2512                 printf("%s: mode %o uid %d gid %d\n", child,
2513                        data.ioc_obdo1.o_mode, data.ioc_obdo1.o_uid,
2514                        data.ioc_obdo1.o_gid);
2515         }
2516
2517         return rc;
2518 }
2519
2520 int jt_cfg_dump_log(int argc, char **argv)
2521 {
2522         struct obd_ioctl_data data;
2523         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2524         int rc;
2525
2526         if (argc != 2)
2527                 return CMD_HELP;
2528
2529         memset(&data, 0, sizeof(data));
2530         data.ioc_dev = cur_device;
2531         data.ioc_inllen1 = strlen(argv[1]) + 1;
2532         data.ioc_inlbuf1 = argv[1];
2533
2534         memset(buf, 0, sizeof(rawbuf));
2535         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2536         if (rc) {
2537                 fprintf(stderr, "error: %s: invalid ioctl\n",
2538                         jt_cmdname(argv[0]));
2539                 return rc;
2540         }
2541         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_DUMP_LOG, buf);
2542         if (rc < 0)
2543                 fprintf(stderr, "OBD_IOC_DUMP_LOG failed: %s\n",
2544                         strerror(errno));
2545
2546         return rc;
2547 }
2548
2549 int jt_llog_catlist(int argc, char **argv)
2550 {
2551         struct obd_ioctl_data data;
2552         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2553         int rc;
2554
2555         if (argc != 1)
2556                 return CMD_HELP;
2557
2558         memset(&data, 0, sizeof(data));
2559         data.ioc_dev = cur_device;
2560         data.ioc_inllen1 = sizeof(rawbuf) - cfs_size_round(sizeof(data));
2561         memset(buf, 0, sizeof(rawbuf));
2562         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2563         if (rc) {
2564                 fprintf(stderr, "error: %s: invalid ioctl\n",
2565                         jt_cmdname(argv[0]));
2566                 return rc;
2567         }
2568         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CATLOGLIST, buf);
2569         if (rc == 0)
2570                 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2571         else
2572                 fprintf(stderr, "OBD_IOC_CATLOGLIST failed: %s\n",
2573                         strerror(errno));
2574
2575         return rc;
2576 }
2577
2578 int jt_llog_info(int argc, char **argv)
2579 {
2580         struct obd_ioctl_data data;
2581         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2582         int rc;
2583
2584         if (argc != 2)
2585                 return CMD_HELP;
2586
2587         memset(&data, 0, sizeof(data));
2588         data.ioc_dev = cur_device;
2589         data.ioc_inllen1 = strlen(argv[1]) + 1;
2590         data.ioc_inlbuf1 = argv[1];
2591         data.ioc_inllen2 = sizeof(rawbuf) - cfs_size_round(sizeof(data)) -
2592                 cfs_size_round(data.ioc_inllen1);
2593         memset(buf, 0, sizeof(rawbuf));
2594         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2595         if (rc) {
2596                 fprintf(stderr, "error: %s: invalid ioctl\n",
2597                         jt_cmdname(argv[0]));
2598                 return rc;
2599         }
2600
2601         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_INFO, buf);
2602         if (rc == 0)
2603                 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2604         else
2605                 fprintf(stderr, "OBD_IOC_LLOG_INFO failed: %s\n",
2606                         strerror(errno));
2607
2608         return rc;
2609 }
2610
2611 int jt_llog_print(int argc, char **argv)
2612 {
2613         struct obd_ioctl_data data;
2614         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2615         int rc;
2616
2617         if (argc != 2 && argc != 4)
2618                 return CMD_HELP;
2619
2620         memset(&data, 0, sizeof(data));
2621         data.ioc_dev = cur_device;
2622         data.ioc_inllen1 = strlen(argv[1]) + 1;
2623         data.ioc_inlbuf1 = argv[1];
2624         if (argc == 4) {
2625                 data.ioc_inllen2 = strlen(argv[2]) + 1;
2626                 data.ioc_inlbuf2 = argv[2];
2627                 data.ioc_inllen3 = strlen(argv[3]) + 1;
2628                 data.ioc_inlbuf3 = argv[3];
2629         } else {
2630                 char from[2] = "1", to[3] = "-1";
2631                 data.ioc_inllen2 = strlen(from) + 1;
2632                 data.ioc_inlbuf2 = from;
2633                 data.ioc_inllen3 = strlen(to) + 1;
2634                 data.ioc_inlbuf3 = to;
2635         }
2636         data.ioc_inllen4 = sizeof(rawbuf) - cfs_size_round(sizeof(data)) -
2637                 cfs_size_round(data.ioc_inllen1) -
2638                 cfs_size_round(data.ioc_inllen2) -
2639                 cfs_size_round(data.ioc_inllen3);
2640         memset(buf, 0, sizeof(rawbuf));
2641         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2642         if (rc) {
2643                 fprintf(stderr, "error: %s: invalid ioctl\n",
2644                         jt_cmdname(argv[0]));
2645                 return rc;
2646         }
2647
2648         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_PRINT, buf);
2649         if (rc == 0)
2650                 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2651         else
2652                 fprintf(stderr, "OBD_IOC_LLOG_PRINT failed: %s\n",
2653                         strerror(errno));
2654
2655         return rc;
2656 }
2657
2658 int jt_llog_cancel(int argc, char **argv)
2659 {
2660         struct obd_ioctl_data data;
2661         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2662         int rc;
2663
2664         if (argc != 4)
2665                 return CMD_HELP;
2666
2667         memset(&data, 0, sizeof(data));
2668         data.ioc_dev = cur_device;
2669         data.ioc_inllen1 = strlen(argv[1]) + 1;
2670         data.ioc_inlbuf1 = argv[1];
2671         data.ioc_inllen2 = strlen(argv[2]) + 1;
2672         data.ioc_inlbuf2 = argv[2];
2673         data.ioc_inllen3 = strlen(argv[3]) + 1;
2674         data.ioc_inlbuf3 = argv[3];
2675         memset(buf, 0, sizeof(rawbuf));
2676         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2677         if (rc) {
2678                 fprintf(stderr, "error: %s: invalid ioctl\n",
2679                         jt_cmdname(argv[0]));
2680                 return rc;
2681         }
2682
2683         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CANCEL, buf);
2684         if (rc == 0)
2685                 fprintf(stdout, "index %s be canceled.\n", argv[3]);
2686         else
2687                 fprintf(stderr, "OBD_IOC_LLOG_CANCEL failed: %s\n",
2688                         strerror(errno));
2689
2690         return rc;
2691
2692 }
2693 int jt_llog_check(int argc, char **argv)
2694 {
2695         struct obd_ioctl_data data;
2696         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2697         int rc;
2698
2699         if (argc != 2 && argc != 4)
2700                 return CMD_HELP;
2701
2702         memset(&data, 0, sizeof(data));
2703         data.ioc_dev = cur_device;
2704         data.ioc_inllen1 = strlen(argv[1]) + 1;
2705         data.ioc_inlbuf1 = argv[1];
2706         if (argc == 4) {
2707                 data.ioc_inllen2 = strlen(argv[2]) + 1;
2708                 data.ioc_inlbuf2 = argv[2];
2709                 data.ioc_inllen3 = strlen(argv[3]) + 1;
2710                 data.ioc_inlbuf3 = argv[3];
2711         } else {
2712                 char from[2] = "1", to[3] = "-1";
2713                 data.ioc_inllen2 = strlen(from) + 1;
2714                 data.ioc_inlbuf2 = from;
2715                 data.ioc_inllen3 = strlen(to) + 1;
2716                 data.ioc_inlbuf3 = to;
2717         }
2718         data.ioc_inllen4 = sizeof(rawbuf) - cfs_size_round(sizeof(data)) -
2719                 cfs_size_round(data.ioc_inllen1) -
2720                 cfs_size_round(data.ioc_inllen2) -
2721                 cfs_size_round(data.ioc_inllen3);
2722         memset(buf, 0, sizeof(rawbuf));
2723         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2724         if (rc) {
2725                 fprintf(stderr, "error: %s: invalid ioctl\n",
2726                         jt_cmdname(argv[0]));
2727                 return rc;
2728         }
2729
2730         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CHECK, buf);
2731         if (rc == 0)
2732                 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2733         else
2734                 fprintf(stderr, "OBD_IOC_LLOG_CHECK failed: %s\n",
2735                         strerror(errno));
2736         return rc;
2737 }
2738
2739 int jt_llog_remove(int argc, char **argv)
2740 {
2741         struct obd_ioctl_data data;
2742         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2743         int rc;
2744
2745         if (argc != 3 && argc != 2)
2746                 return CMD_HELP;
2747
2748         memset(&data, 0, sizeof(data));
2749         data.ioc_dev = cur_device;
2750         data.ioc_inllen1 = strlen(argv[1]) + 1;
2751         data.ioc_inlbuf1 = argv[1];
2752         if (argc == 3){
2753                 data.ioc_inllen2 = strlen(argv[2]) + 1;
2754                 data.ioc_inlbuf2 = argv[2];
2755         }
2756         memset(buf, 0, sizeof(rawbuf));
2757         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2758         if (rc) {
2759                 fprintf(stderr, "error: %s: invalid ioctl\n",
2760                         jt_cmdname(argv[0]));
2761                 return rc;
2762         }
2763
2764         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_REMOVE, buf);
2765         if (rc == 0) {
2766                 if (argc == 3)
2767                         fprintf(stdout, "log %s are removed.\n", argv[2]);
2768                 else
2769                         fprintf(stdout, "the log in catalog %s are removed. \n", argv[1]);
2770         } else
2771                 fprintf(stderr, "OBD_IOC_LLOG_REMOVE failed: %s\n",
2772                         strerror(errno));
2773
2774         return rc;
2775 }
2776
2777 /* attach a regular file to virtual block device.
2778  * return vaule:
2779  *  -1: fatal error
2780  *  1: error, it always means the command run failed
2781  *  0: success
2782  */
2783 static int jt_blockdev_run_process(const char *file, char *argv[])
2784 {
2785         pid_t pid;
2786         int rc;
2787
2788         pid = vfork();
2789         if (pid == 0) { /* child process */
2790                 /* don't print error messages */
2791                 close(1), close(2);
2792                 (void)execvp(file, argv);
2793                 exit(-1);
2794         } else if (pid > 0) {
2795                 int status;
2796
2797                 rc = waitpid(pid, &status, 0);
2798                 if (rc < 0 || !WIFEXITED(status))
2799                         return -1;
2800
2801                 return WEXITSTATUS(status);
2802         }
2803
2804         return -1;
2805 }
2806
2807 static int jt_blockdev_find_module(const char *module)
2808 {
2809         FILE *fp;
2810         int found = 0;
2811         char buf[1024];
2812
2813         fp = fopen("/proc/modules", "r");
2814         if (fp == NULL)
2815                 return -1;
2816
2817         while (fgets(buf, 1024, fp) != NULL) {
2818                 *strchr(buf, ' ') = 0;
2819                 if (strcmp(module, buf) == 0) {
2820                         found = 1;
2821                         break;
2822                 }
2823         }
2824         fclose(fp);
2825
2826         return found;
2827 }
2828
2829 static int jt_blockdev_probe_module(const char *module)
2830 {
2831         char buf[1024];
2832         char *argv[10];
2833         int c, rc;
2834
2835         if (jt_blockdev_find_module(module) == 1)
2836                 return 0;
2837
2838         /* run modprobe first */
2839         c = 0;
2840         argv[c++] = "/sbin/modprobe";
2841         argv[c++] = "-q";
2842         argv[c++] = (char *)module;
2843         argv[c++] = NULL;
2844         rc = jt_blockdev_run_process("modprobe", argv);
2845         if (rc != 1)
2846                 return rc;
2847
2848         /* cannot find the module in default directory ... */
2849         sprintf(buf, "../llite/%s.ko", module);
2850         c = 0;
2851         argv[c++] = "/sbin/insmod";
2852         argv[c++] = buf;
2853         argv[c++] = NULL;
2854         rc = jt_blockdev_run_process("insmod", argv);
2855         return rc ? -1 : 0;
2856 }
2857
2858 int jt_blockdev_attach(int argc, char **argv)
2859 {
2860         int rc, fd;
2861         struct stat st;
2862         char *filename, *devname;
2863         unsigned long dev;
2864
2865         if (argc != 3)
2866                 return CMD_HELP;
2867
2868         if (jt_blockdev_probe_module("llite_lloop") < 0) {
2869                 fprintf(stderr, "error: cannot find module llite_lloop.(k)o\n");
2870                 return ENOENT;
2871         }
2872
2873         filename = argv[1];
2874         devname = argv[2];
2875
2876         fd = open(filename, O_RDWR);
2877         if (fd < 0) {
2878                 fprintf(stderr, "file %s can't be opened(%s)\n\n",
2879                         filename, strerror(errno));
2880                 return CMD_HELP;
2881         }
2882
2883         rc = ioctl(fd, LL_IOC_LLOOP_ATTACH, &dev);
2884         if (rc < 0) {
2885                 rc = errno;
2886                 fprintf(stderr, "attach error(%s)\n", strerror(rc));
2887                 goto out;
2888         }
2889
2890         rc = stat(devname, &st);
2891         if (rc == 0 && (!S_ISBLK(st.st_mode) || st.st_rdev != dev)) {
2892                 rc = EEXIST;
2893         } else if (rc < 0) {
2894                 if (errno == ENOENT &&
2895                     !mknod(devname, S_IFBLK|S_IRUSR|S_IWUSR, dev))
2896                         rc = 0;
2897                 else
2898                         rc = errno;
2899         }
2900
2901         if (rc) {
2902                 fprintf(stderr, "error: the file %s could be attached to block "
2903                                 "device %X but creating %s failed: %s\n"
2904                                 "now detaching the block device..",
2905                         filename, (int)dev, devname, strerror(rc));
2906
2907                 (void)ioctl(fd, LL_IOC_LLOOP_DETACH_BYDEV, dev);
2908                 fprintf(stderr, "%s\n", strerror(errno));
2909         }
2910 out:
2911         close(fd);
2912         return -rc;
2913 }
2914
2915 int jt_blockdev_detach(int argc, char **argv)
2916 {
2917         char *filename;
2918         int rc, fd;
2919
2920         if (argc != 2)
2921                 return CMD_HELP;
2922
2923         filename = argv[1];
2924         fd = open(filename, O_RDONLY);
2925         if (fd < 0) {
2926                 fprintf(stderr, "cannot open file %s error %s\n",
2927                         filename, strerror(errno));
2928                 return CMD_HELP;
2929         }
2930
2931         rc = ioctl(fd, LL_IOC_LLOOP_DETACH, 0);
2932         if (rc < 0) {
2933                 rc = errno;
2934                 fprintf(stderr, "detach error(%s)\n", strerror(rc));
2935         } else {
2936                 (void)unlink(filename);
2937         }
2938
2939         close(fd);
2940         return -rc;
2941 }
2942
2943 int jt_blockdev_info(int argc, char **argv)
2944 {
2945         char *filename;
2946         int rc, fd;
2947         struct lu_fid fid;
2948
2949         if (argc != 2)
2950                 return CMD_HELP;
2951
2952         filename = argv[1];
2953         fd = open(filename, O_RDONLY);
2954         if (fd < 0) {
2955                 fprintf(stderr, "cannot open file %s error: %s\n",
2956                         filename, strerror(errno));
2957                 return CMD_HELP;
2958         }
2959
2960         rc = ioctl(fd, LL_IOC_LLOOP_INFO, &fid);
2961         if (rc < 0) {
2962                 rc = errno;
2963                 fprintf(stderr, "error: %s\n", strerror(errno));
2964                 goto out;
2965         }
2966         fprintf(stdout, "lloop device info: ");
2967         if (fid_is_zero(&fid))
2968                 fprintf(stdout, "Not attached\n");
2969         else
2970                 fprintf(stdout, "attached to inode "DFID"\n", PFID(&fid));
2971 out:
2972         close(fd);
2973         return -rc;
2974 }
2975
2976 static void signal_server(int sig)
2977 {
2978         if (sig == SIGINT) {
2979                 do_disconnect("sigint", 1);
2980                 exit(1);
2981         } else
2982                 fprintf(stderr, "%s: got signal %d\n", jt_cmdname("sigint"), sig);
2983 }
2984
2985 int obd_initialize(int argc, char **argv)
2986 {
2987         int i;
2988
2989         for (i = 0; i < LOV_MAX_STRIPE_COUNT; i++)
2990                 lsm_buffer.lsm.lsm_oinfo[i] = lov_oinfos + i;
2991
2992         if (shmem_setup() != 0)
2993                 return -1;
2994
2995         register_ioc_dev(OBD_DEV_ID, OBD_DEV_PATH,
2996                          OBD_DEV_MAJOR, OBD_DEV_MINOR);
2997
2998         return 0;
2999 }
3000
3001 void obd_finalize(int argc, char **argv)
3002 {
3003         struct sigaction sigact;
3004
3005         /* sigact initialization */
3006         sigact.sa_handler = signal_server;
3007         sigfillset(&sigact.sa_mask);
3008         sigact.sa_flags = SA_RESTART;
3009         /* coverity[uninit_use_in_call] */
3010         sigaction(SIGINT, &sigact, NULL);
3011
3012         shmem_cleanup();
3013         do_disconnect(argv[0], 1);
3014 }
3015
3016 static int check_pool_cmd(enum lcfg_command_type cmd,
3017                           char *fsname, char *poolname,
3018                           char *ostname)
3019 {
3020         int rc;
3021
3022         rc = llapi_search_ost(fsname, poolname, ostname);
3023         if (rc < 0 && (cmd != LCFG_POOL_NEW)) {
3024                 fprintf(stderr, "Pool %s.%s not found\n",
3025                         fsname, poolname);
3026                 return rc;
3027         }
3028
3029         switch (cmd) {
3030         case LCFG_POOL_NEW: {
3031                 LASSERT(ostname == NULL);
3032                 if (rc >= 0) {
3033                         fprintf(stderr, "Pool %s.%s already exists\n",
3034                                 fsname, poolname);
3035                         return -EEXIST;
3036                 }
3037                 return 0;
3038         }
3039         case LCFG_POOL_DEL: {
3040                 LASSERT(ostname == NULL);
3041                 if (rc == 1) {
3042                         fprintf(stderr, "Pool %s.%s not empty, "
3043                                 "please remove all members\n",
3044                                 fsname, poolname);
3045                         return -ENOTEMPTY;
3046                 }
3047                 return 0;
3048         }
3049         case LCFG_POOL_ADD: {
3050                 if (rc == 1) {
3051                         fprintf(stderr, "OST %s is already in pool %s.%s\n",
3052                                 ostname, fsname, poolname);
3053                         return -EEXIST;
3054                 }
3055                 rc = llapi_search_ost(fsname, NULL, ostname);
3056                 if (rc == 0) {
3057                         fprintf(stderr, "OST %s is not part of the '%s' fs.\n",
3058                                 ostname, fsname);
3059                         return -ENOENT;
3060                 }
3061                 return 0;
3062         }
3063         case LCFG_POOL_REM: {
3064                 if (rc == 0) {
3065                         fprintf(stderr, "OST %s not found in pool %s.%s\n",
3066                                 ostname, fsname, poolname);
3067                         return -ENOENT;
3068                 }
3069                 return 0;
3070         }
3071         default:
3072                 break;
3073         } /* switch */
3074         return -EINVAL;
3075 }
3076
3077 /* This check only verifies that the changes have been "pushed out" to
3078    the client successfully.  This involves waiting for a config update,
3079    and so may fail because of problems in that code or post-command
3080    network loss. So reporting a warning is appropriate, but not a failure.
3081 */
3082 static int check_pool_cmd_result(enum lcfg_command_type cmd,
3083                                  char *fsname, char *poolname,
3084                                  char *ostname)
3085 {
3086         int cpt = 10;
3087         int rc = 0;
3088
3089         switch (cmd) {
3090         case LCFG_POOL_NEW: {
3091                 do {
3092                         rc = llapi_search_ost(fsname, poolname, NULL);
3093                         if (rc == -ENODEV)
3094                                 return rc;
3095                         if (rc < 0)
3096                                 sleep(2);
3097                         cpt--;
3098                 } while ((rc < 0) && (cpt > 0));
3099                 if (rc >= 0) {
3100                         fprintf(stderr, "Pool %s.%s created\n",
3101                                 fsname, poolname);
3102                         return 0;
3103                 } else {
3104                         fprintf(stderr, "Warning, pool %s.%s not found\n",
3105                                 fsname, poolname);
3106                         return -ENOENT;
3107                 }
3108         }
3109         case LCFG_POOL_DEL: {
3110                 do {
3111                         rc = llapi_search_ost(fsname, poolname, NULL);
3112                         if (rc == -ENODEV)
3113                                 return rc;
3114                         if (rc >= 0)
3115                                 sleep(2);
3116                         cpt--;
3117                 } while ((rc >= 0) && (cpt > 0));
3118                 if (rc < 0) {
3119                         fprintf(stderr, "Pool %s.%s destroyed\n",
3120                                 fsname, poolname);
3121                         return 0;
3122                 } else {
3123                         fprintf(stderr, "Warning, pool %s.%s still found\n",
3124                                 fsname, poolname);
3125                         return -EEXIST;
3126                 }
3127         }
3128         case LCFG_POOL_ADD: {
3129                 do {
3130                         rc = llapi_search_ost(fsname, poolname, ostname);
3131                         if (rc == -ENODEV)
3132                                 return rc;
3133                         if (rc != 1)
3134                                 sleep(2);
3135                         cpt--;
3136                 } while ((rc != 1) && (cpt > 0));
3137                 if (rc == 1) {
3138                         fprintf(stderr, "OST %s added to pool %s.%s\n",
3139                                 ostname, fsname, poolname);
3140                         return 0;
3141                 } else {
3142                         fprintf(stderr, "Warning, OST %s not found in pool %s.%s\n",
3143                                 ostname, fsname, poolname);
3144                         return -ENOENT;
3145                 }
3146         }
3147         case LCFG_POOL_REM: {
3148                 do {
3149                         rc = llapi_search_ost(fsname, poolname, ostname);
3150                         if (rc == -ENODEV)
3151                                 return rc;
3152                         if (rc == 1)
3153                                 sleep(2);
3154                         cpt--;
3155                 } while ((rc == 1) && (cpt > 0));
3156                 if (rc != 1) {
3157                         fprintf(stderr, "OST %s removed from pool %s.%s\n",
3158                                 ostname, fsname, poolname);
3159                         return 0;
3160                 } else {
3161                         fprintf(stderr, "Warning, OST %s still found in pool %s.%s\n",
3162                                 ostname, fsname, poolname);
3163                         return -EEXIST;
3164                 }
3165         }
3166         default:
3167                 break;
3168         }
3169         return -EINVAL;
3170 }
3171
3172 static int check_and_complete_ostname(char *fsname, char *ostname)
3173 {
3174         char *ptr;
3175         char real_ostname[MAX_OBD_NAME + 1];
3176         char i;
3177
3178         /* if OST name does not start with fsname, we add it */
3179         /* if not check if the fsname is the right one */
3180         ptr = strchr(ostname, '-');
3181         if (ptr == NULL) {
3182                 sprintf(real_ostname, "%s-%s", fsname, ostname);
3183         } else if (strncmp(ostname, fsname, strlen(fsname)) != 0) {
3184                 fprintf(stderr, "%s does not start with fsname %s\n",
3185                         ostname, fsname);
3186                 return -EINVAL;
3187         } else {
3188              strcpy(real_ostname, ostname);
3189         }
3190         /* real_ostname is fsname-????? */
3191         ptr = real_ostname + strlen(fsname) + 1;
3192         if (strncmp(ptr, "OST", 3) != 0) {
3193                 fprintf(stderr, "%s does not start by %s-OST nor OST\n",
3194                         ostname, fsname);
3195                 return -EINVAL;
3196         }
3197         /* real_ostname is fsname-OST????? */
3198         ptr += 3;
3199         for (i = 0; i < 4; i++) {
3200                 if (!isxdigit(*ptr)) {
3201                         fprintf(stderr,
3202                                 "ost's index in %s is not an hexa number\n",
3203                                 ostname);
3204                         return -EINVAL;
3205                 }
3206                 ptr++;
3207         }
3208         /* real_ostname is fsname-OSTXXXX????? */
3209         /* if OST name does not end with _UUID, we add it */
3210         if (*ptr == '\0') {
3211                 strcat(real_ostname, "_UUID");
3212         } else if (strcmp(ptr, "_UUID") != 0) {
3213                 fprintf(stderr,
3214                         "ostname %s does not end with _UUID\n", ostname);
3215                 return -EINVAL;
3216         }
3217         /* real_ostname is fsname-OSTXXXX_UUID */
3218         strcpy(ostname, real_ostname);
3219         return 0;
3220 }
3221
3222 /* returns 0 or -errno */
3223 static int pool_cmd(enum lcfg_command_type cmd,
3224                     char *cmdname, char *fullpoolname,
3225                     char *fsname, char *poolname, char *ostname)
3226 {
3227         int rc = 0;
3228         struct obd_ioctl_data data;
3229         struct lustre_cfg_bufs bufs;
3230         struct lustre_cfg *lcfg;
3231         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
3232
3233         rc = check_pool_cmd(cmd, fsname, poolname, ostname);
3234         if (rc == -ENODEV)
3235                 fprintf(stderr, "Can't verify pool command since there "
3236                         "is no local MDT or client, proceeding anyhow...\n");
3237         else if (rc)
3238                 return rc;
3239
3240         lustre_cfg_bufs_reset(&bufs, NULL);
3241         lustre_cfg_bufs_set_string(&bufs, 0, cmdname);
3242         lustre_cfg_bufs_set_string(&bufs, 1, fullpoolname);
3243         if (ostname != NULL)
3244                 lustre_cfg_bufs_set_string(&bufs, 2, ostname);
3245
3246         lcfg = lustre_cfg_new(cmd, &bufs);
3247         if (IS_ERR(lcfg)) {
3248                 rc = PTR_ERR(lcfg);
3249                 return rc;
3250         }
3251
3252         memset(&data, 0, sizeof(data));
3253         rc = data.ioc_dev = get_mgs_device();
3254         if (rc < 0)
3255                 goto out;
3256
3257         data.ioc_type = LUSTRE_CFG_TYPE;
3258         data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
3259                                         lcfg->lcfg_buflens);
3260         data.ioc_pbuf1 = (void *)lcfg;
3261
3262         memset(buf, 0, sizeof(rawbuf));
3263         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
3264         if (rc) {
3265                 fprintf(stderr, "error: %s: invalid ioctl\n",
3266                         jt_cmdname(cmdname));
3267                 return rc;
3268         }
3269         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_POOL, buf);
3270 out:
3271         if (rc)
3272                 rc = -errno;
3273         lustre_cfg_free(lcfg);
3274         return rc;
3275 }
3276
3277 /*
3278  * this function tranforms a rule [start-end/step] into an array
3279  * of matching numbers
3280  * supported forms are:
3281  * [start]                : just this number
3282  * [start-end]            : all numbers from start to end
3283  * [start-end/step]       : numbers from start to end with increment of step
3284  * on return, format contains a printf format string which can be used
3285  * to generate all the strings
3286  */
3287 static int get_array_idx(char *rule, char *format, int **array)
3288 {
3289         char *start, *end, *ptr;
3290         unsigned int lo, hi, step;
3291         int array_sz = 0;
3292         int i, array_idx;
3293         int rc;
3294
3295         start = strchr(rule, '[');
3296         end = strchr(rule, ']');
3297         if ((start == NULL) || (end == NULL)) {
3298                 *array = malloc(sizeof(int));
3299                 if (*array == NULL)
3300                         return 0;
3301                 strcpy(format, rule);
3302                 array_sz = 1;
3303                 return array_sz;
3304         }
3305         *start = '\0';
3306         *end = '\0';
3307         end++;
3308         start++;
3309         /* put in format the printf format (the rule without the range) */
3310         sprintf(format, "%s%%.4x%s", rule, end);
3311
3312         array_idx = 0;
3313         array_sz = 0;
3314         *array = NULL;
3315         /* loop on , separator */
3316         do {
3317                 /* extract the 3 fields */
3318                 rc = sscanf(start, &quo