Whamcloud - gitweb
LU-6210 utils: Use C99 initializer in jt_nodemap_del_idmap()
[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         { .val = 'b',   .name = "child_base_id",
1155                                                 .has_arg = required_argument },
1156         { .val = 'c',   .name = "stripe_count",
1157                                                 .has_arg = required_argument },
1158         { .val = 'd',   .name = "parent_basedir",
1159                                                 .has_arg = required_argument },
1160         { .val = 'D',   .name = "parent_dircount",
1161                                                 .has_arg = required_argument },
1162         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument },
1163         { .val = 'm',   .name = "mode",         .has_arg = required_argument },
1164         { .val = 'n',   .name = "count",        .has_arg = required_argument },
1165         { .val = 't',   .name = "time",         .has_arg = required_argument },
1166         { .val = 'v',   .name = "version",      .has_arg = no_argument },
1167         { .name = NULL } };
1168
1169         while ((c = getopt_long(argc, argv, "b:c:d:D:m:n:t:v",
1170                                 long_opts, NULL)) >= 0) {
1171                 switch (c) {
1172                 case 'b':
1173                         child_base_id = strtoull(optarg, &end, 0);
1174                         if (*end) {
1175                                 fprintf(stderr, "error: %s: bad child_base_id"
1176                                         " '%s'\n", jt_cmdname(argv[0]), optarg);
1177                                 return CMD_HELP;
1178                         }
1179                         break;
1180                 case 'c':
1181                         stripe_count = strtoul(optarg, &end, 0);
1182                         if (*end) {
1183                                 fprintf(stderr, "error: %s: bad stripe count"
1184                                         " '%s'\n", jt_cmdname(argv[0]), optarg);
1185                                 return CMD_HELP;
1186                         }
1187                         break;
1188                 case 'd':
1189                         parent_basedir = optarg;
1190                         break;
1191                 case 'D':
1192                         parent_count = strtoul(optarg, &end, 0);
1193                         if (*end) {
1194                                 fprintf(stderr, "error: %s: bad parent count"
1195                                         " '%s'\n", jt_cmdname(argv[0]), optarg);
1196                                 return CMD_HELP;
1197                         }
1198                         break;
1199                 case 'i':
1200                         stripe_index = strtoul(optarg, &end, 0);
1201                         if (*end) {
1202                                 fprintf(stderr, "error: %s: bad stripe index"
1203                                         " '%s'\n", jt_cmdname(argv[0]), optarg);
1204                                 return CMD_HELP;
1205                         }
1206                         break;
1207                 case 'm':
1208                         mode = strtoul(optarg, &end, 0);
1209                         if (*end) {
1210                                 fprintf(stderr, "error: %s: bad mode '%s'\n",
1211                                         jt_cmdname(argv[0]), optarg);
1212                                 return CMD_HELP;
1213                         }
1214                         break;
1215                 case 'n':
1216                         total_count = strtoul(optarg, &end, 0);
1217                         if (*end || total_count == 0) {
1218                                 fprintf(stderr, "%s: bad child count '%s'\n",
1219                                         jt_cmdname(argv[0]), optarg);
1220                                 return CMD_HELP;
1221                         }
1222                         break;
1223                 case 't':
1224                         seconds = strtoull(optarg, &end, 0);
1225                         if (*end) {
1226                                 fprintf(stderr, "error: %s: seconds '%s'\n",
1227                                         jt_cmdname(argv[0]), optarg);
1228                                 return CMD_HELP;
1229                         }
1230                         break;
1231                 case 'v':
1232                         version = 1;
1233                         break;
1234                 default:
1235                         fprintf(stderr, "error: %s: option '%s' "
1236                                 "unrecognized\n", argv[0], argv[optind - 1]);
1237                         return CMD_HELP;
1238                 }
1239         }
1240
1241         memset(&data, 0, sizeof(data));
1242         data.ioc_dev = cur_device;
1243         if (child_base_id == -1) {
1244                 if (optind >= argc)
1245                         return CMD_HELP;
1246                 name = argv[optind];
1247                 total_count = 1;
1248         } else {
1249                 if (optind < argc) {
1250                         fprintf(stderr, "child_base_id and name can not"
1251                                         " specified at the same time\n");
1252                         return CMD_HELP;
1253                 }
1254         }
1255
1256         if (stripe_count == 0 && stripe_index != -1) {
1257                 fprintf(stderr, "If stripe_count is 0, stripe_index can not"
1258                                 "be specified\n");
1259                 return CMD_HELP;
1260         }
1261
1262         if (total_count == 0 && seconds == 0) {
1263                 fprintf(stderr, "count or seconds needs to be indicated\n");
1264                 return CMD_HELP;
1265         }
1266
1267         if (parent_count <= 0) {
1268                 fprintf(stderr, "parent count must < 0\n");
1269                 return CMD_HELP;
1270         }
1271
1272 #ifdef MAX_THREADS
1273         if (thread) {
1274                 shmem_lock();
1275                 /* threads interleave */
1276                 if (parent_base_id != -1)
1277                         parent_base_id += (thread - 1) % parent_count;
1278
1279                 if (child_base_id != -1)
1280                         child_base_id +=  (thread - 1) * \
1281                                           (MAX_BASE_ID / nthreads);
1282
1283                 shmem_start_time_locked();
1284                 shmem_unlock();
1285         }
1286 #endif
1287         /* If parent directory is not specified, try to get the directory
1288          * from name */
1289         if (parent_basedir == NULL) {
1290                 char *last_lash;
1291                 if (name == NULL) {
1292                         fprintf(stderr, "parent_basedir or name must be"
1293                                         "indicated!\n");
1294                         return CMD_HELP;
1295                 }
1296                 /*Get directory and name from name*/
1297                 last_lash = strrchr(name, '/');
1298                 if (last_lash == NULL || name[0] != '/') {
1299                         fprintf(stderr, "Can not locate %s\n", name);
1300                         return CMD_HELP;
1301                 }
1302
1303                 if (last_lash == name) {
1304                         sprintf(dirname, "%s", "/");
1305                         name++;
1306                 } else {
1307                         int namelen = (unsigned long)last_lash -
1308                                       (unsigned long)name + 1;
1309                         snprintf(dirname, namelen, "%s", name);
1310                         name = last_lash + 1;
1311                 }
1312
1313                 data.ioc_pbuf1 = dirname;
1314                 data.ioc_plen1 = strlen(dirname);
1315
1316                 data.ioc_pbuf2 = name;
1317                 data.ioc_plen2 = strlen(name);
1318         } else {
1319                 if (name != NULL) {
1320                         data.ioc_pbuf2 = name;
1321                         data.ioc_plen2 = strlen(name);
1322                 }
1323                 if (parent_base_id > 0)
1324                         sprintf(dirname, "%s%d", parent_basedir,
1325                                 parent_base_id);
1326                 else
1327                         sprintf(dirname, "%s", parent_basedir);
1328                 data.ioc_pbuf1 = dirname;
1329                 data.ioc_plen1 = strlen(dirname);
1330         }
1331
1332         if (cmd == ECHO_MD_MKDIR || cmd == ECHO_MD_RMDIR)
1333                 create_mode = S_IFDIR;
1334         else
1335                 create_mode = S_IFREG;
1336
1337         data.ioc_obdo1.o_mode = mode | S_IFDIR;
1338         data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
1339                                  OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1340         data.ioc_command = cmd;
1341
1342         gettimeofday(&start, NULL);
1343         while (shmem_running()) {
1344                 struct lu_fid fid = { 0 };
1345
1346                 if (child_base_id != -1)
1347                         data.ioc_obdo2.o_oi.oi.oi_id = child_base_id;
1348                 data.ioc_obdo2.o_mode = mode | create_mode;
1349                 data.ioc_obdo2.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
1350                                          OBD_MD_FLMODE | OBD_MD_FLFLAGS |
1351                                          OBD_MD_FLGROUP;
1352                 data.ioc_obdo2.o_misc = stripe_count;
1353                 data.ioc_obdo2.o_stripe_idx = stripe_index;
1354
1355                 if (total_count > 0) {
1356                         if ((total_count - count) > MD_STEP_COUNT)
1357                                 data.ioc_count = MD_STEP_COUNT;
1358                         else
1359                                 data.ioc_count = total_count - count;
1360                 } else {
1361                         data.ioc_count = MD_STEP_COUNT;
1362                 }
1363
1364                 if (cmd == ECHO_MD_CREATE || cmd == ECHO_MD_MKDIR) {
1365                         /*Allocate fids for the create */
1366                         rc = jt_obd_alloc_fids(&fid_space, &fid,
1367                                                &data.ioc_count);
1368                         if (rc) {
1369                                 fprintf(stderr, "Allocate fids error %d.\n",rc);
1370                                 return rc;
1371                         }
1372                         data.ioc_obdo1.o_oi.oi_fid = fid;
1373                 }
1374
1375                 child_base_id += data.ioc_count;
1376                 count += data.ioc_count;
1377
1378                 memset(buf, 0, sizeof(rawbuf));
1379                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1380                 if (rc) {
1381                         fprintf(stderr, "error: %s: invalid ioctl %d\n",
1382                                 jt_cmdname(argv[0]), rc);
1383                         return rc;
1384                 }
1385
1386                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_ECHO_MD, buf);
1387                 if (rc) {
1388                         fprintf(stderr, "error: %s: %s\n",
1389                                 jt_cmdname(argv[0]), strerror(rc = errno));
1390                         return rc;
1391                 }
1392                 shmem_bump(data.ioc_count);
1393
1394                 gettimeofday(&end_time, NULL);
1395                 diff = difftime(&end_time, &start);
1396                 if (seconds > 0 && (__u64)diff > seconds)
1397                         break;
1398
1399                 if (count >= total_count && total_count > 0)
1400                         break;
1401         }
1402
1403         if (count > 0 && version) {
1404                 gettimeofday(&end_time, NULL);
1405                 diff = difftime(&end_time, &start);
1406                 printf("%s: %d in %.3fs (%.3f /s): %s",
1407                         jt_cmdname(argv[0]), count, diff,
1408                         (double)count/diff, ctime(&end_time.tv_sec));
1409         }
1410
1411 #ifdef MAX_THREADS
1412         if (thread) {
1413                 shmem_lock();
1414                 shmem_end_time_locked();
1415                 shmem_unlock();
1416         }
1417 #endif
1418         return rc;
1419 }
1420
1421 int jt_obd_test_create(int argc, char **argv)
1422 {
1423         return jt_obd_md_common(argc, argv, ECHO_MD_CREATE);
1424 }
1425
1426 int jt_obd_test_mkdir(int argc, char **argv)
1427 {
1428         return jt_obd_md_common(argc, argv, ECHO_MD_MKDIR);
1429 }
1430
1431 int jt_obd_test_destroy(int argc, char **argv)
1432 {
1433         return jt_obd_md_common(argc, argv, ECHO_MD_DESTROY);
1434 }
1435
1436 int jt_obd_test_rmdir(int argc, char **argv)
1437 {
1438         return jt_obd_md_common(argc, argv, ECHO_MD_RMDIR);
1439 }
1440
1441 int jt_obd_test_lookup(int argc, char **argv)
1442 {
1443         return jt_obd_md_common(argc, argv, ECHO_MD_LOOKUP);
1444 }
1445
1446 int jt_obd_test_setxattr(int argc, char **argv)
1447 {
1448         return jt_obd_md_common(argc, argv, ECHO_MD_SETATTR);
1449 }
1450
1451 int jt_obd_test_md_getattr(int argc, char **argv)
1452 {
1453         return jt_obd_md_common(argc, argv, ECHO_MD_GETATTR);
1454 }
1455
1456 int jt_obd_create(int argc, char **argv)
1457 {
1458         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1459         struct obd_ioctl_data data;
1460         struct timeval next_time;
1461         __u64 count = 1, next_count, base_id = 1;
1462         int verbose = 1, mode = 0100644, rc = 0, i;
1463         char *end;
1464
1465         memset(&data, 0, sizeof(data));
1466         data.ioc_dev = cur_device;
1467         if (argc < 2 || argc > 4)
1468                 return CMD_HELP;
1469
1470         count = strtoull(argv[1], &end, 0);
1471         if (*end) {
1472                 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1473                         jt_cmdname(argv[0]), argv[1]);
1474                 return CMD_HELP;
1475         }
1476
1477         if (argc > 2) {
1478                 mode = strtoul(argv[2], &end, 0);
1479                 if (*end) {
1480                         fprintf(stderr, "error: %s: invalid mode '%s'\n",
1481                                 jt_cmdname(argv[0]), argv[2]);
1482                         return CMD_HELP;
1483                 }
1484                 if (!(mode & S_IFMT))
1485                         mode |= S_IFREG;
1486         }
1487
1488         if (argc > 3) {
1489                 verbose = get_verbose(argv[0], argv[3]);
1490                 if (verbose == BAD_VERBOSE)
1491                         return CMD_HELP;
1492         }
1493
1494         printf("%s: %jd objects\n", jt_cmdname(argv[0]), (uintmax_t)count);
1495         gettimeofday(&next_time, NULL);
1496         next_time.tv_sec -= verbose;
1497
1498         ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1499         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1500                 /*
1501                  * base_id is 1 so we don't need to worry about it being
1502                  * greater than OBIF_MAX_OID
1503                  */
1504                 data.ioc_obdo1.o_oi.oi_fid.f_oid = base_id;
1505                 data.ioc_obdo1.o_mode = mode;
1506                 data.ioc_obdo1.o_uid = 0;
1507                 data.ioc_obdo1.o_gid = 0;
1508                 data.ioc_obdo1.o_projid = 0;
1509                 data.ioc_obdo1.o_valid = OBD_MD_FLTYPE | OBD_MD_FLMODE |
1510                                          OBD_MD_FLID | OBD_MD_FLUID |
1511                                          OBD_MD_FLGID | OBD_MD_FLGROUP |
1512                                          OBD_MD_FLPROJID;
1513
1514                 memset(buf, 0, sizeof(rawbuf));
1515                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1516                 if (rc) {
1517                         fprintf(stderr, "error: %s: invalid ioctl\n",
1518                                 jt_cmdname(argv[0]));
1519                         return rc;
1520                 }
1521                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_CREATE, buf);
1522                 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
1523                 shmem_bump(1);
1524                 if (rc < 0) {
1525                         fprintf(stderr, "error: %s: #%d - %s\n",
1526                                 jt_cmdname(argv[0]), i, strerror(rc = errno));
1527                         break;
1528                 }
1529                 if (!(data.ioc_obdo1.o_valid & OBD_MD_FLID)) {
1530                         fprintf(stderr, "error: %s: oid not valid #%d:%#jx\n",
1531                                 jt_cmdname(argv[0]), i,
1532                                 (uintmax_t)data.ioc_obdo1.o_valid);
1533                         rc = EINVAL;
1534                         break;
1535                 }
1536
1537                 if (be_verbose(verbose, &next_time, i, &next_count, count))
1538                         printf("%s: #%d is object id %#jx\n",
1539                                jt_cmdname(argv[0]), i,
1540                                (uintmax_t) ostid_id(&data.ioc_obdo1.o_oi));
1541         }
1542         return rc;
1543 }
1544
1545 int jt_obd_setattr(int argc, char **argv)
1546 {
1547         struct obd_ioctl_data data;
1548         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1549         __u64 objid;
1550         char *end;
1551         int mode;
1552         int rc;
1553
1554         memset(&data, 0, sizeof(data));
1555         data.ioc_dev = cur_device;
1556         if (argc != 2)
1557                 return CMD_HELP;
1558
1559         objid = strtoull(argv[1], &end, 0);
1560         if (*end) {
1561                 fprintf(stderr, "error: %s: objid '%s' is not a number\n",
1562                         jt_cmdname(argv[0]), argv[1]);
1563                 return CMD_HELP;
1564         }
1565
1566         if (objid >= OBIF_MAX_OID) {
1567                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1568                         jt_cmdname(argv[0]), argv[1]);
1569                 return CMD_HELP;
1570         }
1571
1572         mode = strtoul(argv[2], &end, 0);
1573         if (*end) {
1574                 fprintf(stderr, "error: %s: invalid mode '%s'\n",
1575                         jt_cmdname(argv[0]), argv[2]);
1576                 return CMD_HELP;
1577         }
1578
1579         ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1580         data.ioc_obdo1.o_mode = S_IFREG | mode;
1581         data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1582         data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
1583
1584         memset(buf, 0, sizeof(rawbuf));
1585         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1586         if (rc) {
1587                 fprintf(stderr, "error: %s: invalid ioctl\n",
1588                         jt_cmdname(argv[0]));
1589                 return rc;
1590         }
1591         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, buf);
1592         if (rc < 0)
1593                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1594                         strerror(rc = errno));
1595
1596         return rc;
1597 }
1598
1599 int jt_obd_test_setattr(int argc, char **argv)
1600 {
1601         struct obd_ioctl_data data;
1602         struct timeval start, next_time;
1603         __u64 i, count, next_count;
1604         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1605         int verbose = 1;
1606         __u64 objid = 3;
1607         char *end;
1608         int rc = 0;
1609
1610         if (argc < 2 || argc > 4)
1611                 return CMD_HELP;
1612
1613         memset(&data, 0, sizeof(data));
1614         data.ioc_dev = cur_device;
1615         count = strtoull(argv[1], &end, 0);
1616         if (*end) {
1617                 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1618                         jt_cmdname(argv[0]), argv[1]);
1619                 return CMD_HELP;
1620         }
1621
1622         if (argc >= 3) {
1623                 verbose = get_verbose(argv[0], argv[2]);
1624                 if (verbose == BAD_VERBOSE)
1625                         return CMD_HELP;
1626         }
1627
1628         if (argc >= 4) {
1629                 if (argv[3][0] == 't') {
1630                         objid = strtoull(argv[3] + 1, &end, 0);
1631                         if (thread)
1632                                 objid += thread - 1;
1633                 } else
1634                         objid = strtoull(argv[3], &end, 0);
1635                 if (*end) {
1636                         fprintf(stderr, "error: %s: invalid objid '%s'\n",
1637                                 jt_cmdname(argv[0]), argv[3]);
1638                         return CMD_HELP;
1639                 }
1640         }
1641
1642         gettimeofday(&start, NULL);
1643         next_time.tv_sec = start.tv_sec - verbose;
1644         next_time.tv_usec = start.tv_usec;
1645         if (verbose != 0)
1646                 printf("%s: setting %jd attrs (objid %#jx): %s",
1647                        jt_cmdname(argv[0]), (uintmax_t)count,
1648                        (uintmax_t)objid, ctime(&start.tv_sec));
1649
1650         ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1651         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1652                 if (objid >= OBIF_MAX_OID) {
1653                         fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
1654                                 jt_cmdname(argv[0]), objid);
1655                         return -E2BIG;
1656                 }
1657
1658                 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1659                 data.ioc_obdo1.o_mode = S_IFREG;
1660                 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
1661                 memset(buf, 0, sizeof(rawbuf));
1662                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1663                 if (rc) {
1664                         fprintf(stderr, "error: %s: invalid ioctl\n",
1665                                 jt_cmdname(argv[0]));
1666                         return rc;
1667                 }
1668                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, &data);
1669                 shmem_bump(1);
1670                 if (rc < 0) {
1671                         fprintf(stderr, "error: %s: #%jd - %d:%s\n",
1672                                 jt_cmdname(argv[0]), (uintmax_t)i,
1673                                 errno, strerror(rc = errno));
1674                         break;
1675                 } else {
1676                         if (be_verbose
1677                             (verbose, &next_time, i, &next_count, count))
1678                                 printf("%s: set attr #%jd\n",
1679                                        jt_cmdname(argv[0]), (uintmax_t)i);
1680                 }
1681         }
1682
1683         if (!rc) {
1684                 struct timeval end;
1685                 double diff;
1686
1687                 gettimeofday(&end, NULL);
1688
1689                 diff = difftime(&end, &start);
1690
1691                 --i;
1692                 if (verbose != 0)
1693                         printf("%s: %jd attrs in %.3fs (%.3f attr/s): %s",
1694                                jt_cmdname(argv[0]), (uintmax_t)i, diff,
1695                                i / diff, ctime(&end.tv_sec));
1696         }
1697         return rc;
1698 }
1699
1700 int jt_obd_destroy(int argc, char **argv)
1701 {
1702         struct obd_ioctl_data data;
1703         struct timeval next_time;
1704         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1705         __u64 count = 1, next_count;
1706         int verbose = 1;
1707         __u64 id;
1708         char *end;
1709         int rc = 0, i;
1710
1711         memset(&data, 0, sizeof(data));
1712         data.ioc_dev = cur_device;
1713         if (argc < 2 || argc > 4)
1714                 return CMD_HELP;
1715
1716         id = strtoull(argv[1], &end, 0);
1717         if (*end || id == 0 || errno != 0) {
1718                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1719                         jt_cmdname(argv[0]), argv[1]);
1720                 return CMD_HELP;
1721         }
1722         if (argc > 2) {
1723                 count = strtoull(argv[2], &end, 0);
1724                 if (*end) {
1725                         fprintf(stderr,
1726                                 "error: %s: invalid iteration count '%s'\n",
1727                                 jt_cmdname(argv[0]), argv[2]);
1728                         return CMD_HELP;
1729                 }
1730         }
1731
1732         if (argc > 3) {
1733                 verbose = get_verbose(argv[0], argv[3]);
1734                 if (verbose == BAD_VERBOSE)
1735                         return CMD_HELP;
1736         }
1737
1738         printf("%s: %jd objects\n", jt_cmdname(argv[0]), (uintmax_t)count);
1739         gettimeofday(&next_time, NULL);
1740         next_time.tv_sec -= verbose;
1741
1742         ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1743         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++, id++) {
1744                 if (id >= OBIF_MAX_OID) {
1745                         fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
1746                                 jt_cmdname(argv[0]), id);
1747                         return -E2BIG;
1748                 }
1749
1750                 data.ioc_obdo1.o_oi.oi_fid.f_oid = id;
1751                 data.ioc_obdo1.o_mode = S_IFREG | 0644;
1752                 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLMODE;
1753
1754                 memset(buf, 0, sizeof(rawbuf));
1755                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1756                 if (rc) {
1757                         fprintf(stderr, "error: %s: invalid ioctl\n",
1758                                 jt_cmdname(argv[0]));
1759                         return rc;
1760                 }
1761                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_DESTROY, buf);
1762                 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
1763                 shmem_bump(1);
1764                 if (rc < 0) {
1765                         fprintf(stderr, "error: %s: objid %#jx: %s\n",
1766                                 jt_cmdname(argv[0]), (uintmax_t)id,
1767                                 strerror(rc = errno));
1768                         break;
1769                 }
1770
1771                 if (be_verbose(verbose, &next_time, i, &next_count, count))
1772                         printf("%s: #%d is object id %#jx\n",
1773                                jt_cmdname(argv[0]), i, (uintmax_t)id);
1774         }
1775
1776         return rc;
1777 }
1778
1779 int jt_obd_getattr(int argc, char **argv)
1780 {
1781         struct obd_ioctl_data data;
1782         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1783         __u64 objid;
1784         char *end;
1785         int rc;
1786
1787         if (argc != 2)
1788                 return CMD_HELP;
1789
1790         objid = strtoull(argv[1], &end, 0);
1791         if (*end) {
1792                 fprintf(stderr, "error: %s: objid '%s' is not a number\n",
1793                         jt_cmdname(argv[0]), argv[1]);
1794                 return CMD_HELP;
1795         }
1796
1797         if (objid >= OBIF_MAX_OID) {
1798                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1799                         jt_cmdname(argv[0]), argv[1]);
1800                 return CMD_HELP;
1801         }
1802
1803         memset(&data, 0, sizeof(data));
1804         data.ioc_dev = cur_device;
1805         ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1806         data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1807         /* to help obd filter */
1808         data.ioc_obdo1.o_mode = 0100644;
1809         data.ioc_obdo1.o_valid = 0xffffffff;
1810         printf("%s: object id %#jx\n", jt_cmdname(argv[0]),
1811                (uintmax_t)ostid_id(&data.ioc_obdo1.o_oi));
1812
1813         memset(buf, 0, sizeof(rawbuf));
1814         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1815         if (rc) {
1816                 fprintf(stderr, "error: %s: invalid ioctl\n",
1817                         jt_cmdname(argv[0]));
1818                 return rc;
1819         }
1820         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, buf);
1821         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
1822         if (rc) {
1823                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1824                         strerror(rc = errno));
1825         } else {
1826                 printf("%s: object id %ju, mode %o\n", jt_cmdname(argv[0]),
1827                        (uintmax_t)ostid_id(&data.ioc_obdo1.o_oi),
1828                        data.ioc_obdo1.o_mode);
1829         }
1830         return rc;
1831 }
1832
1833 int jt_obd_test_getattr(int argc, char **argv)
1834 {
1835         struct obd_ioctl_data data;
1836         struct timeval start, next_time;
1837         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1838         __u64 i, count, next_count;
1839         int verbose = 1;
1840         __u64 objid = 3;
1841         char *end;
1842         int rc = 0;
1843
1844         if (argc < 2 || argc > 4)
1845                 return CMD_HELP;
1846
1847         memset(&data, 0, sizeof(data));
1848         data.ioc_dev = cur_device;
1849         count = strtoull(argv[1], &end, 0);
1850         if (*end) {
1851                 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1852                         jt_cmdname(argv[0]), argv[1]);
1853                 return CMD_HELP;
1854         }
1855
1856         if (argc >= 3) {
1857                 verbose = get_verbose(argv[0], argv[2]);
1858                 if (verbose == BAD_VERBOSE)
1859                         return CMD_HELP;
1860         }
1861
1862         if (argc >= 4) {
1863                 if (argv[3][0] == 't') {
1864                         objid = strtoull(argv[3] + 1, &end, 0);
1865                         if (thread)
1866                                 objid += thread - 1;
1867                 } else
1868                         objid = strtoull(argv[3], &end, 0);
1869                 if (*end) {
1870                         fprintf(stderr, "error: %s: invalid objid '%s'\n",
1871                                 jt_cmdname(argv[0]), argv[3]);
1872                         return CMD_HELP;
1873                 }
1874         }
1875
1876         gettimeofday(&start, NULL);
1877         next_time.tv_sec = start.tv_sec - verbose;
1878         next_time.tv_usec = start.tv_usec;
1879         if (verbose != 0)
1880                 printf("%s: getting %jd attrs (objid %#jx): %s",
1881                        jt_cmdname(argv[0]), (uintmax_t) count,
1882                        (uintmax_t)objid, ctime(&start.tv_sec));
1883
1884         ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1885         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1886                 if (objid >= OBIF_MAX_OID) {
1887                         fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
1888                                 jt_cmdname(argv[0]), objid);
1889                         return -E2BIG;
1890                 }
1891
1892                 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1893                 data.ioc_obdo1.o_mode = S_IFREG;
1894                 data.ioc_obdo1.o_valid = 0xffffffff;
1895                 memset(buf, 0, sizeof(rawbuf));
1896                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1897                 if (rc) {
1898                         fprintf(stderr, "error: %s: invalid ioctl\n",
1899                                 jt_cmdname(argv[0]));
1900                         return rc;
1901                 }
1902                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, &data);
1903                 shmem_bump(1);
1904                 if (rc < 0) {
1905                         fprintf(stderr, "error: %s: #%jd - %d:%s\n",
1906                                 jt_cmdname(argv[0]), (uintmax_t)i,
1907                                 errno, strerror(rc = errno));
1908                         break;
1909                 } else {
1910                         if (be_verbose
1911                             (verbose, &next_time, i, &next_count, count))
1912                                 printf("%s: got attr #%jd\n",
1913                                        jt_cmdname(argv[0]), (uintmax_t)i);
1914                 }
1915         }
1916
1917         if (!rc) {
1918                 struct timeval end;
1919                 double diff;
1920
1921                 gettimeofday(&end, NULL);
1922
1923                 diff = difftime(&end, &start);
1924
1925                 --i;
1926                 if (verbose != 0)
1927                         printf("%s: %jd attrs in %.3fs (%.3f attr/s): %s",
1928                                jt_cmdname(argv[0]), (uintmax_t) i, diff,
1929                                i / diff, ctime(&end.tv_sec));
1930         }
1931         return rc;
1932 }
1933
1934 /* test_brw <cnt>                                               count
1935         <r|w[r(repeat)x(noverify)]>                             mode
1936         <q|v|#(print interval)>                                 verbosity
1937         <npages[+offset]>                                       blocksize
1938         <[[<interleave_threads>]t(inc obj by thread#)]obj>      object
1939         [p|g<args>]                                             batch */
1940 int jt_obd_test_brw(int argc, char **argv)
1941 {
1942         struct obd_ioctl_data data;
1943         struct timeval start, next_time;
1944         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1945         __u64 count, next_count, len, stride, thr_offset = 0, objid = 3;
1946         int write = 0, verbose = 1, cmd, i, rc = 0, pages = 1;
1947         int offset_pages = 0;
1948         long n;
1949         int repeat_offset = 0;
1950         unsigned long long ull;
1951         int  nthr_per_obj = 0;
1952         int  verify = 1;
1953         int  obj_idx = 0;
1954         char *end;
1955
1956         if (argc < 2 || argc > 7) {
1957                 fprintf(stderr, "error: %s: bad number of arguments: %d\n",
1958                         jt_cmdname(argv[0]), argc);
1959                 return CMD_HELP;
1960         }
1961
1962         count = strtoull(argv[1], &end, 0);
1963         if (*end) {
1964                 fprintf(stderr, "error: %s: bad iteration count '%s'\n",
1965                         jt_cmdname(argv[0]), argv[1]);
1966                 return CMD_HELP;
1967         }
1968
1969         if (argc >= 3) {
1970                 if (argv[2][0] == 'w' || argv[2][0] == '1')
1971                         write = 1;
1972                 /* else it's a read */
1973
1974                 if (argv[2][0] != 0)
1975                         for (i = 1; argv[2][i] != 0; i++)
1976                                 switch (argv[2][i]) {
1977                                 case 'r':
1978                                         repeat_offset = 1;
1979                                         break;
1980
1981                                 case 'x':
1982                                         verify = 0;
1983                                         break;
1984
1985                                 default:
1986                                         fprintf (stderr, "Can't parse cmd '%s'\n",
1987                                                  argv[2]);
1988                                         return CMD_HELP;
1989                                 }
1990         }
1991
1992         if (argc >= 4) {
1993                 verbose = get_verbose(argv[0], argv[3]);
1994                 if (verbose == BAD_VERBOSE)
1995                         return CMD_HELP;
1996         }
1997
1998         if (argc >= 5) {
1999                 pages = strtoul(argv[4], &end, 0);
2000
2001                 if (*end == '+')
2002                         offset_pages = strtoul(end + 1, &end, 0);
2003
2004                 if (*end != 0 ||
2005                     offset_pages < 0 || offset_pages >= pages) {
2006                         fprintf(stderr, "error: %s: bad npages[+offset] parameter '%s'\n",
2007                                 jt_cmdname(argv[0]), argv[4]);
2008                         return CMD_HELP;
2009                 }
2010         }
2011
2012         if (argc >= 6) {
2013                 if (thread &&
2014                     (n = strtol(argv[5], &end, 0)) > 0 &&
2015                     *end == 't' &&
2016                     (ull = strtoull(end + 1, &end, 0)) > 0 &&
2017                     *end == 0) {
2018                         nthr_per_obj = n;
2019                         objid = ull;
2020                 } else if (thread &&
2021                            argv[5][0] == 't') {
2022                         nthr_per_obj = 1;
2023                         objid = strtoull(argv[5] + 1, &end, 0);
2024                 } else {
2025                         nthr_per_obj = 0;
2026                         objid = strtoull(argv[5], &end, 0);
2027                 }
2028                 if (*end) {
2029                         fprintf(stderr, "error: %s: bad objid '%s'\n",
2030                                 jt_cmdname(argv[0]), argv[5]);
2031                         return CMD_HELP;
2032                 }
2033         }
2034
2035         memset(&data, 0, sizeof(data));
2036         data.ioc_dev = cur_device;
2037
2038         /* communicate the 'type' of brw test and batching to echo_client.
2039          * don't start.  we'd love to refactor this lctl->echo_client
2040          * interface */
2041         data.ioc_pbuf1 = (void *)1;
2042         data.ioc_plen1 = 1;
2043
2044         if (argc >= 7) {
2045                 switch(argv[6][0]) {
2046                         case 'g': /* plug and unplug */
2047                                 data.ioc_pbuf1 = (void *)2;
2048                                 data.ioc_plen1 = strtoull(argv[6] + 1, &end,
2049                                                           0);
2050                                 break;
2051                         case 'p': /* prep and commit */
2052                                 data.ioc_pbuf1 = (void *)3;
2053                                 data.ioc_plen1 = strtoull(argv[6] + 1, &end,
2054                                                           0);
2055                                 break;
2056                         default:
2057                                 fprintf(stderr, "error: %s: batching '%s' "
2058                                         "needs to specify 'p' or 'g'\n",
2059                                         jt_cmdname(argv[0]), argv[6]);
2060                                 return CMD_HELP;
2061                 }
2062
2063                 if (*end) {
2064                         fprintf(stderr, "error: %s: bad batching '%s'\n",
2065                                 jt_cmdname(argv[0]), argv[6]);
2066                         return CMD_HELP;
2067                 }
2068                 data.ioc_plen1 *= getpagesize();
2069         }
2070
2071         len = pages * getpagesize();
2072         thr_offset = offset_pages * getpagesize();
2073         stride = len;
2074
2075 #ifdef MAX_THREADS
2076         if (thread) {
2077                 shmem_lock ();
2078                 if (nthr_per_obj != 0) {
2079                         /* threads interleave */
2080                         obj_idx = (thread - 1)/nthr_per_obj;
2081                         objid += obj_idx;
2082                         stride *= nthr_per_obj;
2083                         if ((thread - 1) % nthr_per_obj == 0) {
2084                                 shared_data->body.offsets[obj_idx] =
2085                                         stride + thr_offset;
2086                         }
2087                         thr_offset += ((thread - 1) % nthr_per_obj) * len;
2088                 } else {
2089                         /* threads disjoint */
2090                         thr_offset += (thread - 1) * len;
2091                 }
2092
2093                 shmem_start_time_locked();
2094                 shmem_unlock ();
2095         }
2096 #endif
2097
2098         ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
2099         if (objid >= OBIF_MAX_OID) {
2100                 fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
2101                         jt_cmdname(argv[0]), objid);
2102                 return -E2BIG;
2103         }
2104
2105         data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
2106         data.ioc_obdo1.o_mode = S_IFREG;
2107         data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
2108                                  OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
2109         data.ioc_obdo1.o_flags = (verify ? OBD_FL_DEBUG_CHECK : 0);
2110         data.ioc_count = len;
2111         data.ioc_offset = (repeat_offset ? 0 : thr_offset);
2112
2113         gettimeofday(&start, NULL);
2114         next_time.tv_sec = start.tv_sec - verbose;
2115         next_time.tv_usec = start.tv_usec;
2116
2117         if (verbose != 0)
2118                 printf("%s: %s %jux%d pages (obj %#jx, off %ju): %s",
2119                        jt_cmdname(argv[0]), write ? "writing" : "reading",
2120                        (uintmax_t)count, pages, (uintmax_t) objid,
2121                        (uintmax_t)data.ioc_offset, ctime(&start.tv_sec));
2122
2123         cmd = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
2124         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
2125                 data.ioc_obdo1.o_valid &= ~(OBD_MD_FLBLOCKS|OBD_MD_FLGRANT);
2126                 memset(buf, 0, sizeof(rawbuf));
2127                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2128                 if (rc) {
2129                         fprintf(stderr, "error: %s: invalid ioctl\n",
2130                                 jt_cmdname(argv[0]));
2131                         return rc;
2132                 }
2133                 rc = l2_ioctl(OBD_DEV_ID, cmd, buf);
2134                 shmem_bump(1);
2135                 if (rc) {
2136                         fprintf(stderr, "error: %s: #%d - %s on %s\n",
2137                                 jt_cmdname(argv[0]), i, strerror(rc = errno),
2138                                 write ? "write" : "read");
2139                         break;
2140                 } else if (be_verbose(verbose, &next_time,i, &next_count,count)) {
2141                         shmem_lock ();
2142                         printf("%s: %s number %d @ %jd:%ju for %d\n",
2143                                jt_cmdname(argv[0]), write ? "write" : "read", i,
2144                                (uintmax_t)ostid_id(&data.ioc_obdo1.o_oi),
2145                                (uintmax_t)data.ioc_offset,
2146                                (int)(pages * getpagesize()));
2147                         shmem_unlock ();
2148                 }
2149
2150                 if (!repeat_offset) {
2151 #ifdef MAX_THREADS
2152                         if (stride == len) {
2153                                 data.ioc_offset += stride;
2154                         } else if (i < count) {
2155                                 shmem_lock ();
2156                                 data.ioc_offset =
2157                                         shared_data->body.offsets[obj_idx];
2158                                 shared_data->body.offsets[obj_idx] += len;
2159                                 shmem_unlock ();
2160                         }
2161 #else
2162                         data.ioc_offset += len;
2163                         obj_idx = 0; /* avoids an unused var warning */
2164 #endif
2165                 }
2166         }
2167
2168         if (!rc) {
2169                 struct timeval end;
2170                 double diff;
2171
2172                 gettimeofday(&end, NULL);
2173
2174                 diff = difftime(&end, &start);
2175
2176                 --i;
2177                 if (verbose != 0)
2178                         printf("%s: %s %dx%d pages in %.3fs (%.3f MB/s): %s",
2179                                jt_cmdname(argv[0]), write ? "wrote" : "read",
2180                                i, pages, diff,
2181                                ((double)i * pages * getpagesize()) /
2182                                (diff * 1048576.0),
2183                                ctime(&end.tv_sec));
2184         }
2185
2186 #ifdef MAX_THREADS
2187         if (thread) {
2188                 shmem_lock();
2189                 shmem_end_time_locked();
2190                 shmem_unlock();
2191         }
2192 #endif
2193         return rc;
2194 }
2195
2196 int jt_obd_lov_getconfig(int argc, char **argv)
2197 {
2198         struct obd_ioctl_data data;
2199         struct lov_desc desc;
2200         struct obd_uuid *uuidarray;
2201         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2202         __u32 *obdgens;
2203         char *path;
2204         int rc, fd;
2205
2206         memset(&data, 0, sizeof(data));
2207         data.ioc_dev = cur_device;
2208
2209         if (argc != 2)
2210                 return CMD_HELP;
2211
2212         path = argv[1];
2213         fd = open(path, O_RDONLY);
2214         if (fd < 0) {
2215                 fprintf(stderr, "open \"%s\" failed: %s\n", path,
2216                         strerror(errno));
2217                 return -errno;
2218         }
2219
2220         memset(&desc, 0, sizeof(desc));
2221         obd_str2uuid(&desc.ld_uuid, argv[1]);
2222         desc.ld_tgt_count = ((OBD_MAX_IOCTL_BUFFER-sizeof(data)-sizeof(desc)) /
2223                              (sizeof(*uuidarray) + sizeof(*obdgens)));
2224
2225 repeat:
2226         uuidarray = calloc(desc.ld_tgt_count, sizeof(*uuidarray));
2227         if (!uuidarray) {
2228                 fprintf(stderr, "error: %s: no memory for %d uuid's\n",
2229                         jt_cmdname(argv[0]), desc.ld_tgt_count);
2230                 rc = -ENOMEM;
2231                 goto out;
2232         }
2233         obdgens = calloc(desc.ld_tgt_count, sizeof(*obdgens));
2234         if (!obdgens) {
2235                 fprintf(stderr, "error: %s: no memory for %d generation #'s\n",
2236                         jt_cmdname(argv[0]), desc.ld_tgt_count);
2237                 rc = -ENOMEM;
2238                 goto out_uuidarray;
2239         }
2240
2241         memset(buf, 0, sizeof(rawbuf));
2242         data.ioc_inllen1 = sizeof(desc);
2243         data.ioc_inlbuf1 = (char *)&desc;
2244         data.ioc_inllen2 = desc.ld_tgt_count * sizeof(*uuidarray);
2245         data.ioc_inlbuf2 = (char *)uuidarray;
2246         data.ioc_inllen3 = desc.ld_tgt_count * sizeof(*obdgens);
2247         data.ioc_inlbuf3 = (char *)obdgens;
2248
2249         if (obd_ioctl_pack(&data, &buf, sizeof(rawbuf))) {
2250                 fprintf(stderr, "error: %s: invalid ioctl\n",
2251                         jt_cmdname(argv[0]));
2252                 rc = -EINVAL;
2253                 goto out_obdgens;
2254         }
2255         rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
2256         if (rc == -ENOSPC) {
2257                 free(uuidarray);
2258                 free(obdgens);
2259                 goto repeat;
2260         } else if (rc) {
2261                 fprintf(stderr, "error: %s: ioctl error: %s\n",
2262                         jt_cmdname(argv[0]), strerror(rc = errno));
2263         } else {
2264                 struct obd_uuid *uuidp;
2265                 __u32 *genp;
2266                 int i;
2267
2268                 if (obd_ioctl_unpack(&data, buf, sizeof(rawbuf))) {
2269                         fprintf(stderr, "error: %s: invalid reply\n",
2270                                 jt_cmdname(argv[0]));
2271                         rc = -EINVAL;
2272                         goto out;
2273                 }
2274                 if (desc.ld_default_stripe_count == (__u32)-1)
2275                         printf("default_stripe_count: %d\n", -1);
2276                 else
2277                         printf("default_stripe_count: %u\n",
2278                                desc.ld_default_stripe_count);
2279                 printf("default_stripe_size: %ju\n",
2280                        (uintmax_t)desc.ld_default_stripe_size);
2281                 printf("default_stripe_offset: %ju\n",
2282                        (uintmax_t)desc.ld_default_stripe_offset);
2283                 printf("default_stripe_pattern: %u\n", desc.ld_pattern);
2284                 printf("obd_count: %u\n", desc.ld_tgt_count);
2285                 printf("OBDS:\tobdidx\t\tobdgen\t\t obduuid\n");
2286                 uuidp = uuidarray;
2287                 genp = obdgens;
2288                 for (i = 0; i < desc.ld_tgt_count; i++, uuidp++, genp++)
2289                         printf("\t%6u\t%14u\t\t %s\n", i, *genp, (char *)uuidp);
2290         }
2291 out_obdgens:
2292         free(obdgens);
2293 out_uuidarray:
2294         free(uuidarray);
2295 out:
2296         close(fd);
2297         return rc;
2298 }
2299
2300 static int do_activate(int argc, char **argv, int flag)
2301 {
2302         struct obd_ioctl_data data;
2303         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2304         int rc;
2305
2306         memset(&data, 0, sizeof(data));
2307         data.ioc_dev = cur_device;
2308         if (argc != 1)
2309                 return CMD_HELP;
2310
2311         /* reuse offset for 'active' */
2312         data.ioc_offset = flag;
2313
2314         memset(buf, 0, sizeof(rawbuf));
2315         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2316         if (rc) {
2317                 fprintf(stderr, "error: %s: invalid ioctl\n",
2318                         jt_cmdname(argv[0]));
2319                 return rc;
2320         }
2321         rc = l2_ioctl(OBD_DEV_ID, IOC_OSC_SET_ACTIVE, buf);
2322         if (rc)
2323                 fprintf(stderr, "error: %s: failed: %s\n",
2324                         jt_cmdname(argv[0]), strerror(rc = errno));
2325
2326         return rc;
2327 }
2328
2329 /**
2330  * Replace nids for given device.
2331  * lctl replace_nids <devicename> <nid1>[,nid2,nid3]
2332  * Command should be started on MGS server.
2333  * Only MGS server should be started (command execution
2334  * returns error in another cases). Command mount
2335  * -t lustre <MDT partition> -o nosvc <mount point>
2336  * can be used for that.
2337  *
2338  * llogs for MDTs and clients are processed. All
2339  * records copied as is except add_uuid and setup. This records
2340  * are skipped and recorded with new nids and uuid.
2341  *
2342  * \see mgs_replace_nids
2343  * \see mgs_replace_nids_log
2344  * \see mgs_replace_handler
2345  */
2346 int jt_replace_nids(int argc, char **argv)
2347 {
2348         int rc;
2349         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2350         struct obd_ioctl_data data;
2351
2352         memset(&data, 0, sizeof(data));
2353         data.ioc_dev = get_mgs_device();
2354         if (argc != 3)
2355                 return CMD_HELP;
2356
2357         data.ioc_inllen1 = strlen(argv[1]) + 1;
2358         data.ioc_inlbuf1 = argv[1];
2359
2360         data.ioc_inllen2 = strlen(argv[2]) + 1;
2361         data.ioc_inlbuf2 = argv[2];
2362         memset(buf, 0, sizeof(rawbuf));
2363         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2364         if (rc) {
2365                 fprintf(stderr, "error: %s: invalid ioctl\n",
2366                         jt_cmdname(argv[0]));
2367                 return rc;
2368         }
2369
2370         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_REPLACE_NIDS, buf);
2371         if (rc < 0) {
2372                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
2373                         strerror(rc = errno));
2374         }
2375
2376         return rc;
2377 }
2378
2379 int jt_obd_deactivate(int argc, char **argv)
2380 {
2381         return do_activate(argc, argv, 0);
2382 }
2383
2384 int jt_obd_activate(int argc, char **argv)
2385 {
2386         return do_activate(argc, argv, 1);
2387 }
2388
2389 int jt_obd_recover(int argc, char **argv)
2390 {
2391         int rc;
2392         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2393         struct obd_ioctl_data data;
2394
2395         memset(&data, 0, sizeof(data));
2396         data.ioc_dev = cur_device;
2397         if (argc > 2)
2398                 return CMD_HELP;
2399
2400         if (argc == 2) {
2401                 data.ioc_inllen1 = strlen(argv[1]) + 1;
2402                 data.ioc_inlbuf1 = argv[1];
2403         }
2404
2405         memset(buf, 0, sizeof(rawbuf));
2406         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2407         if (rc) {
2408                 fprintf(stderr, "error: %s: invalid ioctl\n",
2409                         jt_cmdname(argv[0]));
2410                 return rc;
2411         }
2412         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_CLIENT_RECOVER, buf);
2413         if (rc < 0) {
2414                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
2415                         strerror(rc = errno));
2416         }
2417
2418         return rc;
2419 }
2420
2421 int jt_obd_mdc_lookup(int argc, char **argv)
2422 {
2423         struct obd_ioctl_data data;
2424         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2425         char *parent, *child;
2426         int rc, fd, verbose = 1;
2427
2428         if (argc < 3 || argc > 4)
2429                 return CMD_HELP;
2430
2431         parent = argv[1];
2432         child = argv[2];
2433         if (argc == 4)
2434                 verbose = get_verbose(argv[0], argv[3]);
2435
2436         memset(&data, 0, sizeof(data));
2437         data.ioc_dev = cur_device;
2438
2439         data.ioc_inllen1 = strlen(child) + 1;
2440         data.ioc_inlbuf1 = child;
2441
2442         memset(buf, 0, sizeof(rawbuf));
2443         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2444         if (rc) {
2445                 fprintf(stderr, "error: %s: invalid ioctl\n",
2446                         jt_cmdname(argv[0]));
2447                 return rc;
2448         }
2449
2450         fd = open(parent, O_RDONLY);
2451         if (fd < 0) {
2452                 fprintf(stderr, "open \"%s\" failed: %s\n", parent,
2453                         strerror(errno));
2454                 return -1;
2455         }
2456
2457         rc = ioctl(fd, IOC_MDC_LOOKUP, buf);
2458         if (rc < 0) {
2459                 fprintf(stderr, "error: %s: ioctl error: %s\n",
2460                         jt_cmdname(argv[0]), strerror(rc = errno));
2461         }
2462         close(fd);
2463
2464         if (verbose) {
2465                 rc = obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
2466                 if (rc) {
2467                         fprintf(stderr, "error: %s: invalid reply\n",
2468                                 jt_cmdname(argv[0]));
2469                         return rc;
2470                 }
2471                 printf("%s: mode %o uid %d gid %d\n", child,
2472                        data.ioc_obdo1.o_mode, data.ioc_obdo1.o_uid,
2473                        data.ioc_obdo1.o_gid);
2474         }
2475
2476         return rc;
2477 }
2478
2479 int jt_lcfg_fork(int argc, char **argv)
2480 {
2481         struct obd_ioctl_data data;
2482         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2483         int rc;
2484
2485         if (argc != 3)
2486                 return CMD_HELP;
2487
2488         memset(&data, 0, sizeof(data));
2489         data.ioc_dev = get_mgs_device();
2490         data.ioc_inllen1 = strlen(argv[1]) + 1;
2491         data.ioc_inlbuf1 = argv[1];
2492         data.ioc_inllen2 = strlen(argv[2]) + 1;
2493         data.ioc_inlbuf2 = argv[2];
2494
2495         memset(buf, 0, sizeof(rawbuf));
2496         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2497         if (rc) {
2498                 fprintf(stderr, "error: %s: invalid ioctl\n",
2499                         jt_cmdname(argv[0]));
2500                 return rc;
2501         }
2502
2503         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LCFG_FORK, buf);
2504         if (rc < 0)
2505                 fprintf(stderr, "error: %s: OBD_IOC_LCFG_FORK failed: %s\n",
2506                         jt_cmdname(argv[0]), strerror(errno));
2507
2508         return rc;
2509 }
2510
2511 int jt_lcfg_erase(int argc, char **argv)
2512 {
2513         struct obd_ioctl_data data;
2514         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2515         int rc;
2516
2517         if (argc == 3) {
2518                 if (strncmp(argv[2], "-q", strlen("-q")) != 0 &&
2519                     strncmp(argv[2], "--quiet", strlen("--quiet")) != 0)
2520                         return CMD_HELP;
2521         } else if (argc != 2) {
2522                 return CMD_HELP;
2523         }
2524
2525         memset(&data, 0, sizeof(data));
2526         data.ioc_dev = get_mgs_device();
2527         data.ioc_inllen1 = strlen(argv[1]) + 1;
2528         data.ioc_inlbuf1 = argv[1];
2529
2530         memset(buf, 0, sizeof(rawbuf));
2531         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2532         if (rc) {
2533                 fprintf(stderr, "error: %s: invalid ioctl\n",
2534                         jt_cmdname(argv[0]));
2535                 return rc;
2536         }
2537
2538         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LCFG_ERASE, buf);
2539         if (rc < 0)
2540                 fprintf(stderr, "error: %s: OBD_IOC_LCFG_ERASE failed: %s\n",
2541                         jt_cmdname(argv[0]), strerror(errno));
2542
2543         return rc;
2544 }
2545
2546 int jt_llog_catlist(int argc, char **argv)
2547 {
2548         struct obd_ioctl_data data;
2549         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2550         int rc;
2551
2552         if (argc != 1)
2553                 return CMD_HELP;
2554
2555         memset(&data, 0, sizeof(data));
2556         data.ioc_dev = cur_device;
2557         data.ioc_inllen1 = sizeof(rawbuf) - cfs_size_round(sizeof(data));
2558         memset(buf, 0, sizeof(rawbuf));
2559         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2560         if (rc) {
2561                 fprintf(stderr, "error: %s: invalid ioctl\n",
2562                         jt_cmdname(argv[0]));
2563                 return rc;
2564         }
2565         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CATLOGLIST, buf);
2566         if (rc == 0)
2567                 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2568         else
2569                 fprintf(stderr, "OBD_IOC_CATLOGLIST failed: %s\n",
2570                         strerror(errno));
2571
2572         return rc;
2573 }
2574
2575 int jt_llog_info(int argc, char **argv)
2576 {
2577         struct obd_ioctl_data data;
2578         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2579         int rc;
2580
2581         if (argc != 2)
2582                 return CMD_HELP;
2583
2584         memset(&data, 0, sizeof(data));
2585         data.ioc_dev = cur_device;
2586         data.ioc_inllen1 = strlen(argv[1]) + 1;
2587         data.ioc_inlbuf1 = argv[1];
2588         data.ioc_inllen2 = sizeof(rawbuf) - cfs_size_round(sizeof(data)) -
2589                 cfs_size_round(data.ioc_inllen1);
2590         memset(buf, 0, sizeof(rawbuf));
2591         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2592         if (rc) {
2593                 fprintf(stderr, "error: %s: invalid ioctl\n",
2594                         jt_cmdname(argv[0]));
2595                 return rc;
2596         }
2597
2598         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_INFO, buf);
2599         if (rc == 0)
2600                 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2601         else
2602                 fprintf(stderr, "OBD_IOC_LLOG_INFO failed: %s\n",
2603                         strerror(errno));
2604
2605         return rc;
2606 }
2607
2608 int jt_llog_print(int argc, char **argv)
2609 {
2610         struct obd_ioctl_data data;
2611         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2612         int rc;
2613
2614         if (argc != 2 && argc != 4)
2615                 return CMD_HELP;
2616
2617         memset(&data, 0, sizeof(data));
2618         data.ioc_dev = cur_device;
2619         data.ioc_inllen1 = strlen(argv[1]) + 1;
2620         data.ioc_inlbuf1 = argv[1];
2621         if (argc == 4) {
2622                 data.ioc_inllen2 = strlen(argv[2]) + 1;
2623                 data.ioc_inlbuf2 = argv[2];
2624                 data.ioc_inllen3 = strlen(argv[3]) + 1;
2625                 data.ioc_inlbuf3 = argv[3];
2626         } else {
2627                 char from[2] = "1", to[3] = "-1";
2628                 data.ioc_inllen2 = strlen(from) + 1;
2629                 data.ioc_inlbuf2 = from;
2630                 data.ioc_inllen3 = strlen(to) + 1;
2631                 data.ioc_inlbuf3 = to;
2632         }
2633         data.ioc_inllen4 = sizeof(rawbuf) - cfs_size_round(sizeof(data)) -
2634                 cfs_size_round(data.ioc_inllen1) -
2635                 cfs_size_round(data.ioc_inllen2) -
2636                 cfs_size_round(data.ioc_inllen3);
2637         memset(buf, 0, sizeof(rawbuf));
2638         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2639         if (rc) {
2640                 fprintf(stderr, "error: %s: invalid ioctl\n",
2641                         jt_cmdname(argv[0]));
2642                 return rc;
2643         }
2644
2645         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_PRINT, buf);
2646         if (rc == 0)
2647                 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2648         else
2649                 fprintf(stderr, "OBD_IOC_LLOG_PRINT failed: %s\n",
2650                         strerror(errno));
2651
2652         return rc;
2653 }
2654
2655 static int llog_cancel_parse_optional(int argc, char **argv,
2656                                       struct obd_ioctl_data *data)
2657 {
2658         int cOpt;
2659         const char *const short_opts = "c:l:i:h";
2660         const struct option long_opts[] = {
2661         { .val = 'c',   .name = "catalog",      .has_arg = required_argument },
2662         { .val = 'h',   .name = "help",         .has_arg = no_argument },
2663         { .val = 'i',   .name = "log_idx",      .has_arg = required_argument },
2664         { .val = 'l',   .name = "log_id",       .has_arg = required_argument },
2665         { .name = NULL } };
2666
2667         /* sanity check */
2668         if (!data || argc <= 1) {
2669                 return -1;
2670         }
2671
2672         /*now process command line arguments*/
2673         while ((cOpt = getopt_long(argc, argv, short_opts,
2674                                         long_opts, NULL)) != -1) {
2675                 switch (cOpt) {
2676                 case 'c':
2677                         data->ioc_inllen1 = strlen(optarg) + 1;
2678                         data->ioc_inlbuf1 = optarg;
2679                         break;
2680
2681                 case 'l':
2682                         data->ioc_inllen2 = strlen(optarg) + 1;
2683                         data->ioc_inlbuf2 = optarg;
2684                         break;
2685
2686                 case 'i':
2687                         data->ioc_inllen3 = strlen(optarg) + 1;
2688                         data->ioc_inlbuf3 = optarg;
2689                         break;
2690
2691                 case 'h':
2692                 default:
2693                         return -1;
2694                 }
2695         }
2696
2697         if ((data->ioc_inlbuf1 == NULL) || (data->ioc_inlbuf3 == NULL)) {
2698                 /* missing mandatory parameters */
2699                 return -1;
2700         }
2701
2702         return 0;
2703 }
2704
2705 int jt_llog_cancel(int argc, char **argv)
2706 {
2707         struct obd_ioctl_data data;
2708         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2709         int rc, i;
2710
2711         /* check that the arguments provided are either all
2712          * optional or all positional.  No mixing allowed
2713          *
2714          * if argc is 4 or 3 then check all arguments to ensure that none
2715          * of them start with a '-'.  If so then this is invalid.
2716          * Otherwise if arg is > 4 then assume that this is optional
2717          * arguments, and parse as such ignoring any thing that's not
2718          * optional.  The result is that user must use optional arguments
2719          * for all mandatory parameters.  Code will ignore all other args
2720          *
2721          * The positional arguments option should eventually be phased out.
2722          */
2723         memset(&data, 0, sizeof(data));
2724         data.ioc_dev = cur_device;
2725
2726         if (argc == 3 || argc == 4) {
2727                 for (i = 1; i < argc; i++) {
2728                         if (argv[i][0] == '-')
2729                                 return CMD_HELP;
2730                 }
2731                 data.ioc_inllen1 = strlen(argv[1]) + 1;
2732                 data.ioc_inlbuf1 = argv[1];
2733                 if (argc == 4) {
2734                         data.ioc_inllen2 = strlen(argv[2]) + 1;
2735                         data.ioc_inlbuf2 = argv[2];
2736                         data.ioc_inllen3 = strlen(argv[3]) + 1;
2737                         data.ioc_inlbuf3 = argv[3];
2738                 } else {
2739                         data.ioc_inllen3 = strlen(argv[2]) + 1;
2740                         data.ioc_inlbuf3 = argv[2];
2741                 }
2742         } else {
2743                 if (llog_cancel_parse_optional(argc, argv, &data) != 0)
2744                         return CMD_HELP;
2745         }
2746
2747         memset(buf, 0, sizeof(rawbuf));
2748         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2749         if (rc) {
2750                 fprintf(stderr, "error: %s: invalid ioctl\n",
2751                         jt_cmdname(argv[0]));
2752                 return rc;
2753         }
2754
2755         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CANCEL, buf);
2756         if (rc == 0)
2757                 fprintf(stdout, "index %s was canceled.\n",
2758                         argc == 4 ? argv[3] : argv[2]);
2759         else
2760                 fprintf(stderr, "OBD_IOC_LLOG_CANCEL failed: %s\n",
2761                         strerror(errno));
2762
2763         return rc;
2764 }
2765
2766 int jt_llog_check(int argc, char **argv)
2767 {
2768         struct obd_ioctl_data data;
2769         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2770         int rc;
2771
2772         if (argc != 2 && argc != 4)
2773                 return CMD_HELP;
2774
2775         memset(&data, 0, sizeof(data));
2776         data.ioc_dev = cur_device;
2777         data.ioc_inllen1 = strlen(argv[1]) + 1;
2778         data.ioc_inlbuf1 = argv[1];
2779         if (argc == 4) {
2780                 data.ioc_inllen2 = strlen(argv[2]) + 1;
2781                 data.ioc_inlbuf2 = argv[2];
2782                 data.ioc_inllen3 = strlen(argv[3]) + 1;
2783                 data.ioc_inlbuf3 = argv[3];
2784         } else {
2785                 char from[2] = "1", to[3] = "-1";
2786                 data.ioc_inllen2 = strlen(from) + 1;
2787                 data.ioc_inlbuf2 = from;
2788                 data.ioc_inllen3 = strlen(to) + 1;
2789                 data.ioc_inlbuf3 = to;
2790         }
2791         data.ioc_inllen4 = sizeof(rawbuf) - cfs_size_round(sizeof(data)) -
2792                 cfs_size_round(data.ioc_inllen1) -
2793                 cfs_size_round(data.ioc_inllen2) -
2794                 cfs_size_round(data.ioc_inllen3);
2795         memset(buf, 0, sizeof(rawbuf));
2796         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2797         if (rc) {
2798                 fprintf(stderr, "error: %s: invalid ioctl\n",
2799                         jt_cmdname(argv[0]));
2800                 return rc;
2801         }
2802
2803         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CHECK, buf);
2804         if (rc == 0)
2805                 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2806         else
2807                 fprintf(stderr, "OBD_IOC_LLOG_CHECK failed: %s\n",
2808                         strerror(errno));
2809         return rc;
2810 }
2811
2812 int jt_llog_remove(int argc, char **argv)
2813 {
2814         struct obd_ioctl_data data;
2815         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2816         int rc;
2817
2818         if (argc != 3 && argc != 2)
2819                 return CMD_HELP;
2820
2821         memset(&data, 0, sizeof(data));
2822         data.ioc_dev = cur_device;
2823         data.ioc_inllen1 = strlen(argv[1]) + 1;
2824         data.ioc_inlbuf1 = argv[1];
2825         if (argc == 3){
2826                 data.ioc_inllen2 = strlen(argv[2]) + 1;
2827                 data.ioc_inlbuf2 = argv[2];
2828         }
2829         memset(buf, 0, sizeof(rawbuf));
2830         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2831         if (rc) {
2832                 fprintf(stderr, "error: %s: invalid ioctl\n",
2833                         jt_cmdname(argv[0]));
2834                 return rc;
2835         }
2836
2837         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_REMOVE, buf);
2838         if (rc == 0) {
2839                 if (argc == 2)
2840                         fprintf(stdout, "log %s is removed.\n", argv[1]);
2841                 else
2842                         fprintf(stdout, "the log in catalog %s is removed. \n",
2843                                 argv[1]);
2844         } else
2845                 fprintf(stderr, "OBD_IOC_LLOG_REMOVE failed: %s\n",
2846                         strerror(errno));
2847
2848         return rc;
2849 }
2850
2851 static void signal_server(int sig)
2852 {
2853         if (sig == SIGINT) {
2854                 do_disconnect("sigint", 1);
2855                 exit(1);
2856         } else
2857                 fprintf(stderr, "%s: got signal %d\n", jt_cmdname("sigint"), sig);
2858 }
2859
2860 int obd_initialize(int argc, char **argv)
2861 {
2862         if (shmem_setup() != 0)
2863                 return -1;
2864
2865         register_ioc_dev(OBD_DEV_ID, OBD_DEV_PATH,
2866                          OBD_DEV_MAJOR, OBD_DEV_MINOR);
2867
2868         return 0;
2869 }
2870
2871 void obd_finalize(int argc, char **argv)
2872 {
2873         struct sigaction sigact;
2874
2875         /* sigact initialization */
2876         sigact.sa_handler = signal_server;
2877         sigfillset(&sigact.sa_mask);
2878         sigact.sa_flags = SA_RESTART;
2879         /* coverity[uninit_use_in_call] */
2880         sigaction(SIGINT, &sigact, NULL);
2881
2882         shmem_cleanup();
2883         do_disconnect(argv[0], 1);
2884 }
2885
2886 static int check_pool_cmd(enum lcfg_command_type cmd,
2887                           char *fsname, char *poolname,
2888                           char *ostname)
2889 {
2890         int rc;
2891
2892         rc = llapi_search_ost(fsname, poolname, ostname);
2893         if (rc < 0 && (cmd != LCFG_POOL_NEW)) {
2894                 fprintf(stderr, "Pool %s.%s not found\n",
2895                         fsname, poolname);
2896                 return rc;
2897         }
2898
2899         switch (cmd) {
2900         case LCFG_POOL_NEW: {
2901                 if (ostname != NULL)
2902                         return -EINVAL;
2903
2904                 if (rc >= 0) {
2905                         fprintf(stderr, "Pool %s.%s already exists\n",
2906                                 fsname, poolname);
2907                         return -EEXIST;
2908                 }
2909                 return 0;
2910         }
2911         case LCFG_POOL_DEL: {
2912                 if (ostname != NULL)
2913                         return -EINVAL;
2914
2915                 if (rc == 1) {
2916                         fprintf(stderr, "Pool %s.%s not empty, "
2917                                 "please remove all members\n",
2918                                 fsname, poolname);
2919                         return -ENOTEMPTY;
2920                 }
2921                 return 0;
2922         }
2923         case LCFG_POOL_ADD: {
2924                 if (rc == 1) {
2925                         fprintf(stderr, "OST %s is already in pool %s.%s\n",
2926                                 ostname, fsname, poolname);
2927                         return -EEXIST;
2928                 }
2929                 rc = llapi_search_ost(fsname, NULL, ostname);
2930                 if (rc == 0) {
2931                         fprintf(stderr, "OST %s is not part of the '%s' fs.\n",
2932                                 ostname, fsname);
2933                         return -ENOENT;
2934                 }
2935                 return 0;
2936         }
2937         case LCFG_POOL_REM: {
2938                 if (rc == 0) {
2939                         fprintf(stderr, "OST %s not found in pool %s.%s\n",
2940                                 ostname, fsname, poolname);
2941                         return -ENOENT;
2942                 }
2943                 return 0;
2944         }
2945         default:
2946                 break;
2947         } /* switch */
2948         return -EINVAL;
2949 }
2950
2951 /* This check only verifies that the changes have been "pushed out" to
2952    the client successfully.  This involves waiting for a config update,
2953    and so may fail because of problems in that code or post-command
2954    network loss. So reporting a warning is appropriate, but not a failure.
2955 */
2956 static int check_pool_cmd_result(enum lcfg_command_type cmd,
2957                                  char *fsname, char *poolname,
2958                                  char *ostname)
2959 {
2960         int cpt = 10;
2961         int rc = 0;
2962
2963         switch (cmd) {
2964         case LCFG_POOL_NEW: {
2965                 do {
2966                         rc = llapi_search_ost(fsname, poolname, NULL);
2967                         if (rc == -ENODEV)
2968                                 return rc;
2969                         if (rc < 0)
2970                                 sleep(2);
2971                         cpt--;
2972                 } while ((rc < 0) && (cpt > 0));
2973                 if (rc >= 0) {
2974                         fprintf(stderr, "Pool %s.%s created\n",
2975                                 fsname, poolname);
2976                         return 0;
2977                 } else {
2978                         fprintf(stderr, "Warning, pool %s.%s not found\n",
2979                                 fsname, poolname);
2980                         return -ENOENT;
2981                 }
2982         }
2983         case LCFG_POOL_DEL: {
2984                 do {
2985                         rc = llapi_search_ost(fsname, poolname, NULL);
2986                         if (rc == -ENODEV)
2987                                 return rc;
2988                         if (rc >= 0)
2989                                 sleep(2);
2990                         cpt--;
2991                 } while ((rc >= 0) && (cpt > 0));
2992                 if (rc < 0) {
2993                         fprintf(stderr, "Pool %s.%s destroyed\n",
2994                                 fsname, poolname);
2995                         return 0;
2996                 } else {
2997                         fprintf(stderr, "Warning, pool %s.%s still found\n",
2998                                 fsname, poolname);
2999                         return -EEXIST;
3000                 }
3001         }
3002         case LCFG_POOL_ADD: {
3003                 do {
3004                         rc = llapi_search_ost(fsname, poolname, ostname);
3005                         if (rc == -ENODEV)
3006                                 return rc;
3007                         if (rc != 1)
3008                                 sleep(2);
3009                         cpt--;
3010                 } while ((rc != 1) && (cpt > 0));
3011                 if (rc == 1) {
3012                         fprintf(stderr, "OST %s added to pool %s.%s\n",
3013                                 ostname, fsname, poolname);
3014                         return 0;
3015                 } else {
3016                         fprintf(stderr, "Warning, OST %s not found in pool %s.%s\n",
3017                                 ostname, fsname, poolname);
3018                         return -ENOENT;
3019                 }
3020         }
3021         case LCFG_POOL_REM: {
3022                 do {
3023                         rc = llapi_search_ost(fsname, poolname, ostname);
3024                         if (rc == -ENODEV)
3025                                 return rc;
3026                         if (rc == 1)
3027                                 sleep(2);
3028                         cpt--;
3029                 } while ((rc == 1) && (cpt > 0));
3030                 if (rc != 1) {
3031                         fprintf(stderr, "OST %s removed from pool %s.%s\n",
3032                                 ostname, fsname, poolname);
3033                         return 0;
3034                 } else {
3035                         fprintf(stderr, "Warning, OST %s still found in pool %s.%s\n",
3036                                 ostname, fsname, poolname);
3037                         return -EEXIST;
3038                 }
3039         }
3040         default:
3041                 break;
3042         }
3043         return -EINVAL;
3044 }
3045
3046 static int check_and_complete_ostname(char *fsname, char *ostname)
3047 {
3048         char *ptr;
3049         char real_ostname[MAX_OBD_NAME + 1];
3050         char i;
3051
3052         /* if OST name does not start with fsname, we add it */
3053         /* if not check if the fsname is the right one */
3054         ptr = strchr(ostname, '-');
3055         if (ptr == NULL) {
3056                 sprintf(real_ostname, "%s-%s", fsname, ostname);
3057         } else if (strncmp(ostname, fsname, strlen(fsname)) != 0) {
3058                 fprintf(stderr, "%s does not start with fsname %s\n",
3059                         ostname, fsname);
3060                 return -EINVAL;
3061         } else {
3062                 if (strlen(ostname) > sizeof(real_ostname)-1)
3063                         return -E2BIG;
3064                 strncpy(real_ostname, ostname, sizeof(real_ostname));
3065         }
3066         /* real_ostname is fsname-????? */
3067         ptr = real_ostname + strlen(fsname) + 1;
3068         if (strncmp(ptr, "OST", 3) != 0) {
3069                 fprintf(stderr, "%s does not start by %s-OST nor OST\n",
3070                         ostname, fsname);
3071                 return -EINVAL;
3072         }
3073         /* real_ostname is fsname-OST????? */
3074         ptr += 3;
3075         for (i = 0; i < 4; i++) {
3076                 if (!isxdigit(*ptr)) {
3077                         fprintf(stderr,
3078                                 "ost's index in %s is not an hexa number\n",
3079                                 ostname);
3080                         return -EINVAL;
3081                 }
3082                 ptr++;
3083         }
3084         /* real_ostname is fsname-OSTXXXX????? */
3085         /* if OST name does not end with _UUID, we add it */
3086         if (*ptr == '\0') {
3087                 strcat(real_ostname, "_UUID");
3088         } else if (strcmp(ptr, "_UUID") != 0) {
3089                 fprintf(stderr,
3090                         "ostname %s does not end with _UUID\n", ostname);
3091                 return -EINVAL;
3092         }
3093         /* real_ostname is fsname-OSTXXXX_UUID */
3094         strcpy(ostname, real_ostname);
3095         return 0;
3096 }
3097
3098 /* returns 0 or -errno */
3099 static int pool_cmd(enum lcfg_command_type cmd,
3100                     char *cmdname, char *fullpoolname,
3101                     char *fsname, char *poolname, char *ostname)
3102 {
3103         int rc = 0;
3104         struct obd_ioctl_data data;
3105         struct lustre_cfg_bufs bufs;
3106         struct lustre_cfg *lcfg;
3107         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
3108
3109         rc = check_pool_cmd(cmd, fsname, poolname, ostname);
3110         if (rc == -ENODEV)
3111                 fprintf(stderr, "Can't verify pool command since there "
3112                         "is no local MDT or client, proceeding anyhow...\n");
3113         else if (rc)
3114                 return rc;
3115
3116         lustre_cfg_bufs_reset(&bufs, NULL);
3117         lustre_cfg_bufs_set_string(&bufs, 0, cmdname);
3118         lustre_cfg_bufs_set_string(&bufs, 1, fullpoolname);
3119         if (ostname != NULL)
3120                 lustre_cfg_bufs_set_string(&bufs, 2, ostname);
3121
3122
3123         lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
3124         if (lcfg == NULL)
3125                 return -ENOMEM;
3126         lustre_cfg_init(lcfg, cmd, &bufs);
3127
3128         memset(&data, 0, sizeof(data));
3129         rc = data.ioc_dev = get_mgs_device();
3130         if (rc < 0)
3131                 goto out;
3132
3133         data.ioc_type = LUSTRE_CFG_TYPE;
3134         data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
3135                                         lcfg->lcfg_buflens);
3136         data.ioc_pbuf1 = (void *)lcfg;
3137
3138         memset(buf, 0, sizeof(rawbuf));
3139         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
3140         if (rc) {
3141                 fprintf(stderr, "error: %s: invalid ioctl\n",
3142                         jt_cmdname(cmdname));
3143                 free(lcfg);
3144                 return rc;
3145         }
3146         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_POOL, buf);
3147 out:
3148         if (rc)
3149                 rc = -errno;
3150         free(lcfg);
3151         return rc;
3152 }
3153
3154 /**
3155  * Format and send the ioctl to the MGS.
3156  *
3157  * \param       cmd             IOCTL to send
3158  * \param       ret_data        void pointer to return anything from
3159  *                              ioctl
3160  * \param       num_args        number of arguments to pack into the
3161  *                              ioctl buffer
3162  * \param       argv[]          variable number of string arguments
3163  *
3164  * \retval                      0 on success
3165  */
3166 static int nodemap_cmd(enum lcfg_command_type cmd, void *ret_data,
3167                        unsigned int ret_size, ...)
3168 {
3169         va_list                 ap;
3170         char                    *arg;
3171         int                     i = 0;
3172         struct lustre_cfg_bufs  bufs;
3173         struct obd_ioctl_data   data;
3174         struct lustre_cfg       *lcfg;
3175         char                    rawbuf[MAX_IOC_BUFLEN];
3176         char                    *buf = rawbuf;
3177         int                     rc = 0;
3178
3179         lustre_cfg_bufs_reset(&bufs, NULL);
3180
3181         va_start(ap, ret_size);
3182         arg = va_arg(ap, char *);
3183         while (arg != NULL) {
3184                 lustre_cfg_bufs_set_string(&bufs, i, arg);
3185                 i++;
3186                 arg = va_arg(ap, char *);
3187         }
3188         va_end(ap);
3189
3190         lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
3191         if (lcfg == NULL)
3192                 return -ENOMEM;
3193         lustre_cfg_init(lcfg, cmd, &bufs);
3194
3195         memset(&data, 0, sizeof(data));
3196         rc = data.ioc_dev = get_mgs_device();
3197         if (rc < 0)
3198                 goto out;
3199
3200         data.ioc_type = LUSTRE_CFG_TYPE;
3201         data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
3202                          lcfg->lcfg_buflens);
3203         data.ioc_pbuf1 = (void *)lcfg;
3204
3205         memset(buf, 0, sizeof(rawbuf));
3206         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
3207         if (rc != 0) {
3208                 fprintf(stderr, "error: invalid ioctl: %08x errno: %d with "
3209                                "rc=%d\n", cmd, errno, rc);
3210                 goto out;
3211         }
3212
3213         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_NODEMAP, buf);
3214         if (rc != 0) {
3215                 fprintf(stderr, "error: invalid ioctl: %08x errno: %d with "
3216                                "rc=%d\n", cmd, errno, rc);
3217                 goto out;
3218         }
3219
3220         if (ret_data != NULL) {
3221                 rc = obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
3222                 if (rc != 0)
3223                         goto out;
3224
3225                 if (ret_size > data.ioc_plen1)
3226                         ret_size = data.ioc_plen1;
3227
3228                 memcpy(ret_data, data.ioc_pbuf1, ret_size);
3229         }
3230 out:
3231         free(lcfg);
3232
3233         return rc;
3234 }
3235
3236 /**
3237  * activate nodemap functions
3238  *
3239  * \param       argc            number of args
3240  * \param       argv[]          variable string arguments
3241  *
3242  * argv[0]                      1 for activate or 0 for deactivate
3243  *
3244  * \retval                      0 on success
3245  */
3246 int jt_nodemap_activate(int argc, char **argv)
3247 {
3248         int rc;
3249
3250         rc = nodemap_cmd(LCFG_NODEMAP_ACTIVATE, NULL, 0, argv[0], argv[1],
3251                          NULL);
3252
3253         if (rc != 0) {
3254                 errno = -rc;
3255                 perror(argv[0]);
3256         }
3257
3258         return rc;
3259 }
3260
3261 /**
3262  * add a nodemap
3263  *
3264  * \param       argc            number of args
3265  * \param       argv[]          variable string arguments
3266  *
3267  * argv[0]                      nodemap name
3268  *
3269  * \retval                      0 on success
3270  */
3271 int jt_nodemap_add(int argc, char **argv)
3272 {
3273         int rc;
3274
3275         rc = llapi_nodemap_exists(argv[1]);
3276         if (rc == 0) {
3277                 fprintf(stderr, "error: %s existing nodemap name\n", argv[1]);
3278                 return 1;
3279         }
3280
3281         rc = nodemap_cmd(LCFG_NODEMAP_ADD, NULL, 0, argv[0], argv[1], NULL);
3282
3283         if (rc != 0) {
3284                 errno = -rc;
3285                 perror(argv[0]);
3286         }
3287
3288         return rc;
3289 }
3290
3291 /**
3292  * delete a nodemap
3293  *
3294  * \param       argc            number of args
3295  * \param       argv[]          variable string arguments