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