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