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