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