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