4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 * Author: Peter J. Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Andreas Dilger <adilger@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
43 #include <sys/types.h>
61 #include "lustreapi_internal.h"
62 #include <libcfs/util/list.h>
63 #include <libcfs/util/ioctl.h>
64 #include <libcfs/util/param.h>
65 #include <libcfs/util/parser.h>
66 #include <libcfs/util/string.h>
68 #include <linux/lnet/nidstr.h>
69 #include <linux/lnet/lnetctl.h>
70 #ifdef HAVE_SERVER_SUPPPORT
71 #include <linux/lustre/lustre_barrier_user.h>
72 #include <linux/lustre/lustre_disk.h>
74 #include <linux/lustre/lustre_cfg.h>
75 #include <linux/lustre/lustre_ioctl.h>
76 #include <linux/lustre/lustre_ostid.h>
77 #include <linux/lustre/lustre_param.h>
78 #include <linux/lustre/lustre_ver.h>
80 #include <lustre/lustreapi.h>
82 #define MAX_STRING_SIZE 128
89 #define MAX_THREADS 4096
90 #define MAX_BASE_ID 0xffffffff
91 #define NIDSTRING_LENGTH 64
93 pthread_mutex_t mutex;
97 __u64 counters[MAX_THREADS];
98 __u64 offsets[MAX_THREADS];
102 struct timeval start_time;
103 struct timeval end_time;
107 static struct shared_data *shared_data;
108 static __u64 counter_snapshot[2][MAX_THREADS];
109 static int prev_valid;
110 static struct timeval prev_time;
115 const int nthreads = 1;
118 static int cur_device = -1;
120 int lcfg_ioctl(char *func, int dev_id, struct lustre_cfg *lcfg)
122 struct obd_ioctl_data data;
123 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
126 memset(&data, 0, sizeof(data));
127 data.ioc_dev = cur_device;
128 data.ioc_type = LUSTRE_CFG_TYPE;
129 data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
131 data.ioc_pbuf1 = (void *)lcfg;
132 memset(buf, 0, sizeof(rawbuf));
133 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
135 fprintf(stderr, "error: %s: invalid ioctl\n", jt_cmdname(func));
139 rc = l_ioctl(dev_id, OBD_IOC_PROCESS_CFG, buf);
144 static int do_device(char *func, char *devname);
146 static int get_mgs_device(void)
149 static int mgs_device = -1;
151 if (mgs_device == -1) {
154 do_disconnect(NULL, 1);
155 rc = do_device("mgsioc", mgs);
158 "This command must be run on the MGS.\n");
162 mgs_device = cur_device;
167 /* Returns -1 on error with errno set */
168 int lcfg_mgs_ioctl(char *func, int dev_id, struct lustre_cfg *lcfg)
170 struct obd_ioctl_data data;
171 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
174 memset(&data, 0, sizeof(data));
175 rc = data.ioc_dev = get_mgs_device();
178 data.ioc_type = LUSTRE_CFG_TYPE;
179 data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
181 data.ioc_pbuf1 = (void *)lcfg;
182 memset(buf, 0, sizeof(rawbuf));
183 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
185 fprintf(stderr, "error: %s: invalid ioctl\n", jt_cmdname(func));
189 rc = l_ioctl(dev_id, OBD_IOC_PARAM, buf);
191 if (rc && errno == ENOSYS)
192 fprintf(stderr, "Make sure cfg_device is set first.\n");
197 char *obdo_print(struct obdo *obd)
201 snprintf(buf, sizeof(buf), "id: %#jx\ngrp: %#jx\natime: %ju\n"
202 "mtime: %ju\nctime: %ju\nsize: %ju\nblocks: %ju"
203 "\nblksize: %u\nmode: %o\nuid: %d\ngid: %d\nflags: %x\n"
204 "misc: %x\nnlink: %d,\nvalid %#jx\n",
205 (uintmax_t)ostid_id(&obd->o_oi),
206 (uintmax_t)ostid_seq(&obd->o_oi),
207 (uintmax_t)obd->o_atime, (uintmax_t)obd->o_mtime,
208 (uintmax_t)obd->o_ctime, (uintmax_t)obd->o_size,
209 (uintmax_t)obd->o_blocks, obd->o_blksize, obd->o_mode,
210 obd->o_uid, obd->o_gid, obd->o_flags, obd->o_misc,
211 obd->o_nlink, (uintmax_t)obd->o_valid);
215 #define BAD_VERBOSE (-999999999)
217 char *jt_cmdname(char *func)
219 static char buf[512];
222 sprintf(buf, "%s-%d", func, thread);
229 #define difftime(a, b) \
230 ((a)->tv_sec - (b)->tv_sec + \
231 ((a)->tv_usec - (b)->tv_usec) / 1000000.0)
233 static int be_verbose(int verbose, struct timeval *next_time,
234 __u64 num, __u64 *next_num, int num_total)
242 gettimeofday(&now, NULL);
244 /* A positive verbosity means to print every X iterations */
245 if (verbose > 0 && (num >= *next_num || num >= num_total)) {
246 *next_num += verbose;
248 next_time->tv_sec = now.tv_sec - verbose;
249 next_time->tv_usec = now.tv_usec;
254 /* A negative verbosity means to print at most each X seconds */
255 if (verbose < 0 && next_time && difftime(&now, next_time) >= 0.0) {
256 next_time->tv_sec = now.tv_sec - verbose;
257 next_time->tv_usec = now.tv_usec;
265 static int get_verbose(char *func, const char *arg)
270 if (!arg || arg[0] == 'v') {
272 } else if (arg[0] == 's' || arg[0] == 'q') {
275 verbose = (int)strtoul(arg, &end, 0);
277 fprintf(stderr, "error: %s: bad verbose option '%s'\n",
278 jt_cmdname(func), arg);
284 printf("Print status every %d seconds\n", -verbose);
285 else if (verbose == 1)
286 printf("Print status every operation\n");
287 else if (verbose > 1)
288 printf("Print status every %d operations\n", verbose);
293 int do_disconnect(char *func, int verbose)
295 lcfg_set_devname(NULL);
301 static int shmem_setup(void)
303 pthread_mutexattr_t mattr;
304 pthread_condattr_t cattr;
311 /* Create new segment */
312 shmid = shmget(IPC_PRIVATE, sizeof(*shared_data), 0600);
314 fprintf(stderr, "Can't create shared data: %s\n",
319 /* Attatch to new segment */
320 shared_data = (struct shared_data *)shmat(shmid, NULL, 0);
322 if (shared_data == (struct shared_data *)(-1)) {
323 fprintf(stderr, "Can't attach shared data: %s\n",
330 * Mark segment as destroyed, so it will disappear when we exit.
331 * Forks will inherit attached segments, so we should be OK.
333 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
334 fprintf(stderr, "Can't destroy shared data: %s\n",
339 pthread_mutexattr_init(&mattr);
340 pthread_condattr_init(&cattr);
342 rc = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
344 fprintf(stderr, "Can't set shared mutex attr\n");
348 rc = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
350 fprintf(stderr, "Can't set shared cond attr\n");
354 pthread_mutex_init(&shared_data->mutex, &mattr);
355 pthread_cond_init(&shared_data->cond, &cattr);
357 pthread_mutexattr_destroy(&mattr);
358 pthread_condattr_destroy(&cattr);
363 static inline void shmem_lock(void)
365 pthread_mutex_lock(&shared_data->mutex);
368 static inline void shmem_unlock(void)
370 pthread_mutex_unlock(&shared_data->mutex);
373 static inline void shmem_wait(void)
375 pthread_cond_wait(&shared_data->cond, &shared_data->mutex);
378 static inline void shmem_wakeup_all(void)
380 pthread_cond_broadcast(&shared_data->cond);
383 static inline void shmem_reset(int total_threads)
388 memset(&shared_data->body, 0, sizeof(shared_data->body));
389 memset(counter_snapshot, 0, sizeof(counter_snapshot));
391 shared_data->stopping = 0;
392 shared_data->body.start_barrier = total_threads;
393 shared_data->body.stop_barrier = total_threads;
396 static inline void shmem_bump(__u32 counter)
398 static bool running_not_bumped = true;
400 if (!shared_data || thread <= 0 || thread > MAX_THREADS)
404 shared_data->body.counters[thread - 1] += counter;
405 if (running_not_bumped) {
406 shared_data->body.thr_running++;
407 running_not_bumped = false;
412 static void shmem_total(int total_threads)
418 if (!shared_data || total_threads > MAX_THREADS)
422 for (i = 0; i < total_threads; i++)
423 total += shared_data->body.counters[i];
425 secs = difftime(&shared_data->body.end_time,
426 &shared_data->body.start_time);
429 printf("Total: total %ju threads %d sec %f %f/second\n",
430 (uintmax_t)total, total_threads, secs, total / secs);
433 static void shmem_snap(int total_threads, int live_threads)
435 struct timeval this_time;
442 if (!shared_data || total_threads > MAX_THREADS)
446 memcpy(counter_snapshot[0], shared_data->body.counters,
447 total_threads * sizeof(counter_snapshot[0][0]));
448 running = shared_data->body.thr_running;
451 gettimeofday(&this_time, NULL);
453 for (i = 0; i < total_threads; i++) {
454 long long this_count =
455 counter_snapshot[0][i] - counter_snapshot[1][i];
457 if (this_count != 0) {
463 secs = difftime(&this_time, &prev_time);
464 if (prev_valid && secs > 1.0) { /* someone screwed with the time? */
465 printf("%d/%d Total: %f/second\n", non_zero, total_threads,
468 memcpy(counter_snapshot[1], counter_snapshot[0],
469 total_threads * sizeof(counter_snapshot[0][0]));
470 prev_time = this_time;
472 if (!prev_valid && running == total_threads) {
474 /* drop counters when all threads were started */
475 memcpy(counter_snapshot[1], counter_snapshot[0],
476 total_threads * sizeof(counter_snapshot[0][0]));
477 prev_time = this_time;
481 static void shmem_stop(void)
486 shared_data->stopping = 1;
489 static void shmem_cleanup(void)
496 pthread_mutex_destroy(&shared_data->mutex);
497 pthread_cond_destroy(&shared_data->cond);
500 static int shmem_running(void)
502 return (!shared_data || !shared_data->stopping);
505 static void shmem_end_time_locked(void)
507 shared_data->body.stop_barrier--;
508 if (shared_data->body.stop_barrier == 0)
509 gettimeofday(&shared_data->body.end_time, NULL);
512 static void shmem_start_time_locked(void)
514 shared_data->body.start_barrier--;
515 if (shared_data->body.start_barrier == 0) {
517 gettimeofday(&shared_data->body.start_time, NULL);
524 static int shmem_setup(void)
529 static inline void shmem_reset(int total_threads)
533 static inline void shmem_bump(__u32 counters)
537 static void shmem_lock(void)
541 static void shmem_unlock(void)
545 static void shmem_cleanup(void)
549 static int shmem_running(void)
555 extern command_t cmdlist[];
557 static int do_device(char *func, char *devname)
561 dev = parse_devname(func, devname, cur_device);
565 lcfg_set_devname(devname);
570 int jt_obd_get_device(void)
575 int jt_obd_device(int argc, char **argv)
583 printf("current device is %d - %s\n",
584 cur_device, lcfg_get_devname() ? : "not set");
587 rc = do_device("device", argv[1]);
591 int jt_opt_device(int argc, char **argv)
599 rc = do_device("device", argv[1]);
602 rc = cfs_parser(argc - 1, argv + 1, cmdlist);
604 ret = do_disconnect(argv[0], 0);
612 static void parent_sighandler(int sig)
616 int jt_opt_threads(int argc, char **argv)
618 static char cmdstr[129];
621 struct sigaction sigact;
622 struct sigaction saveact1;
623 struct sigaction saveact2;
624 unsigned long threads;
628 int report_count = -1;
635 threads = strtoul(argv[1], &end, 0);
638 report_count = strtoul(end + 1, &end, 0);
640 if (*end || threads > MAX_THREADS) {
641 fprintf(stderr, "error: %s: invalid thread count '%s'\n",
642 jt_cmdname(argv[0]), argv[1]);
646 verbose = get_verbose(argv[0], argv[2]);
647 if (verbose == BAD_VERBOSE)
651 snprintf(cmdstr, sizeof(cmdstr), "%s", argv[4]);
652 for (i = 5; i < argc; i++)
653 snprintf(cmdstr + strlen(cmdstr),
654 sizeof(cmdstr) - strlen(cmdstr),
657 printf("%s: starting %ld threads on device %s running %s\n",
658 argv[0], threads, argv[3], cmdstr);
665 shmem_reset(threads);
667 sigemptyset(&sigset);
668 sigaddset(&sigset, SIGALRM);
669 sigaddset(&sigset, SIGCHLD);
670 sigprocmask(SIG_BLOCK, &sigset, &saveset);
674 for (i = 1, next_thread = verbose; i <= threads; i++) {
677 fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
678 strerror(rc = errno));
680 } else if (rc == 0) {
681 sigprocmask(SIG_SETMASK, &saveset, NULL);
684 argv[2] = "--device";
685 exit(jt_opt_device(argc - 2, argv + 2));
686 } else if (be_verbose(verbose, NULL, i, &next_thread, threads))
687 printf("%s: thread #%d (PID %d) started\n",
692 if (!thread) { /* parent process */
693 int live_threads = threads;
695 sigemptyset(&sigset);
696 sigemptyset(&sigact.sa_mask);
697 sigact.sa_handler = parent_sighandler;
700 sigaction(SIGALRM, &sigact, &saveact1);
701 sigaction(SIGCHLD, &sigact, &saveact2);
703 while (live_threads > 0) {
707 if (verbose < 0) /* periodic stats */
713 while (live_threads > 0) {
714 ret = waitpid(0, &status, WNOHANG);
720 "error: %s: wait - %s\n",
721 argv[0], strerror(errno));
727 * This is a hack. We _should_ be able
728 * to use WIFEXITED(status) to see if
729 * there was an error, but it appears
730 * to be broken and it always returns 1
733 int err = WEXITSTATUS(status);
735 if (err || WIFSIGNALED(status))
737 "%s: PID %d had rc=%d\n",
746 /* Show stats while all threads running */
748 shmem_snap(threads, live_threads);
749 if (report_count > 0 && --report_count == 0)
753 sigaction(SIGCHLD, &saveact2, NULL);
754 sigaction(SIGALRM, &saveact1, NULL);
757 shmem_total(threads);
758 sigprocmask(SIG_SETMASK, &saveset, NULL);
763 int jt_opt_threads(int argc, char **argv)
765 fprintf(stderr, "%s not-supported in a single-threaded runtime\n",
766 jt_cmdname(argv[0]));
771 int jt_opt_net(int argc, char **argv)
782 rc = jt_ptl_network(2, arg2);
785 rc = cfs_parser(argc - 1, argv + 1, cmdlist);
790 #ifdef HAVE_SERVER_SUPPORT
792 * Place this here so we can build tools that work with
793 * older Lustre versions
795 #ifndef OBD_IOC_NO_TRANSNO
796 #define OBD_IOC_NO_TRANSNO _IOW('f', 140, OBD_IOC_DATA_TYPE)
799 int jt_obd_no_transno(int argc, char **argv)
801 struct obd_ioctl_data data;
802 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
811 memset(buf, 0, sizeof(rawbuf));
813 rc = cfs_get_param_paths(&path, "no_transno");
817 fd = open(path.gl_pathv[0], O_WRONLY);
819 cfs_free_param_data(&path);
823 snprintf(rawbuf, sizeof(rawbuf), "%d", cur_device);
825 count = write(fd, rawbuf, strlen(rawbuf));
829 cfs_free_param_data(&path);
837 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
838 memset(&data, 0, sizeof(data));
839 data.ioc_dev = cur_device;
841 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
843 fprintf(stderr, "error: %s: invalid ioctl\n",
844 jt_cmdname(argv[0]));
847 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_NO_TRANSNO, buf);
849 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
850 strerror(rc = errno));
855 int jt_obd_set_readonly(int argc, char **argv)
857 struct obd_ioctl_data data;
858 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
861 memset(&data, 0, sizeof(data));
862 data.ioc_dev = cur_device;
867 memset(buf, 0, sizeof(rawbuf));
868 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
870 fprintf(stderr, "error: %s: invalid ioctl\n",
871 jt_cmdname(argv[0]));
874 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_SET_READONLY, buf);
876 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
877 strerror(rc = errno));
882 static int obd_abort_recovery(char *cmd, enum obd_abort_recovery_flags flags)
884 struct obd_ioctl_data data = {
885 .ioc_dev = cur_device,
888 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
891 memset(buf, 0, sizeof(rawbuf));
892 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
894 fprintf(stderr, "error: %s: invalid ioctl\n",
898 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_ABORT_RECOVERY, buf);
900 fprintf(stderr, "error: %s: %s\n", jt_cmdname(cmd),
901 strerror(rc = errno));
906 int jt_obd_abort_recovery(int argc, char **argv)
911 return obd_abort_recovery(argv[0], OBD_FLG_ABORT_RECOV_OST);
914 int jt_obd_abort_recovery_mdt(int argc, char **argv)
919 return obd_abort_recovery(argv[0], OBD_FLG_ABORT_RECOV_MDT);
921 #else /* ! HAVE_SERVER_SUPPROT */
922 int jt_obd_no_transno(int argc, char **argv)
927 fprintf(stderr, "error: %s: invalid ioctl\n",
928 jt_cmdname(argv[0]));
932 int jt_obd_set_readonly(int argc, char **argv)
937 fprintf(stderr, "error: %s: invalid ioctl\n",
938 jt_cmdname(argv[0]));
942 int jt_obd_abort_recovery(int argc, char **argv)
947 fprintf(stderr, "error: %s: invalid ioctl\n",
948 jt_cmdname(argv[0]));
952 int jt_obd_abort_recovery_mdt(int argc, char **argv)
957 fprintf(stderr, "error: %s: invalid ioctl\n",
958 jt_cmdname(argv[0]));
961 #endif /* HAVE_SERVER_SUPPORT */
963 int jt_get_version(int argc, char **argv)
971 rc = llapi_get_version_string(version, sizeof(version));
973 printf("Lustre version: %s\n", LUSTRE_VERSION_STRING);
975 printf("Lustre version: %s\n", version);
980 struct jt_fid_space {
986 int jt_obd_alloc_fids(struct jt_fid_space *space, struct lu_fid *fid,
991 if (space->jt_seq == 0 || space->jt_id == space->jt_width) {
992 struct obd_ioctl_data data;
993 char rawbuf[MAX_IOC_BUFLEN];
998 memset(&data, 0, sizeof(data));
999 data.ioc_dev = cur_device;
1001 data.ioc_pbuf1 = (char *)&seqnr;
1002 data.ioc_plen1 = sizeof(seqnr);
1004 data.ioc_pbuf2 = (char *)&max_count;
1005 data.ioc_plen2 = sizeof(max_count);
1007 memset(buf, 0, sizeof(rawbuf));
1008 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1010 fprintf(stderr, "error: invalid ioctl rc = %d\n", rc);
1014 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_ECHO_ALLOC_SEQ, buf);
1016 fprintf(stderr, "ioctl error: rc = %d\n", rc);
1020 space->jt_seq = *(__u64 *)data.ioc_pbuf1;
1021 space->jt_width = *(int *)data.ioc_pbuf2;
1024 fid->f_seq = space->jt_seq;
1025 fid->f_oid = space->jt_id;
1028 space->jt_id = space->jt_id + *count;
1029 if (space->jt_id > space->jt_width)
1030 space->jt_id = space->jt_width;
1032 *count = space->jt_id - fid->f_oid;
1036 #define MD_STEP_COUNT 1000
1037 int jt_obd_md_common(int argc, char **argv, int cmd)
1039 struct obd_ioctl_data data;
1040 struct timeval start;
1041 struct timeval end_time;
1042 char rawbuf[MAX_IOC_BUFLEN];
1047 char *parent_basedir = NULL;
1049 int parent_base_id = 0;
1050 int parent_count = 1;
1051 __u64 child_base_id = -1;
1052 int stripe_count = 0;
1053 int stripe_index = -1;
1059 __u64 total_count = 0;
1061 struct jt_fid_space fid_space = {0};
1063 struct option long_opts[] = {
1064 { .val = 'b', .name = "child_base_id",
1065 .has_arg = required_argument },
1066 { .val = 'c', .name = "stripe_count",
1067 .has_arg = required_argument },
1068 { .val = 'd', .name = "parent_basedir",
1069 .has_arg = required_argument },
1070 { .val = 'D', .name = "parent_dircount",
1071 .has_arg = required_argument },
1072 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
1073 { .val = 'm', .name = "mode", .has_arg = required_argument },
1074 { .val = 'n', .name = "count", .has_arg = required_argument },
1075 { .val = 't', .name = "time", .has_arg = required_argument },
1076 { .val = 'v', .name = "version", .has_arg = no_argument },
1079 while ((c = getopt_long(argc, argv, "b:c:d:D:m:n:t:v",
1080 long_opts, NULL)) >= 0) {
1083 child_base_id = strtoull(optarg, &end, 0);
1086 "error: %s: bad child_base_id '%s'\n",
1087 jt_cmdname(argv[0]), optarg);
1092 stripe_count = strtoul(optarg, &end, 0);
1095 "error: %s: bad stripe count '%s'\n",
1096 jt_cmdname(argv[0]), optarg);
1101 parent_basedir = optarg;
1104 parent_count = strtoul(optarg, &end, 0);
1107 "error: %s: bad parent count '%s'\n",
1108 jt_cmdname(argv[0]), optarg);
1113 stripe_index = strtoul(optarg, &end, 0);
1116 "error: %s: bad stripe index '%s'\n",
1117 jt_cmdname(argv[0]), optarg);
1122 mode = strtoul(optarg, &end, 0);
1124 fprintf(stderr, "error: %s: bad mode '%s'\n",
1125 jt_cmdname(argv[0]), optarg);
1130 total_count = strtoul(optarg, &end, 0);
1131 if (*end || total_count == 0) {
1132 fprintf(stderr, "%s: bad child count '%s'\n",
1133 jt_cmdname(argv[0]), optarg);
1138 seconds = strtoull(optarg, &end, 0);
1140 fprintf(stderr, "error: %s: seconds '%s'\n",
1141 jt_cmdname(argv[0]), optarg);
1150 "error: %s: option '%s' unrecognized\n",
1151 argv[0], argv[optind - 1]);
1156 memset(&data, 0, sizeof(data));
1157 data.ioc_dev = cur_device;
1158 if (child_base_id == -1) {
1161 name = argv[optind];
1164 if (optind < argc) {
1166 "child_base_id and name can not specified at the same time\n");
1171 if (stripe_count == 0 && stripe_index != -1) {
1173 "If stripe_count is 0, stripe_index can not be specified\n");
1177 if (total_count == 0 && seconds == 0) {
1178 fprintf(stderr, "count or seconds needs to be indicated\n");
1182 if (parent_count <= 0) {
1183 fprintf(stderr, "parent count must < 0\n");
1194 /* threads interleave */
1195 if (parent_base_id != -1)
1196 parent_base_id += (thread - 1) % parent_count;
1198 if (child_base_id != -1)
1199 child_base_id += (thread - 1) *
1200 (MAX_BASE_ID / nthreads);
1202 shmem_start_time_locked();
1207 * If parent directory is not specified, try to get the directory
1210 if (!parent_basedir) {
1215 "parent_basedir or name must be indicated!\n");
1218 /*Get directory and name from name*/
1219 last_lash = strrchr(name, '/');
1220 if (!last_lash || name[0] != '/') {
1221 fprintf(stderr, "Can not locate %s\n", name);
1225 if (last_lash == name) {
1226 sprintf(dirname, "%s", "/");
1229 int namelen = (unsigned long)last_lash -
1230 (unsigned long)name + 1;
1231 snprintf(dirname, namelen, "%s", name);
1232 name = last_lash + 1;
1235 data.ioc_pbuf1 = dirname;
1236 data.ioc_plen1 = strlen(dirname);
1238 data.ioc_pbuf2 = name;
1239 data.ioc_plen2 = strlen(name);
1242 data.ioc_pbuf2 = name;
1243 data.ioc_plen2 = strlen(name);
1245 if (parent_base_id > 0)
1246 sprintf(dirname, "%s%d", parent_basedir,
1249 sprintf(dirname, "%s", parent_basedir);
1250 data.ioc_pbuf1 = dirname;
1251 data.ioc_plen1 = strlen(dirname);
1254 if (cmd == ECHO_MD_MKDIR || cmd == ECHO_MD_RMDIR)
1255 create_mode = S_IFDIR;
1257 create_mode = S_IFREG;
1259 data.ioc_obdo1.o_mode = mode | S_IFDIR;
1260 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
1261 OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1262 data.ioc_command = cmd;
1264 gettimeofday(&start, NULL);
1265 while (shmem_running()) {
1266 struct lu_fid fid = { 0 };
1268 if (child_base_id != -1)
1269 data.ioc_obdo2.o_oi.oi.oi_id = child_base_id;
1270 data.ioc_obdo2.o_mode = mode | create_mode;
1271 data.ioc_obdo2.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
1272 OBD_MD_FLMODE | OBD_MD_FLFLAGS |
1274 data.ioc_obdo2.o_misc = stripe_count;
1275 data.ioc_obdo2.o_stripe_idx = stripe_index;
1277 if (total_count > 0) {
1278 if ((total_count - count) > MD_STEP_COUNT)
1279 data.ioc_count = MD_STEP_COUNT;
1281 data.ioc_count = total_count - count;
1283 data.ioc_count = MD_STEP_COUNT;
1286 if (cmd == ECHO_MD_CREATE || cmd == ECHO_MD_MKDIR) {
1287 /*Allocate fids for the create */
1288 rc = jt_obd_alloc_fids(&fid_space, &fid,
1291 fprintf(stderr, "Allocate fids error %d.\n",
1295 data.ioc_obdo1.o_oi.oi_fid = fid;
1298 child_base_id += data.ioc_count;
1299 count += data.ioc_count;
1301 memset(buf, 0, sizeof(rawbuf));
1302 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1304 fprintf(stderr, "error: %s: invalid ioctl %d\n",
1305 jt_cmdname(argv[0]), rc);
1309 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_ECHO_MD, buf);
1311 fprintf(stderr, "error: %s: %s\n",
1312 jt_cmdname(argv[0]), strerror(rc = errno));
1315 shmem_bump(data.ioc_count);
1317 gettimeofday(&end_time, NULL);
1318 diff = difftime(&end_time, &start);
1319 if (seconds > 0 && (__u64)diff > seconds)
1322 if (count >= total_count && total_count > 0)
1326 if (count > 0 && version) {
1327 gettimeofday(&end_time, NULL);
1328 diff = difftime(&end_time, &start);
1329 printf("%s: %d in %.3fs (%.3f /s): %s",
1330 jt_cmdname(argv[0]), count, diff,
1331 (double)count / diff, ctime(&end_time.tv_sec));
1337 shmem_end_time_locked();
1344 int jt_obd_test_create(int argc, char **argv)
1346 return jt_obd_md_common(argc, argv, ECHO_MD_CREATE);
1349 int jt_obd_test_mkdir(int argc, char **argv)
1351 return jt_obd_md_common(argc, argv, ECHO_MD_MKDIR);
1354 int jt_obd_test_destroy(int argc, char **argv)
1356 return jt_obd_md_common(argc, argv, ECHO_MD_DESTROY);
1359 int jt_obd_test_rmdir(int argc, char **argv)
1361 return jt_obd_md_common(argc, argv, ECHO_MD_RMDIR);
1364 int jt_obd_test_lookup(int argc, char **argv)
1366 return jt_obd_md_common(argc, argv, ECHO_MD_LOOKUP);
1369 int jt_obd_test_setxattr(int argc, char **argv)
1371 return jt_obd_md_common(argc, argv, ECHO_MD_SETATTR);
1374 int jt_obd_test_md_getattr(int argc, char **argv)
1376 return jt_obd_md_common(argc, argv, ECHO_MD_GETATTR);
1379 int jt_obd_create(int argc, char **argv)
1381 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1382 struct obd_ioctl_data data;
1383 struct timeval next_time;
1384 __u64 count = 1, next_count, base_id = 1;
1385 int verbose = 1, mode = 0100644, rc = 0, i;
1388 memset(&data, 0, sizeof(data));
1389 data.ioc_dev = cur_device;
1390 if (argc < 2 || argc > 4)
1393 count = strtoull(argv[1], &end, 0);
1395 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1396 jt_cmdname(argv[0]), argv[1]);
1401 mode = strtoul(argv[2], &end, 0);
1403 fprintf(stderr, "error: %s: invalid mode '%s'\n",
1404 jt_cmdname(argv[0]), argv[2]);
1407 if (!(mode & S_IFMT))
1412 verbose = get_verbose(argv[0], argv[3]);
1413 if (verbose == BAD_VERBOSE)
1417 printf("%s: %jd objects\n", jt_cmdname(argv[0]), (uintmax_t)count);
1418 gettimeofday(&next_time, NULL);
1419 next_time.tv_sec -= verbose;
1421 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1426 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1428 * base_id is 1 so we don't need to worry about it being
1429 * greater than OBIF_MAX_OID
1431 data.ioc_obdo1.o_oi.oi_fid.f_oid = base_id;
1432 data.ioc_obdo1.o_mode = mode;
1433 data.ioc_obdo1.o_uid = 0;
1434 data.ioc_obdo1.o_gid = 0;
1435 data.ioc_obdo1.o_projid = 0;
1436 data.ioc_obdo1.o_valid = OBD_MD_FLTYPE | OBD_MD_FLMODE |
1437 OBD_MD_FLID | OBD_MD_FLUID |
1438 OBD_MD_FLGID | OBD_MD_FLGROUP |
1441 memset(buf, 0, sizeof(rawbuf));
1442 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1444 fprintf(stderr, "error: %s: invalid ioctl\n",
1445 jt_cmdname(argv[0]));
1448 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CREATE, buf);
1449 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
1452 fprintf(stderr, "error: %s: #%d - %s\n",
1453 jt_cmdname(argv[0]), i, strerror(rc = errno));
1456 if (!(data.ioc_obdo1.o_valid & OBD_MD_FLID)) {
1457 fprintf(stderr, "error: %s: oid not valid #%d:%#jx\n",
1458 jt_cmdname(argv[0]), i,
1459 (uintmax_t)data.ioc_obdo1.o_valid);
1464 if (be_verbose(verbose, &next_time, i, &next_count, count))
1465 printf("%s: #%d is object id %#jx\n",
1466 jt_cmdname(argv[0]), i,
1467 (uintmax_t)ostid_id(&data.ioc_obdo1.o_oi));
1473 int jt_obd_setattr(int argc, char **argv)
1475 struct obd_ioctl_data data;
1476 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1482 memset(&data, 0, sizeof(data));
1483 data.ioc_dev = cur_device;
1487 objid = strtoull(argv[1], &end, 0);
1489 fprintf(stderr, "error: %s: objid '%s' is not a number\n",
1490 jt_cmdname(argv[0]), argv[1]);
1494 if (objid > OBIF_MAX_OID) {
1495 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1496 jt_cmdname(argv[0]), argv[1]);
1500 mode = strtoul(argv[2], &end, 0);
1502 fprintf(stderr, "error: %s: invalid mode '%s'\n",
1503 jt_cmdname(argv[0]), argv[2]);
1507 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1508 data.ioc_obdo1.o_mode = S_IFREG | mode;
1509 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1510 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
1512 memset(buf, 0, sizeof(rawbuf));
1513 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1515 fprintf(stderr, "error: %s: invalid ioctl\n",
1516 jt_cmdname(argv[0]));
1519 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, buf);
1521 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1522 strerror(rc = errno));
1527 int jt_obd_test_setattr(int argc, char **argv)
1529 struct obd_ioctl_data data;
1530 struct timeval start, next_time;
1531 __u64 i, count, next_count;
1532 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1538 if (argc < 2 || argc > 4)
1541 memset(&data, 0, sizeof(data));
1542 data.ioc_dev = cur_device;
1543 count = strtoull(argv[1], &end, 0);
1545 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1546 jt_cmdname(argv[0]), argv[1]);
1551 verbose = get_verbose(argv[0], argv[2]);
1552 if (verbose == BAD_VERBOSE)
1557 if (argv[3][0] == 't') {
1558 objid = strtoull(argv[3] + 1, &end, 0);
1560 objid += thread - 1;
1562 objid = strtoull(argv[3], &end, 0);
1565 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1566 jt_cmdname(argv[0]), argv[3]);
1571 gettimeofday(&start, NULL);
1572 next_time.tv_sec = start.tv_sec - verbose;
1573 next_time.tv_usec = start.tv_usec;
1575 printf("%s: setting %jd attrs (objid %#jx): %s",
1576 jt_cmdname(argv[0]), (uintmax_t)count,
1577 (uintmax_t)objid, ctime(&start.tv_sec));
1579 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1584 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1585 if (objid > OBIF_MAX_OID) {
1586 fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
1587 jt_cmdname(argv[0]), (unsigned long long)objid);
1591 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1592 data.ioc_obdo1.o_mode = S_IFREG;
1593 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
1595 memset(buf, 0, sizeof(rawbuf));
1596 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1598 fprintf(stderr, "error: %s: invalid ioctl\n",
1599 jt_cmdname(argv[0]));
1602 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, &data);
1605 fprintf(stderr, "error: %s: #%jd - %d:%s\n",
1606 jt_cmdname(argv[0]), (uintmax_t)i,
1607 errno, strerror(rc = errno));
1610 if (be_verbose(verbose, &next_time, i, &next_count, count))
1611 printf("%s: set attr #%jd\n",
1612 jt_cmdname(argv[0]), (uintmax_t)i);
1619 gettimeofday(&end, NULL);
1621 diff = difftime(&end, &start);
1625 printf("%s: %jd attrs in %.3fs (%.3f attr/s): %s",
1626 jt_cmdname(argv[0]), (uintmax_t)i, diff,
1627 i / diff, ctime(&end.tv_sec));
1632 int jt_obd_destroy(int argc, char **argv)
1634 struct obd_ioctl_data data;
1635 struct timeval next_time;
1636 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1637 __u64 count = 1, next_count;
1643 memset(&data, 0, sizeof(data));
1644 data.ioc_dev = cur_device;
1645 if (argc < 2 || argc > 4)
1649 id = strtoull(argv[1], &end, 0);
1650 if (*end || id == 0 || errno != 0) {
1651 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1652 jt_cmdname(argv[0]), argv[1]);
1656 count = strtoull(argv[2], &end, 0);
1659 "error: %s: invalid iteration count '%s'\n",
1660 jt_cmdname(argv[0]), argv[2]);
1666 verbose = get_verbose(argv[0], argv[3]);
1667 if (verbose == BAD_VERBOSE)
1671 printf("%s: %jd objects\n", jt_cmdname(argv[0]), (uintmax_t)count);
1672 gettimeofday(&next_time, NULL);
1673 next_time.tv_sec -= verbose;
1675 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1680 for (i = 1, next_count = verbose; i <= count && shmem_running();
1682 if (id > OBIF_MAX_OID) {
1683 fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
1684 jt_cmdname(argv[0]), (unsigned long long)id);
1688 data.ioc_obdo1.o_oi.oi_fid.f_oid = id;
1689 data.ioc_obdo1.o_mode = S_IFREG | 0644;
1690 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLMODE;
1692 memset(buf, 0, sizeof(rawbuf));
1693 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1695 fprintf(stderr, "error: %s: invalid ioctl\n",
1696 jt_cmdname(argv[0]));
1699 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_DESTROY, buf);
1700 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
1703 fprintf(stderr, "error: %s: objid %#jx: %s\n",
1704 jt_cmdname(argv[0]), (uintmax_t)id,
1705 strerror(rc = errno));
1709 if (be_verbose(verbose, &next_time, i, &next_count, count))
1710 printf("%s: #%d is object id %#jx\n",
1711 jt_cmdname(argv[0]), i, (uintmax_t)id);
1717 static int jt_str_to_ost_id(const char *str, struct ost_id *oi)
1722 oid = strtoull(str, &end, 0);
1724 /* If str is a single number then assume old echo
1726 if (oid > OBIF_MAX_OID)
1729 ostid_set_seq_echo(oi);
1730 oi->oi_fid.f_oid = oid;
1734 return llapi_fid_parse(str, &oi->oi_fid, NULL);
1737 int jt_obd_getattr(int argc, char **argv)
1739 struct obd_ioctl_data data;
1741 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1747 memset(&data, 0, sizeof(data));
1748 data.ioc_dev = cur_device;
1749 oa = &data.ioc_obdo1;
1751 rc = jt_str_to_ost_id(argv[1], &oa->o_oi);
1753 fprintf(stderr, "error: %s: invalid objid of FID '%s'\n",
1754 jt_cmdname(argv[0]), argv[1]);
1758 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1760 memset(buf, 0, sizeof(rawbuf));
1761 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1763 fprintf(stderr, "error: %s: invalid ioctl\n",
1764 jt_cmdname(argv[0]));
1767 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, buf);
1768 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
1770 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1771 strerror(rc = errno));
1775 #define OP4(bits, name, format, value) \
1777 if ((oa->o_valid & (bits)) == (bits)) \
1778 printf("%s: "format"\n", (name), value); \
1781 #define OPM(bits, member, format) \
1782 OP4(bits, #member, format, (uintmax_t)(oa->o_ ## member))
1784 #define OPO(bits, member) OPM(bits, member, "%#jo")
1785 #define OPU(bits, member) OPM(bits, member, "%ju")
1786 #define OPX(bits, member) OPM(bits, member, "%#jx")
1789 OPX(OBD_MD_FLID | OBD_MD_FLGROUP, oi.oi.oi_id);
1790 OPX(OBD_MD_FLID | OBD_MD_FLGROUP, oi.oi.oi_seq);
1791 OP4(OBD_MD_FLID | OBD_MD_FLGROUP, "oi.oi_fid", DFID, PFID(&oa->o_oi.oi_fid));
1792 OPU(OBD_MD_FLATIME, atime);
1793 OPU(OBD_MD_FLMTIME, mtime);
1794 OPU(OBD_MD_FLCTIME, ctime);
1795 OPU(OBD_MD_FLSIZE, size);
1796 OPU(OBD_MD_FLBLOCKS, blocks);
1797 OPU(OBD_MD_FLBLKSZ, blksize);
1798 OPO(OBD_MD_FLMODE | OBD_MD_FLTYPE, mode);
1799 OPU(OBD_MD_FLUID, uid);
1800 OPU(OBD_MD_FLGID, gid);
1801 OPU(OBD_MD_FLFLAGS, flags);
1802 OPU(OBD_MD_FLNLINK, nlink);
1803 OPX(OBD_MD_FLPARENT | OBD_MD_FLFID, parent_seq);
1804 OPX(OBD_MD_FLPARENT | OBD_MD_FLFID, parent_oid);
1805 OPX(OBD_MD_FLPARENT | OBD_MD_FLFID, parent_ver);
1806 OPU(OBD_MD_LAYOUT_VERSION, layout_version);
1807 OPU(OBD_MD_FLGRANT, grant);
1808 OPU(OBD_MD_FLPROJID, projid);
1809 OPU(OBD_MD_FLDATAVERSION, data_version);
1819 int jt_obd_test_getattr(int argc, char **argv)
1821 struct obd_ioctl_data data;
1822 struct timeval start, next_time;
1823 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1824 __u64 i, count, next_count;
1830 if (argc < 2 || argc > 4)
1833 memset(&data, 0, sizeof(data));
1834 data.ioc_dev = cur_device;
1835 count = strtoull(argv[1], &end, 0);
1837 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1838 jt_cmdname(argv[0]), argv[1]);
1843 verbose = get_verbose(argv[0], argv[2]);
1844 if (verbose == BAD_VERBOSE)
1849 if (argv[3][0] == 't') {
1850 objid = strtoull(argv[3] + 1, &end, 0);
1852 objid += thread - 1;
1854 objid = strtoull(argv[3], &end, 0);
1857 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1858 jt_cmdname(argv[0]), argv[3]);
1863 gettimeofday(&start, NULL);
1864 next_time.tv_sec = start.tv_sec - verbose;
1865 next_time.tv_usec = start.tv_usec;
1867 printf("%s: getting %jd attrs (objid %#jx): %s",
1868 jt_cmdname(argv[0]), (uintmax_t)count,
1869 (uintmax_t)objid, ctime(&start.tv_sec));
1871 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1876 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1877 if (objid > OBIF_MAX_OID) {
1878 fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
1879 jt_cmdname(argv[0]), (unsigned long long)objid);
1883 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1884 data.ioc_obdo1.o_mode = S_IFREG;
1885 data.ioc_obdo1.o_valid = 0xffffffff;
1886 memset(buf, 0, sizeof(rawbuf));
1887 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1889 fprintf(stderr, "error: %s: invalid ioctl\n",
1890 jt_cmdname(argv[0]));
1893 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, &data);
1896 fprintf(stderr, "error: %s: #%jd - %d:%s\n",
1897 jt_cmdname(argv[0]), (uintmax_t)i,
1898 errno, strerror(rc = errno));
1901 if (be_verbose(verbose, &next_time, i, &next_count, count))
1902 printf("%s: got attr #%jd\n",
1903 jt_cmdname(argv[0]), (uintmax_t)i);
1910 gettimeofday(&end, NULL);
1912 diff = difftime(&end, &start);
1916 printf("%s: %jd attrs in %.3fs (%.3f attr/s): %s",
1917 jt_cmdname(argv[0]), (uintmax_t)i, diff,
1918 i / diff, ctime(&end.tv_sec));
1925 * test_brw <cnt> count
1926 * <r|w[r(repeat)x(noverify)]> mode
1927 * <q|v|#(print interval)> verbosity
1928 * <npages[+offset]> blocksize
1929 * <[[<interleave_threads>]t(inc obj by thread#)]obj> object
1932 int jt_obd_test_brw(int argc, char **argv)
1934 struct obd_ioctl_data data;
1935 struct timeval start, next_time;
1936 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1937 __u64 count, next_count, len, stride, thr_offset = 0, objid = 3;
1938 int write = 0, verbose = 1, cmd, i, rc = 0, pages = 1;
1939 int offset_pages = 0;
1941 int repeat_offset = 0;
1942 unsigned long long ull;
1943 int nthr_per_obj = 0;
1948 if (argc < 2 || argc > 7) {
1949 fprintf(stderr, "error: %s: bad number of arguments: %d\n",
1950 jt_cmdname(argv[0]), argc);
1954 count = strtoull(argv[1], &end, 0);
1956 fprintf(stderr, "error: %s: bad iteration count '%s'\n",
1957 jt_cmdname(argv[0]), argv[1]);
1962 if (argv[2][0] == 'w' || argv[2][0] == '1')
1964 /* else it's a read */
1966 if (argv[2][0] != 0)
1967 for (i = 1; argv[2][i] != 0; i++)
1968 switch (argv[2][i]) {
1979 "Can't parse cmd '%s'\n",
1986 verbose = get_verbose(argv[0], argv[3]);
1987 if (verbose == BAD_VERBOSE)
1992 pages = strtoul(argv[4], &end, 0);
1995 offset_pages = strtoul(end + 1, &end, 0);
1997 if (*end != 0 || offset_pages < 0 || offset_pages >= pages) {
1998 fprintf(stderr, "error: %s: bad npages[+offset] parameter '%s'\n",
1999 jt_cmdname(argv[0]), argv[4]);
2005 if (thread && (n = strtol(argv[5], &end, 0)) > 0 &&
2006 *end == 't' && (ull = strtoull(end + 1, &end, 0)) > 0 &&
2010 } else if (thread && argv[5][0] == 't') {
2012 objid = strtoull(argv[5] + 1, &end, 0);
2015 objid = strtoull(argv[5], &end, 0);
2018 fprintf(stderr, "error: %s: bad objid '%s'\n",
2019 jt_cmdname(argv[0]), argv[5]);
2024 memset(&data, 0, sizeof(data));
2025 data.ioc_dev = cur_device;
2028 * communicate the 'type' of brw test and batching to echo_client.
2029 * don't start. we'd love to refactor this lctl->echo_client
2032 data.ioc_pbuf1 = (void *)1;
2036 switch (argv[6][0]) {
2037 case 'g': /* plug and unplug */
2038 data.ioc_pbuf1 = (void *)2;
2039 data.ioc_plen1 = strtoull(argv[6] + 1, &end, 0);
2041 case 'p': /* prep and commit */
2042 data.ioc_pbuf1 = (void *)3;
2043 data.ioc_plen1 = strtoull(argv[6] + 1, &end, 0);
2047 "error: %s: batching '%s' needs to specify 'p' or 'g'\n",
2048 jt_cmdname(argv[0]), argv[6]);
2053 fprintf(stderr, "error: %s: bad batching '%s'\n",
2054 jt_cmdname(argv[0]), argv[6]);
2057 data.ioc_plen1 *= getpagesize();
2060 len = pages * getpagesize();
2061 thr_offset = offset_pages * getpagesize();
2071 if (nthr_per_obj != 0) {
2072 /* threads interleave */
2073 obj_idx = (thread - 1) / nthr_per_obj;
2075 stride *= nthr_per_obj;
2076 if ((thread - 1) % nthr_per_obj == 0) {
2077 shared_data->body.offsets[obj_idx] =
2078 stride + thr_offset;
2080 thr_offset += ((thread - 1) % nthr_per_obj) * len;
2082 /* threads disjoint */
2083 thr_offset += (thread - 1) * len;
2086 shmem_start_time_locked();
2091 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
2092 if (objid > OBIF_MAX_OID) {
2093 fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
2094 jt_cmdname(argv[0]), (unsigned long long)objid);
2098 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
2099 data.ioc_obdo1.o_mode = S_IFREG;
2100 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
2101 OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
2102 data.ioc_obdo1.o_flags = (verify ? OBD_FL_DEBUG_CHECK : 0);
2103 data.ioc_count = len;
2104 data.ioc_offset = (repeat_offset ? 0 : thr_offset);
2106 gettimeofday(&start, NULL);
2107 next_time.tv_sec = start.tv_sec - verbose;
2108 next_time.tv_usec = start.tv_usec;
2111 printf("%s: %s %jux%d pages (obj %#jx, off %ju): %s",
2112 jt_cmdname(argv[0]), write ? "writing" : "reading",
2113 (uintmax_t)count, pages, (uintmax_t)objid,
2114 (uintmax_t)data.ioc_offset, ctime(&start.tv_sec));
2116 cmd = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
2117 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
2118 data.ioc_obdo1.o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
2119 memset(buf, 0, sizeof(rawbuf));
2120 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2122 fprintf(stderr, "error: %s: invalid ioctl\n",
2123 jt_cmdname(argv[0]));
2126 rc = l_ioctl(OBD_DEV_ID, cmd, buf);
2129 fprintf(stderr, "error: %s: #%d - %s on %s\n",
2130 jt_cmdname(argv[0]), i, strerror(rc = errno),
2131 write ? "write" : "read");
2133 } else if (be_verbose(verbose, &next_time, i,
2134 &next_count, count)) {
2136 printf("%s: %s number %d @ %jd:%ju for %d\n",
2137 jt_cmdname(argv[0]), write ? "write" : "read", i,
2138 (uintmax_t)ostid_id(&data.ioc_obdo1.o_oi),
2139 (uintmax_t)data.ioc_offset,
2140 (int)(pages * getpagesize()));
2144 if (!repeat_offset) {
2146 if (stride == len) {
2147 data.ioc_offset += stride;
2148 } else if (i < count) {
2151 shared_data->body.offsets[obj_idx];
2152 shared_data->body.offsets[obj_idx] += len;
2156 data.ioc_offset += len;
2157 obj_idx = 0; /* avoids an unused var warning */
2166 gettimeofday(&end, NULL);
2168 diff = difftime(&end, &start);
2172 printf("%s: %s %dx%d pages in %.3fs (%.3f MB/s): %s",
2173 jt_cmdname(argv[0]), write ? "wrote" : "read",
2175 ((double)i * pages * getpagesize()) /
2176 (diff * 1048576.0), ctime(&end.tv_sec));
2182 shmem_end_time_locked();
2189 static int do_activate(int argc, char **argv, int flag)
2191 struct obd_ioctl_data data;
2192 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2195 memset(&data, 0, sizeof(data));
2196 data.ioc_dev = cur_device;
2200 /* reuse offset for 'active' */
2201 data.ioc_offset = flag;
2203 memset(buf, 0, sizeof(rawbuf));
2204 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2206 fprintf(stderr, "error: %s: invalid ioctl\n",
2207 jt_cmdname(argv[0]));
2210 rc = llapi_ioctl_dev(OBD_DEV_ID, OBD_IOC_SET_ACTIVE, buf);
2212 fprintf(stderr, "error: %s: failed: %s\n",
2213 jt_cmdname(argv[0]), strerror(rc = errno));
2219 * Replace nids for given device.
2220 * lctl replace_nids <devicename> <nid1>[,nid2,nid3]
2221 * Command should be started on MGS server.
2222 * Only MGS server should be started (command execution
2223 * returns error in another cases). Command mount
2224 * -t lustre <MDT partition> -o nosvc <mount point>
2225 * can be used for that.
2227 * llogs for MDTs and clients are processed. All
2228 * records copied as is except add_uuid and setup. This records
2229 * are skipped and recorded with new nids and uuid.
2231 * \see mgs_replace_nids
2232 * \see mgs_replace_log
2233 * \see mgs_replace_nids_handler
2235 int jt_replace_nids(int argc, char **argv)
2238 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2239 struct obd_ioctl_data data;
2241 memset(&data, 0, sizeof(data));
2242 data.ioc_dev = get_mgs_device();
2246 data.ioc_inllen1 = strlen(argv[1]) + 1;
2247 data.ioc_inlbuf1 = argv[1];
2249 data.ioc_inllen2 = strlen(argv[2]) + 1;
2250 data.ioc_inlbuf2 = argv[2];
2251 memset(buf, 0, sizeof(rawbuf));
2252 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2254 fprintf(stderr, "error: %s: invalid ioctl\n",
2255 jt_cmdname(argv[0]));
2259 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_REPLACE_NIDS, buf);
2261 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
2262 strerror(rc = errno));
2268 int jt_obd_deactivate(int argc, char **argv)
2270 return do_activate(argc, argv, 0);
2273 int jt_obd_activate(int argc, char **argv)
2275 return do_activate(argc, argv, 1);
2278 int jt_obd_recover(int argc, char **argv)
2280 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2281 struct obd_ioctl_data data;
2284 memset(&data, 0, sizeof(data));
2285 data.ioc_dev = cur_device;
2290 data.ioc_inllen1 = strlen(argv[1]) + 1;
2291 data.ioc_inlbuf1 = argv[1];
2294 memset(buf, 0, sizeof(rawbuf));
2295 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2297 fprintf(stderr, "error: %s: invalid ioctl\n",
2298 jt_cmdname(argv[0]));
2301 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CLIENT_RECOVER, buf);
2303 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
2304 strerror(rc = errno));
2310 int jt_obd_mdc_lookup(int argc, char **argv)
2312 struct obd_ioctl_data data;
2313 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2314 char *parent, *child;
2315 int rc, fd, verbose = 1;
2317 if (argc < 3 || argc > 4)
2323 verbose = get_verbose(argv[0], argv[3]);
2325 memset(&data, 0, sizeof(data));
2326 data.ioc_dev = cur_device;
2328 data.ioc_inllen1 = strlen(child) + 1;
2329 data.ioc_inlbuf1 = child;
2331 memset(buf, 0, sizeof(rawbuf));
2332 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2334 fprintf(stderr, "error: %s: invalid ioctl\n",
2335 jt_cmdname(argv[0]));
2339 fd = open(parent, O_RDONLY);
2341 fprintf(stderr, "open \"%s\" failed: %s\n", parent,
2346 rc = ioctl(fd, IOC_MDC_LOOKUP, buf);
2348 fprintf(stderr, "error: %s: ioctl error: %s\n",
2349 jt_cmdname(argv[0]), strerror(rc = errno));
2354 rc = llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
2356 fprintf(stderr, "error: %s: invalid reply\n",
2357 jt_cmdname(argv[0]));
2360 printf("%s: mode %o uid %d gid %d\n",
2361 child, data.ioc_obdo1.o_mode, data.ioc_obdo1.o_uid,
2362 data.ioc_obdo1.o_gid);
2368 #ifdef HAVE_SERVER_SUPPORT
2370 * Clear config logs for given device or filesystem.
2371 * lctl clear_conf <devicename|fsname>
2372 * Command has to be run on MGS node having MGS device mounted with -o
2375 * Configuration logs for filesystem or one particular log is
2376 * processed. New log is created, original log is read, its records
2377 * marked SKIP do not get copied to new log. Others are copied as-is.
2378 * Original file is renamed to log.${time}.bak.
2380 * \see mgs_clear_configs
2381 * \see mgs_replace_log
2382 * \see mgs_clear_config_handler
2384 int jt_lcfg_clear(int argc, char **argv)
2387 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2388 struct obd_ioctl_data data;
2390 memset(&data, 0, sizeof(data));
2391 data.ioc_dev = get_mgs_device();
2395 data.ioc_inllen1 = strlen(argv[1]) + 1;
2396 data.ioc_inlbuf1 = argv[1];
2398 memset(buf, 0, sizeof(rawbuf));
2399 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2401 fprintf(stderr, "error: %s: invalid ioctl\n",
2402 jt_cmdname(argv[0]));
2406 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CLEAR_CONFIGS, buf);
2408 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
2409 strerror(rc = errno));
2415 int jt_lcfg_fork(int argc, char **argv)
2417 struct obd_ioctl_data data;
2418 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2424 memset(&data, 0, sizeof(data));
2425 data.ioc_dev = get_mgs_device();
2426 data.ioc_inllen1 = strlen(argv[1]) + 1;
2427 data.ioc_inlbuf1 = argv[1];
2428 data.ioc_inllen2 = strlen(argv[2]) + 1;
2429 data.ioc_inlbuf2 = argv[2];
2431 memset(buf, 0, sizeof(rawbuf));
2432 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2434 fprintf(stderr, "error: %s: invalid ioctl\n",
2435 jt_cmdname(argv[0]));
2439 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LCFG_FORK, buf);
2441 fprintf(stderr, "error: %s: OBD_IOC_LCFG_FORK failed: %s\n",
2442 jt_cmdname(argv[0]), strerror(errno));
2447 int jt_lcfg_erase(int argc, char **argv)
2449 struct obd_ioctl_data data;
2450 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2454 if (strncmp(argv[2], "-q", strlen("-q")) != 0 &&
2455 strncmp(argv[2], "--quiet", strlen("--quiet")) != 0)
2457 } else if (argc != 2) {
2461 memset(&data, 0, sizeof(data));
2462 data.ioc_dev = get_mgs_device();
2463 data.ioc_inllen1 = strlen(argv[1]) + 1;
2464 data.ioc_inlbuf1 = argv[1];
2466 memset(buf, 0, sizeof(rawbuf));
2467 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2469 fprintf(stderr, "error: %s: invalid ioctl\n",
2470 jt_cmdname(argv[0]));
2474 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LCFG_ERASE, buf);
2476 fprintf(stderr, "error: %s: OBD_IOC_LCFG_ERASE failed: %s\n",
2477 jt_cmdname(argv[0]), strerror(errno));
2481 #else /* !HAVE_SERVER_SUPPORT */
2482 int jt_lcfg_clear(int argc, char **argv)
2487 fprintf(stderr, "error: %s: invalid ioctl\n",
2488 jt_cmdname(argv[0]));
2492 int jt_lcfg_fork(int argc, char **argv)
2497 fprintf(stderr, "error: %s: invalid ioctl\n",
2498 jt_cmdname(argv[0]));
2502 int jt_lcfg_erase(int argc, char **argv)
2507 fprintf(stderr, "error: %s: invalid ioctl\n",
2508 jt_cmdname(argv[0]));
2511 #endif /* HAVE_SERVER_SUPPORT */
2513 enum llog_default_dev_op {
2514 LLOG_DFLT_MGS_SET = 0,
2518 static int llog_default_device(enum llog_default_dev_op op)
2521 static int dflt_dev = -1;
2523 if (op == LLOG_DFLT_MGS_SET && (cur_device == -1)) {
2524 char mgs[] = "$MGS";
2526 rc = do_device("llog_default_device", mgs);
2527 dflt_dev = cur_device;
2529 } else if (op == LLOG_DFLT_DEV_RESET && (dflt_dev != -1)) {
2530 do_disconnect(NULL, 1);
2537 static int llog_catlist_next(int index, char *buf, size_t buflen)
2539 struct obd_ioctl_data data;
2542 memset(&data, 0, sizeof(data));
2543 data.ioc_dev = cur_device;
2544 data.ioc_inllen1 = buflen - __ALIGN_KERNEL(sizeof(data), 8);
2545 data.ioc_count = index;
2546 memset(buf, 0, buflen);
2547 rc = llapi_ioctl_pack(&data, &buf, buflen);
2549 fprintf(stderr, "error: invalid llapi_ioctl_pack: %s\n",
2553 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CATLOGLIST, buf);
2555 fprintf(stderr, "OBD_IOC_CATLOGLIST failed: %s\n",
2559 return ((struct obd_ioctl_data *)buf)->ioc_count;
2562 int jt_llog_catlist(int argc, char **argv)
2564 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2571 if (llog_default_device(LLOG_DFLT_MGS_SET))
2572 return CMD_INCOMPLETE;
2575 start = llog_catlist_next(start, rawbuf, sizeof(rawbuf));
2578 tmp = ((struct obd_ioctl_data *)buf)->ioc_bulk;
2579 if (strlen(tmp) > 0)
2580 fprintf(stdout, "%s", tmp);
2585 llog_default_device(LLOG_DFLT_DEV_RESET);
2590 int jt_llog_info(int argc, char **argv)
2592 const struct option long_opts[] = {
2593 /* Allow optional "--catalog" for compatibility with llog commands. */
2594 { .val = 'c', .name = "catalog", .has_arg = required_argument },
2595 { .val = 'h', .name = "help", .has_arg = no_argument },
2597 struct obd_ioctl_data data = { 0 };
2598 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
2599 char *cmd = argv[0];
2600 char *catalog = NULL;
2603 while ((c = getopt_long(argc, argv, "c:h", long_opts, NULL)) != -1) {
2615 /* support "logname" positional parameter */
2619 "%s: catalog is set, unknown argument '%s'\n",
2624 } else if (!catalog || argc > 1) {
2628 /* Manage default device */
2629 if (llog_default_device(LLOG_DFLT_MGS_SET))
2630 return CMD_INCOMPLETE;
2632 data.ioc_dev = cur_device;
2633 data.ioc_inllen1 = strlen(catalog) + 1;
2634 data.ioc_inlbuf1 = catalog;
2635 data.ioc_inllen2 = sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(data), 8) -
2636 __ALIGN_KERNEL(data.ioc_inllen1, 8);
2637 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2639 fprintf(stderr, "%s: ioctl_pack failed for catalog '%s': %s\n",
2640 jt_cmdname(cmd), catalog, strerror(-rc));
2644 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_INFO, buf);
2646 fprintf(stdout, "%s", ((struct obd_ioctl_data *)buf)->ioc_bulk);
2648 fprintf(stderr, "%s: OBD_IOC_LLOG_INFO failed: %s\n",
2649 jt_cmdname(cmd), strerror(errno));
2652 llog_default_device(LLOG_DFLT_DEV_RESET);
2657 int jt_llog_print_cb(const char *record, void *private)
2659 printf("%s\n", record);
2665 llog_process_records(int (record_cb)(const char *record, void *private),
2666 const char *record, void *private, bool reverse)
2674 ptr = strchr(record, '\n');
2677 rc = record_cb(record, private);
2682 } while (ptr && *(ptr + 1));
2684 tmp = (char *)record;
2686 ptr = strrchr(record, '\n');
2691 while ((ptr = strrchr(record, '\n'))) {
2694 rc = record_cb(tmp, private);
2698 rc = record_cb(record, private);
2707 * Iterate over llog records, typically YAML-formatted configuration logs
2709 * \param logname[in] name of llog file or FID
2710 * \param start[in] first record to process
2711 * \param end[in] last record to process (inclusive)
2712 * \param cb[in] callback for records. Return -ve error, or +ve abort.
2713 * \param private[in,out] private data passed to the \a record_cb function
2714 * \param reverse[in] print the llog records from the beginning or the end
2716 * \retval 0 on success
2717 * others handled by the caller
2719 int jt_llog_print_iter(char *logname, long start, long end,
2720 int (record_cb)(const char *record, void *private),
2721 void *private, bool reverse, bool raw)
2723 struct obd_ioctl_data data = { 0 };
2724 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2725 char startbuf[16], endbuf[16];
2726 static long inc = sizeof(rawbuf) / 128;
2730 /* default end of indexes is max indexes in a llog bitmap */
2732 end = LLOG_MIN_CHUNK_SIZE * 8 - 1;
2734 data.ioc_dev = cur_device;
2735 data.ioc_inlbuf1 = logname;
2736 data.ioc_inllen1 = strlen(logname) + 1;
2739 * Estimate about 128 characters per configuration record. Not all
2740 * records will be printed in any case, so they should easily fit. If
2741 * not, the kernel will return -EOVERFLOW and ask for fewer records.
2743 * We don't want to request records from the kernel one-at-a-time, as
2744 * it restarts the config llog iteration from the beginning, so we
2745 * fetch multiple records from the kernel per call and split locally.
2747 for (rec = start; rec < end; rec += inc) {
2748 char *record = ((struct obd_ioctl_data *)buf)->ioc_bulk;
2749 __u32 *is_llog_eof = &((struct obd_ioctl_data *)buf)->ioc_u32_2;
2752 snprintf(startbuf, sizeof(startbuf), "%lu", rec);
2753 snprintf(endbuf, sizeof(endbuf), "%lu",
2754 end < rec + inc - 1 ? end : rec + inc - 1);
2756 data.ioc_u32_1 = raw ? 1 : 0;
2757 /* start and end record numbers are passed as ASCII digits */
2758 data.ioc_inlbuf2 = startbuf;
2759 data.ioc_inllen2 = strlen(startbuf) + 1;
2760 data.ioc_inlbuf3 = endbuf;
2761 data.ioc_inllen3 = strlen(endbuf) + 1;
2763 data.ioc_inllen4 = sizeof(rawbuf) -
2764 __ALIGN_KERNEL(sizeof(data), 8) -
2765 __ALIGN_KERNEL(data.ioc_inllen1, 8) -
2766 __ALIGN_KERNEL(data.ioc_inllen2, 8) -
2767 __ALIGN_KERNEL(data.ioc_inllen3, 8);
2768 memset(buf, 0, sizeof(rawbuf));
2769 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2771 fprintf(stderr, "%s: invalid ioctl data\n", logname);
2775 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_PRINT, buf);
2776 if (rc == -EOVERFLOW && inc > 2) {
2781 fprintf(stderr, "%s: OBD_IOC_LLOG_PRINT failed: %s\n",
2782 logname, strerror(errno));
2787 /* record was not modified -> all indexes are skipped */
2788 if (strcmp(record, logname) != 0)
2789 rc = llog_process_records(record_cb, record, private,
2794 /* end of llog file ? */
2803 static int llog_parse_catalog_options(int *argc, char ***argv, char **catalog,
2804 long *start, long *end, int *raw)
2806 const struct option long_opts[] = {
2807 /* the --catalog option is not required, just for consistency */
2808 { .val = 'c', .name = "catalog", .has_arg = required_argument },
2809 { .val = 'e', .name = "end", .has_arg = required_argument },
2810 { .val = 'h', .name = "help", .has_arg = no_argument },
2811 { .val = 'r', .name = "raw", .has_arg = no_argument },
2812 { .val = 's', .name = "start", .has_arg = required_argument },
2814 char *cmd = (*argv)[0];
2818 if (!catalog || !start || !end)
2821 /* now process command line arguments*/
2822 while ((c = getopt_long(*argc, *argv, "c:e:hrs:",
2823 long_opts, NULL)) != -1) {
2829 *end = strtol(optarg, &endp, 0);
2830 if (*endp != '\0') {
2831 fprintf(stderr, "%s: bad end value '%s'\n",
2842 *start = strtol(optarg, &endp, 0);
2843 if (*endp != '\0') {
2844 fprintf(stderr, "%s: bad start value '%s'\n",
2858 * support old optional positional parameters only if they were
2859 * not already specified with named arguments: logname [start [end]]
2864 "%s: logname is set, unknown argument '%s'\n",
2868 *catalog = (*argv)[0];
2873 if (*catalog == NULL) {
2874 fprintf(stderr, "%s: no logname specified\n", cmd);
2881 "%s: --start is set, unknown argument '%s'\n",
2886 *start = strtol((*argv)[0], &endp, 0);
2887 if (*endp != '\0') {
2888 fprintf(stderr, "%s: bad start value '%s'\n",
2898 "%s: --end is set, unknown argument '%s'\n",
2903 *end = strtol((*argv)[0], &endp, 0);
2904 if (*endp != '\0') {
2905 fprintf(stderr, "%s: bad end value '%s'\n",
2913 fprintf(stderr, "%s: unknown argument '%s'\n", cmd, (*argv)[0]);
2917 if (*end != -1 && *end < *start) {
2918 fprintf(stderr, "%s: end '%lu' less than than start '%lu'\n",
2926 int jt_llog_print(int argc, char **argv)
2928 char *catalog = NULL;
2929 long start = 1, end = -1;
2933 rc = llog_parse_catalog_options(&argc, &argv, &catalog, &start, &end,
2938 if (llog_default_device(LLOG_DFLT_MGS_SET))
2939 return CMD_INCOMPLETE;
2941 rc = jt_llog_print_iter(catalog, start, end, jt_llog_print_cb,
2942 NULL, false, !!raw);
2944 llog_default_device(LLOG_DFLT_DEV_RESET);
2950 * Parse catalog, log ID, and optionally a log index with either optional
2951 * arguments or positional arguments. Only the initial catalog argument
2952 * may be positional with other optional arguments.
2954 * The positional arguments option should eventually be phased out.
2956 static int llog_parse_catalog_log_idx(int *argc, char ***argv, const char *opts,
2957 struct obd_ioctl_data *data)
2959 const struct option long_opts[] = {
2960 /* the --catalog option is not required, just for consistency */
2961 { .val = 'c', .name = "catalog", .has_arg = required_argument },
2962 { .val = 'h', .name = "help", .has_arg = no_argument },
2963 { .val = 'i', .name = "log_idx", .has_arg = required_argument },
2964 { .val = 'l', .name = "log_id", .has_arg = required_argument },
2969 if (!data || *argc <= 1)
2972 data->ioc_dev = cur_device;
2974 /* now process command line arguments*/
2975 while ((c = getopt_long(*argc, *argv, opts, long_opts, NULL)) != -1) {
2978 data->ioc_inllen1 = strlen(optarg) + 1;
2979 data->ioc_inlbuf1 = optarg;
2982 data->ioc_inllen3 = strlen(optarg) + 1;
2983 data->ioc_inlbuf3 = optarg;
2985 case 'l': /* The log_id option isn't currently needed for
2986 * cancel as mdt_iocontrol() handles IOC_LLOG_CANCEL,
2987 * but we may as well keep it for now.
2989 data->ioc_inllen2 = strlen(optarg) + 1;
2990 data->ioc_inlbuf2 = optarg;
3001 /* Allow catalog to be specified as first option without --catalog */
3002 if (!data->ioc_inlbuf1 && *argc > 0) {
3003 data->ioc_inlbuf1 = (*argv)[0];
3004 data->ioc_inllen1 = strlen((*argv)[0]) + 1;
3012 int jt_llog_cancel(int argc, char **argv)
3014 struct obd_ioctl_data data = { 0 };
3015 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
3016 char *cmd = argv[0];
3019 /* Manage default device */
3020 if (llog_default_device(LLOG_DFLT_MGS_SET))
3021 return CMD_INCOMPLETE;
3023 /* Parse catalog file (in inlbuf1) and named parameters */
3024 rc = llog_parse_catalog_log_idx(&argc, &argv, "c:hi:l:", &data);
3027 * Handle old positional parameters if not using named parameters,
3028 * either "<catalog> <log_idx>" or "<catalog> <log_id> <log_idx>".
3029 * It was "inlbuf3 = log_idx", and "inlbuf2 = log_id" (ignored by
3030 * config log cancel), and shows why I hate positional parameters.
3033 data.ioc_inllen3 = strlen(argv[0]) + 1;
3034 data.ioc_inlbuf3 = argv[0];
3035 } else if (argc == 2) {
3036 data.ioc_inllen2 = strlen(argv[0]) + 1;
3037 data.ioc_inlbuf2 = argv[0];
3038 data.ioc_inllen3 = strlen(argv[1]) + 1;
3039 data.ioc_inlbuf3 = argv[1];
3042 if (!data.ioc_inlbuf1 || !data.ioc_inlbuf3) {
3043 /* missing mandatory parameters */
3048 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3050 fprintf(stderr, "%s: ioctl_pack for catalog '%s' failed: %s\n",
3051 jt_cmdname(cmd), data.ioc_inlbuf1, strerror(-rc));
3055 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CANCEL, buf);
3057 fprintf(stderr, "%s: cancel catalog '%s:%s' failed: %s\n",
3058 jt_cmdname(cmd), data.ioc_inlbuf1, data.ioc_inlbuf3,
3062 llog_default_device(LLOG_DFLT_DEV_RESET);
3066 int jt_llog_check(int argc, char **argv)
3068 struct obd_ioctl_data data = { 0 };
3069 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
3070 char *catalog = NULL;
3071 char startbuf[16], endbuf[16];
3072 long start = 1, end = -1;
3073 char *cmd = argv[0];
3076 rc = llog_parse_catalog_options(&argc, &argv, &catalog, &start,
3081 if (llog_default_device(LLOG_DFLT_MGS_SET))
3082 return CMD_INCOMPLETE;
3087 data.ioc_dev = cur_device;
3088 data.ioc_inllen1 = strlen(catalog) + 1;
3089 data.ioc_inlbuf1 = catalog;
3091 snprintf(startbuf, sizeof(startbuf), "%lu", start);
3092 snprintf(endbuf, sizeof(endbuf), "%lu", end);
3093 /* start and end record numbers are passed as ASCII digits */
3094 data.ioc_inllen2 = strlen(startbuf) + 1;
3095 data.ioc_inlbuf2 = startbuf;
3096 data.ioc_inllen3 = strlen(endbuf) + 1;
3097 data.ioc_inlbuf3 = endbuf;
3099 data.ioc_inllen4 = sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(data), 8) -
3100 __ALIGN_KERNEL(data.ioc_inllen1, 8) -
3101 __ALIGN_KERNEL(data.ioc_inllen2, 8) -
3102 __ALIGN_KERNEL(data.ioc_inllen3, 8);
3103 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3105 fprintf(stderr, "%s: ioctl_pack failed for catalog '%s': %s\n",
3106 jt_cmdname(cmd), data.ioc_inlbuf1, strerror(-rc));
3110 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CHECK, buf);
3112 fprintf(stdout, "%s", ((struct obd_ioctl_data *)buf)->ioc_bulk);
3114 fprintf(stderr, "%s: OBD_IOC_LLOG_CHECK failed: %s\n",
3115 jt_cmdname(cmd), strerror(errno));
3117 llog_default_device(LLOG_DFLT_DEV_RESET);
3121 int jt_llog_remove(int argc, char **argv)
3123 struct obd_ioctl_data data = { 0 };
3124 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
3125 char *cmd = argv[0];
3128 if (llog_default_device(LLOG_DFLT_MGS_SET))
3129 return CMD_INCOMPLETE;
3131 rc = llog_parse_catalog_log_idx(&argc, &argv, "c:hl:", &data);
3136 if (data.ioc_inlbuf2) {
3138 "%s: --log_id is set, unknown argument '%s'\n",
3139 jt_cmdname(cmd), argv[0]);
3144 data.ioc_inllen2 = strlen(argv[0]) + 1;
3145 data.ioc_inlbuf2 = argv[0];
3148 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3150 fprintf(stderr, "%s: ioctl_pack for catalog '%s' failed: %s\n",
3151 jt_cmdname(cmd), data.ioc_inlbuf1, strerror(-rc));
3155 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_REMOVE, buf);
3157 fprintf(stderr, "%s: cancel catalog '%s:%s' failed: %s\n",
3158 jt_cmdname(cmd), data.ioc_inlbuf1, data.ioc_inlbuf2,
3162 llog_default_device(LLOG_DFLT_DEV_RESET);
3166 static void signal_server(int sig)
3168 if (sig == SIGINT) {
3169 do_disconnect("sigint", 1);
3172 fprintf(stderr, "%s: got signal %d\n", jt_cmdname("sigint"),
3177 int obd_initialize(int argc, char **argv)
3179 register_ioc_dev(OBD_DEV_ID, OBD_DEV_PATH);
3184 void obd_finalize(int argc, char **argv)
3186 struct sigaction sigact;
3188 /* sigact initialization */
3189 sigact.sa_handler = signal_server;
3190 sigfillset(&sigact.sa_mask);
3191 sigact.sa_flags = SA_RESTART;
3192 /* coverity[uninit_use_in_call] */
3193 sigaction(SIGINT, &sigact, NULL);
3196 do_disconnect(argv[0], 1);
3200 * Get the index of the last llog record
3202 * logid: [0x3:0xa:0x0]:0
3207 * \param logname[in] pointer to config log name
3209 * \retval > 0 on success
3212 static long llog_last_index(char *logname)
3214 struct obd_ioctl_data data = { 0 };
3215 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
3219 data.ioc_dev = cur_device;
3220 data.ioc_inllen1 = strlen(logname) + 1;
3221 data.ioc_inlbuf1 = logname;
3222 data.ioc_inllen2 = sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(data), 8) -
3223 __ALIGN_KERNEL(data.ioc_inllen1, 8);
3224 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3226 fprintf(stderr, "%s: ioctl_pack failed for catalog '%s': %s\n",
3227 __func__, logname, strerror(-rc));
3231 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_INFO, buf);
3233 last_index = strstr(((struct obd_ioctl_data *)buf)->ioc_bulk,
3235 return strtol(last_index + 11, NULL, 10);
3243 static char *get_llog_event_name(__u32 cmd)
3245 #ifdef HAVE_SERVER_SUPPORT
3246 struct lcfg_type_data *data;
3248 data = lcfg_cmd2data(cmd);
3250 return data->ltd_name;
3255 static char *get_event_filter(__u32 cmd)
3258 char *filter = NULL;
3261 event_name = get_llog_event_name(cmd);
3263 /* 9 bytes for "event: , " */
3264 len = 9 + strlen(event_name);
3265 filter = malloc(len + 1);
3268 memset(filter, 0, len + 1);
3269 snprintf(filter, len, "event: %s, ", event_name);
3277 * Callback to search ostname in llog
3278 * - { index: 23, event: attach, device: lustre-OST0000-osc, type: osc,
3279 * UUID: lustre-clilov_UUID }
3280 * - { index: 24, event: setup, device: lustre-OST0000-osc,
3281 * UUID: lustre-OST0000_UUID, node: 192.168.0.120@tcp }
3282 * - { index: 25, event: add_osc, device: lustre-clilov,
3283 * ost: lustre-OST0000_UUID, index: 0, gen: 1 }
3285 * \param record[in] pointer to llog record
3286 * \param data[in] pointer to ostname
3288 * \retval 1 if ostname is found
3289 * 0 if ostname is not found
3290 * -ENOENT if ostname is deleted
3292 static int llog_search_ost_cb(const char *record, void *data)
3294 char *ostname = data;
3295 char ost_filter[MAX_STRING_SIZE] = {'\0'};
3296 char *add_osc, *del_osc, *setup, *cleanup;
3298 add_osc = get_event_filter(LCFG_LOV_ADD_OBD);
3299 del_osc = get_event_filter(LCFG_LOV_DEL_OBD);
3300 setup = get_event_filter(LCFG_SETUP);
3301 cleanup = get_event_filter(LCFG_CLEANUP);
3302 if (!add_osc || !del_osc || !setup || !cleanup)
3305 if (ostname && ostname[0])
3306 snprintf(ost_filter, sizeof(ost_filter), " %s,", ostname);
3308 if (strstr(record, ost_filter)) {
3309 if (strstr(record, add_osc) || strstr(record, setup))
3311 if (strstr(record, del_osc) || strstr(record, cleanup))
3324 * Search ost in llog
3326 * \param logname[in] pointer to config log name
3327 * \param last_index[in] the index of the last llog record
3328 * \param ostname[in] pointer to ost name
3330 * \retval 1 if ostname is found
3331 * 0 if ostname is not found
3333 static int llog_search_ost(char *logname, long last_index, char *ostname)
3335 long start, end, inc = MAX_IOC_BUFLEN / 128;
3338 for (end = last_index; end > 1; end -= inc) {
3339 start = end - inc > 0 ? end - inc : 1;
3340 rc = jt_llog_print_iter(logname, start, end, llog_search_ost_cb,
3341 ostname, true, false);
3346 return (rc == 1 ? 1 : 0);
3349 struct llog_del_ost_priv {
3357 * Callback to search and delete ostname in llog
3359 * \param record[in] pointer to llog record
3360 * \param data[in] pointer to ostname
3362 * \retval 1 if ostname is found and entry deleted
3363 * 0 if ostname is not found
3366 static int llog_del_ost_cb(const char *record, void *data)
3368 char ost_filter[MAX_STRING_SIZE] = {'\0'};
3369 char log_idxstr[MAX_STRING_SIZE] = {'\0'};
3370 long int log_idx = 0;
3371 struct llog_del_ost_priv *priv = data;
3372 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
3373 struct obd_ioctl_data ioc_data = { 0 };
3376 if (priv->ostname && priv->ostname[0])
3377 snprintf(ost_filter, sizeof(ost_filter), " %s", priv->ostname);
3379 if (!strstr(record, ost_filter))
3382 rc = sscanf(record, "- { index: %ld", &log_idx);
3384 fprintf(stderr, "error: record without index:\n%s\n",
3388 snprintf(log_idxstr, sizeof(log_idxstr), "%ld", log_idx);
3390 ioc_data.ioc_dev = cur_device;
3391 ioc_data.ioc_inllen1 = strlen(priv->logname) + 1;
3392 ioc_data.ioc_inlbuf1 = priv->logname;
3393 ioc_data.ioc_inllen3 = strlen(log_idxstr) + 1;
3394 ioc_data.ioc_inlbuf3 = log_idxstr;
3396 rc = llapi_ioctl_pack(&ioc_data, &buf, sizeof(rawbuf));
3398 fprintf(stderr, "ioctl_pack for catalog '%s' failed: %s\n",
3399 ioc_data.ioc_inlbuf1, strerror(-rc));
3404 fprintf(stdout, "[DRY RUN] cancel catalog '%s:%s':\"%s\"\n",
3405 ioc_data.ioc_inlbuf1, ioc_data.ioc_inlbuf3, record);
3407 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CANCEL, buf);
3409 fprintf(stderr, "cancel catalog '%s:%s' failed: %s\n",
3410 ioc_data.ioc_inlbuf1, ioc_data.ioc_inlbuf3,
3413 fprintf(stdout, "cancel catalog %s log_idx %ld: done\n",
3414 priv->logname, log_idx);
3422 * Search and delete ost in llog
3424 * \param logname[in] pointer to config log name
3425 * \param last_index[in] the index of the last llog record
3426 * \param ostname[in] pointer to ost name
3427 * \param dryrun[in] dry run?
3429 * \retval 1 if ostname is found and deleted
3430 * 0 if ostname is not found
3432 static int llog_del_ost(char *logname, long last_index, char *ostname,
3435 long start, end, inc = MAX_IOC_BUFLEN / 128;
3437 struct llog_del_ost_priv priv = { logname, ostname, false, dryrun };
3439 for (end = last_index; end > 1; end -= inc) {
3440 start = end - inc > 0 ? end - inc : 1;
3441 rc = jt_llog_print_iter(logname, start, end, llog_del_ost_cb,
3442 &priv, true, false);
3448 fprintf(stdout, "del_ost: cancelled %d catalog entries\n",
3451 fprintf(stdout, "del_ost: no catalog entry deleted\n");
3456 struct llog_pool_data {
3457 char lpd_fsname[LUSTRE_MAXFSNAME + 1];
3458 char lpd_poolname[LOV_MAXPOOLNAME + 1];
3459 char lpd_ostname[MAX_OBD_NAME + 1];
3460 enum lcfg_command_type lpd_cmd_type;
3461 bool lpd_pool_exists;
3466 * Called for each formatted line in the config log (within range).
3468 * - { index: 74, event: new_pool, device: tfs-clilov, fsname: tfs, pool: tmp }
3469 * - { index: 77, event: add_pool, device: tfs-clilov, fsname: tfs, pool: tmp,
3470 * ost: tfs-OST0000_UUID }
3471 * - { index: 224, event: remove_pool, device: tfs-clilov, fsname: tfs,
3472 * pool: tmp, ost: tfs-OST0003_UUID }
3473 * - { index: 227, event: del_pool, device: tfs-clilov, fsname: tfs, pool: tmp }
3475 * \param record[in] pointer to llog record
3476 * \param data[in] pointer to llog_pool_data
3478 * \retval 1 if pool or OST is found
3479 * 0 if pool or OST is not found
3480 * -ENOENT if pool or OST is removed
3482 static int llog_search_pool_cb(const char *record, void *data)
3484 struct llog_pool_data *lpd = data;
3485 char pool_filter[MAX_STRING_SIZE] = "";
3486 char *new_pool, *del_pool, *add_pool, *rem_pool;
3488 int fs_pool_len = 0, rc = 0;
3490 new_pool = get_event_filter(LCFG_POOL_NEW);
3491 del_pool = get_event_filter(LCFG_POOL_DEL);
3492 add_pool = get_event_filter(LCFG_POOL_ADD);
3493 rem_pool = get_event_filter(LCFG_POOL_REM);
3494 if (!new_pool || !del_pool || !add_pool || !rem_pool) {
3499 fs_pool_len = 16 + strlen(lpd->lpd_fsname) + strlen(lpd->lpd_poolname);
3500 snprintf(pool_filter, fs_pool_len + 1, "fsname: %s, pool: %s",
3501 lpd->lpd_fsname, lpd->lpd_poolname);
3503 /* search poolname */
3504 found = strstr(record, pool_filter);
3506 (found[fs_pool_len] == ' ' || found[fs_pool_len] == ',')) {
3507 if (strstr(record, new_pool)) {
3508 lpd->lpd_pool_exists = true;
3512 if (strstr(record, del_pool)) {
3513 lpd->lpd_pool_exists = false;
3518 if (lpd->lpd_cmd_type == LCFG_POOL_NEW ||
3519 lpd->lpd_cmd_type == LCFG_POOL_DEL) {
3520 /* In function mgs_pool_cmd(), a pool's pool_new/add
3521 * record will be marked as "SKIP" if its pool_destroy/
3522 * remove record is logged later. That means the "SKIP"
3523 * record won't be printed here and thus lpd_ost_num
3524 * doesn't need to be decreased as well.
3526 if (strstr(record, add_pool))
3528 } else if (lpd->lpd_ostname[0] != '\0') {
3529 if (strstr(record, lpd->lpd_ostname)) {
3530 lpd->lpd_pool_exists = true;
3531 if (strstr(record, add_pool)) {
3532 lpd->lpd_ost_num = 1;
3536 if (strstr(record, rem_pool)) {
3537 lpd->lpd_ost_num = 0;
3557 /* Search pool and its ost in llog
3559 * \param logname[in] pointer to config log name
3560 * \param last_index[in] the index of the last llog record
3561 * \param fsname[in] pointer to filesystem name
3562 * \param poolname[in] pointer pool name
3563 * \param ostname[in] pointer to OST name(OSTnnnn-UUID)
3564 * \param cmd[in] pool command type
3566 * \retval < 0 on error
3567 * 0 if pool is empty or OST is not found
3568 * 1 if pool is not empty or OST is found
3570 static int llog_search_pool(char *logname, long last_index, char *fsname,
3571 char *poolname, char *ostname,
3572 enum lcfg_command_type cmd)
3574 struct llog_pool_data lpd;
3575 long start, end, inc = MAX_IOC_BUFLEN / 128;
3578 memset(&lpd, 0, sizeof(lpd));
3579 lpd.lpd_cmd_type = cmd;
3580 lpd.lpd_pool_exists = false;
3581 lpd.lpd_ost_num = 0;
3582 strncpy(lpd.lpd_fsname, fsname, sizeof(lpd.lpd_fsname) - 1);
3583 if (poolname && poolname[0])
3584 strncpy(lpd.lpd_poolname, poolname,
3585 sizeof(lpd.lpd_poolname) - 1);
3586 if (ostname && ostname[0])
3587 strncpy(lpd.lpd_ostname, ostname, sizeof(lpd.lpd_ostname) - 1);
3589 for (end = last_index; end > 1; end -= inc) {
3590 start = end - inc > 0 ? end - inc : 1;
3591 rc = jt_llog_print_iter(logname, start, end,
3592 llog_search_pool_cb, &lpd, true, false);
3594 if (rc == 1 && lpd.lpd_pool_exists)
3595 rc = lpd.lpd_ost_num ? 1 : 0;
3596 else if (rc == -ENOENT && lpd.lpd_pool_exists &&
3608 static bool combined_mgs_mds(char *fsname)
3613 rc = cfs_get_param_paths(&path, "mdt/%s-MDT0000", fsname);
3615 cfs_free_param_data(&path);
3617 if (get_mgs_device() > 0 && !rc)
3624 * if pool is NULL, search ostname in target_obd
3625 * if pool is not NULL:
3626 * - if pool not found returns errno < 0
3627 * - if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
3628 * - if ostname is not NULL, returns 1 if OST is in pool and 0 if not
3630 int lctl_search_ost(char *fsname, char *poolname, char *ostname,
3631 enum lcfg_command_type cmd)
3633 char logname[MAX_OBD_NAME] = {'\0'};
3636 if (fsname && fsname[0] == '\0')
3641 if (combined_mgs_mds(fsname))
3642 return llapi_search_ost(fsname, poolname, ostname);
3644 /* fetch the last_index of llog record */
3645 snprintf(logname, sizeof(logname), "%s-client", fsname);
3646 last_index = llog_last_index(logname);
3650 /* if pool is NULL, search ostname in target_obd */
3651 if (!poolname && ostname)
3652 return llog_search_ost(logname, last_index, ostname);
3654 return llog_search_pool(logname, last_index, fsname, poolname,
3658 static int check_pool_cmd(enum lcfg_command_type cmd, char *fsname,
3659 char *poolname, char *ostname)
3663 rc = lctl_search_ost(fsname, poolname, ostname, cmd);
3664 if (rc < 0 && (cmd != LCFG_POOL_NEW)) {
3665 fprintf(stderr, "Pool %s.%s not found\n",
3671 case LCFG_POOL_NEW: {
3676 fprintf(stderr, "Pool %s.%s already exists\n",
3682 case LCFG_POOL_DEL: {
3688 "Pool %s.%s not empty, please remove all members\n",
3694 case LCFG_POOL_ADD: {
3696 fprintf(stderr, "OST %s is already in pool %s.%s\n",
3697 ostname, fsname, poolname);
3700 rc = lctl_search_ost(fsname, NULL, ostname, cmd);
3702 fprintf(stderr, "OST %s is not part of the '%s' fs.\n",
3708 case LCFG_POOL_REM: {
3710 fprintf(stderr, "OST %s not found in pool %s.%s\n",
3711 ostname, fsname, poolname);
3723 * This check only verifies that the changes have been "pushed out" to
3724 * the client successfully. This involves waiting for a config update,
3725 * and so may fail because of problems in that code or post-command
3726 * network loss. So reporting a warning is appropriate, but not a failure.
3728 static int check_pool_cmd_result(enum lcfg_command_type cmd, char *fsname,
3729 char *poolname, char *ostname)
3735 case LCFG_POOL_NEW: {
3737 rc = lctl_search_ost(fsname, poolname, NULL, cmd);
3743 } while ((rc < 0) && (cpt > 0));
3745 fprintf(stderr, "Pool %s.%s created\n",
3750 fprintf(stderr, "Warning, pool %s.%s not found\n", fsname,
3754 case LCFG_POOL_DEL: {
3756 rc = lctl_search_ost(fsname, poolname, NULL, cmd);
3762 } while ((rc >= 0) && (cpt > 0));
3764 fprintf(stderr, "Pool %s.%s destroyed\n",
3769 fprintf(stderr, "Warning, pool %s.%s still found\n", fsname,
3773 case LCFG_POOL_ADD: {
3775 rc = lctl_search_ost(fsname, poolname, ostname, cmd);
3781 } while ((rc != 1) && (cpt > 0));
3783 fprintf(stderr, "OST %s added to pool %s.%s\n",
3784 ostname, fsname, poolname);
3787 fprintf(stderr, "Warning, OST %s not found in pool %s.%s\n",
3788 ostname, fsname, poolname);
3791 case LCFG_POOL_REM: {
3793 rc = lctl_search_ost(fsname, poolname, ostname, cmd);
3799 } while ((rc == 1) && (cpt > 0));
3801 fprintf(stderr, "OST %s removed from pool %s.%s\n",
3802 ostname, fsname, poolname);
3805 fprintf(stderr, "Warning, OST %s still found in pool %s.%s\n",
3806 ostname, fsname, poolname);
3815 static int check_and_complete_ostname(char *fsname, char *ostname)
3818 char real_ostname[MAX_OBD_NAME + 1];
3821 /* if OST name does not start with fsname, we add it */
3822 /* if not check if the fsname is the right one */
3823 ptr = strchr(ostname, '-');
3825 sprintf(real_ostname, "%s-%s", fsname, ostname);
3826 } else if (strncmp(ostname, fsname, strlen(fsname)) != 0) {
3827 fprintf(stderr, "%s does not start with fsname %s\n",
3831 if (strlen(ostname) > sizeof(real_ostname) - 1)
3834 strncpy(real_ostname, ostname, sizeof(real_ostname));
3837 /* real_ostname is fsname-????? */
3838 ptr = real_ostname + strlen(fsname) + 1;
3839 if (strncmp(ptr, "OST", 3) != 0) {
3840 fprintf(stderr, "%s does not start by %s-OST nor OST\n",
3844 /* real_ostname is fsname-OST????? */
3846 for (i = 0; i < 4; i++) {
3847 if (!isxdigit(*ptr)) {
3849 "ost's index in %s is not an hexa number\n",
3855 /* real_ostname is fsname-OSTXXXX????? */
3856 /* if OST name does not end with _UUID, we add it */
3858 strcat(real_ostname, "_UUID");
3859 } else if (strcmp(ptr, "_UUID") != 0) {
3861 "ostname %s does not end with _UUID\n", ostname);
3864 /* real_ostname is fsname-OSTXXXX_UUID */
3865 strcpy(ostname, real_ostname);
3869 /* returns 0 or -errno */
3870 static int pool_cmd(enum lcfg_command_type cmd, char *cmdname,
3871 char *fullpoolname, char *fsname, char *poolname,
3875 struct obd_ioctl_data data;
3876 struct lustre_cfg_bufs bufs;
3877 struct lustre_cfg *lcfg;
3878 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
3880 rc = check_pool_cmd(cmd, fsname, poolname, ostname);
3883 "Can't verify pool command since there is no local MDT or client, proceeding anyhow...\n");
3887 lustre_cfg_bufs_reset(&bufs, NULL);
3888 lustre_cfg_bufs_set_string(&bufs, 0, cmdname);
3889 lustre_cfg_bufs_set_string(&bufs, 1, fullpoolname);
3891 lustre_cfg_bufs_set_string(&bufs, 2, ostname);
3893 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
3896 lustre_cfg_init(lcfg, cmd, &bufs);
3898 memset(&data, 0, sizeof(data));
3899 rc = data.ioc_dev = get_mgs_device();
3903 data.ioc_type = LUSTRE_CFG_TYPE;
3904 data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
3905 lcfg->lcfg_buflens);
3906 data.ioc_pbuf1 = (void *)lcfg;
3908 memset(buf, 0, sizeof(rawbuf));
3909 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3911 fprintf(stderr, "error: %s: invalid ioctl\n",
3912 jt_cmdname(cmdname));
3916 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_POOL, buf);
3923 "error: %s: either the pool or file system name is too long (max pool name len is %d and file system name is %d)\n",
3924 jt_cmdname(cmdname), LOV_MAXPOOLNAME, LUSTRE_MAXFSNAME);
3928 "error: %s can contain only alphanumeric characters, underscores, and dashes besides the required '.'\n",
3929 jt_cmdname(cmdname));
3937 int jt_del_ost(int argc, char **argv)
3939 char *fsname = NULL, *ptr, *logname;
3940 char mdtpattern[16], clipattern[16];
3941 char ostname[MAX_OBD_NAME + 1];
3944 int rc, start = 0, dryrun = 0;
3947 static struct option long_opts[] = {
3948 { .val = 'h', .name = "help", .has_arg = no_argument },
3949 { .val = 'n', .name = "dryrun", .has_arg = no_argument },
3950 { .val = 't', .name = "target", .has_arg = required_argument },
3953 while ((c = getopt_long(argc, argv, "hnt:", long_opts, NULL)) != -1) {
3956 fsname = strdup(optarg);
3971 if (llog_default_device(LLOG_DFLT_MGS_SET)) {
3972 rc = CMD_INCOMPLETE;
3976 ptr = strstr(fsname, "-OST");
3983 fprintf(stdout, "del_ost: dry run for target %s\n", fsname);
3986 rc = sscanf(ptr, "OST%04x", &index);
3992 if (strlen(ptr) > sizeof(ostname) - 1) {
3997 snprintf(mdtpattern, sizeof(mdtpattern), "%s-MDT", fsname);
3998 snprintf(clipattern, sizeof(clipattern), "%s-client", fsname);
3999 snprintf(ostname, sizeof(ostname), "%s-%s", fsname, ptr);
4002 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
4005 start = llog_catlist_next(start, rawbuf, sizeof(rawbuf));
4008 begin = ((struct obd_ioctl_data *)buf)->ioc_bulk;
4009 if (strlen(begin) == 0)
4012 while ((end = strchr(begin, '\n'))) {
4014 logname = strstr(begin, "config_log: ");
4016 if (logname && (strstr(logname, mdtpattern) ||
4017 strstr(logname, clipattern))) {
4020 fprintf(stdout, "config_log: %s\n", logname);
4022 last_index = llog_last_index(logname);
4023 if (last_index < 0) {
4025 "error with catalog %s: %s\n",
4026 logname, strerror(-last_index));
4030 rc = llog_del_ost(logname, last_index, ostname,
4040 llog_default_device(LLOG_DFLT_DEV_RESET);
4046 #ifdef HAVE_SERVER_SUPPORT
4048 * Format and send the ioctl to the MGS.
4050 * \param cmd IOCTL to send
4051 * \param ret_data void pointer to return anything from
4053 * \param num_args number of arguments to pack into the
4055 * \param argv[] variable number of string arguments
4057 * \retval 0 on success
4059 static int nodemap_cmd(enum lcfg_command_type cmd, void *ret_data,
4060 unsigned int ret_size, ...)
4065 struct lustre_cfg_bufs bufs;
4066 struct obd_ioctl_data data;
4067 struct lustre_cfg *lcfg;
4068 char rawbuf[MAX_IOC_BUFLEN];
4072 lustre_cfg_bufs_reset(&bufs, NULL);
4074 va_start(ap, ret_size);
4075 arg = va_arg(ap, char *);
4077 lustre_cfg_bufs_set_string(&bufs, i, arg);
4079 arg = va_arg(ap, char *);
4083 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
4086 lustre_cfg_init(lcfg, cmd, &bufs);
4088 memset(&data, 0, sizeof(data));
4089 rc = data.ioc_dev = get_mgs_device();
4093 data.ioc_type = LUSTRE_CFG_TYPE;
4094 data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
4095 lcfg->lcfg_buflens);
4096 data.ioc_pbuf1 = (void *)lcfg;
4098 memset(buf, 0, sizeof(rawbuf));
4099 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
4102 "error: invalid ioctl request: %08x errno: %d: %s\n",
4103 cmd, errno, strerror(-rc));
4107 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_NODEMAP, buf);
4110 "error: invalid ioctl: %08x errno: %d: %s\n",
4111 cmd, errno, strerror(errno));
4116 rc = llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
4120 if (ret_size > data.ioc_plen1)
4121 ret_size = data.ioc_plen1;
4123 memcpy(ret_data, data.ioc_pbuf1, ret_size);
4132 * activate nodemap functions
4134 * \param argc number of args
4135 * \param argv[] variable string arguments
4137 * argv[0] 1 for activate or 0 for deactivate
4139 * \retval 0 on success
4141 int jt_nodemap_activate(int argc, char **argv)
4145 rc = nodemap_cmd(LCFG_NODEMAP_ACTIVATE, NULL, 0, argv[0], argv[1],
4159 * \param argc number of args
4160 * \param argv[] variable string arguments
4162 * argv[0] nodemap name
4164 * \retval 0 on success
4166 int jt_nodemap_add(int argc, char **argv)
4170 rc = llapi_nodemap_exists(argv[1]);
4172 fprintf(stderr, "error: %s existing nodemap name\n", argv[1]);
4176 rc = nodemap_cmd(LCFG_NODEMAP_ADD, NULL, 0, argv[0], argv[1], NULL);
4189 * \param argc number of args
4190 * \param argv[] variable string arguments
4192 * argv[0] nodemap name
4194 * \retval 0 on success
4196 int jt_nodemap_del(int argc, char **argv)
4200 rc = llapi_nodemap_exists(argv[1]);
4202 fprintf(stderr, "error: %s not existing nodemap name\n",
4206 rc = nodemap_cmd(LCFG_NODEMAP_DEL, NULL, 0, argv[0], argv[1], NULL);
4217 * test a nid for nodemap membership
4219 * \param argc number of args
4220 * \param argv[] variable string arguments
4222 * argv[0] properly formatted nid
4224 * \retval 0 on success
4226 int jt_nodemap_test_nid(int argc, char **argv)
4228 char rawbuf[MAX_IOC_BUFLEN];
4231 rc = nodemap_cmd(LCFG_NODEMAP_TEST_NID, &rawbuf, sizeof(rawbuf),
4232 argv[0], argv[1], NULL);
4234 printf("%s\n", (char *)rawbuf);
4240 * test a nodemap id pair for mapping
4242 * \param argc number of args
4243 * \param argv[[] variable string arguments
4245 * \retval 0 on success
4247 * The argv array should contain the nodemap name, the id
4248 * to checking the mapping on, and the id type (UID or GID)
4251 int jt_nodemap_test_id(int argc, char **argv)
4253 char rawbuf[MAX_IOC_BUFLEN];
4254 char *nidstr = NULL;
4256 char *typestr = NULL;
4260 static struct option long_opts[] = {
4261 { .val = 'i', .name = "id", .has_arg = required_argument },
4262 { .val = 'n', .name = "nid", .has_arg = required_argument },
4263 { .val = 't', .name = "idtype",
4264 .has_arg = required_argument },
4267 while ((c = getopt_long(argc, argv, "n:t:i:",
4268 long_opts, NULL)) != -1) {
4282 if (!nidstr || !typestr || !idstr) {
4284 "usage: nodemap_test_id --nid <nid> --idtype [uid|gid] --id <id>\n");
4288 rc = nodemap_cmd(LCFG_NODEMAP_TEST_ID, &rawbuf, sizeof(rawbuf),
4289 argv[0], nidstr, typestr, idstr, NULL);
4291 printf("%s\n", (char *)rawbuf);
4299 * \param nodemap_range --range string
4300 * \param nid_range nid range string, min_nid:max_nid
4302 * \retval 0 on success
4304 static int parse_nid_range(char *nodemap_range, char *nid_range, int range_len)
4306 char min_nid[LNET_NIDSTR_SIZE + 1];
4307 char max_nid[LNET_NIDSTR_SIZE + 1];
4308 struct list_head nidlist;
4311 INIT_LIST_HEAD(&nidlist);
4313 if (cfs_parse_nidlist(nodemap_range, strlen(nodemap_range),
4316 "error: nodemap_xxx_range: can't parse nid range: %s\n",
4321 rc = cfs_nidrange_find_min_max(&nidlist, &min_nid[0], &max_nid[0],
4326 "error: nodemap_xxx_range: nid range uses currently unsupported features\n");
4327 else if (rc == -ERANGE)
4329 "error: nodemap_xxx_range: nodemap ranges must be contiguous\n");
4334 snprintf(nid_range, range_len, "%s:%s", min_nid, max_nid);
4340 * add an nid range to a nodemap
4342 * \param argc number of args
4343 * \param argv[] variable string arguments
4345 * --name nodemap name
4346 * --range properly formatted nid range
4348 * \retval 0 on success, -errno on error
4350 int jt_nodemap_add_range(int argc, char **argv)
4352 char *nodemap_name = NULL;
4353 char *nodemap_range = NULL;
4354 char nid_range[2 * LNET_NIDSTR_SIZE + 2];
4358 static struct option long_opts[] = {
4359 { .val = 'n', .name = "name", .has_arg = required_argument },
4360 { .val = 'r', .name = "range", .has_arg = required_argument },
4363 while ((c = getopt_long(argc, argv, "n:r:",
4364 long_opts, NULL)) != -1) {
4367 nodemap_name = optarg;
4370 nodemap_range = optarg;
4375 if (!nodemap_name || !nodemap_range) {
4377 "usage: nodemap_add_range --name <name> --range <range>\n");
4381 rc = parse_nid_range(nodemap_range, nid_range, sizeof(nid_range));
4385 rc = nodemap_cmd(LCFG_NODEMAP_ADD_RANGE, NULL, 0, argv[0],
4386 nodemap_name, nid_range, NULL);
4389 "error: %s: cannot add range '%s' to nodemap '%s': %s\n",
4390 jt_cmdname(argv[0]), nodemap_range, nodemap_name,
4398 * delete an nid range to a nodemap
4400 * \param argc number of args
4401 * \param argv[] variable string arguments
4403 * --name nodemap name
4404 * --range properly formatted nid range
4406 * \retval 0 on success
4408 int jt_nodemap_del_range(int argc, char **argv)
4410 char *nodemap_name = NULL;
4411 char *nodemap_range = NULL;
4412 char nid_range[2 * LNET_NIDSTR_SIZE + 2];
4416 static struct option long_opts[] = {
4417 { .val = 'n', .name = "name", .has_arg = required_argument },
4418 { .val = 'r', .name = "range", .has_arg = required_argument },
4421 while ((c = getopt_long(argc, argv, "n:r:",
4422 long_opts, NULL)) != -1) {
4425 nodemap_name = optarg;
4428 nodemap_range = optarg;
4433 if (!nodemap_name || !nodemap_range) {
4435 "usage: nodemap_del_range --name <name> --range <range>\n");
4439 rc = parse_nid_range(nodemap_range, nid_range, sizeof(nid_range));
4444 rc = nodemap_cmd(LCFG_NODEMAP_DEL_RANGE, NULL, 0, argv[0],
4445 nodemap_name, nid_range, NULL);
4449 "error: %s: cannot delete range '%s' to nodemap '%s': rc = %d\n",
4450 jt_cmdname(argv[0]), nodemap_range, nodemap_name, rc);
4457 * set a fileset on a nodemap
4459 * \param argc number of args
4460 * \param argv[] variable string arguments
4462 * --name nodemap name
4463 * --fileset fileset name
4465 * \retval 0 on success
4467 int jt_nodemap_set_fileset(int argc, char **argv)
4469 char *nodemap_name = NULL;
4470 char *fileset_name = NULL;
4474 static struct option long_opts[] = {
4475 { .val = 'f', .name = "fileset", .has_arg = required_argument },
4476 { .val = 'n', .name = "name", .has_arg = required_argument },
4479 while ((c = getopt_long(argc, argv, "n:f:",
4480 long_opts, NULL)) != -1) {
4483 nodemap_name = optarg;
4486 fileset_name = optarg;
4491 if (!nodemap_name || !fileset_name) {
4493 "usage: nodemap_set_fileset --name <name> --fileset <fileset>\n");
4497 rc = nodemap_cmd(LCFG_NODEMAP_SET_FILESET, NULL, 0, argv[0],
4498 nodemap_name, fileset_name, NULL);
4502 "error: %s: cannot set fileset '%s' on nodemap '%s': rc = %d\n",
4503 jt_cmdname(argv[0]), fileset_name, nodemap_name, rc);
4510 * set SELinux policy info on a nodemap
4512 * \param argc number of args
4513 * \param argv[] variable string arguments
4515 * --name nodemap name
4516 * --sepol SELinux policy info
4518 * \retval 0 on success
4520 int jt_nodemap_set_sepol(int argc, char **argv)
4522 char *nodemap_name = NULL;
4527 static struct option long_options[] = {
4530 .has_arg = required_argument,
4535 .has_arg = required_argument,
4543 while ((c = getopt_long(argc, argv, "n:s:",
4544 long_options, NULL)) != -1) {
4547 nodemap_name = optarg;
4555 if (!nodemap_name || !sepol) {
4557 "usage: nodemap_set_sepol --name <name> --sepol <sepol>\n");
4561 rc = nodemap_cmd(LCFG_NODEMAP_SET_SEPOL, NULL, 0, argv[0],
4562 nodemap_name, sepol, NULL);
4566 "error: %s: cannot set sepol '%s' on nodemap '%s': rc = %d\n",
4567 jt_cmdname(argv[0]), sepol, nodemap_name, rc);
4574 * modify a nodemap's behavior
4576 * \param argc number of args
4577 * \param argv[] variable string arguments
4579 * --name nodemap name
4580 * --property nodemap property to change
4581 * admin, trusted, squash_uid, squash_gid)
4582 * --value value to set property
4584 * \retval 0 on success
4586 int jt_nodemap_modify(int argc, char **argv)
4590 enum lcfg_command_type cmd = 0;
4591 char *nodemap_name = NULL;
4595 static struct option long_opts[] = {
4596 { .val = 'n', .name = "name", .has_arg = required_argument },
4597 { .val = 'p', .name = "property", .has_arg = required_argument },
4598 { .val = 'v', .name = "value", .has_arg = required_argument },
4601 while ((c = getopt_long(argc, argv, "n:p:v:",
4602 long_opts, NULL)) != -1) {
4605 nodemap_name = optarg;
4616 if (!nodemap_name || !param || !value) {
4618 "usage: nodemap_modify --name <nodemap_name> --property <property_name> --value <value>\n");
4620 "valid properties: admin trusted map_mode squash_uid squash_gid squash_projid deny_unknown audit_mode forbid_encryption readonly_mount rbac\n");
4624 if (strcmp("admin", param) == 0) {
4625 cmd = LCFG_NODEMAP_ADMIN;
4626 } else if (strcmp("trusted", param) == 0) {
4627 cmd = LCFG_NODEMAP_TRUSTED;
4628 } else if (strcmp("deny_unknown", param) == 0) {
4629 cmd = LCFG_NODEMAP_DENY_UNKNOWN;
4630 } else if (strcmp("squash_uid", param) == 0) {
4631 cmd = LCFG_NODEMAP_SQUASH_UID;
4632 } else if (strcmp("squash_gid", param) == 0) {
4633 cmd = LCFG_NODEMAP_SQUASH_GID;
4634 } else if (strcmp("squash_projid", param) == 0) {
4635 cmd = LCFG_NODEMAP_SQUASH_PROJID;
4636 } else if (strcmp("map_mode", param) == 0) {
4637 cmd = LCFG_NODEMAP_MAP_MODE;
4638 } else if (strcmp("audit_mode", param) == 0) {
4639 cmd = LCFG_NODEMAP_AUDIT_MODE;
4640 } else if (strcmp("forbid_encryption", param) == 0) {
4641 cmd = LCFG_NODEMAP_FORBID_ENCRYPT;
4642 } else if (strcmp("readonly_mount", param) == 0) {
4643 cmd = LCFG_NODEMAP_READONLY_MOUNT;
4644 } else if (strcmp("rbac", param) == 0) {
4645 cmd = LCFG_NODEMAP_RBAC;
4648 "error: %s: nodemap_modify invalid subcommand: %s\n",
4649 jt_cmdname(argv[0]), param);
4653 rc = nodemap_cmd(cmd, NULL, 0, argv[0], nodemap_name, param,
4658 "error: %s: cannot modify nodemap '%s' to param '%s': value '%s': rc = %d\n",
4659 jt_cmdname(argv[0]), nodemap_name, param, value, rc);
4665 int jt_nodemap_add_idmap(int argc, char **argv)
4668 enum lcfg_command_type cmd = 0;
4669 char *nodemap_name = NULL;
4671 char *idtype = NULL;
4674 static struct option long_opts[] = {
4675 { .val = 'i', .name = "idtype", .has_arg = required_argument },
4676 { .val = 'm', .name = "idmap", .has_arg = required_argument },
4677 { .val = 'n', .name = "name", .has_arg = required_argument },
4680 while ((c = getopt_long(argc, argv, "n:m:i:",
4681 long_opts, NULL)) != -1) {
4684 nodemap_name = optarg;
4695 if (!nodemap_name || !idmap || !idtype) {
4697 "usage: %s --name <name> --idtype [uid | gid | projid] --idmap <client id>:<filesystem id>\n",
4702 if (strcmp("uid", idtype) == 0) {
4703 cmd = LCFG_NODEMAP_ADD_UIDMAP;
4704 } else if (strcmp("gid", idtype) == 0) {
4705 cmd = LCFG_NODEMAP_ADD_GIDMAP;
4706 } else if (strcmp("projid", idtype) == 0) {
4707 cmd = LCFG_NODEMAP_ADD_PROJIDMAP;
4710 "usage: %s --name <name> --idtype [uid | gid | projid] --idmap <client id>:<filesystem id>\n",
4715 rc = nodemap_cmd(cmd, NULL, 0, argv[0], nodemap_name, idmap, NULL);
4719 "cannot add %smap '%s' to nodemap '%s': rc = %d\n",
4720 idtype, idmap, nodemap_name, rc);
4726 int jt_nodemap_del_idmap(int argc, char **argv)
4729 enum lcfg_command_type cmd = 0;
4730 char *nodemap_name = NULL;
4732 char *idtype = NULL;
4735 static struct option long_opts[] = {
4736 { .val = 'i', .name = "idtype", .has_arg = required_argument },
4737 { .val = 'm', .name = "idmap", .has_arg = required_argument },
4738 { .val = 'n', .name = "name", .has_arg = required_argument },
4741 while ((c = getopt_long(argc, argv, "n:m:i:",
4742 long_opts, NULL)) != -1) {
4745 nodemap_name = optarg;
4756 if (!nodemap_name || !idmap || !idtype) {
4758 "usage: %s --name <name> --idtype [uid | gid | projid] --idmap <client id>:<filesystem id>\n",
4763 if (strcmp("uid", idtype) == 0) {
4764 cmd = LCFG_NODEMAP_DEL_UIDMAP;
4765 } else if (strcmp("gid", idtype) == 0) {
4766 cmd = LCFG_NODEMAP_DEL_GIDMAP;
4767 } else if (strcmp("projid", idtype) == 0) {
4768 cmd = LCFG_NODEMAP_DEL_PROJIDMAP;
4771 "usage: %s --name <name> --idtype [uid | gid | projid] --idmap <client id>:<filesystem id>\n",
4776 rc = nodemap_cmd(cmd, NULL, 0, argv[0], nodemap_name, idmap, NULL);
4780 "cannot delete %smap '%s' from nodemap '%s': rc = %d\n",
4781 idtype, idmap, nodemap_name, rc);
4786 #else /* !HAVE_SERVER_SUPPORT */
4787 int jt_nodemap_activate(int argc, char **argv)
4789 fprintf(stderr, "error: %s: invalid ioctl\n",
4790 jt_cmdname(argv[0]));
4794 int jt_nodemap_add(int argc, char **argv)
4796 fprintf(stderr, "error: %s: invalid ioctl\n",
4797 jt_cmdname(argv[0]));
4801 int jt_nodemap_del(int argc, char **argv)
4803 fprintf(stderr, "error: %s: invalid ioctl\n",
4804 jt_cmdname(argv[0]));
4808 int jt_nodemap_modify(int argc, char **argv)
4810 fprintf(stderr, "error: %s: invalid ioctl\n",
4811 jt_cmdname(argv[0]));
4815 int jt_nodemap_add_range(int argc, char **argv)
4817 fprintf(stderr, "error: %s: invalid ioctl\n",
4818 jt_cmdname(argv[0]));
4822 int jt_nodemap_test_nid(int argc, char **argv)
4824 fprintf(stderr, "error: %s: invalid ioctl\n",
4825 jt_cmdname(argv[0]));
4829 int jt_nodemap_del_range(int argc, char **argv)
4831 fprintf(stderr, "error: %s: invalid ioctl\n",
4832 jt_cmdname(argv[0]));
4836 int jt_nodemap_add_idmap(int argc, char **argv)
4838 fprintf(stderr, "error: %s: invalid ioctl\n",
4839 jt_cmdname(argv[0]));
4843 int jt_nodemap_del_idmap(int argc, char **argv)
4845 fprintf(stderr, "error: %s: invalid ioctl\n",
4846 jt_cmdname(argv[0]));
4850 int jt_nodemap_test_id(int argc, char **argv)
4852 fprintf(stderr, "error: %s: invalid ioctl\n",
4853 jt_cmdname(argv[0]));
4857 int jt_nodemap_set_fileset(int argc, char **argv)
4859 fprintf(stderr, "error: %s: invalid ioctl\n",
4860 jt_cmdname(argv[0]));
4864 int jt_nodemap_set_sepol(int argc, char **argv)
4866 fprintf(stderr, "error: %s: invalid ioctl\n",
4867 jt_cmdname(argv[0]));
4871 int jt_nodemap_info(int argc, char **argv)
4873 fprintf(stderr, "error: %s: invalid ioctl\n",
4874 jt_cmdname(argv[0]));
4877 #endif /* HAVE_SERVER_SUPPORT */
4880 * this function tranforms a rule [start-end/step] into an array
4881 * of matching numbers
4882 * supported forms are:
4883 * [start] : just this number
4884 * [start-end] : all numbers from start to end
4885 * [start-end/step] : numbers from start to end with increment of step
4886 * on return, format contains a printf format string which can be used
4887 * to generate all the strings
4889 static int get_array_idx(char *rule, char *format, int **array)
4891 char *start, *end, *ptr;
4892 unsigned int lo, hi, step;
4897 start = strchr(rule, '[');
4898 end = strchr(rule, ']');
4899 if ((!start) || (!end)) {
4900 *array = malloc(sizeof(int));
4903 strcpy(format, rule);
4911 /* put in format the printf format (the rule without the range) */
4912 sprintf(format, "%s%%.4x%s", rule, end);
4917 /* loop on , separator */
4919 /* extract the 3 fields */
4920 rc = sscanf(start, "%x-%x/%u", &lo, &hi, &step);
4928 tmp = realloc(*array, array_sz * sizeof(int));
4932 (*array)[array_idx] = lo;
4938 /* do not break to share code with case 3: */
4943 if ((hi < lo) || (step == 0))
4945 array_sz += (hi - lo) / step + 1;
4946 tmp = realloc(*array, array_sz * sizeof(int));
4950 for (i = lo; i <= hi; i += step, array_idx++)
4951 (*array)[array_idx] = i;
4955 ptr = strchr(start, ',');
4969 struct llog_pool_name {
4970 char lpn_name[UUID_MAX];
4971 struct list_head lpn_list;
4974 struct llog_pool_list_data {
4975 char lpld_fsname[LUSTRE_MAXFSNAME + 1];
4976 char lpld_poolname[LOV_MAXPOOLNAME + 1];
4978 struct list_head lpld_list_head;
4982 * Callback to list pool information in llog
4983 * - { index: 74, event: new_pool, device: tfs-clilov, fsname: tfs, pool: tmp }
4984 * - { index: 77, event: add_pool, device: tfs-clilov, fsname: tfs, pool: tmp,
4985 * ost: tfs-OST0000_UUID }
4986 * - { index: 224, event: remove_pool, device: tfs-clilov, fsname: tfs,
4987 * pool: tmp, ost: tfs-OST0003_UUID }
4988 * - { index: 227, event: del_pool, device: tfs-clilov, fsname: tfs, pool: tmp }
4990 * \param record[in] pointer to llog record
4991 * \param data[in] pointer to struct llog_pool_list_data
4993 * \retval 0 on success
4996 static int llog_poollist_cb(const char *record, void *data)
4998 struct llog_pool_list_data *lpld = data;
4999 char pool_filter[MAX_STRING_SIZE] = "";
5000 char *new_record, *del_record, *del_pool, *found;
5002 int filter_len, rc = 0;
5004 filter_len = snprintf(pool_filter, sizeof(pool_filter), " fsname: %s,",
5006 if (lpld->lpld_poolname[0] == '\0') {
5007 new_record = get_event_filter(LCFG_POOL_NEW);
5008 del_record = get_event_filter(LCFG_POOL_DEL);
5009 strncpy(type, " pool: ", sizeof(type));
5011 filter_len += snprintf(pool_filter + filter_len,
5012 sizeof(pool_filter) - filter_len,
5013 " pool: %s", lpld->lpld_poolname);
5014 new_record = get_event_filter(LCFG_POOL_ADD);
5015 del_record = get_event_filter(LCFG_POOL_REM);
5016 strncpy(type, " ost: ", sizeof(type));
5018 del_pool = get_event_filter(LCFG_POOL_DEL);
5020 if (!new_record || !del_record || !del_pool) {
5025 found = strstr(record, pool_filter);
5027 (found[filter_len] == ' ' || found[filter_len] == ',')) {
5028 struct llog_pool_name *tmp = NULL;
5029 struct list_head *head = &lpld->lpld_list_head;
5031 int name_len, type_len = strlen(type);
5033 lpld->lpld_exists = true;
5034 if (strstr(record, new_record)) {
5035 name = strstr(record, type);
5036 /* 2 bytes for " }" */
5037 name_len = strlen(name) - type_len - 2;
5038 if (name_len <= 0 || name_len > sizeof(tmp->lpn_name))
5040 tmp = malloc(sizeof(struct llog_pool_name));
5045 memset(tmp, 0, sizeof(struct llog_pool_name));
5046 strncpy(tmp->lpn_name, name + type_len, name_len);
5047 list_add_tail(&tmp->lpn_list, &lpld->lpld_list_head);
5048 } else if (strstr(record, del_record)) {
5049 name = strstr(record, type);
5050 name_len = strlen(name) - type_len - 2;
5051 list_for_each_entry(tmp, head, lpn_list) {
5052 if (strncmp(tmp->lpn_name, name + type_len,
5054 tmp->lpn_name[name_len] == '\0') {
5055 list_del(&tmp->lpn_list);
5061 /* verify if the specified pool still exists */
5062 if (lpld->lpld_poolname[0] && strstr(record, del_pool))
5063 lpld->lpld_exists = false;
5077 * List pool information by config log
5079 * \param fsname[in] pointer to filesystem name
5080 * \param poolname[in] pointer to pool name
5082 * \retval 0 on success
5085 int llog_poollist(char *fsname, char *poolname)
5087 char logname[MAX_OBD_NAME] = {'\0'};
5088 struct llog_pool_list_data lpld;
5089 struct llog_pool_name *tmp;
5090 struct list_head *head;
5093 if (fsname && fsname[0] == '\0')
5098 memset(&lpld, 0, sizeof(lpld));
5099 INIT_LIST_HEAD(&lpld.lpld_list_head);
5100 lpld.lpld_exists = false;
5101 strncpy(lpld.lpld_fsname, fsname, sizeof(lpld.lpld_fsname) - 1);
5102 if (poolname && poolname[0])
5103 strncpy(lpld.lpld_poolname, poolname,
5104 sizeof(lpld.lpld_poolname) - 1);
5105 snprintf(logname, sizeof(logname), "%s-client", fsname);
5106 rc = jt_llog_print_iter(logname, 0, -1, llog_poollist_cb, &lpld, false,
5109 if (poolname && poolname[0])
5110 printf("Pool: %s.%s\n", fsname, poolname);
5112 printf("Pools from %s:\n", fsname);
5114 head = &lpld.lpld_list_head;
5115 if (poolname && poolname[0] && !lpld.lpld_exists && list_empty(head))
5118 list_for_each_entry(tmp, head, lpn_list) {
5119 if (poolname && poolname[0])
5120 printf("%s\n", tmp->lpn_name);
5122 printf("%s.%s\n", fsname, tmp->lpn_name);
5123 list_del(&tmp->lpn_list);
5130 static bool get_pools_path(char *fsname)
5135 rc = cfs_get_param_paths(&path, "lov/%s-*/pools", fsname);
5137 cfs_free_param_data(&path);
5142 static int extract_fsname_poolname(char **argv, char *fsname,
5145 char *cmd = argv[0], *param = argv[1];
5149 snprintf(fsname, PATH_MAX + 1, "%s", param);
5150 ptr = strchr(fsname, '.');
5152 if (strcmp(cmd, "pool_list") == 0) {
5156 fprintf(stderr, ". is missing in %s\n", fsname);
5161 if ((ptr - fsname) == 0) {
5162 fprintf(stderr, "fsname is empty\n");
5170 if (ptr[0] == '\0') {
5171 fprintf(stderr, "poolname is empty\n");
5176 strncpy(poolname, ptr, LOV_MAXPOOLNAME);
5177 poolname[LOV_MAXPOOLNAME] = '\0';
5179 if (lov_pool_is_reserved(poolname)) {
5180 fprintf(stderr, "poolname cannot be '%s'\n", poolname);
5187 fprintf(stderr, "argument %s must be <fsname>.<poolname>\n", param);
5191 int jt_pool_cmd(int argc, char **argv)
5193 enum lcfg_command_type cmd;
5194 char fsname[PATH_MAX + 1];
5195 char poolname[LOV_MAXPOOLNAME + 1];
5196 char *ostnames_buf = NULL;
5198 int *array = NULL, array_sz;
5206 case 1: return CMD_HELP;
5208 rc = extract_fsname_poolname(argv, fsname, poolname);
5212 if (strcmp("pool_new", argv[0]) == 0) {
5213 cmd = LCFG_POOL_NEW;
5214 } else if (strcmp("pool_destroy", argv[0]) == 0) {
5215 cmd = LCFG_POOL_DEL;
5216 } else if (strcmp("pool_list", argv[0]) == 0) {
5217 if (get_pools_path(fsname))
5218 return llapi_poollist(argv[1]);
5219 if (get_mgs_device() > 0)
5220 return llog_poollist(fsname, poolname);
5222 "Cannot run pool_list command since there is no local MGS/MDT or client\n");
5228 rc = pool_cmd(cmd, argv[0], argv[1], fsname, poolname, NULL);
5232 check_pool_cmd_result(cmd, fsname, poolname, NULL);
5236 char format[2 * MAX_OBD_NAME];
5238 if (strcmp("pool_remove", argv[0]) == 0)
5239 cmd = LCFG_POOL_REM;
5240 else if (strcmp("pool_add", argv[0]) == 0)
5241 cmd = LCFG_POOL_ADD;
5245 rc = extract_fsname_poolname(argv, fsname, poolname);
5249 for (i = 2; i < argc; i++) {
5252 array_sz = get_array_idx(argv[i], format, &array);
5256 cmds = malloc(array_sz * sizeof(cmds[0]));
5258 ostnames_buf = malloc(array_sz *
5259 (MAX_OBD_NAME + 1));
5266 for (j = 0; j < array_sz; j++) {
5267 char ostname[MAX_OBD_NAME + 1];
5270 snprintf(ostname, MAX_OBD_NAME, format,
5272 ostname[MAX_OBD_NAME] = '\0';
5274 rc2 = check_and_complete_ostname(fsname,
5286 &ostnames_buf[(MAX_OBD_NAME + 1) * j];
5287 strcpy(cmds[j].ostname, ostname);
5289 cmds[j].ostname = NULL;
5291 cmds[j].rc = pool_cmd(cmd, argv[0], argv[1],
5294 /* Return an err if any of the add/dels fail */
5298 for (j = 0; j < array_sz; j++) {
5300 char ostname[MAX_OBD_NAME + 1];
5302 if (!cmds[j].ostname) {
5303 snprintf(ostname, MAX_OBD_NAME,
5305 ostname[MAX_OBD_NAME] = '\0';
5306 check_and_complete_ostname(
5312 check_pool_cmd_result(cmd, fsname,
5337 #ifdef HAVE_SERVER_SUPPORT
5338 static const char *barrier_status2name(enum barrier_status status)
5343 case BS_FREEZING_P1:
5344 return "freezing_p1";
5345 case BS_FREEZING_P2:
5346 return "freezing_p2";
5364 int jt_barrier_freeze(int argc, char **argv)
5366 struct obd_ioctl_data data;
5367 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
5368 struct barrier_ctl bc;
5371 if (argc < 2 || argc > 3)
5374 memset(&data, 0, sizeof(data));
5375 rc = data.ioc_dev = get_mgs_device();
5379 memset(&bc, 0, sizeof(bc));
5380 bc.bc_version = BARRIER_VERSION_V1;
5381 bc.bc_cmd = BC_FREEZE;
5383 bc.bc_timeout = atoi(argv[2]);
5384 if (bc.bc_timeout == 0)
5385 bc.bc_timeout = BARRIER_TIMEOUT_DEFAULT;
5387 if (strlen(argv[1]) > 8) {
5389 "%s: fsname name %s is too long. It should not exceed 8.\n",
5394 strncpy(bc.bc_name, argv[1], sizeof(bc.bc_name));
5395 data.ioc_inlbuf1 = (char *)&bc;
5396 data.ioc_inllen1 = sizeof(bc);
5397 memset(buf, 0, sizeof(rawbuf));
5398 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5400 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
5404 rc = llapi_ioctl_dev(OBD_DEV_ID, OBD_IOC_BARRIER_V2, buf);
5406 fprintf(stderr, "Fail to freeze barrier for %s: %s\n",
5407 argv[1], strerror(errno));
5412 int jt_barrier_thaw(int argc, char **argv)
5414 struct obd_ioctl_data data;
5415 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
5416 struct barrier_ctl bc;
5422 memset(&data, 0, sizeof(data));
5423 rc = data.ioc_dev = get_mgs_device();
5427 memset(&bc, 0, sizeof(bc));
5428 bc.bc_version = BARRIER_VERSION_V1;
5429 bc.bc_cmd = BC_THAW;
5431 if (strlen(argv[1]) > 8) {
5433 "fsname name %s is too long. It should not exceed 8.\n",
5438 strncpy(bc.bc_name, argv[1], sizeof(bc.bc_name));
5439 data.ioc_inlbuf1 = (char *)&bc;
5440 data.ioc_inllen1 = sizeof(bc);
5441 memset(buf, 0, sizeof(rawbuf));
5442 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5444 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
5448 rc = llapi_ioctl_dev(OBD_DEV_ID, OBD_IOC_BARRIER_V2, buf);
5450 fprintf(stderr, "Fail to thaw barrier for %s: %s\n",
5451 argv[1], strerror(errno));
5456 int __jt_barrier_stat(const char *fsname, struct barrier_ctl *bc)
5458 struct obd_ioctl_data data;
5459 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
5462 memset(&data, 0, sizeof(data));
5463 rc = data.ioc_dev = get_mgs_device();
5467 memset(bc, 0, sizeof(*bc));
5468 bc->bc_version = BARRIER_VERSION_V1;
5469 bc->bc_cmd = BC_STAT;
5470 strncpy(bc->bc_name, fsname, sizeof(bc->bc_name) - 1);
5471 data.ioc_inlbuf1 = (char *)bc;
5472 data.ioc_inllen1 = sizeof(*bc);
5473 memset(buf, 0, sizeof(rawbuf));
5474 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5476 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
5480 rc = llapi_ioctl_dev(OBD_DEV_ID, OBD_IOC_BARRIER_V2, buf);
5482 fprintf(stderr, "Fail to query barrier for %s: %s\n",
5483 fsname, strerror(errno));
5485 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
5490 int jt_barrier_stat(int argc, char **argv)
5492 struct barrier_ctl bc;
5493 static struct option long_opt_barrier_stat[] = {
5497 .has_arg = no_argument,
5501 .has_arg = no_argument,
5512 bool timeout = false;
5514 while ((opt = getopt_long(argc, argv, "st", long_opt_barrier_stat,
5531 name = argv[optind];
5532 if (strlen(name) > 8) {
5534 "fsname name %s is too long. It should not exceed 8.\n",
5539 rc = __jt_barrier_stat(name, &bc);
5541 if (state && !timeout)
5542 printf("%s\n", barrier_status2name(bc.bc_status));
5543 else if (timeout && !state)
5545 (bc.bc_status == BS_FREEZING_P1 ||
5546 bc.bc_status == BS_FREEZING_P2 ||
5547 bc.bc_status == BS_FROZEN) ?
5550 printf("state: %s\ntimeout: %d seconds\n",
5551 barrier_status2name(bc.bc_status),
5552 (bc.bc_status == BS_FREEZING_P1 ||
5553 bc.bc_status == BS_FREEZING_P2 ||
5554 bc.bc_status == BS_FROZEN) ?
5561 int jt_barrier_rescan(int argc, char **argv)
5563 struct obd_ioctl_data data;
5564 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
5565 struct barrier_ctl bc;
5568 if (argc < 2 || argc > 3)
5571 memset(&data, 0, sizeof(data));
5572 rc = data.ioc_dev = get_mgs_device();
5576 memset(&bc, 0, sizeof(bc));
5577 bc.bc_version = BARRIER_VERSION_V1;
5578 bc.bc_cmd = BC_RESCAN;
5580 bc.bc_timeout = atoi(argv[2]);
5581 if (bc.bc_timeout == 0)
5582 bc.bc_timeout = BARRIER_TIMEOUT_DEFAULT;
5584 if (strlen(argv[1]) > 8) {
5586 "fsname name %s is too long. It should not exceed 8.\n",
5591 strncpy(bc.bc_name, argv[1], sizeof(bc.bc_name));
5592 data.ioc_inlbuf1 = (char *)&bc;
5593 data.ioc_inllen1 = sizeof(bc);
5594 memset(buf, 0, sizeof(rawbuf));
5595 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5597 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
5601 rc = llapi_ioctl_dev(OBD_DEV_ID, OBD_IOC_BARRIER_V2, buf);
5603 fprintf(stderr, "Fail to rescan barrier bitmap for %s: %s\n",
5604 argv[1], strerror(errno));
5606 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
5607 printf("%u of %u MDT(s) in the filesystem %s are inactive\n",
5608 bc.bc_absence, bc.bc_total, argv[1]);
5613 #endif /* HAVE_SERVER_SUPPORT */
5615 int jt_get_obj_version(int argc, char **argv)
5618 struct obd_ioctl_data data;
5619 __u64 version, id = ULLONG_MAX, group = ULLONG_MAX;
5620 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf, *fidstr;
5623 while ((c = getopt(argc, argv, "i:g:")) != -1) {
5626 id = strtoull(optarg, NULL, 0);
5629 group = strtoull(optarg, NULL, 0);
5637 fidstr = *(argv + optind);
5639 if (!(id != ULLONG_MAX && group != ULLONG_MAX && argc == 0) &&
5640 !(id == ULLONG_MAX && group == ULLONG_MAX && argc == 1))
5643 memset(&data, 0, sizeof(data));
5644 data.ioc_dev = cur_device;
5646 rc = llapi_fid_parse(fidstr, &fid, NULL);
5648 fprintf(stderr, "%s: error parsing FID '%s': %s\n",
5649 jt_cmdname(argv[0]), fidstr, strerror(-rc));
5653 data.ioc_inlbuf1 = (char *)&fid;
5654 data.ioc_inllen1 = sizeof(fid);
5656 data.ioc_inlbuf3 = (char *)&id;
5657 data.ioc_inllen3 = sizeof(id);
5658 data.ioc_inlbuf4 = (char *)&group;
5659 data.ioc_inllen4 = sizeof(group);
5661 data.ioc_inlbuf2 = (char *)&version;
5662 data.ioc_inllen2 = sizeof(version);
5664 memset(buf, 0, sizeof(*buf));
5665 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5667 fprintf(stderr, "error: %s: packing ioctl arguments: %s\n",
5668 jt_cmdname(argv[0]), strerror(-rc));
5672 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_GET_OBJ_VERSION, buf);
5674 fprintf(stderr, "error: %s: ioctl: %s\n",
5675 jt_cmdname(argv[0]), strerror(errno));
5679 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
5680 printf("%#jx\n", (uintmax_t)version);
5685 #ifdef HAVE_SERVER_SUPPORT
5686 int jt_changelog_register(int argc, char **argv)
5688 struct option long_opts[] = {
5689 { .val = 'h', .name = "help", .has_arg = no_argument },
5690 { .val = 'm', .name = "mask", .has_arg = required_argument },
5691 { .val = 'n', .name = "nameonly", .has_arg = no_argument },
5692 { .val = 'u', .name = "user", .has_arg = required_argument },
5694 struct obd_ioctl_data data = { 0 };
5695 char rawbuf[MAX_IOC_BUFLEN] = "";
5697 char *device = lcfg_get_devname();
5698 char *username = NULL, *usermask = NULL;
5699 bool print_name_only = false;
5703 if (cur_device < 0 || !device)
5706 while ((c = getopt_long(argc, argv, "hm:nu:", long_opts, NULL)) != -1) {
5709 usermask = strdup(optarg);
5712 "error: %s: %s: cannot copy '%s'\n",
5713 jt_cmdname(argv[0]), strerror(errno),
5719 print_name_only = true;
5722 username = strdup(optarg);
5725 "error: %s: %s: cannot copy '%s'\n",
5726 jt_cmdname(argv[0]), strerror(errno),
5739 data.ioc_dev = cur_device;
5741 data.ioc_inlbuf1 = username;
5742 data.ioc_inllen1 = strlen(username) + 1;
5746 data.ioc_inlbuf2 = usermask;
5747 data.ioc_inllen2 = strlen(usermask) + 1;
5750 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5752 fprintf(stderr, "error: %s: cannot pack ioctl: %s\n",
5753 jt_cmdname(argv[0]), strerror(-rc));
5756 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CHANGELOG_REG, buf);
5759 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
5760 rc == -EEXIST ? "User exists" : strerror(-rc));
5764 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
5766 if (data.ioc_u32_1 == 0) {
5767 fprintf(stderr, "received invalid userid!\n");
5772 if (print_name_only)
5773 printf("%s%u%s%s\n", CHANGELOG_USER_PREFIX, data.ioc_u32_1,
5774 username ? "-" : "", username ? : "");
5776 printf("%s: Registered changelog userid '%s%u%s%s'\n",
5777 device, CHANGELOG_USER_PREFIX, data.ioc_u32_1,
5778 username ? "-" : "", username ? : "");
5785 int jt_changelog_deregister(int argc, char **argv)
5787 struct option long_opts[] = {
5788 { .val = 'h', .name = "help", .has_arg = no_argument },
5789 { .val = 'u', .name = "user", .has_arg = required_argument },
5791 struct obd_ioctl_data data = { 0 };
5792 char rawbuf[MAX_IOC_BUFLEN] = "";
5794 char *device = lcfg_get_devname();
5795 char *username = NULL;
5799 if (cur_device < 0 || !device)
5802 while ((c = getopt_long(argc, argv, "hu:", long_opts, NULL)) != -1) {
5805 username = strdup(optarg);
5808 "error: %s: %s: cannot copy '%s'\n",
5809 jt_cmdname(argv[0]), strerror(errno),
5821 if (1 == optind && argc > 1) {
5822 /* first check if pure ID was passed */
5823 id = atoi(argv[optind]);
5824 /* nameless cl<ID> format or cl<ID>-... format, only ID matters */
5826 sscanf(argv[optind], CHANGELOG_USER_PREFIX"%d", &id);
5828 /* no valid ID was parsed */
5832 "error: %s: expect <ID> or cl<ID>[-name] got '%s'\n",
5833 strerror(-rc), argv[optind]);
5839 if (optind < argc || argc == 1)
5842 data.ioc_dev = cur_device;
5843 data.ioc_u32_1 = id;
5845 data.ioc_inlbuf1 = username;
5846 data.ioc_inllen1 = strlen(username) + 1;
5849 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5851 fprintf(stderr, "error: %s: invalid ioctl\n",
5852 jt_cmdname(argv[0]));
5856 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CHANGELOG_DEREG, buf);
5859 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
5860 rc == -ENOENT ? "User not found" : strerror(-rc));
5864 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
5865 printf("%s: Deregistered changelog user #%u\n", device, data.ioc_u32_1);
5869 #else /* !HAVE_SERVER_SUPPORT */
5870 int jt_changelog_register(int argc, char **argv)
5872 fprintf(stderr, "error: %s: invalid ioctl\n",
5873 jt_cmdname(argv[0]));
5877 int jt_changelog_deregister(int argc, char **argv)
5879 fprintf(stderr, "error: %s: invalid ioctl\n",
5880 jt_cmdname(argv[0]));
5883 #endif /* HAVE_SERVER_SUPPORT */
5885 int jt_pcc_add(int argc, char **argv)
5887 struct option long_opts[] = {
5888 { .val = 'p', .name = "param", .has_arg = required_argument },
5890 const char *mntpath;
5891 const char *pccpath;
5897 while ((rc = getopt_long(argc, argv, "p:", long_opts, NULL)) != -1) {
5908 fprintf(stderr, "%s: must specify the config param for PCC\n",
5909 jt_cmdname(argv[0]));
5913 if (optind + 2 != argc) {
5915 "%s: must specify mount path and PCC path %d:%d\n",
5916 jt_cmdname(argv[0]), optind, argc);
5920 mntpath = argv[optind++];
5921 pccpath = argv[optind];
5923 snprintf(cmd, PATH_MAX, "add %s %s", pccpath, param);
5924 rc = llapi_pccdev_set(mntpath, cmd);
5926 fprintf(stderr, "%s: failed to run '%s' on %s\n",
5927 jt_cmdname(argv[0]), cmd, mntpath);
5932 int jt_pcc_del(int argc, char **argv)
5934 const char *mntpath;
5935 const char *pccpath;
5941 fprintf(stderr, "%s: require 3 arguments\n",
5942 jt_cmdname(argv[0]));
5946 mntpath = argv[optind++];
5947 pccpath = argv[optind++];
5949 snprintf(cmd, PATH_MAX, "del %s", pccpath);
5950 rc = llapi_pccdev_set(mntpath, cmd);
5952 fprintf(stderr, "%s: failed to run '%s' on %s\n",
5953 jt_cmdname(argv[0]), cmd, mntpath);
5958 int jt_pcc_clear(int argc, char **argv)
5960 const char *mntpath;
5965 fprintf(stderr, "%s: require 2 arguments\n",
5966 jt_cmdname(argv[0]));
5970 mntpath = argv[optind];
5971 rc = llapi_pccdev_set(mntpath, "clear");
5973 fprintf(stderr, "%s: failed to run 'clear' on %s\n",
5974 jt_cmdname(argv[0]), mntpath);
5979 int jt_pcc_list(int argc, char **argv)
5981 const char *mntpath;
5986 fprintf(stderr, "%s: require 2 arguments\n",
5987 jt_cmdname(argv[0]));
5991 mntpath = argv[optind];
5992 rc = llapi_pccdev_get(mntpath);
5994 fprintf(stderr, "%s: failed to run 'pcc list' on %s\n",
5995 jt_cmdname(argv[0]), mntpath);