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