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