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