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