Whamcloud - gitweb
LU-881 utils: running bumped w/o lock
[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 (c) 2002, 2010, Oracle and/or its affiliates. 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 <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48 #include <sys/types.h>
49 #include <sys/un.h>
50 #include <sys/wait.h>
51
52 #include <ctype.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <glob.h>
56 #include <signal.h>
57 #include <stdarg.h>
58 #include <stdbool.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <time.h>
63 #include <unistd.h>
64
65 #include "obdctl.h"
66
67 #include <obd.h>          /* for struct lov_stripe_md */
68 #include <lustre/lustre_build_version.h>
69
70 #include <obd_class.h>
71 #include <lnet/lnetctl.h>
72 #include <libcfs/libcfsutil.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_seq, 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 bool running_not_bumped = true;
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 (running_not_bumped) {
497                 shared_data->running++;
498                 running_not_bumped = false;
499         }
500         shmem_unlock();
501 }
502
503 static void shmem_snap(int total_threads, int live_threads)
504 {
505         struct timeval this_time;
506         int non_zero = 0;
507         __u64 total = 0;
508         double secs;
509         int running;
510         int i;
511
512         if (shared_data == NULL || total_threads > MAX_THREADS)
513                 return;
514
515         shmem_lock();
516         memcpy(counter_snapshot[0], shared_data->counters,
517                total_threads * sizeof(counter_snapshot[0][0]));
518         running = shared_data->running;
519         shmem_unlock();
520
521         gettimeofday(&this_time, NULL);
522
523         for (i = 0; i < total_threads; i++) {
524                 long long this_count =
525                         counter_snapshot[0][i] - counter_snapshot[1][i];
526
527                 if (this_count != 0) {
528                         non_zero++;
529                         total += this_count;
530                 }
531         }
532
533         secs = (this_time.tv_sec + this_time.tv_usec / 1000000.0) -
534                (prev_time.tv_sec + prev_time.tv_usec / 1000000.0);
535
536         if (prev_valid &&
537             secs > 1.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                         exit(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];
905         char *version;
906
907         if (argc != 1)
908                 return CMD_HELP;
909
910         rc = llapi_get_version(rawbuf, MAX_IOC_BUFLEN, &version);
911         if (rc)
912                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
913                         strerror(-rc));
914         else
915                 printf("Lustre version: %s\n", version);
916
917         printf("lctl   version: %s\n", BUILD_VERSION);
918         return rc;
919 }
920
921 /*
922  * Print an obd device line with the ost_conn_uuid inserted, if the obd
923  * is an osc.
924  */
925 static void print_obd_line(char *s)
926 {
927         char buf[MAX_STRING_SIZE];
928         char obd_name[MAX_OBD_NAME];
929         FILE *fp = NULL;
930         char *ptr;
931
932         if (sscanf(s, " %*d %*s osc %s %*s %*d ", obd_name) == 0)
933                 goto try_mdc;
934         snprintf(buf, sizeof(buf),
935                  "/proc/fs/lustre/osc/%s/ost_conn_uuid", obd_name);
936         if ((fp = fopen(buf, "r")) == NULL)
937                 goto try_mdc;
938         goto got_one;
939
940 try_mdc:
941         if (sscanf(s, " %*d %*s mdc %s %*s %*d ", obd_name) == 0)
942                 goto fail;
943         snprintf(buf, sizeof(buf),
944                  "/proc/fs/lustre/mdc/%s/mds_conn_uuid", obd_name);
945         if ((fp = fopen(buf, "r")) == NULL)
946                 goto fail;
947
948 got_one:
949         /* should not ignore fgets(3)'s return value */
950         if (!fgets(buf, sizeof(buf), fp)) {
951                 fprintf(stderr, "reading from %s: %s", buf, strerror(errno));
952                 fclose(fp);
953                 return;
954         }
955         fclose(fp);
956
957         /* trim trailing newlines */
958         ptr = strrchr(buf, '\n');
959         if (ptr) *ptr = '\0';
960         ptr = strrchr(s, '\n');
961         if (ptr) *ptr = '\0';
962
963         printf("%s %s\n", s, buf);
964         return;
965
966 fail:
967         printf("%s", s);
968         return;
969 }
970
971 /* get device list by ioctl */
972 int jt_obd_list_ioctl(int argc, char **argv)
973 {
974         int rc, index;
975         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
976         struct obd_ioctl_data *data = (struct obd_ioctl_data *)buf;
977
978         if (argc > 2)
979                 return CMD_HELP;
980         /* Just ignore a -t option.  Only supported with /proc. */
981         else if (argc == 2 && strcmp(argv[1], "-t") != 0)
982                 return CMD_HELP;
983
984         for (index = 0;; index++) {
985                 memset(buf, 0, sizeof(rawbuf));
986                 data->ioc_version = OBD_IOCTL_VERSION;
987                 data->ioc_inllen1 =
988                         sizeof(rawbuf) - cfs_size_round(sizeof(*data));
989                 data->ioc_inlbuf1 = buf + cfs_size_round(sizeof(*data));
990                 data->ioc_len = obd_ioctl_packlen(data);
991                 data->ioc_count = index;
992
993                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_GETDEVICE, buf);
994                 if (rc != 0)
995                         break;
996                 printf("%s\n", (char *)data->ioc_bulk);
997         }
998         if (rc != 0) {
999                 if (errno == ENOENT)
1000                         /* no device or the last device */
1001                         rc = 0;
1002                 else
1003                         fprintf(stderr, "Error getting device list: %s: "
1004                                 "check dmesg.\n", strerror(errno));
1005         }
1006         return rc;
1007 }
1008
1009 int jt_obd_list(int argc, char **argv)
1010 {
1011         int rc;
1012         char buf[MAX_STRING_SIZE];
1013         FILE *fp = NULL;
1014         int print_obd = 0;
1015
1016         if (argc > 2)
1017                 return CMD_HELP;
1018         else if (argc == 2) {
1019                 if (strcmp(argv[1], "-t") == 0)
1020                         print_obd = 1;
1021                 else
1022                         return CMD_HELP;
1023         }
1024
1025         fp = fopen(DEVICES_LIST, "r");
1026         if (fp == NULL) {
1027                 fprintf(stderr, "error: %s: %s opening "DEVICES_LIST"\n",
1028                         jt_cmdname(argv[0]), strerror(rc =  errno));
1029                 return jt_obd_list_ioctl(argc, argv);
1030         }
1031
1032         while (fgets(buf, sizeof(buf), fp) != NULL)
1033                 if (print_obd)
1034                         print_obd_line(buf);
1035                 else
1036                         printf("%s", buf);
1037
1038         fclose(fp);
1039         return 0;
1040 }
1041
1042 /* Create one or more objects, arg[4] may describe stripe meta-data.  If
1043  * not, defaults assumed.  This echo-client instance stashes the stripe
1044  * object ids.  Use get_stripe on this node to print full lsm and
1045  * set_stripe on another node to cut/paste between nodes.
1046  */
1047 /* create <count> [<file_create_mode>] [q|v|# verbosity] [striping] */
1048 int jt_obd_create(int argc, char **argv)
1049 {
1050         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1051         struct obd_ioctl_data data;
1052         struct timeval next_time;
1053         __u64 count = 1, next_count, base_id = 0;
1054         int verbose = 1, mode = 0100644, rc = 0, i, valid_lsm = 0;
1055         char *end;
1056
1057         memset(&data, 0x00, sizeof(data));
1058         data.ioc_dev = cur_device;
1059         if (argc < 2 || argc > 5)
1060                 return CMD_HELP;
1061
1062         count = strtoull(argv[1], &end, 0);
1063         if (*end) {
1064                 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1065                         jt_cmdname(argv[0]), argv[1]);
1066                 return CMD_HELP;
1067         }
1068
1069         if (argc > 2) {
1070                 mode = strtoul(argv[2], &end, 0);
1071                 if (*end) {
1072                         fprintf(stderr, "error: %s: invalid mode '%s'\n",
1073                                 jt_cmdname(argv[0]), argv[2]);
1074                         return CMD_HELP;
1075                 }
1076                 if (!(mode & S_IFMT))
1077                         mode |= S_IFREG;
1078         }
1079
1080         if (argc > 3) {
1081                 verbose = get_verbose(argv[0], argv[3]);
1082                 if (verbose == BAD_VERBOSE)
1083                         return CMD_HELP;
1084         }
1085
1086         if (argc < 5) {
1087                 reset_lsmb (&lsm_buffer);       /* will set default */
1088         } else {
1089                 rc = parse_lsm (&lsm_buffer, argv[4]);
1090                 if (rc != 0) {
1091                         fprintf(stderr, "error: %s: invalid lsm '%s'\n",
1092                                 jt_cmdname(argv[0]), argv[4]);
1093                         return CMD_HELP;
1094                 }
1095                 base_id = lsm_buffer.lsm.lsm_object_id;
1096                 valid_lsm = 1;
1097         }
1098
1099         printf("%s: "LPD64" objects\n", jt_cmdname(argv[0]), count);
1100         gettimeofday(&next_time, NULL);
1101         next_time.tv_sec -= verbose;
1102
1103         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1104                 data.ioc_obdo1.o_mode = mode;
1105                 data.ioc_obdo1.o_id = base_id;
1106                 data.ioc_obdo1.o_uid = 0;
1107                 data.ioc_obdo1.o_gid = 0;
1108                 data.ioc_obdo1.o_valid = OBD_MD_FLTYPE | OBD_MD_FLMODE |
1109                         OBD_MD_FLID | OBD_MD_FLUID | OBD_MD_FLGID;
1110
1111                 if (valid_lsm) {
1112                         data.ioc_plen1 = sizeof lsm_buffer;
1113                         data.ioc_pbuf1 = (char *)&lsm_buffer;
1114                 }
1115
1116                 memset(buf, 0, sizeof(rawbuf));
1117                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1118                 if (rc) {
1119                         fprintf(stderr, "error: %s: invalid ioctl\n",
1120                                 jt_cmdname(argv[0]));
1121                         return rc;
1122                 }
1123                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_CREATE, buf);
1124                 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
1125                 shmem_bump();
1126                 if (rc < 0) {
1127                         fprintf(stderr, "error: %s: #%d - %s\n",
1128                                 jt_cmdname(argv[0]), i, strerror(rc = errno));
1129                         break;
1130                 }
1131                 if (!(data.ioc_obdo1.o_valid & OBD_MD_FLID)) {
1132                         fprintf(stderr,"error: %s: oid not valid #%d:"LPX64"\n",
1133                                 jt_cmdname(argv[0]), i, data.ioc_obdo1.o_valid);
1134                         rc = EINVAL;
1135                         break;
1136                 }
1137
1138                 if (be_verbose(verbose, &next_time, i, &next_count, count))
1139                         printf("%s: #%d is object id "LPX64"\n",
1140                                 jt_cmdname(argv[0]), i, data.ioc_obdo1.o_id);
1141         }
1142         return rc;
1143 }
1144
1145 int jt_obd_setattr(int argc, char **argv)
1146 {
1147         struct obd_ioctl_data data;
1148         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1149         char *end;
1150         int rc;
1151
1152         memset(&data, 0x00, sizeof(data));
1153         data.ioc_dev = cur_device;
1154         if (argc != 2)
1155                 return CMD_HELP;
1156
1157         data.ioc_obdo1.o_id = strtoull(argv[1], &end, 0);
1158         if (*end) {
1159                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1160                         jt_cmdname(argv[0]), argv[1]);
1161                 return CMD_HELP;
1162         }
1163         data.ioc_obdo1.o_mode = S_IFREG | strtoul(argv[2], &end, 0);
1164         if (*end) {
1165                 fprintf(stderr, "error: %s: invalid mode '%s'\n",
1166                         jt_cmdname(argv[0]), argv[2]);
1167                 return CMD_HELP;
1168         }
1169         data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
1170
1171         memset(buf, 0, sizeof(rawbuf));
1172         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1173         if (rc) {
1174                 fprintf(stderr, "error: %s: invalid ioctl\n",
1175                         jt_cmdname(argv[0]));
1176                 return rc;
1177         }
1178         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, buf);
1179         if (rc < 0)
1180                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1181                         strerror(rc = errno));
1182
1183         return rc;
1184 }
1185
1186 int jt_obd_test_setattr(int argc, char **argv)
1187 {
1188         struct obd_ioctl_data data;
1189         struct timeval start, next_time;
1190         __u64 i, count, next_count;
1191         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1192         int verbose = 1;
1193         obd_id objid = 3;
1194         char *end;
1195         int rc = 0;
1196
1197         if (argc < 2 || argc > 4)
1198                 return CMD_HELP;
1199
1200         memset(&data, 0x00, sizeof(data));
1201         data.ioc_dev = cur_device;
1202         count = strtoull(argv[1], &end, 0);
1203         if (*end) {
1204                 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1205                         jt_cmdname(argv[0]), argv[1]);
1206                 return CMD_HELP;
1207         }
1208
1209         if (argc >= 3) {
1210                 verbose = get_verbose(argv[0], argv[2]);
1211                 if (verbose == BAD_VERBOSE)
1212                         return CMD_HELP;
1213         }
1214
1215         if (argc >= 4) {
1216                 if (argv[3][0] == 't') {
1217                         objid = strtoull(argv[3] + 1, &end, 0);
1218                         if (thread)
1219                                 objid += thread - 1;
1220                 } else
1221                         objid = strtoull(argv[3], &end, 0);
1222                 if (*end) {
1223                         fprintf(stderr, "error: %s: invalid objid '%s'\n",
1224                                 jt_cmdname(argv[0]), argv[3]);
1225                         return CMD_HELP;
1226                 }
1227         }
1228
1229         gettimeofday(&start, NULL);
1230         next_time.tv_sec = start.tv_sec - verbose;
1231         next_time.tv_usec = start.tv_usec;
1232         if (verbose != 0)
1233                 printf("%s: setting "LPD64" attrs (objid "LPX64"): %s",
1234                        jt_cmdname(argv[0]), count, objid, ctime(&start.tv_sec));
1235
1236         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1237                 data.ioc_obdo1.o_id = objid;
1238                 data.ioc_obdo1.o_mode = S_IFREG;
1239                 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
1240                 memset(buf, 0x00, sizeof(rawbuf));
1241                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1242                 if (rc) {
1243                         fprintf(stderr, "error: %s: invalid ioctl\n",
1244                                 jt_cmdname(argv[0]));
1245                         return rc;
1246                 }
1247                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, &data);
1248                 shmem_bump();
1249                 if (rc < 0) {
1250                         fprintf(stderr, "error: %s: #"LPD64" - %d:%s\n",
1251                                 jt_cmdname(argv[0]), i, errno, strerror(rc = errno));
1252                         break;
1253                 } else {
1254                         if (be_verbose
1255                             (verbose, &next_time, i, &next_count, count))
1256                                 printf("%s: set attr #"LPD64"\n",
1257                                        jt_cmdname(argv[0]), i);
1258                 }
1259         }
1260
1261         if (!rc) {
1262                 struct timeval end;
1263                 double diff;
1264
1265                 gettimeofday(&end, NULL);
1266
1267                 diff = difftime(&end, &start);
1268
1269                 --i;
1270                 if (verbose != 0)
1271                         printf("%s: "LPD64" attrs in %.3fs (%.3f attr/s): %s",
1272                                jt_cmdname(argv[0]), i, diff, i / diff,
1273                                ctime(&end.tv_sec));
1274         }
1275         return rc;
1276 }
1277
1278 int jt_obd_destroy(int argc, char **argv)
1279 {
1280         struct obd_ioctl_data data;
1281         struct timeval next_time;
1282         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1283         __u64 count = 1, next_count;
1284         int verbose = 1;
1285         __u64 id;
1286         char *end;
1287         int rc = 0, i;
1288
1289         memset(&data, 0x00, sizeof(data));
1290         data.ioc_dev = cur_device;
1291         if (argc < 2 || argc > 4)
1292                 return CMD_HELP;
1293
1294         id = strtoull(argv[1], &end, 0);
1295         if (*end || id == 0 || errno != 0) {
1296                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1297                         jt_cmdname(argv[0]), argv[1]);
1298                 return CMD_HELP;
1299         }
1300         if (argc > 2) {
1301                 count = strtoull(argv[2], &end, 0);
1302                 if (*end) {
1303                         fprintf(stderr,
1304                                 "error: %s: invalid iteration count '%s'\n",
1305                                 jt_cmdname(argv[0]), argv[2]);
1306                         return CMD_HELP;
1307                 }
1308         }
1309
1310         if (argc > 3) {
1311                 verbose = get_verbose(argv[0], argv[3]);
1312                 if (verbose == BAD_VERBOSE)
1313                         return CMD_HELP;
1314         }
1315
1316         printf("%s: "LPD64" objects\n", jt_cmdname(argv[0]), count);
1317         gettimeofday(&next_time, NULL);
1318         next_time.tv_sec -= verbose;
1319
1320         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++, id++) {
1321                 data.ioc_obdo1.o_id = id;
1322                 data.ioc_obdo1.o_mode = S_IFREG | 0644;
1323                 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLMODE;
1324
1325                 memset(buf, 0, sizeof(rawbuf));
1326                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1327                 if (rc) {
1328                         fprintf(stderr, "error: %s: invalid ioctl\n",
1329                                 jt_cmdname(argv[0]));
1330                         return rc;
1331                 }
1332                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_DESTROY, buf);
1333                 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
1334                 shmem_bump();
1335                 if (rc < 0) {
1336                         fprintf(stderr, "error: %s: objid "LPX64": %s\n",
1337                                 jt_cmdname(argv[0]), id, strerror(rc = errno));
1338                         break;
1339                 }
1340
1341                 if (be_verbose(verbose, &next_time, i, &next_count, count))
1342                         printf("%s: #%d is object id "LPX64"\n",
1343                                jt_cmdname(argv[0]), i, id);
1344         }
1345
1346         return rc;
1347 }
1348
1349 int jt_obd_getattr(int argc, char **argv)
1350 {
1351         struct obd_ioctl_data data;
1352         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1353         char *end;
1354         int rc;
1355
1356         if (argc != 2)
1357                 return CMD_HELP;
1358
1359         memset(&data, 0x00, sizeof(data));
1360         data.ioc_dev = cur_device;
1361         data.ioc_obdo1.o_id = strtoull(argv[1], &end, 0);
1362         if (*end) {
1363                 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1364                         jt_cmdname(argv[0]), argv[1]);
1365                 return CMD_HELP;
1366         }
1367         /* to help obd filter */
1368         data.ioc_obdo1.o_mode = 0100644;
1369         data.ioc_obdo1.o_valid = 0xffffffff;
1370         printf("%s: object id "LPX64"\n", jt_cmdname(argv[0]),data.ioc_obdo1.o_id);
1371
1372         memset(buf, 0, sizeof(rawbuf));
1373         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1374         if (rc) {
1375                 fprintf(stderr, "error: %s: invalid ioctl\n",
1376                         jt_cmdname(argv[0]));
1377                 return rc;
1378         }
1379         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, buf);
1380         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
1381         if (rc) {
1382                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1383                         strerror(rc = errno));
1384         } else {
1385                 printf("%s: object id "LPX64", mode %o\n", jt_cmdname(argv[0]),
1386                        data.ioc_obdo1.o_id, data.ioc_obdo1.o_mode);
1387         }
1388         return rc;
1389 }
1390
1391 int jt_obd_test_getattr(int argc, char **argv)
1392 {
1393         struct obd_ioctl_data data;
1394         struct timeval start, next_time;
1395         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1396         __u64 i, count, next_count;
1397         int verbose = 1;
1398         obd_id objid = 3;
1399         char *end;
1400         int rc = 0;
1401
1402         if (argc < 2 || argc > 4)
1403                 return CMD_HELP;
1404
1405         memset(&data, 0x00, sizeof(data));
1406         data.ioc_dev = cur_device;
1407         count = strtoull(argv[1], &end, 0);
1408         if (*end) {
1409                 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1410                         jt_cmdname(argv[0]), argv[1]);
1411                 return CMD_HELP;
1412         }
1413
1414         if (argc >= 3) {
1415                 verbose = get_verbose(argv[0], argv[2]);
1416                 if (verbose == BAD_VERBOSE)
1417                         return CMD_HELP;
1418         }
1419
1420         if (argc >= 4) {
1421                 if (argv[3][0] == 't') {
1422                         objid = strtoull(argv[3] + 1, &end, 0);
1423                         if (thread)
1424                                 objid += thread - 1;
1425                 } else
1426                         objid = strtoull(argv[3], &end, 0);
1427                 if (*end) {
1428                         fprintf(stderr, "error: %s: invalid objid '%s'\n",
1429                                 jt_cmdname(argv[0]), argv[3]);
1430                         return CMD_HELP;
1431                 }
1432         }
1433
1434         gettimeofday(&start, NULL);
1435         next_time.tv_sec = start.tv_sec - verbose;
1436         next_time.tv_usec = start.tv_usec;
1437         if (verbose != 0)
1438                 printf("%s: getting "LPD64" attrs (objid "LPX64"): %s",
1439                        jt_cmdname(argv[0]), count, objid, ctime(&start.tv_sec));
1440
1441         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1442                 data.ioc_obdo1.o_id = objid;
1443                 data.ioc_obdo1.o_mode = S_IFREG;
1444                 data.ioc_obdo1.o_valid = 0xffffffff;
1445                 memset(buf, 0x00, sizeof(rawbuf));
1446                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1447                 if (rc) {
1448                         fprintf(stderr, "error: %s: invalid ioctl\n",
1449                                 jt_cmdname(argv[0]));
1450                         return rc;
1451                 }
1452                 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, &data);
1453                 shmem_bump();
1454                 if (rc < 0) {
1455                         fprintf(stderr, "error: %s: #"LPD64" - %d:%s\n",
1456                                 jt_cmdname(argv[0]), i, errno, strerror(rc = errno));
1457                         break;
1458                 } else {
1459                         if (be_verbose
1460                             (verbose, &next_time, i, &next_count, count))
1461                                 printf("%s: got attr #"LPD64"\n",
1462                                        jt_cmdname(argv[0]), i);
1463                 }
1464         }
1465
1466         if (!rc) {
1467                 struct timeval end;
1468                 double diff;
1469
1470                 gettimeofday(&end, NULL);
1471
1472                 diff = difftime(&end, &start);
1473
1474                 --i;
1475                 if (verbose != 0)
1476                         printf("%s: "LPD64" attrs in %.3fs (%.3f attr/s): %s",
1477                                jt_cmdname(argv[0]), i, diff, i / diff,
1478                                ctime(&end.tv_sec));
1479         }
1480         return rc;
1481 }
1482
1483 /* test_brw <cnt>                                               count
1484         <r|w[r(repeat)x(noverify)]>                             mode
1485         <q|v|#(print interval)>                                 verbosity
1486         <npages[+offset]>                                       blocksize
1487         <[[<interleave_threads>]t(inc obj by thread#)]obj>      object
1488         [p|g<args>]                                             batch */
1489 int jt_obd_test_brw(int argc, char **argv)
1490 {
1491         struct obd_ioctl_data data;
1492         struct timeval start, next_time;
1493         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1494         __u64 count, next_count, len, stride, thr_offset = 0, objid = 3;
1495         int write = 0, verbose = 1, cmd, i, rc = 0, pages = 1;
1496         int offset_pages = 0;
1497         long n;
1498         int repeat_offset = 0;
1499         unsigned long long ull;
1500         int  nthr_per_obj = 0;
1501         int  verify = 1;
1502         int  obj_idx = 0;
1503         char *end;
1504
1505         if (argc < 2 || argc > 7) {
1506                 fprintf(stderr, "error: %s: bad number of arguments: %d\n",
1507                         jt_cmdname(argv[0]), argc);
1508                 return CMD_HELP;
1509         }
1510
1511         count = strtoull(argv[1], &end, 0);
1512         if (*end) {
1513                 fprintf(stderr, "error: %s: bad iteration count '%s'\n",
1514                         jt_cmdname(argv[0]), argv[1]);
1515                 return CMD_HELP;
1516         }
1517
1518         if (argc >= 3) {
1519                 if (argv[2][0] == 'w' || argv[2][0] == '1')
1520                         write = 1;
1521                 /* else it's a read */
1522
1523                 if (argv[2][0] != 0)
1524                         for (i = 1; argv[2][i] != 0; i++)
1525                                 switch (argv[2][i]) {
1526                                 case 'r':
1527                                         repeat_offset = 1;
1528                                         break;
1529
1530                                 case 'x':
1531                                         verify = 0;
1532                                         break;
1533
1534                                 default:
1535                                         fprintf (stderr, "Can't parse cmd '%s'\n",
1536                                                  argv[2]);
1537                                         return CMD_HELP;
1538                                 }
1539         }
1540
1541         if (argc >= 4) {
1542                 verbose = get_verbose(argv[0], argv[3]);
1543                 if (verbose == BAD_VERBOSE)
1544                         return CMD_HELP;
1545         }
1546
1547         if (argc >= 5) {
1548                 pages = strtoul(argv[4], &end, 0);
1549
1550                 if (*end == '+')
1551                         offset_pages = strtoul(end + 1, &end, 0);
1552
1553                 if (*end != 0 ||
1554                     offset_pages < 0 || offset_pages >= pages) {
1555                         fprintf(stderr, "error: %s: bad npages[+offset] parameter '%s'\n",
1556                                 jt_cmdname(argv[0]), argv[4]);
1557                         return CMD_HELP;
1558                 }
1559         }
1560
1561         if (argc >= 6) {
1562                 if (thread &&
1563                     (n = strtol(argv[5], &end, 0)) > 0 &&
1564                     *end == 't' &&
1565                     (ull = strtoull(end + 1, &end, 0)) > 0 &&
1566                     *end == 0) {
1567                         nthr_per_obj = n;
1568                         objid = ull;
1569                 } else if (thread &&
1570                            argv[5][0] == 't') {
1571                         nthr_per_obj = 1;
1572                         objid = strtoull(argv[5] + 1, &end, 0);
1573                 } else {
1574                         nthr_per_obj = 0;
1575                         objid = strtoull(argv[5], &end, 0);
1576                 }
1577                 if (*end) {
1578                         fprintf(stderr, "error: %s: bad objid '%s'\n",
1579                                 jt_cmdname(argv[0]), argv[5]);
1580                         return CMD_HELP;
1581                 }
1582         }
1583
1584         memset(&data, 0x00, sizeof(data));
1585         data.ioc_dev = cur_device;
1586
1587         /* communicate the 'type' of brw test and batching to echo_client.
1588          * don't start.  we'd love to refactor this lctl->echo_client
1589          * interface */
1590         data.ioc_pbuf1 = (void *)1;
1591         data.ioc_plen1 = 1;
1592
1593         if (argc >= 7) {
1594                 switch(argv[6][0]) {
1595                         case 'g': /* plug and unplug */
1596                                 data.ioc_pbuf1 = (void *)2;
1597                                 data.ioc_plen1 = strtoull(argv[6] + 1, &end,
1598                                                           0);
1599                                 break;
1600                         case 'p': /* prep and commit */
1601                                 data.ioc_pbuf1 = (void *)3;
1602                                 data.ioc_plen1 = strtoull(argv[6] + 1, &end,
1603                                                           0);
1604                                 break;
1605                         default:
1606                                 fprintf(stderr, "error: %s: batching '%s' "
1607                                         "needs to specify 'p' or 'g'\n",
1608                                         jt_cmdname(argv[0]), argv[6]);
1609                                 return CMD_HELP;
1610                 }
1611
1612                 if (*end) {
1613                         fprintf(stderr, "error: %s: bad batching '%s'\n",
1614                                 jt_cmdname(argv[0]), argv[6]);
1615                         return CMD_HELP;
1616                 }
1617                 data.ioc_plen1 *= getpagesize();
1618         }
1619
1620         len = pages * getpagesize();
1621         thr_offset = offset_pages * getpagesize();
1622         stride = len;
1623
1624 #ifdef MAX_THREADS
1625         if (thread) {
1626                 shmem_lock ();
1627                 if (nthr_per_obj != 0) {
1628                         /* threads interleave */
1629                         obj_idx = (thread - 1)/nthr_per_obj;
1630                         objid += obj_idx;
1631                         stride *= nthr_per_obj;
1632                         if ((thread - 1) % nthr_per_obj == 0)
1633                                 shared_data->offsets[obj_idx] = stride + thr_offset;
1634                         thr_offset += ((thread - 1) % nthr_per_obj) * len;
1635                 } else {
1636                         /* threads disjoint */
1637                         thr_offset += (thread - 1) * len;
1638                 }
1639
1640                 shared_data->barrier--;
1641                 if (shared_data->barrier == 0)
1642                         l_cond_broadcast(&shared_data->cond);
1643                 else
1644                         l_cond_wait(&shared_data->cond,
1645                                     &shared_data->mutex);
1646
1647                 shmem_unlock ();
1648         }
1649 #endif
1650
1651         data.ioc_obdo1.o_id = objid;
1652         data.ioc_obdo1.o_mode = S_IFREG;
1653         data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLFLAGS;
1654         data.ioc_obdo1.o_flags = (verify ? OBD_FL_DEBUG_CHECK : 0);
1655         data.ioc_count = len;
1656         data.ioc_offset = (repeat_offset ? 0 : thr_offset);
1657
1658         gettimeofday(&start, NULL);
1659         next_time.tv_sec = start.tv_sec - verbose;
1660         next_time.tv_usec = start.tv_usec;
1661
1662         if (verbose != 0)
1663                 printf("%s: %s "LPU64"x%d pages (obj "LPX64", off "LPU64"): %s",
1664                        jt_cmdname(argv[0]), write ? "writing" : "reading", count,
1665                        pages, objid, data.ioc_offset, ctime(&start.tv_sec));
1666
1667         cmd = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
1668         for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1669                 data.ioc_obdo1.o_valid &= ~(OBD_MD_FLBLOCKS|OBD_MD_FLGRANT);
1670                 memset(buf, 0x00, sizeof(rawbuf));
1671                 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1672                 if (rc) {
1673                         fprintf(stderr, "error: %s: invalid ioctl\n",
1674                                 jt_cmdname(argv[0]));
1675                         return rc;
1676                 }
1677                 rc = l2_ioctl(OBD_DEV_ID, cmd, buf);
1678                 shmem_bump();
1679                 if (rc) {
1680                         fprintf(stderr, "error: %s: #%d - %s on %s\n",
1681                                 jt_cmdname(argv[0]), i, strerror(rc = errno),
1682                                 write ? "write" : "read");
1683                         break;
1684                 } else if (be_verbose(verbose, &next_time,i, &next_count,count)) {
1685                         shmem_lock ();
1686                         printf("%s: %s number %d @ "LPD64":"LPU64" for %d\n",
1687                                jt_cmdname(argv[0]), write ? "write" : "read", i,
1688                                data.ioc_obdo1.o_id, data.ioc_offset,
1689                                (int)(pages * getpagesize()));
1690                         shmem_unlock ();
1691                 }
1692
1693                 if (!repeat_offset) {
1694 #ifdef MAX_THREADS
1695                         if (stride == len) {
1696                                 data.ioc_offset += stride;
1697                         } else if (i < count) {
1698                                 shmem_lock ();
1699                                 data.ioc_offset = shared_data->offsets[obj_idx];
1700                                 shared_data->offsets[obj_idx] += len;
1701                                 shmem_unlock ();
1702                         }
1703 #else
1704                         data.ioc_offset += len;
1705                         obj_idx = 0; /* avoids an unused var warning */
1706 #endif
1707                 }
1708         }
1709
1710         if (!rc) {
1711                 struct timeval end;
1712                 double diff;
1713
1714                 gettimeofday(&end, NULL);
1715
1716                 diff = difftime(&end, &start);
1717
1718                 --i;
1719                 if (verbose != 0)
1720                         printf("%s: %s %dx%d pages in %.3fs (%.3f MB/s): %s",
1721                                jt_cmdname(argv[0]), write ? "wrote" : "read",
1722                                i, pages, diff,
1723                                ((double)i * pages * getpagesize()) /
1724                                (diff * 1048576.0),
1725                                ctime(&end.tv_sec));
1726         }
1727
1728         return rc;
1729 }
1730
1731 int jt_obd_lov_getconfig(int argc, char **argv)
1732 {
1733         struct obd_ioctl_data data;
1734         struct lov_desc desc;
1735         struct obd_uuid *uuidarray;
1736         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1737         __u32 *obdgens;
1738         char *path;
1739         int rc, fd;
1740
1741         memset(&data, 0x00, sizeof(data));
1742         data.ioc_dev = cur_device;
1743
1744         if (argc != 2)
1745                 return CMD_HELP;
1746
1747         path = argv[1];
1748         fd = open(path, O_RDONLY);
1749         if (fd < 0) {
1750                 fprintf(stderr, "open \"%s\" failed: %s\n", path,
1751                         strerror(errno));
1752                 return -errno;
1753         }
1754
1755         memset(&desc, 0, sizeof(desc));
1756         obd_str2uuid(&desc.ld_uuid, argv[1]);
1757         desc.ld_tgt_count = ((OBD_MAX_IOCTL_BUFFER-sizeof(data)-sizeof(desc)) /
1758                              (sizeof(*uuidarray) + sizeof(*obdgens)));
1759
1760 repeat:
1761         uuidarray = calloc(desc.ld_tgt_count, sizeof(*uuidarray));
1762         if (!uuidarray) {
1763                 fprintf(stderr, "error: %s: no memory for %d uuid's\n",
1764                         jt_cmdname(argv[0]), desc.ld_tgt_count);
1765                 rc = -ENOMEM;
1766                 goto out;
1767         }
1768         obdgens = calloc(desc.ld_tgt_count, sizeof(*obdgens));
1769         if (!obdgens) {
1770                 fprintf(stderr, "error: %s: no memory for %d generation #'s\n",
1771                         jt_cmdname(argv[0]), desc.ld_tgt_count);
1772                 rc = -ENOMEM;
1773                 goto out_uuidarray;
1774         }
1775
1776         memset(buf, 0x00, sizeof(rawbuf));
1777         data.ioc_inllen1 = sizeof(desc);
1778         data.ioc_inlbuf1 = (char *)&desc;
1779         data.ioc_inllen2 = desc.ld_tgt_count * sizeof(*uuidarray);
1780         data.ioc_inlbuf2 = (char *)uuidarray;
1781         data.ioc_inllen3 = desc.ld_tgt_count * sizeof(*obdgens);
1782         data.ioc_inlbuf3 = (char *)obdgens;
1783
1784         if (obd_ioctl_pack(&data, &buf, sizeof(rawbuf))) {
1785                 fprintf(stderr, "error: %s: invalid ioctl\n",
1786                         jt_cmdname(argv[0]));
1787                 rc = -EINVAL;
1788                 goto out_obdgens;
1789         }
1790         rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
1791         if (rc == -ENOSPC) {
1792                 free(uuidarray);
1793                 free(obdgens);
1794                 goto repeat;
1795         } else if (rc) {
1796                 fprintf(stderr, "error: %s: ioctl error: %s\n",
1797                         jt_cmdname(argv[0]), strerror(rc = errno));
1798         } else {
1799                 struct obd_uuid *uuidp;
1800                 __u32 *genp;
1801                 int i;
1802
1803                 if (obd_ioctl_unpack(&data, buf, sizeof(rawbuf))) {
1804                         fprintf(stderr, "error: %s: invalid reply\n",
1805                                 jt_cmdname(argv[0]));
1806                         rc = -EINVAL;
1807                         goto out;
1808                 }
1809                 if (desc.ld_default_stripe_count == (__u32)-1)
1810                         printf("default_stripe_count: %d\n", -1);
1811                 else
1812                         printf("default_stripe_count: %u\n",
1813                                desc.ld_default_stripe_count);
1814                 printf("default_stripe_size: "LPU64"\n",
1815                        desc.ld_default_stripe_size);
1816                 printf("default_stripe_offset: "LPU64"\n",
1817                        desc.ld_default_stripe_offset);
1818                 printf("default_stripe_pattern: %u\n", desc.ld_pattern);
1819                 printf("obd_count: %u\n", desc.ld_tgt_count);
1820                 printf("OBDS:\tobdidx\t\tobdgen\t\t obduuid\n");
1821                 uuidp = uuidarray;
1822                 genp = obdgens;
1823                 for (i = 0; i < desc.ld_tgt_count; i++, uuidp++, genp++)
1824                         printf("\t%6u\t%14u\t\t %s\n", i, *genp, (char *)uuidp);
1825         }
1826 out_obdgens:
1827         free(obdgens);
1828 out_uuidarray:
1829         free(uuidarray);
1830 out:
1831         close(fd);
1832         return rc;
1833 }
1834
1835 int jt_obd_ldlm_regress_start(int argc, char **argv)
1836 {
1837         int rc;
1838         struct obd_ioctl_data data;
1839         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1840         char argstring[200];
1841         int i, count = sizeof(argstring) - 1;
1842
1843         memset(&data, 0x00, sizeof(data));
1844         data.ioc_dev = cur_device;
1845         if (argc > 5)
1846                 return CMD_HELP;
1847
1848         argstring[0] = '\0';
1849         for (i = 1; i < argc; i++) {
1850                 strncat(argstring, " ", count);
1851                 count--;
1852                 strncat(argstring, argv[i], count);
1853                 count -= strlen(argv[i]);
1854         }
1855
1856         if (strlen(argstring)) {
1857                 data.ioc_inlbuf1 = argstring;
1858                 data.ioc_inllen1 = strlen(argstring) + 1;
1859         }
1860
1861         memset(buf, 0, sizeof(rawbuf));
1862         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1863         if (rc) {
1864                 fprintf(stderr, "error: %s: invalid ioctl\n",
1865                         jt_cmdname(argv[0]));
1866                 return rc;
1867         }
1868         rc = l2_ioctl(OBD_DEV_ID, IOC_LDLM_REGRESS_START, buf);
1869         if (rc)
1870                 fprintf(stderr, "error: %s: test failed: %s\n",
1871                         jt_cmdname(argv[0]), strerror(rc = errno));
1872
1873         return rc;
1874 }
1875
1876 int jt_obd_ldlm_regress_stop(int argc, char **argv)
1877 {
1878         int rc;
1879         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1880         struct obd_ioctl_data data;
1881
1882         memset(&data, 0x00, sizeof(data));
1883         data.ioc_dev = cur_device;
1884
1885         if (argc != 1)
1886                 return CMD_HELP;
1887
1888         memset(buf, 0, sizeof(rawbuf));
1889         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1890         if (rc) {
1891                 fprintf(stderr, "error: %s: invalid ioctl\n",
1892                         jt_cmdname(argv[0]));
1893                 return rc;
1894         }
1895         rc = l2_ioctl(OBD_DEV_ID, IOC_LDLM_REGRESS_STOP, buf);
1896
1897         if (rc)
1898                 fprintf(stderr, "error: %s: test failed: %s\n",
1899                         jt_cmdname(argv[0]), strerror(rc = errno));
1900         return rc;
1901 }
1902
1903 static int do_activate(int argc, char **argv, int flag)
1904 {
1905         struct obd_ioctl_data data;
1906         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1907         int rc;
1908
1909         memset(&data, 0x00, sizeof(data));
1910         data.ioc_dev = cur_device;
1911         if (argc != 1)
1912                 return CMD_HELP;
1913
1914         /* reuse offset for 'active' */
1915         data.ioc_offset = flag;
1916
1917         memset(buf, 0, sizeof(rawbuf));
1918         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1919         if (rc) {
1920                 fprintf(stderr, "error: %s: invalid ioctl\n",
1921                         jt_cmdname(argv[0]));
1922                 return rc;
1923         }
1924         rc = l2_ioctl(OBD_DEV_ID, IOC_OSC_SET_ACTIVE, buf);
1925         if (rc)
1926                 fprintf(stderr, "error: %s: failed: %s\n",
1927                         jt_cmdname(argv[0]), strerror(rc = errno));
1928
1929         return rc;
1930 }
1931
1932 int jt_obd_deactivate(int argc, char **argv)
1933 {
1934         return do_activate(argc, argv, 0);
1935 }
1936
1937 int jt_obd_activate(int argc, char **argv)
1938 {
1939         return do_activate(argc, argv, 1);
1940 }
1941
1942 int jt_obd_recover(int argc, char **argv)
1943 {
1944         int rc;
1945         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1946         struct obd_ioctl_data data;
1947
1948         memset(&data, 0x00, sizeof(data));
1949         data.ioc_dev = cur_device;
1950         if (argc > 2)
1951                 return CMD_HELP;
1952
1953         if (argc == 2) {
1954                 data.ioc_inllen1 = strlen(argv[1]) + 1;
1955                 data.ioc_inlbuf1 = argv[1];
1956         }
1957
1958         memset(buf, 0, sizeof(rawbuf));
1959         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1960         if (rc) {
1961                 fprintf(stderr, "error: %s: invalid ioctl\n",
1962                         jt_cmdname(argv[0]));
1963                 return rc;
1964         }
1965         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_CLIENT_RECOVER, buf);
1966         if (rc < 0) {
1967                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1968                         strerror(rc = errno));
1969         }
1970
1971         return rc;
1972 }
1973
1974 int jt_obd_mdc_lookup(int argc, char **argv)
1975 {
1976         struct obd_ioctl_data data;
1977         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1978         char *parent, *child;
1979         int rc, fd, verbose = 1;
1980
1981         if (argc < 3 || argc > 4)
1982                 return CMD_HELP;
1983
1984         parent = argv[1];
1985         child = argv[2];
1986         if (argc == 4)
1987                 verbose = get_verbose(argv[0], argv[3]);
1988
1989         memset(&data, 0x00, sizeof(data));
1990         data.ioc_dev = cur_device;
1991
1992         data.ioc_inllen1 = strlen(child) + 1;
1993         data.ioc_inlbuf1 = child;
1994
1995         memset(buf, 0, sizeof(rawbuf));
1996         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1997         if (rc) {
1998                 fprintf(stderr, "error: %s: invalid ioctl\n",
1999                         jt_cmdname(argv[0]));
2000                 return rc;
2001         }
2002
2003         fd = open(parent, O_RDONLY);
2004         if (fd < 0) {
2005                 fprintf(stderr, "open \"%s\" failed: %s\n", parent,
2006                         strerror(errno));
2007                 return -1;
2008         }
2009
2010         rc = ioctl(fd, IOC_MDC_LOOKUP, buf);
2011         if (rc < 0) {
2012                 fprintf(stderr, "error: %s: ioctl error: %s\n",
2013                         jt_cmdname(argv[0]), strerror(rc = errno));
2014         }
2015         close(fd);
2016
2017         if (verbose) {
2018                 rc = obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
2019                 if (rc) {
2020                         fprintf(stderr, "error: %s: invalid reply\n",
2021                                 jt_cmdname(argv[0]));
2022                         return rc;
2023                 }
2024                 printf("%s: mode %o uid %d gid %d\n", child,
2025                        data.ioc_obdo1.o_mode, data.ioc_obdo1.o_uid,
2026                        data.ioc_obdo1.o_gid);
2027         }
2028
2029         return rc;
2030 }
2031
2032 int jt_cfg_dump_log(int argc, char **argv)
2033 {
2034         struct obd_ioctl_data data;
2035         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2036         int rc;
2037
2038         if (argc != 2)
2039                 return CMD_HELP;
2040
2041         memset(&data, 0x00, sizeof(data));
2042         data.ioc_dev = cur_device;
2043         data.ioc_inllen1 = strlen(argv[1]) + 1;
2044         data.ioc_inlbuf1 = argv[1];
2045
2046         memset(buf, 0, sizeof(rawbuf));
2047         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2048         if (rc) {
2049                 fprintf(stderr, "error: %s: invalid ioctl\n",
2050                         jt_cmdname(argv[0]));
2051                 return rc;
2052         }
2053         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_DUMP_LOG, buf);
2054         if (rc < 0)
2055                 fprintf(stderr, "OBD_IOC_DUMP_LOG failed: %s\n",
2056                         strerror(errno));
2057
2058         return rc;
2059 }
2060
2061 int jt_llog_catlist(int argc, char **argv)
2062 {
2063         struct obd_ioctl_data data;
2064         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2065         int rc;
2066
2067         if (argc != 1)
2068                 return CMD_HELP;
2069
2070         memset(&data, 0x00, sizeof(data));
2071         data.ioc_dev = cur_device;
2072         data.ioc_inllen1 = sizeof(rawbuf) - cfs_size_round(sizeof(data));
2073         memset(buf, 0, sizeof(rawbuf));
2074         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2075         if (rc) {
2076                 fprintf(stderr, "error: %s: invalid ioctl\n",
2077                         jt_cmdname(argv[0]));
2078                 return rc;
2079         }
2080         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CATLOGLIST, buf);
2081         if (rc == 0)
2082                 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2083         else
2084                 fprintf(stderr, "OBD_IOC_CATLOGLIST failed: %s\n",
2085                         strerror(errno));
2086
2087         return rc;
2088 }
2089
2090 int jt_llog_info(int argc, char **argv)
2091 {
2092         struct obd_ioctl_data data;
2093         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2094         int rc;
2095
2096         if (argc != 2)
2097                 return CMD_HELP;
2098
2099         memset(&data, 0x00, sizeof(data));
2100         data.ioc_dev = cur_device;
2101         data.ioc_inllen1 = strlen(argv[1]) + 1;
2102         data.ioc_inlbuf1 = argv[1];
2103         data.ioc_inllen2 = sizeof(rawbuf) - cfs_size_round(sizeof(data)) -
2104                 cfs_size_round(data.ioc_inllen1);
2105         memset(buf, 0, sizeof(rawbuf));
2106         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2107         if (rc) {
2108                 fprintf(stderr, "error: %s: invalid ioctl\n",
2109                         jt_cmdname(argv[0]));
2110                 return rc;
2111         }
2112
2113         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_INFO, buf);
2114         if (rc == 0)
2115                 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2116         else
2117                 fprintf(stderr, "OBD_IOC_LLOG_INFO failed: %s\n",
2118                         strerror(errno));
2119
2120         return rc;
2121 }
2122
2123 int jt_llog_print(int argc, char **argv)
2124 {
2125         struct obd_ioctl_data data;
2126         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2127         int rc;
2128
2129         if (argc != 2 && argc != 4)
2130                 return CMD_HELP;
2131
2132         memset(&data, 0x00, sizeof(data));
2133         data.ioc_dev = cur_device;
2134         data.ioc_inllen1 = strlen(argv[1]) + 1;
2135         data.ioc_inlbuf1 = argv[1];
2136         if (argc == 4) {
2137                 data.ioc_inllen2 = strlen(argv[2]) + 1;
2138                 data.ioc_inlbuf2 = argv[2];
2139                 data.ioc_inllen3 = strlen(argv[3]) + 1;
2140                 data.ioc_inlbuf3 = argv[3];
2141         } else {
2142                 char from[2] = "1", to[3] = "-1";
2143                 data.ioc_inllen2 = strlen(from) + 1;
2144                 data.ioc_inlbuf2 = from;
2145                 data.ioc_inllen3 = strlen(to) + 1;
2146                 data.ioc_inlbuf3 = to;
2147         }
2148         data.ioc_inllen4 = sizeof(rawbuf) - cfs_size_round(sizeof(data)) -
2149                 cfs_size_round(data.ioc_inllen1) -
2150                 cfs_size_round(data.ioc_inllen2) -
2151                 cfs_size_round(data.ioc_inllen3);
2152         memset(buf, 0, sizeof(rawbuf));
2153         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2154         if (rc) {
2155                 fprintf(stderr, "error: %s: invalid ioctl\n",
2156                         jt_cmdname(argv[0]));
2157                 return rc;
2158         }
2159
2160         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_PRINT, buf);
2161         if (rc == 0)
2162                 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2163         else
2164                 fprintf(stderr, "OBD_IOC_LLOG_PRINT failed: %s\n",
2165                         strerror(errno));
2166
2167         return rc;
2168 }
2169
2170 int jt_llog_cancel(int argc, char **argv)
2171 {
2172         struct obd_ioctl_data data;
2173         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2174         int rc;
2175
2176         if (argc != 4)
2177                 return CMD_HELP;
2178
2179         memset(&data, 0x00, sizeof(data));
2180         data.ioc_dev = cur_device;
2181         data.ioc_inllen1 = strlen(argv[1]) + 1;
2182         data.ioc_inlbuf1 = argv[1];
2183         data.ioc_inllen2 = strlen(argv[2]) + 1;
2184         data.ioc_inlbuf2 = argv[2];
2185         data.ioc_inllen3 = strlen(argv[3]) + 1;
2186         data.ioc_inlbuf3 = argv[3];
2187         memset(buf, 0, sizeof(rawbuf));
2188         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2189         if (rc) {
2190                 fprintf(stderr, "error: %s: invalid ioctl\n",
2191                         jt_cmdname(argv[0]));
2192                 return rc;
2193         }
2194
2195         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CANCEL, buf);
2196         if (rc == 0)
2197                 fprintf(stdout, "index %s be canceled.\n", argv[3]);
2198         else
2199                 fprintf(stderr, "OBD_IOC_LLOG_CANCEL failed: %s\n",
2200                         strerror(errno));
2201
2202         return rc;
2203
2204 }
2205 int jt_llog_check(int argc, char **argv)
2206 {
2207         struct obd_ioctl_data data;
2208         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2209         int rc;
2210
2211         if (argc != 2 && argc != 4)
2212                 return CMD_HELP;
2213
2214         memset(&data, 0x00, sizeof(data));
2215         data.ioc_dev = cur_device;
2216         data.ioc_inllen1 = strlen(argv[1]) + 1;
2217         data.ioc_inlbuf1 = argv[1];
2218         if (argc == 4) {
2219                 data.ioc_inllen2 = strlen(argv[2]) + 1;
2220                 data.ioc_inlbuf2 = argv[2];
2221                 data.ioc_inllen3 = strlen(argv[3]) + 1;
2222                 data.ioc_inlbuf3 = argv[3];
2223         } else {
2224                 char from[2] = "1", to[3] = "-1";
2225                 data.ioc_inllen2 = strlen(from) + 1;
2226                 data.ioc_inlbuf2 = from;
2227                 data.ioc_inllen3 = strlen(to) + 1;
2228                 data.ioc_inlbuf3 = to;
2229         }
2230         data.ioc_inllen4 = sizeof(rawbuf) - cfs_size_round(sizeof(data)) -
2231                 cfs_size_round(data.ioc_inllen1) -
2232                 cfs_size_round(data.ioc_inllen2) -
2233                 cfs_size_round(data.ioc_inllen3);
2234         memset(buf, 0, sizeof(rawbuf));
2235         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2236         if (rc) {
2237                 fprintf(stderr, "error: %s: invalid ioctl\n",
2238                         jt_cmdname(argv[0]));
2239                 return rc;
2240         }
2241
2242         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CHECK, buf);
2243         if (rc == 0)
2244                 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2245         else
2246                 fprintf(stderr, "OBD_IOC_LLOG_CHECK failed: %s\n",
2247                         strerror(errno));
2248         return rc;
2249 }
2250
2251 int jt_llog_remove(int argc, char **argv)
2252 {
2253         struct obd_ioctl_data data;
2254         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2255         int rc;
2256
2257         if (argc != 3 && argc != 2)
2258                 return CMD_HELP;
2259
2260         memset(&data, 0x00, sizeof(data));
2261         data.ioc_dev = cur_device;
2262         data.ioc_inllen1 = strlen(argv[1]) + 1;
2263         data.ioc_inlbuf1 = argv[1];
2264         if (argc == 3){
2265                 data.ioc_inllen2 = strlen(argv[2]) + 1;
2266                 data.ioc_inlbuf2 = argv[2];
2267         }
2268         memset(buf, 0, sizeof(rawbuf));
2269         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2270         if (rc) {
2271                 fprintf(stderr, "error: %s: invalid ioctl\n",
2272                         jt_cmdname(argv[0]));
2273                 return rc;
2274         }
2275
2276         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_REMOVE, buf);
2277         if (rc == 0) {
2278                 if (argc == 3)
2279                         fprintf(stdout, "log %s are removed.\n", argv[2]);
2280                 else
2281                         fprintf(stdout, "the log in catalog %s are removed. \n", argv[1]);
2282         } else
2283                 fprintf(stderr, "OBD_IOC_LLOG_REMOVE failed: %s\n",
2284                         strerror(errno));
2285
2286         return rc;
2287 }
2288
2289 /* attach a regular file to virtual block device.
2290  * return vaule:
2291  *  -1: fatal error
2292  *  1: error, it always means the command run failed
2293  *  0: success
2294  */
2295 static int jt_blockdev_run_process(const char *file, char *argv[])
2296 {
2297         pid_t pid;
2298         int rc;
2299
2300         pid = vfork();
2301         if (pid == 0) { /* child process */
2302                 /* don't print error messages */
2303                 close(1), close(2);
2304                 (void)execvp(file, argv);
2305                 exit(-1);
2306         } else if (pid > 0) {
2307                 int status;
2308
2309                 rc = waitpid(pid, &status, 0);
2310                 if (rc < 0 || !WIFEXITED(status))
2311                         return -1;
2312
2313                 return WEXITSTATUS(status);
2314         }
2315
2316         return -1;
2317 }
2318
2319 static int jt_blockdev_find_module(const char *module)
2320 {
2321         FILE *fp;
2322         int found = 0;
2323         char buf[1024];
2324
2325         fp = fopen("/proc/modules", "r");
2326         if (fp == NULL)
2327                 return -1;
2328
2329         while (fgets(buf, 1024, fp) != NULL) {
2330                 *strchr(buf, ' ') = 0;
2331                 if (strcmp(module, buf) == 0) {
2332                         found = 1;
2333                         break;
2334                 }
2335         }
2336         fclose(fp);
2337
2338         return found;
2339 }
2340
2341 static int jt_blockdev_probe_module(const char *module)
2342 {
2343         char buf[1024];
2344         char *argv[10];
2345         int c, rc;
2346
2347         if (jt_blockdev_find_module(module) == 1)
2348                 return 0;
2349
2350         /* run modprobe first */
2351         c = 0;
2352         argv[c++] = "/sbin/modprobe";
2353         argv[c++] = "-q";
2354         argv[c++] = (char *)module;
2355         argv[c++] = NULL;
2356         rc = jt_blockdev_run_process("modprobe", argv);
2357         if (rc != 1)
2358                 return rc;
2359
2360         /* cannot find the module in default directory ... */
2361         sprintf(buf, "../llite/%s.ko", module);
2362         c = 0;
2363         argv[c++] = "/sbin/insmod";
2364         argv[c++] = buf;
2365         argv[c++] = NULL;
2366         rc = jt_blockdev_run_process("insmod", argv);
2367         return rc ? -1 : 0;
2368 }
2369
2370 int jt_blockdev_attach(int argc, char **argv)
2371 {
2372         int rc, fd;
2373         struct stat st;
2374         char *filename, *devname;
2375         unsigned long dev;
2376
2377         if (argc != 3)
2378                 return CMD_HELP;
2379
2380         if (jt_blockdev_probe_module("llite_lloop") < 0) {
2381                 fprintf(stderr, "error: cannot find module llite_lloop.(k)o\n");
2382                 return ENOENT;
2383         }
2384
2385         filename = argv[1];
2386         devname = argv[2];
2387
2388         fd = open(filename, O_RDWR);
2389         if (fd < 0) {
2390                 fprintf(stderr, "file %s can't be opened(%s)\n\n",
2391                         filename, strerror(errno));
2392                 return CMD_HELP;
2393         }
2394
2395         rc = ioctl(fd, LL_IOC_LLOOP_ATTACH, &dev);
2396         if (rc < 0) {
2397                 rc = errno;
2398                 fprintf(stderr, "attach error(%s)\n", strerror(rc));
2399                 goto out;
2400         }
2401
2402         rc = stat(devname, &st);
2403         if (rc == 0 && (!S_ISBLK(st.st_mode) || st.st_rdev != dev)) {
2404                 rc = EEXIST;
2405         } else if (rc < 0) {
2406                 if (errno == ENOENT &&
2407                     !mknod(devname, S_IFBLK|S_IRUSR|S_IWUSR, dev))
2408                         rc = 0;
2409                 else
2410                         rc = errno;
2411         }
2412
2413         if (rc) {
2414                 fprintf(stderr, "error: the file %s could be attached to block "
2415                                 "device %X but creating %s failed: %s\n"
2416                                 "now detaching the block device..",
2417                         filename, (int)dev, devname, strerror(rc));
2418
2419                 (void)ioctl(fd, LL_IOC_LLOOP_DETACH_BYDEV, dev);
2420                 fprintf(stderr, "%s\n", strerror(errno));
2421         }
2422 out:
2423         close(fd);
2424         return -rc;
2425 }
2426
2427 int jt_blockdev_detach(int argc, char **argv)
2428 {
2429         char *filename;
2430         int rc, fd;
2431
2432         if (argc != 2)
2433                 return CMD_HELP;
2434
2435         filename = argv[1];
2436         fd = open(filename, O_RDONLY);
2437         if (fd < 0) {
2438                 fprintf(stderr, "cannot open file %s error %s\n",
2439                         filename, strerror(errno));
2440                 return CMD_HELP;
2441         }
2442
2443         rc = ioctl(fd, LL_IOC_LLOOP_DETACH, 0);
2444         if (rc < 0) {
2445                 rc = errno;
2446                 fprintf(stderr, "detach error(%s)\n", strerror(rc));
2447         } else {
2448                 (void)unlink(filename);
2449         }
2450
2451         close(fd);
2452         return -rc;
2453 }
2454
2455 int jt_blockdev_info(int argc, char **argv)
2456 {
2457         char *filename;
2458         int rc, fd;
2459         struct lu_fid fid;
2460
2461         if (argc != 2)
2462                 return CMD_HELP;
2463
2464         filename = argv[1];
2465         fd = open(filename, O_RDONLY);
2466         if (fd < 0) {
2467                 fprintf(stderr, "cannot open file %s error: %s\n",
2468                         filename, strerror(errno));
2469                 return CMD_HELP;
2470         }
2471
2472         rc = ioctl(fd, LL_IOC_LLOOP_INFO, &fid);
2473         if (rc < 0) {
2474                 rc = errno;
2475                 fprintf(stderr, "error: %s\n", strerror(errno));
2476                 goto out;
2477         }
2478         fprintf(stdout, "lloop device info: ");
2479         if (fid_is_zero(&fid))
2480                 fprintf(stdout, "Not attached\n");
2481         else
2482                 fprintf(stdout, "attached to inode "DFID"\n", PFID(&fid));
2483 out:
2484         close(fd);
2485         return -rc;
2486 }
2487
2488 static void signal_server(int sig)
2489 {
2490         if (sig == SIGINT) {
2491                 do_disconnect("sigint", 1);
2492                 exit(1);
2493         } else
2494                 fprintf(stderr, "%s: got signal %d\n", jt_cmdname("sigint"), sig);
2495 }
2496
2497 int obd_initialize(int argc, char **argv)
2498 {
2499         int i;
2500
2501         for (i = 0; i < MAX_STRIPES; i++)
2502                 lsm_buffer.lsm.lsm_oinfo[i] = lov_oinfos + i;
2503
2504         shmem_setup();
2505         register_ioc_dev(OBD_DEV_ID, OBD_DEV_PATH,
2506                          OBD_DEV_MAJOR, OBD_DEV_MINOR);
2507
2508         return 0;
2509 }
2510
2511 void obd_finalize(int argc, char **argv)
2512 {
2513         struct sigaction sigact;
2514
2515         sigact.sa_handler = signal_server;
2516         sigfillset(&sigact.sa_mask);
2517         sigact.sa_flags = SA_RESTART;
2518         sigaction(SIGINT, &sigact, NULL);
2519
2520         shmem_stop();
2521         do_disconnect(argv[0], 1);
2522 }
2523
2524 static int check_pool_cmd(enum lcfg_command_type cmd,
2525                           char *fsname, char *poolname,
2526                           char *ostname)
2527 {
2528         int rc;
2529
2530         rc = llapi_search_ost(fsname, poolname, ostname);
2531         if (rc < 0 && (cmd != LCFG_POOL_NEW)) {
2532                 fprintf(stderr, "Pool %s.%s not found\n",
2533                         fsname, poolname);
2534                 return rc;
2535         }
2536
2537         switch (cmd) {
2538         case LCFG_POOL_NEW: {
2539                 LASSERT(ostname == NULL);
2540                 if (rc >= 0) {
2541                         fprintf(stderr, "Pool %s.%s already exists\n",
2542                                 fsname, poolname);
2543                         return -EEXIST;
2544                 }
2545                 return 0;
2546         }
2547         case LCFG_POOL_DEL: {
2548                 LASSERT(ostname == NULL);
2549                 if (rc == 1) {
2550                         fprintf(stderr, "Pool %s.%s not empty, "
2551                                 "please remove all members\n",
2552                                 fsname, poolname);
2553                         return -ENOTEMPTY;
2554                 }
2555                 return 0;
2556         }
2557         case LCFG_POOL_ADD: {
2558                 if (rc == 1) {
2559                         fprintf(stderr, "OST %s is already in pool %s.%s\n",
2560                                 ostname, fsname, poolname);
2561                         return -EEXIST;
2562                 }
2563                 rc = llapi_search_ost(fsname, NULL, ostname);
2564                 if (rc == 0) {
2565                         fprintf(stderr, "OST %s is not part of the '%s' fs.\n",
2566                                 ostname, fsname);
2567                         return -ENOENT;
2568                 }
2569                 return 0;
2570         }
2571         case LCFG_POOL_REM: {
2572                 if (rc == 0) {
2573                         fprintf(stderr, "OST %s not found in pool %s.%s\n",
2574                                 ostname, fsname, poolname);
2575                         return -ENOENT;
2576                 }
2577                 return 0;
2578         }
2579         default:
2580                 break;
2581         } /* switch */
2582         return -EINVAL;
2583 }
2584
2585 /* This check only verifies that the changes have been "pushed out" to
2586    the client successfully.  This involves waiting for a config update,
2587    and so may fail because of problems in that code or post-command
2588    network loss. So reporting a warning is appropriate, but not a failure.
2589 */
2590 static int check_pool_cmd_result(enum lcfg_command_type cmd,
2591                                  char *fsname, char *poolname,
2592                                  char *ostname)
2593 {
2594         int cpt = 10;
2595         int rc = 0;
2596
2597         switch (cmd) {
2598         case LCFG_POOL_NEW: {
2599                 do {
2600                         rc = llapi_search_ost(fsname, poolname, NULL);
2601                         if (rc == -ENODEV)
2602                                 return rc;
2603                         if (rc < 0)
2604                                 sleep(2);
2605                         cpt--;
2606                 } while ((rc < 0) && (cpt > 0));
2607                 if (rc >= 0) {
2608                         fprintf(stderr, "Pool %s.%s created\n",
2609                                 fsname, poolname);
2610                         return 0;
2611                 } else {
2612                         fprintf(stderr, "Warning, pool %s.%s not found\n",
2613                                 fsname, poolname);
2614                         return -ENOENT;
2615                 }
2616         }
2617         case LCFG_POOL_DEL: {
2618                 do {
2619                         rc = llapi_search_ost(fsname, poolname, NULL);
2620                         if (rc == -ENODEV)
2621                                 return rc;
2622                         if (rc >= 0)
2623                                 sleep(2);
2624                         cpt--;
2625                 } while ((rc >= 0) && (cpt > 0));
2626                 if (rc < 0) {
2627                         fprintf(stderr, "Pool %s.%s destroyed\n",
2628                                 fsname, poolname);
2629                         return 0;
2630                 } else {
2631                         fprintf(stderr, "Warning, pool %s.%s still found\n",
2632                                 fsname, poolname);
2633                         return -EEXIST;
2634                 }
2635         }
2636         case LCFG_POOL_ADD: {
2637                 do {
2638                         rc = llapi_search_ost(fsname, poolname, ostname);
2639                         if (rc == -ENODEV)
2640                                 return rc;
2641                         if (rc != 1)
2642                                 sleep(2);
2643                         cpt--;
2644                 } while ((rc != 1) && (cpt > 0));
2645                 if (rc == 1) {
2646                         fprintf(stderr, "OST %s added to pool %s.%s\n",
2647                                 ostname, fsname, poolname);
2648                         return 0;
2649                 } else {
2650                         fprintf(stderr, "Warning, OST %s not found in pool %s.%s\n",
2651                                 ostname, fsname, poolname);
2652                         return -ENOENT;
2653                 }
2654         }
2655         case LCFG_POOL_REM: {
2656                 do {
2657                         rc = llapi_search_ost(fsname, poolname, ostname);
2658                         if (rc == -ENODEV)
2659                                 return rc;
2660                         if (rc == 1)
2661                                 sleep(2);
2662                         cpt--;
2663                 } while ((rc == 1) && (cpt > 0));
2664                 if (rc != 1) {
2665                         fprintf(stderr, "OST %s removed from pool %s.%s\n",
2666                                 ostname, fsname, poolname);
2667                         return 0;
2668                 } else {
2669                         fprintf(stderr, "Warning, OST %s still found in pool %s.%s\n",
2670                                 ostname, fsname, poolname);
2671                         return -EEXIST;
2672                 }
2673         }
2674         default:
2675                 break;
2676         }
2677         return -EINVAL;
2678 }
2679
2680 static int check_and_complete_ostname(char *fsname, char *ostname)
2681 {
2682         char *ptr;
2683         char real_ostname[MAX_OBD_NAME + 1];
2684         char i;
2685
2686         /* if OST name does not start with fsname, we add it */
2687         /* if not check if the fsname is the right one */
2688         ptr = strchr(ostname, '-');
2689         if (ptr == NULL) {
2690                 sprintf(real_ostname, "%s-%s", fsname, ostname);
2691         } else if (strncmp(ostname, fsname, strlen(fsname)) != 0) {
2692                 fprintf(stderr, "%s does not start with fsname %s\n",
2693                         ostname, fsname);
2694                 return -EINVAL;
2695         } else {
2696              strcpy(real_ostname, ostname);
2697         }
2698         /* real_ostname is fsname-????? */
2699         ptr = real_ostname + strlen(fsname) + 1;
2700         if (strncmp(ptr, "OST", 3) != 0) {
2701                 fprintf(stderr, "%s does not start by %s-OST nor OST\n",
2702                         ostname, fsname);
2703                 return -EINVAL;
2704         }
2705         /* real_ostname is fsname-OST????? */
2706         ptr += 3;
2707         for (i = 0; i < 4; i++) {
2708                 if (!isxdigit(*ptr)) {
2709                         fprintf(stderr,
2710                                 "ost's index in %s is not an hexa number\n",
2711                                 ostname);
2712                         return -EINVAL;
2713                 }
2714                 ptr++;
2715         }
2716         /* real_ostname is fsname-OSTXXXX????? */
2717         /* if OST name does not end with _UUID, we add it */
2718         if (*ptr == '\0') {
2719                 strcat(real_ostname, "_UUID");
2720         } else if (strcmp(ptr, "_UUID") != 0) {
2721                 fprintf(stderr,
2722                         "ostname %s does not end with _UUID\n", ostname);
2723                 return -EINVAL;
2724         }
2725         /* real_ostname is fsname-OSTXXXX_UUID */
2726         strcpy(ostname, real_ostname);
2727         return 0;
2728 }
2729
2730 /* returns 0 or -errno */
2731 static int pool_cmd(enum lcfg_command_type cmd,
2732                     char *cmdname, char *fullpoolname,
2733                     char *fsname, char *poolname, char *ostname)
2734 {
2735         int rc = 0;
2736         struct obd_ioctl_data data;
2737         struct lustre_cfg_bufs bufs;
2738         struct lustre_cfg *lcfg;
2739         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2740
2741         rc = check_pool_cmd(cmd, fsname, poolname, ostname);
2742         if (rc == -ENODEV)
2743                 fprintf(stderr, "Can't verify pool command since there "
2744                         "is no local MDT or client, proceeding anyhow...\n");
2745         else if (rc)
2746                 return rc;
2747
2748         lustre_cfg_bufs_reset(&bufs, NULL);
2749         lustre_cfg_bufs_set_string(&bufs, 0, cmdname);
2750         lustre_cfg_bufs_set_string(&bufs, 1, fullpoolname);
2751         if (ostname != NULL)
2752                 lustre_cfg_bufs_set_string(&bufs, 2, ostname);
2753
2754         lcfg = lustre_cfg_new(cmd, &bufs);
2755         if (IS_ERR(lcfg)) {
2756                 rc = PTR_ERR(lcfg);
2757                 return rc;
2758         }
2759
2760         memset(&data, 0x00, sizeof(data));
2761         rc = data.ioc_dev = get_mgs_device();
2762         if (rc < 0)
2763                 goto out;
2764
2765         data.ioc_type = LUSTRE_CFG_TYPE;
2766         data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
2767                                         lcfg->lcfg_buflens);
2768         data.ioc_pbuf1 = (void *)lcfg;
2769
2770         memset(buf, 0, sizeof(rawbuf));
2771         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2772         if (rc) {
2773                 fprintf(stderr, "error: %s: invalid ioctl\n",
2774                         jt_cmdname(cmdname));
2775                 return rc;
2776         }
2777         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_POOL, buf);
2778 out:
2779         if (rc)
2780                 rc = -errno;
2781         lustre_cfg_free(lcfg);
2782         return rc;
2783 }
2784
2785 /*
2786  * this function tranforms a rule [start-end/step] into an array
2787  * of matching numbers
2788  * supported forms are:
2789  * [start]                : just this number
2790  * [start-end]            : all numbers from start to end
2791  * [start-end/step]       : numbers from start to end with increment of step
2792  * on return, format contains a printf format string which can be used
2793  * to generate all the strings
2794  */
2795 static int get_array_idx(char *rule, char *format, int **array)
2796 {
2797         char *start, *end, *ptr;
2798         unsigned int lo, hi, step;
2799         int array_sz = 0;
2800         int i, array_idx;
2801         int rc;
2802
2803         start = strchr(rule, '[');
2804         end = strchr(rule, ']');
2805         if ((start == NULL) || (end == NULL)) {
2806                 *array = malloc(sizeof(int));
2807                 if (*array == NULL)
2808                         return 0;
2809                 strcpy(format, rule);
2810                 array_sz = 1;
2811                 return array_sz;
2812         }
2813         *start = '\0';
2814         *end = '\0';
2815         end++;
2816         start++;
2817         /* put in format the printf format (the rule without the range) */
2818         sprintf(format, "%s%%.4x%s", rule, end);
2819
2820         array_idx = 0;
2821         array_sz = 0;
2822         *array = NULL;
2823         /* loop on , separator */
2824         do {
2825                 /* extract the 3 fields */
2826                 rc = sscanf(start, "%x-%x/%u", &lo, &hi, &step);
2827                 switch (rc) {
2828                 case 0: {
2829                         return 0;
2830                 }
2831                 case 1: {
2832                         array_sz++;
2833                         *array = realloc(*array, array_sz * sizeof(int));
2834                         if (*array == NULL)
2835                                 return 0;
2836                         (*array)[array_idx] = lo;
2837                         array_idx++;
2838                         break;
2839                 }
2840                 case 2: {
2841                         step = 1;
2842                         /* do not break to share code with case 3: */
2843                 }
2844                 case 3: {
2845                         if ((hi < lo) || (step == 0))
2846                                 return 0;
2847                         array_sz += (hi - lo) / step + 1;
2848                         *array = realloc(*array, sizeof(int) * array_sz);
2849                         if (*array == NULL)
2850                                 return 0;
2851                         for (i = lo; i <= hi; i+=step, array_idx++)
2852                                 (*array)[array_idx] = i;
2853                         break;
2854                 }
2855                 }
2856                 ptr = strchr(start, ',');
2857                 if (ptr != NULL)
2858                         start = ptr + 1;
2859
2860         } while (ptr != NULL);
2861         return array_sz;
2862 }
2863
2864 static int extract_fsname_poolname(char *arg, char *fsname, char *poolname)
2865 {
2866         char *ptr;
2867         int len;
2868         int rc;
2869
2870         strcpy(fsname, arg);
2871         ptr = strchr(fsname, '.');
2872         if (ptr == NULL) {
2873                 fprintf(stderr, ". is missing in %s\n", fsname);
2874                 rc = -EINVAL;
2875                 goto err;
2876         }
2877
2878         len = ptr - fsname;
2879         if (len == 0) {
2880                 fprintf(stderr, "fsname is empty\n");
2881                 rc = -EINVAL;
2882                 goto err;
2883         }
2884
2885         len = strlen(ptr + 1);
2886         if (len == 0) {
2887                 fprintf(stderr, "poolname is empty\n");
2888                 rc = -EINVAL;
2889                 goto err;
2890         }
2891         if (len > LOV_MAXPOOLNAME) {
2892                 fprintf(stderr,
2893                         "poolname %s is too long (length is %d max is %d)\n",
2894                         ptr + 1, len, LOV_MAXPOOLNAME);
2895                 rc = -ENAMETOOLONG;
2896                 goto err;
2897         }
2898         strncpy(poolname, ptr + 1, LOV_MAXPOOLNAME);
2899         poolname[LOV_MAXPOOLNAME] = '\0';
2900         *ptr = '\0';
2901         return 0;
2902
2903 err:
2904         fprintf(stderr, "argument %s must be <fsname>.<poolname>\n", arg);
2905         return rc;
2906 }
2907
2908 int jt_pool_cmd(int argc, char **argv)
2909 {
2910         enum lcfg_command_type cmd;
2911         char fsname[PATH_MAX + 1];
2912         char poolname[LOV_MAXPOOLNAME + 1];
2913         char *ostnames_buf = NULL;
2914         int i, rc;
2915         int *array = NULL, array_sz;
2916         struct {
2917                 int     rc;
2918                 char   *ostname;
2919         } *cmds = NULL;
2920
2921         switch (argc) {
2922         case 0:
2923         case 1: return CMD_HELP;
2924         case 2: {
2925                 if (strcmp("pool_new", argv[0]) == 0)
2926                         cmd = LCFG_POOL_NEW;
2927                 else if (strcmp("pool_destroy", argv[0]) == 0)
2928                         cmd = LCFG_POOL_DEL;
2929                 else if (strcmp("pool_list", argv[0]) == 0)
2930                          return llapi_poollist(argv[1]);
2931                 else return CMD_HELP;
2932
2933                 rc = extract_fsname_poolname(argv[1], fsname, poolname);
2934                 if (rc)
2935                         break;
2936
2937                 rc = pool_cmd(cmd, argv[0], argv[1], fsname, poolname, NULL);
2938                 if (rc)
2939                         break;
2940
2941                 check_pool_cmd_result(cmd, fsname, poolname, NULL);
2942                 break;
2943         }
2944         default: {
2945                 char format[2*MAX_OBD_NAME];
2946
2947                 if (strcmp("pool_remove", argv[0]) == 0) {
2948                         cmd = LCFG_POOL_REM;
2949                 } else if (strcmp("pool_add", argv[0]) == 0) {
2950                         cmd = LCFG_POOL_ADD;
2951                 } else {
2952                         return CMD_HELP;
2953                 }
2954
2955                 rc = extract_fsname_poolname(argv[1], fsname, poolname);
2956                 if (rc)
2957                         break;
2958
2959                 for (i = 2; i < argc; i++) {
2960                         int j;
2961
2962                         array_sz = get_array_idx(argv[i], format, &array);
2963                         if (array_sz == 0)
2964                                 return CMD_HELP;
2965
2966                         cmds = malloc(array_sz * sizeof(cmds[0]));
2967                         if (cmds != NULL) {
2968                                 ostnames_buf = malloc(array_sz *
2969                                                       (MAX_OBD_NAME + 1));
2970                         } else {
2971                                 free(array);
2972                                 rc = -ENOMEM;
2973                                 goto out;
2974                         }
2975
2976                         for (j = 0; j < array_sz; j++) {
2977                                 char ostname[MAX_OBD_NAME + 1];
2978
2979                                 snprintf(ostname, MAX_OBD_NAME, format,
2980                                          array[j]);
2981                                 ostname[MAX_OBD_NAME] = '\0';
2982
2983                                 rc = check_and_complete_ostname(fsname,ostname);
2984                                 if (rc) {
2985                                         free(array);
2986                                         free(cmds);
2987                                         if (ostnames_buf)
2988                                                 free(ostnames_buf);
2989                                         goto out;
2990                                 }
2991                                 if (ostnames_buf != NULL) {
2992                                         cmds[j].ostname =
2993                                           &ostnames_buf[(MAX_OBD_NAME + 1) * j];
2994                                         strcpy(cmds[j].ostname, ostname);
2995                                 } else {
2996                                         cmds[j].ostname = NULL;
2997                                 }
2998                                 cmds[j].rc = pool_cmd(cmd, argv[0], argv[1],
2999                                                       fsname, poolname,
3000                                                       ostname);
3001                                 /* Return an err if any of the add/dels fail */
3002                                 if (!rc)
3003                                         rc = cmds[j].rc;
3004                         }
3005                         for (j = 0; j < array_sz; j++) {
3006                                 if (!cmds[j].rc) {
3007                                         char ostname[MAX_OBD_NAME + 1];
3008
3009                                         if (!cmds[j].ostname) {
3010                                                 snprintf(ostname, MAX_OBD_NAME,
3011                                                          format, array[j]);
3012                                                 ostname[MAX_OBD_NAME] = '\0';
3013                                                 check_and_complete_ostname(
3014                                                         fsname, ostname);
3015                                         } else {
3016                                                 strcpy(ostname,
3017                                                        cmds[j].ostname);
3018                                         }
3019                                         check_pool_cmd_result(cmd, fsname,
3020                                                               poolname,ostname);
3021                                 }
3022                         }
3023                         if (array_sz > 0)
3024                                 free(array);
3025                         if (cmds)
3026                                 free(cmds);
3027                         if (ostnames_buf);
3028                                 free(ostnames_buf);
3029                 }
3030                 /* fall through */
3031         }
3032         } /* switch */
3033
3034 out:
3035         if (rc != 0) {
3036                 errno = -rc;
3037                 perror(argv[0]);
3038         }
3039
3040         return rc;
3041 }
3042
3043 int jt_get_obj_version(int argc, char **argv)
3044 {
3045         struct lu_fid fid;
3046         struct obd_ioctl_data data;
3047         __u64 version;
3048         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf, *fidstr;
3049         int rc;
3050
3051         if (argc != 2)
3052                 return CMD_HELP;
3053
3054         fidstr = argv[1];
3055         while (*fidstr == '[')
3056                 fidstr++;
3057         sscanf(fidstr, SFID, RFID(&fid));
3058         if (!fid_is_sane(&fid)) {
3059                 fprintf(stderr, "bad FID format [%s], should be "DFID"\n",
3060                         fidstr, (__u64)1, 2, 0);
3061                 return -EINVAL;
3062         }
3063
3064         memset(&data, 0, sizeof data);
3065         data.ioc_dev = cur_device;
3066         data.ioc_inlbuf1 = (char *) &fid;
3067         data.ioc_inllen1 = sizeof fid;
3068         data.ioc_inlbuf2 = (char *) &version;
3069         data.ioc_inllen2 = sizeof version;
3070
3071         memset(buf, 0, sizeof *buf);
3072         rc = obd_ioctl_pack(&data, &buf, sizeof rawbuf);
3073         if (rc) {
3074                 fprintf(stderr, "error: %s: packing ioctl arguments: %s\n",
3075                         jt_cmdname(argv[0]), strerror(-rc));
3076                 return rc;
3077         }
3078
3079         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_GET_OBJ_VERSION, buf);
3080         if (rc == -1) {
3081                 fprintf(stderr, "error: %s: ioctl: %s\n",
3082                         jt_cmdname(argv[0]), strerror(errno));
3083                 return -errno;
3084         }
3085
3086         obd_ioctl_unpack(&data, buf, sizeof rawbuf);
3087         printf(LPX64"\n", version);
3088         return 0;
3089 }
3090
3091 void  llapi_ping_target(char *obd_type, char *obd_name,
3092                         char *obd_uuid, void *args)
3093 {
3094         int  rc;
3095         struct obd_ioctl_data data;
3096         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
3097
3098         memset(&data, 0, sizeof(data));
3099         data.ioc_inlbuf4 = obd_name;
3100         data.ioc_inllen4 = strlen(obd_name) + 1;
3101         data.ioc_dev = OBD_DEV_BY_DEVNAME;
3102         memset(buf, 0, sizeof(rawbuf));
3103         if (obd_ioctl_pack(&data, &buf, sizeof(rawbuf))) {
3104                 fprintf(stderr, "error: invalid ioctl\n");
3105                 return;
3106         }
3107         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_PING_TARGET, buf);
3108         if (rc)
3109                 rc = errno;
3110         if (rc == ENOTCONN || rc == ESHUTDOWN) {
3111                 printf("%s: INACTIVE\n", obd_name);
3112         } else if (rc) {
3113                 printf("%s: check error: %s\n",
3114                         obd_name, strerror(errno));
3115         } else {
3116                 printf("%s: active\n", obd_name);
3117         }
3118 }
3119
3120 int jt_changelog_register(int argc, char **argv)
3121 {
3122         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
3123         struct obd_ioctl_data data;
3124         char devname[30];
3125         int rc;
3126
3127         if (argc > 2)
3128                 return CMD_HELP;
3129         else if (argc == 2 && strcmp(argv[1], "-n") != 0)
3130                 return CMD_HELP;
3131         if (cur_device < 0)
3132                 return CMD_HELP;
3133
3134         memset(&data, 0x00, sizeof(data));
3135         data.ioc_dev = cur_device;
3136         memset(buf, 0, sizeof(rawbuf));
3137         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
3138         if (rc) {
3139                 fprintf(stderr, "error: %s: invalid ioctl\n",
3140                         jt_cmdname(argv[0]));
3141                return rc;
3142         }
3143
3144         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_CHANGELOG_REG, buf);
3145         if (rc < 0) {
3146                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
3147                         strerror(rc = errno));
3148                 return rc;
3149         }
3150         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
3151
3152         if (data.ioc_u32_1 == 0) {
3153                 fprintf(stderr, "received invalid userid!\n");
3154                 return EPROTO;
3155         }
3156
3157         if (lcfg_get_devname() != NULL)
3158                 strcpy(devname, lcfg_get_devname());
3159         else
3160                 sprintf(devname, "dev %d", cur_device);
3161
3162         if (argc == 2)
3163                 /* -n means bare name */
3164                 printf(CHANGELOG_USER_PREFIX"%u\n", data.ioc_u32_1);
3165         else
3166                 printf("%s: Registered changelog userid '"CHANGELOG_USER_PREFIX
3167                        "%u'\n", devname, data.ioc_u32_1);
3168         return 0;
3169 }
3170
3171 int jt_changelog_deregister(int argc, char **argv)
3172 {
3173         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
3174         struct obd_ioctl_data data;
3175         char devname[30];
3176         int id, rc;
3177
3178         if (argc != 2 || cur_device < 0)
3179                 return CMD_HELP;
3180
3181         id = strtol(argv[1] + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
3182         if ((id == 0) || (strncmp(argv[1], CHANGELOG_USER_PREFIX,
3183                                   strlen(CHANGELOG_USER_PREFIX)) != 0)) {
3184                 fprintf(stderr, "expecting id of the form '"
3185                         CHANGELOG_USER_PREFIX"<num>'; got '%s'\n", argv[1]);
3186                 return CMD_HELP;
3187         }
3188
3189         memset(&data, 0x00, sizeof(data));
3190         data.ioc_dev = cur_device;
3191         data.ioc_u32_1 = id;
3192         memset(buf, 0, sizeof(rawbuf));
3193         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
3194         if (rc) {
3195                 fprintf(stderr, "error: %s: invalid ioctl\n",
3196                         jt_cmdname(argv[0]));
3197                 return rc;
3198         }
3199
3200         rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_CHANGELOG_DEREG, buf);
3201         if (rc < 0) {
3202                 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
3203                         strerror(rc = errno));
3204                 return rc;
3205         }
3206         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
3207
3208         if (data.ioc_u32_1 != id) {
3209                 fprintf(stderr, "No changelog user '%s'.  Blocking user"
3210                         " is '"CHANGELOG_USER_PREFIX"%d'.\n", argv[1],
3211                         data.ioc_u32_1);
3212                 return ENOENT;
3213         }
3214
3215         if (lcfg_get_devname() != NULL)
3216                 strcpy(devname, lcfg_get_devname());
3217         else
3218                 sprintf(devname, "dev %d", cur_device);
3219
3220         printf("%s: Deregistered changelog user '"CHANGELOG_USER_PREFIX"%d'\n",
3221                devname, data.ioc_u32_1);
3222         return 0;
3223 }
3224
3225