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