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