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