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