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