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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
37 * Author: Robert Read <rread@clusterfs.com>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
44 #include <sys/types.h>
62 #include "lustreapi_internal.h"
63 #include <libcfs/util/list.h>
64 #include <libcfs/util/ioctl.h>
65 #include <libcfs/util/param.h>
66 #include <libcfs/util/parser.h>
67 #include <libcfs/util/string.h>
69 #include <linux/lnet/nidstr.h>
70 #include <linux/lnet/lnetctl.h>
71 #ifdef HAVE_SERVER_SUPPPORT
72 #include <linux/lustre/lustre_barrier_user.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 #define N2D_OFF 0x100 /* So we can tell between error codes and devices */
219 static int do_name2dev(char *func, char *name)
221 struct obd_ioctl_data data;
222 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
225 memset(&data, 0, sizeof(data));
226 data.ioc_dev = cur_device;
227 data.ioc_inllen1 = strlen(name) + 1;
228 data.ioc_inlbuf1 = name;
230 memset(buf, 0, sizeof(rawbuf));
231 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
233 fprintf(stderr, "error: %s: invalid ioctl\n", jt_cmdname(func));
236 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_NAME2DEV, buf);
239 rc = llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
241 fprintf(stderr, "error: %s: invalid reply\n", jt_cmdname(func));
245 return data.ioc_dev + N2D_OFF;
249 * resolve a device name to a device number.
250 * supports a number, $name or %uuid.
252 int parse_devname(char *func, char *name)
261 try_digit = isdigit(name[0]);
263 if (name[0] == '$' || name[0] == '%')
266 rc = do_name2dev(func, name);
271 ret = strtoul(name, NULL, 0);
273 fprintf(stderr, "No device found for name %s: %s\n",
278 char *jt_cmdname(char *func)
280 static char buf[512];
283 sprintf(buf, "%s-%d", func, thread);
290 #define difftime(a, b) \
291 ((a)->tv_sec - (b)->tv_sec + \
292 ((a)->tv_usec - (b)->tv_usec) / 1000000.0)
294 static int be_verbose(int verbose, struct timeval *next_time,
295 __u64 num, __u64 *next_num, int num_total)
303 gettimeofday(&now, NULL);
305 /* A positive verbosity means to print every X iterations */
306 if (verbose > 0 && (num >= *next_num || num >= num_total)) {
307 *next_num += verbose;
309 next_time->tv_sec = now.tv_sec - verbose;
310 next_time->tv_usec = now.tv_usec;
315 /* A negative verbosity means to print at most each X seconds */
316 if (verbose < 0 && next_time && difftime(&now, next_time) >= 0.0) {
317 next_time->tv_sec = now.tv_sec - verbose;
318 next_time->tv_usec = now.tv_usec;
326 static int get_verbose(char *func, const char *arg)
331 if (!arg || arg[0] == 'v') {
333 } else if (arg[0] == 's' || arg[0] == 'q') {
336 verbose = (int)strtoul(arg, &end, 0);
338 fprintf(stderr, "error: %s: bad verbose option '%s'\n",
339 jt_cmdname(func), arg);
345 printf("Print status every %d seconds\n", -verbose);
346 else if (verbose == 1)
347 printf("Print status every operation\n");
348 else if (verbose > 1)
349 printf("Print status every %d operations\n", verbose);
354 int do_disconnect(char *func, int verbose)
356 lcfg_set_devname(NULL);
362 static int shmem_setup(void)
364 pthread_mutexattr_t mattr;
365 pthread_condattr_t cattr;
369 /* Create new segment */
370 shmid = shmget(IPC_PRIVATE, sizeof(*shared_data), 0600);
372 fprintf(stderr, "Can't create shared data: %s\n",
377 /* Attatch to new segment */
378 shared_data = (struct shared_data *)shmat(shmid, NULL, 0);
380 if (shared_data == (struct shared_data *)(-1)) {
381 fprintf(stderr, "Can't attach shared data: %s\n",
388 * Mark segment as destroyed, so it will disappear when we exit.
389 * Forks will inherit attached segments, so we should be OK.
391 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
392 fprintf(stderr, "Can't destroy shared data: %s\n",
397 pthread_mutexattr_init(&mattr);
398 pthread_condattr_init(&cattr);
400 rc = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
402 fprintf(stderr, "Can't set shared mutex attr\n");
406 rc = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
408 fprintf(stderr, "Can't set shared cond attr\n");
412 pthread_mutex_init(&shared_data->mutex, &mattr);
413 pthread_cond_init(&shared_data->cond, &cattr);
415 pthread_mutexattr_destroy(&mattr);
416 pthread_condattr_destroy(&cattr);
421 static inline void shmem_lock(void)
423 pthread_mutex_lock(&shared_data->mutex);
426 static inline void shmem_unlock(void)
428 pthread_mutex_unlock(&shared_data->mutex);
431 static inline void shmem_wait(void)
433 pthread_cond_wait(&shared_data->cond, &shared_data->mutex);
436 static inline void shmem_wakeup_all(void)
438 pthread_cond_broadcast(&shared_data->cond);
441 static inline void shmem_reset(int total_threads)
446 memset(&shared_data->body, 0, sizeof(shared_data->body));
447 memset(counter_snapshot, 0, sizeof(counter_snapshot));
449 shared_data->stopping = 0;
450 shared_data->body.start_barrier = total_threads;
451 shared_data->body.stop_barrier = total_threads;
454 static inline void shmem_bump(__u32 counter)
456 static bool running_not_bumped = true;
458 if (!shared_data || thread <= 0 || thread > MAX_THREADS)
462 shared_data->body.counters[thread - 1] += counter;
463 if (running_not_bumped) {
464 shared_data->body.thr_running++;
465 running_not_bumped = false;
470 static void shmem_total(int total_threads)
476 if (!shared_data || total_threads > MAX_THREADS)
480 for (i = 0; i < total_threads; i++)
481 total += shared_data->body.counters[i];
483 secs = difftime(&shared_data->body.end_time,
484 &shared_data->body.start_time);
487 printf("Total: total %ju threads %d sec %f %f/second\n",
488 (uintmax_t)total, total_threads, secs, total / secs);
491 static void shmem_snap(int total_threads, int live_threads)
493 struct timeval this_time;
500 if (!shared_data || total_threads > MAX_THREADS)
504 memcpy(counter_snapshot[0], shared_data->body.counters,
505 total_threads * sizeof(counter_snapshot[0][0]));
506 running = shared_data->body.thr_running;
509 gettimeofday(&this_time, NULL);
511 for (i = 0; i < total_threads; i++) {
512 long long this_count =
513 counter_snapshot[0][i] - counter_snapshot[1][i];
515 if (this_count != 0) {
521 secs = difftime(&this_time, &prev_time);
522 if (prev_valid && secs > 1.0) { /* someone screwed with the time? */
523 printf("%d/%d Total: %f/second\n", non_zero, total_threads,
526 memcpy(counter_snapshot[1], counter_snapshot[0],
527 total_threads * sizeof(counter_snapshot[0][0]));
528 prev_time = this_time;
530 if (!prev_valid && running == total_threads) {
532 /* drop counters when all threads were started */
533 memcpy(counter_snapshot[1], counter_snapshot[0],
534 total_threads * sizeof(counter_snapshot[0][0]));
535 prev_time = this_time;
539 static void shmem_stop(void)
544 shared_data->stopping = 1;
547 static void shmem_cleanup(void)
554 pthread_mutex_destroy(&shared_data->mutex);
555 pthread_cond_destroy(&shared_data->cond);
558 static int shmem_running(void)
560 return (!shared_data || !shared_data->stopping);
563 static void shmem_end_time_locked(void)
565 shared_data->body.stop_barrier--;
566 if (shared_data->body.stop_barrier == 0)
567 gettimeofday(&shared_data->body.end_time, NULL);
570 static void shmem_start_time_locked(void)
572 shared_data->body.start_barrier--;
573 if (shared_data->body.start_barrier == 0) {
575 gettimeofday(&shared_data->body.start_time, NULL);
582 static int shmem_setup(void)
587 static inline void shmem_reset(int total_threads)
591 static inline void shmem_bump(__u32 counters)
595 static void shmem_lock(void)
599 static void shmem_unlock(void)
603 static void shmem_cleanup(void)
607 static int shmem_running(void)
613 extern command_t cmdlist[];
615 static int do_device(char *func, char *devname)
619 dev = parse_devname(func, devname);
623 lcfg_set_devname(devname);
628 int jt_obd_get_device(void)
633 int jt_obd_device(int argc, char **argv)
641 printf("current device is %d - %s\n",
642 cur_device, lcfg_get_devname() ? : "not set");
645 rc = do_device("device", argv[1]);
649 int jt_opt_device(int argc, char **argv)
657 rc = do_device("device", argv[1]);
660 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
662 ret = do_disconnect(argv[0], 0);
670 static void parent_sighandler(int sig)
674 int jt_opt_threads(int argc, char **argv)
676 static char cmdstr[129];
679 struct sigaction sigact;
680 struct sigaction saveact1;
681 struct sigaction saveact2;
682 unsigned long threads;
686 int report_count = -1;
693 threads = strtoul(argv[1], &end, 0);
696 report_count = strtoul(end + 1, &end, 0);
698 if (*end || threads > MAX_THREADS) {
699 fprintf(stderr, "error: %s: invalid thread count '%s'\n",
700 jt_cmdname(argv[0]), argv[1]);
704 verbose = get_verbose(argv[0], argv[2]);
705 if (verbose == BAD_VERBOSE)
709 snprintf(cmdstr, sizeof(cmdstr), "%s", argv[4]);
710 for (i = 5; i < argc; i++)
711 snprintf(cmdstr + strlen(cmdstr),
712 sizeof(cmdstr) - strlen(cmdstr),
715 printf("%s: starting %ld threads on device %s running %s\n",
716 argv[0], threads, argv[3], cmdstr);
719 shmem_reset(threads);
721 sigemptyset(&sigset);
722 sigaddset(&sigset, SIGALRM);
723 sigaddset(&sigset, SIGCHLD);
724 sigprocmask(SIG_BLOCK, &sigset, &saveset);
728 for (i = 1, next_thread = verbose; i <= threads; i++) {
731 fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
732 strerror(rc = errno));
734 } else if (rc == 0) {
735 sigprocmask(SIG_SETMASK, &saveset, NULL);
738 argv[2] = "--device";
739 exit(jt_opt_device(argc - 2, argv + 2));
740 } else if (be_verbose(verbose, NULL, i, &next_thread, threads))
741 printf("%s: thread #%d (PID %d) started\n",
746 if (!thread) { /* parent process */
747 int live_threads = threads;
749 sigemptyset(&sigset);
750 sigemptyset(&sigact.sa_mask);
751 sigact.sa_handler = parent_sighandler;
754 sigaction(SIGALRM, &sigact, &saveact1);
755 sigaction(SIGCHLD, &sigact, &saveact2);
757 while (live_threads > 0) {
761 if (verbose < 0) /* periodic stats */
767 while (live_threads > 0) {
768 ret = waitpid(0, &status, WNOHANG);
774 "error: %s: wait - %s\n",
775 argv[0], strerror(errno));
781 * This is a hack. We _should_ be able
782 * to use WIFEXITED(status) to see if
783 * there was an error, but it appears
784 * to be broken and it always returns 1
787 int err = WEXITSTATUS(status);
789 if (err || WIFSIGNALED(status))
791 "%s: PID %d had rc=%d\n",
800 /* Show stats while all threads running */
802 shmem_snap(threads, live_threads);
803 if (report_count > 0 && --report_count == 0)
807 sigaction(SIGCHLD, &saveact2, NULL);
808 sigaction(SIGALRM, &saveact1, NULL);
811 shmem_total(threads);
812 sigprocmask(SIG_SETMASK, &saveset, NULL);
817 int jt_opt_threads(int argc, char **argv)
819 fprintf(stderr, "%s not-supported in a single-threaded runtime\n",
820 jt_cmdname(argv[0]));
825 int jt_opt_net(int argc, char **argv)
836 rc = jt_ptl_network(2, arg2);
839 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
844 #ifdef HAVE_SERVER_SUPPORT
846 * Place this here so we can build tools that work with
847 * older Lustre versions
849 #ifndef OBD_IOC_NO_TRANSNO
850 #define OBD_IOC_NO_TRANSNO _IOW('f', 140, OBD_IOC_DATA_TYPE)
853 int jt_obd_no_transno(int argc, char **argv)
855 struct obd_ioctl_data data;
856 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
865 memset(buf, 0, sizeof(rawbuf));
867 rc = cfs_get_param_paths(&path, "no_transno");
871 fd = open(path.gl_pathv[0], O_WRONLY);
873 cfs_free_param_data(&path);
877 snprintf(rawbuf, sizeof(rawbuf), "%d", cur_device);
879 count = write(fd, rawbuf, strlen(rawbuf));
883 cfs_free_param_data(&path);
891 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
892 memset(&data, 0, sizeof(data));
893 data.ioc_dev = cur_device;
895 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
897 fprintf(stderr, "error: %s: invalid ioctl\n",
898 jt_cmdname(argv[0]));
901 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_NO_TRANSNO, buf);
903 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
904 strerror(rc = errno));
908 #endif /* HAVE_SERVER_SUPPORT */
910 int jt_obd_set_readonly(int argc, char **argv)
912 struct obd_ioctl_data data;
913 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
916 memset(&data, 0, sizeof(data));
917 data.ioc_dev = cur_device;
922 memset(buf, 0, sizeof(rawbuf));
923 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
925 fprintf(stderr, "error: %s: invalid ioctl\n",
926 jt_cmdname(argv[0]));
929 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_SET_READONLY, buf);
931 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
932 strerror(rc = errno));
937 int jt_obd_abort_recovery(int argc, char **argv)
939 struct obd_ioctl_data data;
940 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
943 memset(&data, 0, sizeof(data));
944 data.ioc_dev = cur_device;
949 memset(buf, 0, sizeof(rawbuf));
950 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
952 fprintf(stderr, "error: %s: invalid ioctl\n",
953 jt_cmdname(argv[0]));
956 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_ABORT_RECOVERY, buf);
958 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
959 strerror(rc = errno));
964 int jt_get_version(int argc, char **argv)
972 rc = llapi_get_version_string(version, sizeof(version));
974 printf("Lustre version: %s\n", LUSTRE_VERSION_STRING);
976 printf("Lustre version: %s\n", version);
981 static void print_obd_line(char *s)
983 const char *param = "osc/%s/ost_conn_uuid";
984 char buf[MAX_STRING_SIZE];
985 char obd_name[MAX_OBD_NAME];
990 /* obd device type is the first 3 characters of param name */
991 snprintf(buf, sizeof(buf), " %%*d %%*s %.3s %%%zus %%*s %%*d ",
992 param, sizeof(obd_name) - 1);
993 if (sscanf(s, buf, obd_name) == 0)
995 if (cfs_get_param_paths(&path, param, obd_name) != 0)
997 fp = fopen(path.gl_pathv[0], "r");
999 /* need to free path data before retry */
1000 cfs_free_param_data(&path);
1002 if (param[0] == 'o') { /* failed with osc, try mdc */
1003 param = "mdc/%s/mds_conn_uuid";
1010 /* should not ignore fgets(3)'s return value */
1011 if (!fgets(buf, sizeof(buf), fp)) {
1012 fprintf(stderr, "reading from %s: %s", buf, strerror(errno));
1018 cfs_free_param_data(&path);
1020 /* trim trailing newlines */
1021 ptr = strrchr(buf, '\n');
1025 ptr = strrchr(s, '\n');
1028 printf("%s%s%s\n", s, buf[0] ? " " : "", buf);
1031 /* get device list by ioctl */
1032 int jt_obd_list_ioctl(int argc, char **argv)
1035 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1036 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buf;
1040 /* Just ignore a -t option. Only supported with /proc. */
1041 else if (argc == 2 && strcmp(argv[1], "-t") != 0)
1044 for (index = 0;; index++) {
1045 memset(buf, 0, sizeof(rawbuf));
1046 data->ioc_version = OBD_IOCTL_VERSION;
1048 sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(*data), 8);
1049 data->ioc_inlbuf1 = buf + __ALIGN_KERNEL(sizeof(*data), 8);
1050 data->ioc_len = obd_ioctl_packlen(data);
1051 data->ioc_count = index;
1053 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_GETDEVICE, buf);
1056 printf("%s\n", (char *)data->ioc_bulk);
1059 if (errno == ENOENT)
1060 /* no device or the last device */
1064 "Error getting device list: %s: check dmesg\n",
1070 int jt_obd_list(int argc, char **argv)
1072 char buf[MAX_STRING_SIZE];
1081 if (strcmp(argv[1], "-t") == 0)
1087 if (cfs_get_param_paths(&path, "devices") ||
1088 !(fp = fopen(path.gl_pathv[0], "r"))) {
1089 cfs_free_param_data(&path);
1091 return jt_obd_list_ioctl(argc, argv);
1094 while (fgets(buf, sizeof(buf), fp) != NULL)
1096 print_obd_line(buf);
1100 cfs_free_param_data(&path);
1105 struct jt_fid_space {
1111 int jt_obd_alloc_fids(struct jt_fid_space *space, struct lu_fid *fid,
1116 if (space->jt_seq == 0 || space->jt_id == space->jt_width) {
1117 struct obd_ioctl_data data;
1118 char rawbuf[MAX_IOC_BUFLEN];
1123 memset(&data, 0, sizeof(data));
1124 data.ioc_dev = cur_device;
1126 data.ioc_pbuf1 = (char *)&seqnr;
1127 data.ioc_plen1 = sizeof(seqnr);
1129 data.ioc_pbuf2 = (char *)&max_count;
1130 data.ioc_plen2 = sizeof(max_count);
1132 memset(buf, 0, sizeof(rawbuf));
1133 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1135 fprintf(stderr, "error: invalid ioctl rc = %d\n", rc);
1139 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_ECHO_ALLOC_SEQ, buf);
1141 fprintf(stderr, "ioctl error: rc = %d\n", rc);
1145 space->jt_seq = *(__u64 *)data.ioc_pbuf1;
1146 space->jt_width = *(int *)data.ioc_pbuf2;
1149 fid->f_seq = space->jt_seq;
1150 fid->f_oid = space->jt_id;
1153 space->jt_id = space->jt_id + *count;
1154 if (space->jt_id > space->jt_width)
1155 space->jt_id = space->jt_width;
1157 *count = space->jt_id - fid->f_oid;
1161 #define MD_STEP_COUNT 1000
1162 int jt_obd_md_common(int argc, char **argv, int cmd)
1164 struct obd_ioctl_data data;
1165 struct timeval start;
1166 struct timeval end_time;
1167 char rawbuf[MAX_IOC_BUFLEN];
1172 char *parent_basedir = NULL;
1174 int parent_base_id = 0;
1175 int parent_count = 1;
1176 __u64 child_base_id = -1;
1177 int stripe_count = 0;
1178 int stripe_index = -1;
1184 __u64 total_count = 0;
1186 struct jt_fid_space fid_space = {0};
1188 struct option long_opts[] = {
1189 { .val = 'b', .name = "child_base_id",
1190 .has_arg = required_argument },
1191 { .val = 'c', .name = "stripe_count",
1192 .has_arg = required_argument },
1193 { .val = 'd', .name = "parent_basedir",
1194 .has_arg = required_argument },
1195 { .val = 'D', .name = "parent_dircount",
1196 .has_arg = required_argument },
1197 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
1198 { .val = 'm', .name = "mode", .has_arg = required_argument },
1199 { .val = 'n', .name = "count", .has_arg = required_argument },
1200 { .val = 't', .name = "time", .has_arg = required_argument },
1201 { .val = 'v', .name = "version", .has_arg = no_argument },
1204 while ((c = getopt_long(argc, argv, "b:c:d:D:m:n:t:v",
1205 long_opts, NULL)) >= 0) {
1208 child_base_id = strtoull(optarg, &end, 0);
1211 "error: %s: bad child_base_id '%s'\n",
1212 jt_cmdname(argv[0]), optarg);
1217 stripe_count = strtoul(optarg, &end, 0);
1220 "error: %s: bad stripe count '%s'\n",
1221 jt_cmdname(argv[0]), optarg);
1226 parent_basedir = optarg;
1229 parent_count = strtoul(optarg, &end, 0);
1232 "error: %s: bad parent count '%s'\n",
1233 jt_cmdname(argv[0]), optarg);
1238 stripe_index = strtoul(optarg, &end, 0);
1241 "error: %s: bad stripe index '%s'\n",
1242 jt_cmdname(argv[0]), optarg);
1247 mode = strtoul(optarg, &end, 0);
1249 fprintf(stderr, "error: %s: bad mode '%s'\n",
1250 jt_cmdname(argv[0]), optarg);
1255 total_count = strtoul(optarg, &end, 0);
1256 if (*end || total_count == 0) {
1257 fprintf(stderr, "%s: bad child count '%s'\n",
1258 jt_cmdname(argv[0]), optarg);
1263 seconds = strtoull(optarg, &end, 0);
1265 fprintf(stderr, "error: %s: seconds '%s'\n",
1266 jt_cmdname(argv[0]), optarg);
1275 "error: %s: option '%s' unrecognized\n",
1276 argv[0], argv[optind - 1]);
1281 memset(&data, 0, sizeof(data));
1282 data.ioc_dev = cur_device;
1283 if (child_base_id == -1) {
1286 name = argv[optind];
1289 if (optind < argc) {
1291 "child_base_id and name can not specified at the same time\n");
1296 if (stripe_count == 0 && stripe_index != -1) {
1298 "If stripe_count is 0, stripe_index can not be specified\n");
1302 if (total_count == 0 && seconds == 0) {
1303 fprintf(stderr, "count or seconds needs to be indicated\n");
1307 if (parent_count <= 0) {
1308 fprintf(stderr, "parent count must < 0\n");
1315 /* threads interleave */
1316 if (parent_base_id != -1)
1317 parent_base_id += (thread - 1) % parent_count;
1319 if (child_base_id != -1)
1320 child_base_id += (thread - 1) *
1321 (MAX_BASE_ID / nthreads);
1323 shmem_start_time_locked();
1328 * If parent directory is not specified, try to get the directory
1331 if (!parent_basedir) {
1336 "parent_basedir or name must be indicated!\n");
1339 /*Get directory and name from name*/
1340 last_lash = strrchr(name, '/');
1341 if (!last_lash || name[0] != '/') {
1342 fprintf(stderr, "Can not locate %s\n", name);
1346 if (last_lash == name) {
1347 sprintf(dirname, "%s", "/");
1350 int namelen = (unsigned long)last_lash -
1351 (unsigned long)name + 1;
1352 snprintf(dirname, namelen, "%s", name);
1353 name = last_lash + 1;
1356 data.ioc_pbuf1 = dirname;
1357 data.ioc_plen1 = strlen(dirname);
1359 data.ioc_pbuf2 = name;
1360 data.ioc_plen2 = strlen(name);
1363 data.ioc_pbuf2 = name;
1364 data.ioc_plen2 = strlen(name);
1366 if (parent_base_id > 0)
1367 sprintf(dirname, "%s%d", parent_basedir,
1370 sprintf(dirname, "%s", parent_basedir);
1371 data.ioc_pbuf1 = dirname;
1372 data.ioc_plen1 = strlen(dirname);
1375 if (cmd == ECHO_MD_MKDIR || cmd == ECHO_MD_RMDIR)
1376 create_mode = S_IFDIR;
1378 create_mode = S_IFREG;
1380 data.ioc_obdo1.o_mode = mode | S_IFDIR;
1381 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
1382 OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1383 data.ioc_command = cmd;
1385 gettimeofday(&start, NULL);
1386 while (shmem_running()) {
1387 struct lu_fid fid = { 0 };
1389 if (child_base_id != -1)
1390 data.ioc_obdo2.o_oi.oi.oi_id = child_base_id;
1391 data.ioc_obdo2.o_mode = mode | create_mode;
1392 data.ioc_obdo2.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
1393 OBD_MD_FLMODE | OBD_MD_FLFLAGS |
1395 data.ioc_obdo2.o_misc = stripe_count;
1396 data.ioc_obdo2.o_stripe_idx = stripe_index;
1398 if (total_count > 0) {
1399 if ((total_count - count) > MD_STEP_COUNT)
1400 data.ioc_count = MD_STEP_COUNT;
1402 data.ioc_count = total_count - count;
1404 data.ioc_count = MD_STEP_COUNT;
1407 if (cmd == ECHO_MD_CREATE || cmd == ECHO_MD_MKDIR) {
1408 /*Allocate fids for the create */
1409 rc = jt_obd_alloc_fids(&fid_space, &fid,
1412 fprintf(stderr, "Allocate fids error %d.\n",
1416 data.ioc_obdo1.o_oi.oi_fid = fid;
1419 child_base_id += data.ioc_count;
1420 count += data.ioc_count;
1422 memset(buf, 0, sizeof(rawbuf));
1423 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1425 fprintf(stderr, "error: %s: invalid ioctl %d\n",
1426 jt_cmdname(argv[0]), rc);
1430 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_ECHO_MD, buf);
1432 fprintf(stderr, "error: %s: %s\n",
1433 jt_cmdname(argv[0]), strerror(rc = errno));
1436 shmem_bump(data.ioc_count);
1438 gettimeofday(&end_time, NULL);
1439 diff = difftime(&end_time, &start);
1440 if (seconds > 0 && (__u64)diff > seconds)
1443 if (count >= total_count && total_count > 0)
1447 if (count > 0 && version) {
1448 gettimeofday(&end_time, NULL);
1449 diff = difftime(&end_time, &start);
1450 printf("%s: %d in %.3fs (%.3f /s): %s",
1451 jt_cmdname(argv[0]), count, diff,
1452 (double)count / diff, ctime(&end_time.tv_sec));
1458 shmem_end_time_locked();
1465 int jt_obd_test_create(int argc, char **argv)
1467 return jt_obd_md_common(argc, argv, ECHO_MD_CREATE);
1470 int jt_obd_test_mkdir(int argc, char **argv)
1472 return jt_obd_md_common(argc, argv, ECHO_MD_MKDIR);
1475 int jt_obd_test_destroy(int argc, char **argv)
1477 return jt_obd_md_common(argc, argv, ECHO_MD_DESTROY);
1480 int jt_obd_test_rmdir(int argc, char **argv)
1482 return jt_obd_md_common(argc, argv, ECHO_MD_RMDIR);
1485 int jt_obd_test_lookup(int argc, char **argv)
1487 return jt_obd_md_common(argc, argv, ECHO_MD_LOOKUP);
1490 int jt_obd_test_setxattr(int argc, char **argv)
1492 return jt_obd_md_common(argc, argv, ECHO_MD_SETATTR);
1495 int jt_obd_test_md_getattr(int argc, char **argv)
1497 return jt_obd_md_common(argc, argv, ECHO_MD_GETATTR);
1500 int jt_obd_create(int argc, char **argv)
1502 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1503 struct obd_ioctl_data data;
1504 struct timeval next_time;
1505 __u64 count = 1, next_count, base_id = 1;
1506 int verbose = 1, mode = 0100644, rc = 0, i;
1509 memset(&data, 0, sizeof(data));
1510 data.ioc_dev = cur_device;
1511 if (argc < 2 || argc > 4)
1514 count = strtoull(argv[1], &end, 0);
1516 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1517 jt_cmdname(argv[0]), argv[1]);
1522 mode = strtoul(argv[2], &end, 0);
1524 fprintf(stderr, "error: %s: invalid mode '%s'\n",
1525 jt_cmdname(argv[0]), argv[2]);
1528 if (!(mode & S_IFMT))
1533 verbose = get_verbose(argv[0], argv[3]);
1534 if (verbose == BAD_VERBOSE)
1538 printf("%s: %jd objects\n", jt_cmdname(argv[0]), (uintmax_t)count);
1539 gettimeofday(&next_time, NULL);
1540 next_time.tv_sec -= verbose;
1542 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1543 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1545 * base_id is 1 so we don't need to worry about it being
1546 * greater than OBIF_MAX_OID
1548 data.ioc_obdo1.o_oi.oi_fid.f_oid = base_id;
1549 data.ioc_obdo1.o_mode = mode;
1550 data.ioc_obdo1.o_uid = 0;
1551 data.ioc_obdo1.o_gid = 0;
1552 data.ioc_obdo1.o_projid = 0;
1553 data.ioc_obdo1.o_valid = OBD_MD_FLTYPE | OBD_MD_FLMODE |
1554 OBD_MD_FLID | OBD_MD_FLUID |
1555 OBD_MD_FLGID | OBD_MD_FLGROUP |
1558 memset(buf, 0, sizeof(rawbuf));
1559 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1561 fprintf(stderr, "error: %s: invalid ioctl\n",
1562 jt_cmdname(argv[0]));
1565 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CREATE, buf);
1566 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
1569 fprintf(stderr, "error: %s: #%d - %s\n",
1570 jt_cmdname(argv[0]), i, strerror(rc = errno));
1573 if (!(data.ioc_obdo1.o_valid & OBD_MD_FLID)) {
1574 fprintf(stderr, "error: %s: oid not valid #%d:%#jx\n",
1575 jt_cmdname(argv[0]), i,
1576 (uintmax_t)data.ioc_obdo1.o_valid);
1581 if (be_verbose(verbose, &next_time, i, &next_count, count))
1582 printf("%s: #%d is object id %#jx\n",
1583 jt_cmdname(argv[0]), i,
1584 (uintmax_t)ostid_id(&data.ioc_obdo1.o_oi));
1590 int jt_obd_setattr(int argc, char **argv)
1592 struct obd_ioctl_data data;
1593 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1599 memset(&data, 0, sizeof(data));
1600 data.ioc_dev = cur_device;
1604 objid = strtoull(argv[1], &end, 0);
1606 fprintf(stderr, "error: %s: objid '%s' is not a number\n",
1607 jt_cmdname(argv[0]), argv[1]);
1611 if (objid >= OBIF_MAX_OID) {
1612 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1613 jt_cmdname(argv[0]), argv[1]);
1617 mode = strtoul(argv[2], &end, 0);
1619 fprintf(stderr, "error: %s: invalid mode '%s'\n",
1620 jt_cmdname(argv[0]), argv[2]);
1624 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1625 data.ioc_obdo1.o_mode = S_IFREG | mode;
1626 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1627 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
1629 memset(buf, 0, sizeof(rawbuf));
1630 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1632 fprintf(stderr, "error: %s: invalid ioctl\n",
1633 jt_cmdname(argv[0]));
1636 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, buf);
1638 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1639 strerror(rc = errno));
1644 int jt_obd_test_setattr(int argc, char **argv)
1646 struct obd_ioctl_data data;
1647 struct timeval start, next_time;
1648 __u64 i, count, next_count;
1649 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1655 if (argc < 2 || argc > 4)
1658 memset(&data, 0, sizeof(data));
1659 data.ioc_dev = cur_device;
1660 count = strtoull(argv[1], &end, 0);
1662 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1663 jt_cmdname(argv[0]), argv[1]);
1668 verbose = get_verbose(argv[0], argv[2]);
1669 if (verbose == BAD_VERBOSE)
1674 if (argv[3][0] == 't') {
1675 objid = strtoull(argv[3] + 1, &end, 0);
1677 objid += thread - 1;
1679 objid = strtoull(argv[3], &end, 0);
1682 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1683 jt_cmdname(argv[0]), argv[3]);
1688 gettimeofday(&start, NULL);
1689 next_time.tv_sec = start.tv_sec - verbose;
1690 next_time.tv_usec = start.tv_usec;
1692 printf("%s: setting %jd attrs (objid %#jx): %s",
1693 jt_cmdname(argv[0]), (uintmax_t)count,
1694 (uintmax_t)objid, ctime(&start.tv_sec));
1696 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1697 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1698 if (objid >= OBIF_MAX_OID) {
1699 fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
1700 jt_cmdname(argv[0]), (unsigned long long)objid);
1704 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1705 data.ioc_obdo1.o_mode = S_IFREG;
1706 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
1708 memset(buf, 0, sizeof(rawbuf));
1709 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1711 fprintf(stderr, "error: %s: invalid ioctl\n",
1712 jt_cmdname(argv[0]));
1715 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, &data);
1718 fprintf(stderr, "error: %s: #%jd - %d:%s\n",
1719 jt_cmdname(argv[0]), (uintmax_t)i,
1720 errno, strerror(rc = errno));
1723 if (be_verbose(verbose, &next_time, i, &next_count, count))
1724 printf("%s: set attr #%jd\n",
1725 jt_cmdname(argv[0]), (uintmax_t)i);
1732 gettimeofday(&end, NULL);
1734 diff = difftime(&end, &start);
1738 printf("%s: %jd attrs in %.3fs (%.3f attr/s): %s",
1739 jt_cmdname(argv[0]), (uintmax_t)i, diff,
1740 i / diff, ctime(&end.tv_sec));
1745 int jt_obd_destroy(int argc, char **argv)
1747 struct obd_ioctl_data data;
1748 struct timeval next_time;
1749 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1750 __u64 count = 1, next_count;
1756 memset(&data, 0, sizeof(data));
1757 data.ioc_dev = cur_device;
1758 if (argc < 2 || argc > 4)
1762 id = strtoull(argv[1], &end, 0);
1763 if (*end || id == 0 || errno != 0) {
1764 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1765 jt_cmdname(argv[0]), argv[1]);
1769 count = strtoull(argv[2], &end, 0);
1772 "error: %s: invalid iteration count '%s'\n",
1773 jt_cmdname(argv[0]), argv[2]);
1779 verbose = get_verbose(argv[0], argv[3]);
1780 if (verbose == BAD_VERBOSE)
1784 printf("%s: %jd objects\n", jt_cmdname(argv[0]), (uintmax_t)count);
1785 gettimeofday(&next_time, NULL);
1786 next_time.tv_sec -= verbose;
1788 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1789 for (i = 1, next_count = verbose; i <= count && shmem_running();
1791 if (id >= OBIF_MAX_OID) {
1792 fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
1793 jt_cmdname(argv[0]), (unsigned long long)id);
1797 data.ioc_obdo1.o_oi.oi_fid.f_oid = id;
1798 data.ioc_obdo1.o_mode = S_IFREG | 0644;
1799 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLMODE;
1801 memset(buf, 0, sizeof(rawbuf));
1802 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1804 fprintf(stderr, "error: %s: invalid ioctl\n",
1805 jt_cmdname(argv[0]));
1808 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_DESTROY, buf);
1809 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
1812 fprintf(stderr, "error: %s: objid %#jx: %s\n",
1813 jt_cmdname(argv[0]), (uintmax_t)id,
1814 strerror(rc = errno));
1818 if (be_verbose(verbose, &next_time, i, &next_count, count))
1819 printf("%s: #%d is object id %#jx\n",
1820 jt_cmdname(argv[0]), i, (uintmax_t)id);
1826 int jt_obd_getattr(int argc, char **argv)
1828 struct obd_ioctl_data data;
1829 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1837 objid = strtoull(argv[1], &end, 0);
1839 fprintf(stderr, "error: %s: objid '%s' is not a number\n",
1840 jt_cmdname(argv[0]), argv[1]);
1844 if (objid >= OBIF_MAX_OID) {
1845 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1846 jt_cmdname(argv[0]), argv[1]);
1850 memset(&data, 0, sizeof(data));
1851 data.ioc_dev = cur_device;
1852 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1853 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1854 /* to help obd filter */
1855 data.ioc_obdo1.o_mode = 0100644;
1856 data.ioc_obdo1.o_valid = 0xffffffff;
1857 printf("%s: object id %#jx\n", jt_cmdname(argv[0]),
1858 (uintmax_t)ostid_id(&data.ioc_obdo1.o_oi));
1860 memset(buf, 0, sizeof(rawbuf));
1861 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1863 fprintf(stderr, "error: %s: invalid ioctl\n",
1864 jt_cmdname(argv[0]));
1867 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, buf);
1868 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
1870 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1871 strerror(rc = errno));
1873 printf("%s: object id %ju, mode %o\n", jt_cmdname(argv[0]),
1874 (uintmax_t)ostid_id(&data.ioc_obdo1.o_oi),
1875 data.ioc_obdo1.o_mode);
1880 int jt_obd_test_getattr(int argc, char **argv)
1882 struct obd_ioctl_data data;
1883 struct timeval start, next_time;
1884 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1885 __u64 i, count, next_count;
1891 if (argc < 2 || argc > 4)
1894 memset(&data, 0, sizeof(data));
1895 data.ioc_dev = cur_device;
1896 count = strtoull(argv[1], &end, 0);
1898 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1899 jt_cmdname(argv[0]), argv[1]);
1904 verbose = get_verbose(argv[0], argv[2]);
1905 if (verbose == BAD_VERBOSE)
1910 if (argv[3][0] == 't') {
1911 objid = strtoull(argv[3] + 1, &end, 0);
1913 objid += thread - 1;
1915 objid = strtoull(argv[3], &end, 0);
1918 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1919 jt_cmdname(argv[0]), argv[3]);
1924 gettimeofday(&start, NULL);
1925 next_time.tv_sec = start.tv_sec - verbose;
1926 next_time.tv_usec = start.tv_usec;
1928 printf("%s: getting %jd attrs (objid %#jx): %s",
1929 jt_cmdname(argv[0]), (uintmax_t)count,
1930 (uintmax_t)objid, ctime(&start.tv_sec));
1932 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1933 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1934 if (objid >= OBIF_MAX_OID) {
1935 fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
1936 jt_cmdname(argv[0]), (unsigned long long)objid);
1940 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1941 data.ioc_obdo1.o_mode = S_IFREG;
1942 data.ioc_obdo1.o_valid = 0xffffffff;
1943 memset(buf, 0, sizeof(rawbuf));
1944 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1946 fprintf(stderr, "error: %s: invalid ioctl\n",
1947 jt_cmdname(argv[0]));
1950 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, &data);
1953 fprintf(stderr, "error: %s: #%jd - %d:%s\n",
1954 jt_cmdname(argv[0]), (uintmax_t)i,
1955 errno, strerror(rc = errno));
1958 if (be_verbose(verbose, &next_time, i, &next_count, count))
1959 printf("%s: got attr #%jd\n",
1960 jt_cmdname(argv[0]), (uintmax_t)i);
1967 gettimeofday(&end, NULL);
1969 diff = difftime(&end, &start);
1973 printf("%s: %jd attrs in %.3fs (%.3f attr/s): %s",
1974 jt_cmdname(argv[0]), (uintmax_t)i, diff,
1975 i / diff, ctime(&end.tv_sec));
1982 * test_brw <cnt> count
1983 * <r|w[r(repeat)x(noverify)]> mode
1984 * <q|v|#(print interval)> verbosity
1985 * <npages[+offset]> blocksize
1986 * <[[<interleave_threads>]t(inc obj by thread#)]obj> object
1989 int jt_obd_test_brw(int argc, char **argv)
1991 struct obd_ioctl_data data;
1992 struct timeval start, next_time;
1993 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1994 __u64 count, next_count, len, stride, thr_offset = 0, objid = 3;
1995 int write = 0, verbose = 1, cmd, i, rc = 0, pages = 1;
1996 int offset_pages = 0;
1998 int repeat_offset = 0;
1999 unsigned long long ull;
2000 int nthr_per_obj = 0;
2005 if (argc < 2 || argc > 7) {
2006 fprintf(stderr, "error: %s: bad number of arguments: %d\n",
2007 jt_cmdname(argv[0]), argc);
2011 count = strtoull(argv[1], &end, 0);
2013 fprintf(stderr, "error: %s: bad iteration count '%s'\n",
2014 jt_cmdname(argv[0]), argv[1]);
2019 if (argv[2][0] == 'w' || argv[2][0] == '1')
2021 /* else it's a read */
2023 if (argv[2][0] != 0)
2024 for (i = 1; argv[2][i] != 0; i++)
2025 switch (argv[2][i]) {
2036 "Can't parse cmd '%s'\n",
2043 verbose = get_verbose(argv[0], argv[3]);
2044 if (verbose == BAD_VERBOSE)
2049 pages = strtoul(argv[4], &end, 0);
2052 offset_pages = strtoul(end + 1, &end, 0);
2054 if (*end != 0 || offset_pages < 0 || offset_pages >= pages) {
2055 fprintf(stderr, "error: %s: bad npages[+offset] parameter '%s'\n",
2056 jt_cmdname(argv[0]), argv[4]);
2062 if (thread && (n = strtol(argv[5], &end, 0)) > 0 &&
2063 *end == 't' && (ull = strtoull(end + 1, &end, 0)) > 0 &&
2067 } else if (thread && argv[5][0] == 't') {
2069 objid = strtoull(argv[5] + 1, &end, 0);
2072 objid = strtoull(argv[5], &end, 0);
2075 fprintf(stderr, "error: %s: bad objid '%s'\n",
2076 jt_cmdname(argv[0]), argv[5]);
2081 memset(&data, 0, sizeof(data));
2082 data.ioc_dev = cur_device;
2085 * communicate the 'type' of brw test and batching to echo_client.
2086 * don't start. we'd love to refactor this lctl->echo_client
2089 data.ioc_pbuf1 = (void *)1;
2093 switch (argv[6][0]) {
2094 case 'g': /* plug and unplug */
2095 data.ioc_pbuf1 = (void *)2;
2096 data.ioc_plen1 = strtoull(argv[6] + 1, &end, 0);
2098 case 'p': /* prep and commit */
2099 data.ioc_pbuf1 = (void *)3;
2100 data.ioc_plen1 = strtoull(argv[6] + 1, &end, 0);
2104 "error: %s: batching '%s' needs to specify 'p' or 'g'\n",
2105 jt_cmdname(argv[0]), argv[6]);
2110 fprintf(stderr, "error: %s: bad batching '%s'\n",
2111 jt_cmdname(argv[0]), argv[6]);
2114 data.ioc_plen1 *= getpagesize();
2117 len = pages * getpagesize();
2118 thr_offset = offset_pages * getpagesize();
2124 if (nthr_per_obj != 0) {
2125 /* threads interleave */
2126 obj_idx = (thread - 1) / nthr_per_obj;
2128 stride *= nthr_per_obj;
2129 if ((thread - 1) % nthr_per_obj == 0) {
2130 shared_data->body.offsets[obj_idx] =
2131 stride + thr_offset;
2133 thr_offset += ((thread - 1) % nthr_per_obj) * len;
2135 /* threads disjoint */
2136 thr_offset += (thread - 1) * len;
2139 shmem_start_time_locked();
2144 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
2145 if (objid >= OBIF_MAX_OID) {
2146 fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
2147 jt_cmdname(argv[0]), (unsigned long long)objid);
2151 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
2152 data.ioc_obdo1.o_mode = S_IFREG;
2153 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
2154 OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
2155 data.ioc_obdo1.o_flags = (verify ? OBD_FL_DEBUG_CHECK : 0);
2156 data.ioc_count = len;
2157 data.ioc_offset = (repeat_offset ? 0 : thr_offset);
2159 gettimeofday(&start, NULL);
2160 next_time.tv_sec = start.tv_sec - verbose;
2161 next_time.tv_usec = start.tv_usec;
2164 printf("%s: %s %jux%d pages (obj %#jx, off %ju): %s",
2165 jt_cmdname(argv[0]), write ? "writing" : "reading",
2166 (uintmax_t)count, pages, (uintmax_t)objid,
2167 (uintmax_t)data.ioc_offset, ctime(&start.tv_sec));
2169 cmd = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
2170 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
2171 data.ioc_obdo1.o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
2172 memset(buf, 0, sizeof(rawbuf));
2173 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2175 fprintf(stderr, "error: %s: invalid ioctl\n",
2176 jt_cmdname(argv[0]));
2179 rc = l_ioctl(OBD_DEV_ID, cmd, buf);
2182 fprintf(stderr, "error: %s: #%d - %s on %s\n",
2183 jt_cmdname(argv[0]), i, strerror(rc = errno),
2184 write ? "write" : "read");
2186 } else if (be_verbose(verbose, &next_time, i,
2187 &next_count, count)) {
2189 printf("%s: %s number %d @ %jd:%ju for %d\n",
2190 jt_cmdname(argv[0]), write ? "write" : "read", i,
2191 (uintmax_t)ostid_id(&data.ioc_obdo1.o_oi),
2192 (uintmax_t)data.ioc_offset,
2193 (int)(pages * getpagesize()));
2197 if (!repeat_offset) {
2199 if (stride == len) {
2200 data.ioc_offset += stride;
2201 } else if (i < count) {
2204 shared_data->body.offsets[obj_idx];
2205 shared_data->body.offsets[obj_idx] += len;
2209 data.ioc_offset += len;
2210 obj_idx = 0; /* avoids an unused var warning */
2219 gettimeofday(&end, NULL);
2221 diff = difftime(&end, &start);
2225 printf("%s: %s %dx%d pages in %.3fs (%.3f MB/s): %s",
2226 jt_cmdname(argv[0]), write ? "wrote" : "read",
2228 ((double)i * pages * getpagesize()) /
2229 (diff * 1048576.0), ctime(&end.tv_sec));
2235 shmem_end_time_locked();
2242 int jt_obd_lov_getconfig(int argc, char **argv)
2244 struct obd_ioctl_data data;
2245 struct lov_desc desc;
2246 struct obd_uuid *uuidarray;
2247 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2252 memset(&data, 0, sizeof(data));
2253 data.ioc_dev = cur_device;
2259 fd = open(path, O_RDONLY);
2261 fprintf(stderr, "open \"%s\" failed: %s\n", path,
2266 memset(&desc, 0, sizeof(desc));
2267 obd_str2uuid(&desc.ld_uuid, argv[1]);
2268 desc.ld_tgt_count = ((MAX_IOC_BUFLEN - sizeof(data) - sizeof(desc)) /
2269 (sizeof(*uuidarray) + sizeof(*obdgens)));
2272 uuidarray = calloc(desc.ld_tgt_count, sizeof(*uuidarray));
2274 fprintf(stderr, "error: %s: no memory for %d uuid's\n",
2275 jt_cmdname(argv[0]), desc.ld_tgt_count);
2279 obdgens = calloc(desc.ld_tgt_count, sizeof(*obdgens));
2281 fprintf(stderr, "error: %s: no memory for %d generation #'s\n",
2282 jt_cmdname(argv[0]), desc.ld_tgt_count);
2287 memset(buf, 0, sizeof(rawbuf));
2288 data.ioc_inllen1 = sizeof(desc);
2289 data.ioc_inlbuf1 = (char *)&desc;
2290 data.ioc_inllen2 = desc.ld_tgt_count * sizeof(*uuidarray);
2291 data.ioc_inlbuf2 = (char *)uuidarray;
2292 data.ioc_inllen3 = desc.ld_tgt_count * sizeof(*obdgens);
2293 data.ioc_inlbuf3 = (char *)obdgens;
2295 if (llapi_ioctl_pack(&data, &buf, sizeof(rawbuf))) {
2296 fprintf(stderr, "error: %s: invalid ioctl\n",
2297 jt_cmdname(argv[0]));
2301 rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
2302 if (rc == -ENOSPC) {
2307 fprintf(stderr, "error: %s: ioctl error: %s\n",
2308 jt_cmdname(argv[0]), strerror(rc = errno));
2310 struct obd_uuid *uuidp;
2314 if (llapi_ioctl_unpack(&data, buf, sizeof(rawbuf))) {
2315 fprintf(stderr, "error: %s: invalid reply\n",
2316 jt_cmdname(argv[0]));
2320 if (desc.ld_default_stripe_count == (__u32)-1)
2321 printf("default_stripe_count: %d\n", -1);
2323 printf("default_stripe_count: %u\n",
2324 desc.ld_default_stripe_count);
2325 printf("default_stripe_size: %ju\n",
2326 (uintmax_t)desc.ld_default_stripe_size);
2327 printf("default_stripe_offset: %jd\n",
2328 (uintmax_t)desc.ld_default_stripe_offset);
2329 printf("default_stripe_pattern: %u\n", desc.ld_pattern);
2330 printf("obd_count: %u\n", desc.ld_tgt_count);
2331 printf("OBDS:\tobdidx\t\tobdgen\t\t obduuid\n");
2334 for (i = 0; i < desc.ld_tgt_count; i++, uuidp++, genp++)
2335 printf("\t%6u\t%14u\t\t %s\n", i, *genp, (char *)uuidp);
2346 static int do_activate(int argc, char **argv, int flag)
2348 struct obd_ioctl_data data;
2349 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2352 memset(&data, 0, sizeof(data));
2353 data.ioc_dev = cur_device;
2357 /* reuse offset for 'active' */
2358 data.ioc_offset = flag;
2360 memset(buf, 0, sizeof(rawbuf));
2361 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2363 fprintf(stderr, "error: %s: invalid ioctl\n",
2364 jt_cmdname(argv[0]));
2367 rc = l_ioctl(OBD_DEV_ID, IOC_OSC_SET_ACTIVE, buf);
2369 fprintf(stderr, "error: %s: failed: %s\n",
2370 jt_cmdname(argv[0]), strerror(rc = errno));
2376 * Replace nids for given device.
2377 * lctl replace_nids <devicename> <nid1>[,nid2,nid3]
2378 * Command should be started on MGS server.
2379 * Only MGS server should be started (command execution
2380 * returns error in another cases). Command mount
2381 * -t lustre <MDT partition> -o nosvc <mount point>
2382 * can be used for that.
2384 * llogs for MDTs and clients are processed. All
2385 * records copied as is except add_uuid and setup. This records
2386 * are skipped and recorded with new nids and uuid.
2388 * \see mgs_replace_nids
2389 * \see mgs_replace_log
2390 * \see mgs_replace_nids_handler
2392 int jt_replace_nids(int argc, char **argv)
2395 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2396 struct obd_ioctl_data data;
2398 memset(&data, 0, sizeof(data));
2399 data.ioc_dev = get_mgs_device();
2403 data.ioc_inllen1 = strlen(argv[1]) + 1;
2404 data.ioc_inlbuf1 = argv[1];
2406 data.ioc_inllen2 = strlen(argv[2]) + 1;
2407 data.ioc_inlbuf2 = argv[2];
2408 memset(buf, 0, sizeof(rawbuf));
2409 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2411 fprintf(stderr, "error: %s: invalid ioctl\n",
2412 jt_cmdname(argv[0]));
2416 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_REPLACE_NIDS, buf);
2418 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
2419 strerror(rc = errno));
2426 * Clear config logs for given device or filesystem.
2427 * lctl clear_conf <devicename|fsname>
2428 * Command has to be run on MGS node having MGS device mounted with -o
2431 * Configuration logs for filesystem or one particular log is
2432 * processed. New log is created, original log is read, its records
2433 * marked SKIP do not get copied to new log. Others are copied as-is.
2434 * Original file is renamed to log.${time}.bak.
2436 * \see mgs_clear_configs
2437 * \see mgs_replace_log
2438 * \see mgs_clear_config_handler
2440 int jt_lcfg_clear(int argc, char **argv)
2443 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2444 struct obd_ioctl_data data;
2446 memset(&data, 0, sizeof(data));
2447 data.ioc_dev = get_mgs_device();
2451 data.ioc_inllen1 = strlen(argv[1]) + 1;
2452 data.ioc_inlbuf1 = argv[1];
2454 memset(buf, 0, sizeof(rawbuf));
2455 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2457 fprintf(stderr, "error: %s: invalid ioctl\n",
2458 jt_cmdname(argv[0]));
2462 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CLEAR_CONFIGS, buf);
2464 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
2465 strerror(rc = errno));
2471 int jt_obd_deactivate(int argc, char **argv)
2473 return do_activate(argc, argv, 0);
2476 int jt_obd_activate(int argc, char **argv)
2478 return do_activate(argc, argv, 1);
2481 int jt_obd_recover(int argc, char **argv)
2483 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2484 struct obd_ioctl_data data;
2487 memset(&data, 0, sizeof(data));
2488 data.ioc_dev = cur_device;
2493 data.ioc_inllen1 = strlen(argv[1]) + 1;
2494 data.ioc_inlbuf1 = argv[1];
2497 memset(buf, 0, sizeof(rawbuf));
2498 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2500 fprintf(stderr, "error: %s: invalid ioctl\n",
2501 jt_cmdname(argv[0]));
2504 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CLIENT_RECOVER, buf);
2506 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
2507 strerror(rc = errno));
2513 int jt_obd_mdc_lookup(int argc, char **argv)
2515 struct obd_ioctl_data data;
2516 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2517 char *parent, *child;
2518 int rc, fd, verbose = 1;
2520 if (argc < 3 || argc > 4)
2526 verbose = get_verbose(argv[0], argv[3]);
2528 memset(&data, 0, sizeof(data));
2529 data.ioc_dev = cur_device;
2531 data.ioc_inllen1 = strlen(child) + 1;
2532 data.ioc_inlbuf1 = child;
2534 memset(buf, 0, sizeof(rawbuf));
2535 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2537 fprintf(stderr, "error: %s: invalid ioctl\n",
2538 jt_cmdname(argv[0]));
2542 fd = open(parent, O_RDONLY);
2544 fprintf(stderr, "open \"%s\" failed: %s\n", parent,
2549 rc = ioctl(fd, IOC_MDC_LOOKUP, buf);
2551 fprintf(stderr, "error: %s: ioctl error: %s\n",
2552 jt_cmdname(argv[0]), strerror(rc = errno));
2557 rc = llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
2559 fprintf(stderr, "error: %s: invalid reply\n",
2560 jt_cmdname(argv[0]));
2563 printf("%s: mode %o uid %d gid %d\n",
2564 child, data.ioc_obdo1.o_mode, data.ioc_obdo1.o_uid,
2565 data.ioc_obdo1.o_gid);
2571 int jt_lcfg_fork(int argc, char **argv)
2573 struct obd_ioctl_data data;
2574 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2580 memset(&data, 0, sizeof(data));
2581 data.ioc_dev = get_mgs_device();
2582 data.ioc_inllen1 = strlen(argv[1]) + 1;
2583 data.ioc_inlbuf1 = argv[1];
2584 data.ioc_inllen2 = strlen(argv[2]) + 1;
2585 data.ioc_inlbuf2 = argv[2];
2587 memset(buf, 0, sizeof(rawbuf));
2588 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2590 fprintf(stderr, "error: %s: invalid ioctl\n",
2591 jt_cmdname(argv[0]));
2595 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LCFG_FORK, buf);
2597 fprintf(stderr, "error: %s: OBD_IOC_LCFG_FORK failed: %s\n",
2598 jt_cmdname(argv[0]), strerror(errno));
2603 int jt_lcfg_erase(int argc, char **argv)
2605 struct obd_ioctl_data data;
2606 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2610 if (strncmp(argv[2], "-q", strlen("-q")) != 0 &&
2611 strncmp(argv[2], "--quiet", strlen("--quiet")) != 0)
2613 } else if (argc != 2) {
2617 memset(&data, 0, sizeof(data));
2618 data.ioc_dev = get_mgs_device();
2619 data.ioc_inllen1 = strlen(argv[1]) + 1;
2620 data.ioc_inlbuf1 = argv[1];
2622 memset(buf, 0, sizeof(rawbuf));
2623 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2625 fprintf(stderr, "error: %s: invalid ioctl\n",
2626 jt_cmdname(argv[0]));
2630 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LCFG_ERASE, buf);
2632 fprintf(stderr, "error: %s: OBD_IOC_LCFG_ERASE failed: %s\n",
2633 jt_cmdname(argv[0]), strerror(errno));
2638 int jt_llog_catlist(int argc, char **argv)
2640 struct obd_ioctl_data data;
2641 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2647 memset(&data, 0, sizeof(data));
2648 data.ioc_dev = cur_device;
2649 data.ioc_inllen1 = sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(data), 8);
2650 memset(buf, 0, sizeof(rawbuf));
2651 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2653 fprintf(stderr, "error: %s: invalid ioctl\n",
2654 jt_cmdname(argv[0]));
2657 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CATLOGLIST, buf);
2659 fprintf(stdout, "%s", ((struct obd_ioctl_data *)buf)->ioc_bulk);
2661 fprintf(stderr, "OBD_IOC_CATLOGLIST failed: %s\n",
2667 int jt_llog_info(int argc, char **argv)
2669 const struct option long_opts[] = {
2670 /* Allow optional "--catalog" for compatibility with llog commands. */
2671 { .val = 'c', .name = "catalog", .has_arg = required_argument },
2672 { .val = 'h', .name = "help", .has_arg = no_argument },
2674 struct obd_ioctl_data data = { 0 };
2675 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
2676 char *cmd = argv[0];
2677 char *catalog = NULL;
2680 while ((c = getopt_long(argc, argv, "c:h", long_opts, NULL)) != -1) {
2692 /* support "logname" positional parameter */
2696 "%s: catalog is set, unknown argument '%s'\n",
2701 } else if (!catalog || argc > 1) {
2705 data.ioc_dev = cur_device;
2706 data.ioc_inllen1 = strlen(catalog) + 1;
2707 data.ioc_inlbuf1 = catalog;
2708 data.ioc_inllen2 = sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(data), 8) -
2709 __ALIGN_KERNEL(data.ioc_inllen1, 8);
2710 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2712 fprintf(stderr, "%s: ioctl_pack failed for catalog '%s': %s\n",
2713 jt_cmdname(cmd), catalog, strerror(-rc));
2717 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_INFO, buf);
2719 fprintf(stdout, "%s", ((struct obd_ioctl_data *)buf)->ioc_bulk);
2721 fprintf(stderr, "%s: OBD_IOC_LLOG_INFO failed: %s\n",
2722 jt_cmdname(cmd), strerror(errno));
2727 int jt_llog_print_cb(const char *record, void *private)
2729 printf("%s\n", record);
2735 llog_process_records(int (record_cb)(const char *record, void *private),
2736 const char *record, void *private, bool reverse)
2744 ptr = strchr(record, '\n');
2747 rc = record_cb(record, private);
2752 } while (ptr && *(ptr + 1));
2754 tmp = (char *)record;
2756 ptr = strrchr(record, '\n');
2761 while ((ptr = strrchr(record, '\n'))) {
2764 rc = record_cb(tmp, private);
2768 rc = record_cb(record, private);
2777 * Iterate over llog records, typically YAML-formatted configuration logs
2779 * \param logname[in] name of llog file or FID
2780 * \param start[in] first record to process
2781 * \param end[in] last record to process (inclusive)
2782 * \param cb[in] callback for records. Return -ve error, or +ve abort.
2783 * \param private[in,out] private data passed to the \a record_cb function
2784 * \param reverse[in] print the llog records from the beginning or the end
2786 * \retval 0 on success
2787 * others handled by the caller
2789 int jt_llog_print_iter(char *logname, long start, long end,
2790 int (record_cb)(const char *record, void *private),
2791 void *private, bool reverse)
2793 struct obd_ioctl_data data = { 0 };
2794 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2795 char startbuf[16], endbuf[16];
2796 static long inc = sizeof(rawbuf) / 128;
2803 data.ioc_dev = cur_device;
2804 data.ioc_inlbuf1 = logname;
2805 data.ioc_inllen1 = strlen(logname) + 1;
2808 * Estimate about 128 characters per configuration record. Not all
2809 * records will be printed in any case, so they should easily fit. If
2810 * not, the kernel will return -EOVERFLOW and ask for fewer records.
2812 * We don't want to request records from the kernel one-at-a-time, as
2813 * it restarts the config llog iteration from the beginning, so we
2814 * fetch multiple records from the kernel per call and split locally.
2816 for (rec = start; rec < end; rec += inc) {
2817 char *record = ((struct obd_ioctl_data *)buf)->ioc_bulk;
2820 snprintf(startbuf, sizeof(startbuf), "%lu", rec);
2821 snprintf(endbuf, sizeof(endbuf), "%lu",
2822 end < rec + inc - 1 ? end : rec + inc - 1);
2824 /* start and end record numbers are passed as ASCII digits */
2825 data.ioc_inlbuf2 = startbuf;
2826 data.ioc_inllen2 = strlen(startbuf) + 1;
2827 data.ioc_inlbuf3 = endbuf;
2828 data.ioc_inllen3 = strlen(endbuf) + 1;
2830 data.ioc_inllen4 = sizeof(rawbuf) -
2831 __ALIGN_KERNEL(sizeof(data), 8) -
2832 __ALIGN_KERNEL(data.ioc_inllen1, 8) -
2833 __ALIGN_KERNEL(data.ioc_inllen2, 8) -
2834 __ALIGN_KERNEL(data.ioc_inllen3, 8);
2835 memset(buf, 0, sizeof(rawbuf));
2836 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2838 fprintf(stderr, "%s: invalid ioctl data\n", logname);
2842 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_PRINT, buf);
2843 if (rc == -EOVERFLOW && inc > 2) {
2848 fprintf(stderr, "%s: OBD_IOC_LLOG_PRINT failed: %s\n",
2849 logname, strerror(errno));
2854 /* There is no "end of list" marker, record was not modified */
2855 if (strcmp(record, logname) == 0)
2858 rc = llog_process_records(record_cb, record, private, reverse);
2867 static int llog_parse_catalog_start_end(int *argc, char **argv[],
2868 char **catalog, long *start, long *end)
2870 const struct option long_opts[] = {
2871 /* the --catalog option is not required, just for consistency */
2872 { .val = 'c', .name = "catalog", .has_arg = required_argument },
2873 { .val = 'e', .name = "end", .has_arg = required_argument },
2874 { .val = 'h', .name = "help", .has_arg = no_argument },
2875 { .val = 's', .name = "start", .has_arg = required_argument },
2877 char *cmd = (*argv)[0];
2881 if (!catalog || !start || !end)
2884 /* now process command line arguments*/
2885 while ((c = getopt_long(*argc, *argv, "c:e:hs:",
2886 long_opts, NULL)) != -1) {
2892 *end = strtol(optarg, &endp, 0);
2893 if (*endp != '\0') {
2894 fprintf(stderr, "%s: bad end value '%s'\n",
2900 *start = strtol(optarg, &endp, 0);
2901 if (*endp != '\0') {
2902 fprintf(stderr, "%s: bad start value '%s'\n",
2916 * support old optional positional parameters only if they were
2917 * not already specified with named arguments: logname [start [end]]
2922 "%s: catalog is set, unknown argument '%s'\n",
2926 *catalog = (*argv)[0];
2934 "%s: --start is set, unknown argument '%s'\n",
2939 *start = strtol((*argv)[0], &endp, 0);
2940 if (*endp != '\0') {
2941 fprintf(stderr, "%s: bad start value '%s'\n",
2951 "%s: --end is set, unknown argument '%s'\n",
2956 *end = strtol((*argv)[0], &endp, 0);
2957 if (*endp != '\0') {
2958 fprintf(stderr, "%s: bad end value '%s'\n",
2966 fprintf(stderr, "%s: unknown argument '%s'\n", cmd, (*argv)[0]);
2970 if (*end != -1 && *end < *start) {
2971 fprintf(stderr, "%s: end '%lu' less than than start '%lu'\n",
2979 int jt_llog_print(int argc, char **argv)
2981 char *catalog = NULL;
2982 long start = 1, end = -1;
2985 rc = llog_parse_catalog_start_end(&argc, &argv, &catalog, &start, &end);
2989 rc = jt_llog_print_iter(catalog, start, end, jt_llog_print_cb,
2996 * Parse catalog, log ID, and optionally a log index with either optional
2997 * arguments or positional arguments. Only the initial catalog argument
2998 * may be positional with other optional arguments.
3000 * The positional arguments option should eventually be phased out.
3002 static int llog_parse_catalog_log_idx(int *argc, char ***argv, const char *opts,
3003 int max_args, struct obd_ioctl_data *data)
3005 const struct option long_opts[] = {
3006 /* the --catalog option is not required, just for consistency */
3007 { .val = 'c', .name = "catalog", .has_arg = required_argument },
3008 { .val = 'h', .name = "help", .has_arg = no_argument },
3009 { .val = 'i', .name = "log_idx", .has_arg = required_argument },
3010 { .val = 'l', .name = "log_id", .has_arg = required_argument },
3015 if (!data || *argc <= 1)
3018 data->ioc_dev = cur_device;
3020 /* now process command line arguments*/
3021 while ((c = getopt_long(*argc, *argv, opts, long_opts, NULL)) != -1) {
3024 data->ioc_inllen1 = strlen(optarg) + 1;
3025 data->ioc_inlbuf1 = optarg;
3028 data->ioc_inllen3 = strlen(optarg) + 1;
3029 data->ioc_inlbuf3 = optarg;
3031 case 'l': /* The log_id option isn't currently needed for
3032 * cancel as mdt_iocontrol() handles IOC_LLOG_CANCEL,
3033 * but we may as well keep it for now.
3035 data->ioc_inllen2 = strlen(optarg) + 1;
3036 data->ioc_inlbuf2 = optarg;
3047 /* Allow catalog to be specified as first option without --catalog */
3048 if (!data->ioc_inlbuf1 && *argc > 0) {
3049 data->ioc_inlbuf1 = (*argv)[0];
3050 data->ioc_inllen1 = strlen((*argv)[0]) + 1;
3058 int jt_llog_cancel(int argc, char **argv)
3060 struct obd_ioctl_data data = { 0 };
3061 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
3062 char *cmd = argv[0];
3065 /* Parse catalog file (in inlbuf1) and named parameters */
3066 rc = llog_parse_catalog_log_idx(&argc, &argv, "c:hi:l:", 3, &data);
3069 * Handle old positional parameters if not using named parameters,
3070 * either "<catalog> <log_idx>" or "<catalog> <log_id> <log_idx>".
3071 * It was "inlbuf3 = log_idx", and "inlbuf2 = log_id" (ignored by
3072 * config log cancel), and shows why I hate positional parameters.
3075 data.ioc_inllen3 = strlen(argv[0]) + 1;
3076 data.ioc_inlbuf3 = argv[0];
3077 } else if (argc == 2) {
3078 data.ioc_inllen2 = strlen(argv[0]) + 1;
3079 data.ioc_inlbuf2 = argv[0];
3080 data.ioc_inllen3 = strlen(argv[1]) + 1;
3081 data.ioc_inlbuf3 = argv[1];
3084 if (!data.ioc_inlbuf1 || !data.ioc_inlbuf3)
3085 /* missing mandatory parameters */
3088 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3090 fprintf(stderr, "%s: ioctl_pack for catalog '%s' failed: %s\n",
3091 jt_cmdname(cmd), data.ioc_inlbuf1, strerror(-rc));
3095 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CANCEL, buf);
3097 fprintf(stderr, "%s: cancel catalog '%s:%s' failed: %s\n",
3098 jt_cmdname(cmd), data.ioc_inlbuf1, data.ioc_inlbuf3,
3104 int jt_llog_check(int argc, char **argv)
3106 struct obd_ioctl_data data = { 0 };
3107 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
3108 char *catalog = NULL;
3109 char startbuf[16], endbuf[16];
3110 long start = 1, end = -1;
3111 char *cmd = argv[0];
3114 rc = llog_parse_catalog_start_end(&argc, &argv, &catalog, &start, &end);
3121 data.ioc_dev = cur_device;
3122 data.ioc_inllen1 = strlen(catalog) + 1;
3123 data.ioc_inlbuf1 = catalog;
3125 snprintf(startbuf, sizeof(startbuf), "%lu", start);
3126 snprintf(endbuf, sizeof(endbuf), "%lu", end);
3127 /* start and end record numbers are passed as ASCII digits */
3128 data.ioc_inllen2 = strlen(startbuf) + 1;
3129 data.ioc_inlbuf2 = startbuf;
3130 data.ioc_inllen3 = strlen(endbuf) + 1;
3131 data.ioc_inlbuf3 = endbuf;
3133 data.ioc_inllen4 = sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(data), 8) -
3134 __ALIGN_KERNEL(data.ioc_inllen1, 8) -
3135 __ALIGN_KERNEL(data.ioc_inllen2, 8) -
3136 __ALIGN_KERNEL(data.ioc_inllen3, 8);
3137 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3139 fprintf(stderr, "%s: ioctl_pack failed for catalog '%s': %s\n",
3140 jt_cmdname(cmd), data.ioc_inlbuf1, strerror(-rc));
3144 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CHECK, buf);
3146 fprintf(stdout, "%s", ((struct obd_ioctl_data *)buf)->ioc_bulk);
3148 fprintf(stderr, "%s: OBD_IOC_LLOG_CHECK failed: %s\n",
3149 jt_cmdname(cmd), strerror(errno));
3153 int jt_llog_remove(int argc, char **argv)
3155 struct obd_ioctl_data data = { 0 };
3156 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
3157 char *cmd = argv[0];
3160 rc = llog_parse_catalog_log_idx(&argc, &argv, "c:hl:", 2, &data);
3165 if (data.ioc_inlbuf2) {
3167 "%s: --log_id is set, unknown argument '%s'\n",
3168 jt_cmdname(cmd), argv[0]);
3172 data.ioc_inllen2 = strlen(argv[0]) + 1;
3173 data.ioc_inlbuf2 = argv[0];
3175 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3177 fprintf(stderr, "%s: ioctl_pack for catalog '%s' failed: %s\n",
3178 jt_cmdname(cmd), data.ioc_inlbuf1, strerror(-rc));
3182 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_REMOVE, buf);
3184 fprintf(stderr, "%s: cancel catalog '%s:%s' failed: %s\n",
3185 jt_cmdname(cmd), data.ioc_inlbuf1, data.ioc_inlbuf2,
3191 static void signal_server(int sig)
3193 if (sig == SIGINT) {
3194 do_disconnect("sigint", 1);
3197 fprintf(stderr, "%s: got signal %d\n", jt_cmdname("sigint"),
3202 int obd_initialize(int argc, char **argv)
3204 if (shmem_setup() != 0)
3207 register_ioc_dev(OBD_DEV_ID, OBD_DEV_PATH);
3212 void obd_finalize(int argc, char **argv)
3214 struct sigaction sigact;
3216 /* sigact initialization */
3217 sigact.sa_handler = signal_server;
3218 sigfillset(&sigact.sa_mask);
3219 sigact.sa_flags = SA_RESTART;
3220 /* coverity[uninit_use_in_call] */
3221 sigaction(SIGINT, &sigact, NULL);
3224 do_disconnect(argv[0], 1);
3228 * Get the index of the last llog record
3230 * logid: [0x3:0xa:0x0]:0
3235 * \param logname[in] pointer to config log name
3237 * \retval > 0 on success
3240 static long llog_last_index(char *logname)
3242 struct obd_ioctl_data data = { 0 };
3243 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
3247 data.ioc_dev = cur_device;
3248 data.ioc_inllen1 = strlen(logname) + 1;
3249 data.ioc_inlbuf1 = logname;
3250 data.ioc_inllen2 = sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(data), 8) -
3251 __ALIGN_KERNEL(data.ioc_inllen1, 8);
3252 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3254 fprintf(stderr, "%s: ioctl_pack failed for catalog '%s': %s\n",
3255 __func__, logname, strerror(-rc));
3259 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_INFO, buf);
3261 last_index = strstr(((struct obd_ioctl_data *)buf)->ioc_bulk,
3263 return strtol(last_index + 11, NULL, 10);
3271 static char *get_llog_event_name(__u32 cmd)
3273 struct lcfg_type_data *data;
3275 data = lcfg_cmd2data(cmd);
3277 return data->ltd_name;
3281 static char *get_event_filter(__u32 cmd)
3284 char *filter = NULL;
3287 event_name = get_llog_event_name(cmd);
3289 /* 9 bytes for "event: , " */
3290 len = 9 + strlen(event_name);
3291 filter = malloc(len + 1);
3294 memset(filter, 0, len + 1);
3295 snprintf(filter, len, "event: %s, ", event_name);
3303 * Callback to search ostname in llog
3304 * - { index: 23, event: attach, device: lustre-OST0000-osc, type: osc,
3305 * UUID: lustre-clilov_UUID }
3306 * - { index: 24, event: setup, device: lustre-OST0000-osc,
3307 * UUID: lustre-OST0000_UUID, node: 192.168.0.120@tcp }
3308 * - { index: 25, event: add_osc, device: lustre-clilov,
3309 * ost: lustre-OST0000_UUID, index: 0, gen: 1 }
3311 * \param record[in] pointer to llog record
3312 * \param data[in] pointer to ostname
3314 * \retval 1 if ostname is found
3315 * 0 if ostname is not found
3316 * -ENOENT if ostname is deleted
3318 static int llog_search_ost_cb(const char *record, void *data)
3320 char *ostname = data;
3321 char ost_filter[MAX_STRING_SIZE] = {'\0'};
3322 char *add_osc, *del_osc, *setup, *cleanup;
3324 add_osc = get_event_filter(LCFG_LOV_ADD_OBD);
3325 del_osc = get_event_filter(LCFG_LOV_DEL_OBD);
3326 setup = get_event_filter(LCFG_SETUP);
3327 cleanup = get_event_filter(LCFG_CLEANUP);
3328 if (!add_osc || !del_osc || !setup || !cleanup)
3331 if (ostname && ostname[0])
3332 snprintf(ost_filter, sizeof(ost_filter), " %s,", ostname);
3334 if (strstr(record, ost_filter)) {
3335 if (strstr(record, add_osc) || strstr(record, setup))
3337 if (strstr(record, del_osc) || strstr(record, cleanup))
3350 * Search ost in llog
3352 * \param logname[in] pointer to config log name
3353 * \param last_index[in] the index of the last llog record
3354 * \param ostname[in] pointer to ost name
3356 * \retval 1 if ostname is found
3357 * 0 if ostname is not found
3359 static int llog_search_ost(char *logname, long last_index, char *ostname)
3361 long start, end, inc = MAX_IOC_BUFLEN / 128;
3364 for (end = last_index; end > 1; end -= inc) {
3365 start = end - inc > 0 ? end - inc : 1;
3366 rc = jt_llog_print_iter(logname, start, end, llog_search_ost_cb,
3372 return (rc == 1 ? 1 : 0);
3375 struct llog_pool_data {
3376 char lpd_fsname[LUSTRE_MAXFSNAME + 1];
3377 char lpd_poolname[LOV_MAXPOOLNAME + 1];
3378 char lpd_ostname[MAX_OBD_NAME + 1];
3379 enum lcfg_command_type lpd_cmd_type;
3380 bool lpd_pool_exists;
3385 * Called for each formatted line in the config log (within range).
3387 * - { index: 74, event: new_pool, device: tfs-clilov, fsname: tfs, pool: tmp }
3388 * - { index: 77, event: add_pool, device: tfs-clilov, fsname: tfs, pool: tmp,
3389 * ost: tfs-OST0000_UUID }
3390 * - { index: 224, event: remove_pool, device: tfs-clilov, fsname: tfs,
3391 * pool: tmp, ost: tfs-OST0003_UUID }
3392 * - { index: 227, event: del_pool, device: tfs-clilov, fsname: tfs, pool: tmp }
3394 * \param record[in] pointer to llog record
3395 * \param data[in] pointer to llog_pool_data
3397 * \retval 1 if pool or OST is found
3398 * 0 if pool or OST is not found
3399 * -ENOENT if pool or OST is removed
3401 static int llog_search_pool_cb(const char *record, void *data)
3403 struct llog_pool_data *lpd = data;
3404 char pool_filter[MAX_STRING_SIZE] = "";
3405 char *new_pool, *del_pool, *add_pool, *rem_pool;
3407 int fs_pool_len = 0, rc = 0;
3409 new_pool = get_event_filter(LCFG_POOL_NEW);
3410 del_pool = get_event_filter(LCFG_POOL_DEL);
3411 add_pool = get_event_filter(LCFG_POOL_ADD);
3412 rem_pool = get_event_filter(LCFG_POOL_REM);
3413 if (!new_pool || !del_pool || !add_pool || !rem_pool) {
3418 fs_pool_len = 16 + strlen(lpd->lpd_fsname) + strlen(lpd->lpd_poolname);
3419 snprintf(pool_filter, fs_pool_len + 1, "fsname: %s, pool: %s",
3420 lpd->lpd_fsname, lpd->lpd_poolname);
3422 /* search poolname */
3423 found = strstr(record, pool_filter);
3425 (found[fs_pool_len] == ' ' || found[fs_pool_len] == ',')) {
3426 if (strstr(record, new_pool)) {
3427 lpd->lpd_pool_exists = true;
3431 if (strstr(record, del_pool)) {
3432 lpd->lpd_pool_exists = false;
3437 if (lpd->lpd_cmd_type == LCFG_POOL_NEW ||
3438 lpd->lpd_cmd_type == LCFG_POOL_DEL) {
3439 if (strstr(record, add_pool))
3441 if (strstr(record, rem_pool))
3443 } else if (lpd->lpd_ostname && lpd->lpd_ostname[0]) {
3444 if (strstr(record, lpd->lpd_ostname)) {
3445 lpd->lpd_pool_exists = true;
3446 if (strstr(record, add_pool)) {
3447 lpd->lpd_ost_num = 1;
3451 if (strstr(record, rem_pool)) {
3452 lpd->lpd_ost_num = 0;
3472 /* Search pool and its ost in llog
3474 * \param logname[in] pointer to config log name
3475 * \param last_index[in] the index of the last llog record
3476 * \param fsname[in] pointer to filesystem name
3477 * \param poolname[in] pointer pool name
3478 * \param ostname[in] pointer to OST name(OSTnnnn-UUID)
3479 * \param cmd[in] pool command type
3481 * \retval < 0 on error
3482 * 0 if pool is empty or OST is not found
3483 * 1 if pool is not empty or OST is found
3485 static int llog_search_pool(char *logname, long last_index, char *fsname,
3486 char *poolname, char *ostname,
3487 enum lcfg_command_type cmd)
3489 struct llog_pool_data lpd;
3490 long start, end, inc = MAX_IOC_BUFLEN / 128;
3493 memset(&lpd, 0, sizeof(lpd));
3494 lpd.lpd_cmd_type = cmd;
3495 lpd.lpd_pool_exists = false;
3496 lpd.lpd_ost_num = 0;
3497 strncpy(lpd.lpd_fsname, fsname, sizeof(lpd.lpd_fsname) - 1);
3498 if (poolname && poolname[0])
3499 strncpy(lpd.lpd_poolname, poolname,
3500 sizeof(lpd.lpd_poolname) - 1);
3501 if (ostname && ostname[0])
3502 strncpy(lpd.lpd_ostname, ostname, sizeof(lpd.lpd_ostname) - 1);
3504 for (end = last_index; end > 1; end -= inc) {
3505 start = end - inc > 0 ? end - inc : 1;
3506 rc = jt_llog_print_iter(logname, start, end,
3507 llog_search_pool_cb, &lpd, true);
3509 if (rc == 1 && lpd.lpd_pool_exists)
3510 rc = lpd.lpd_ost_num ? 1 : 0;
3511 else if (rc == -ENOENT && lpd.lpd_pool_exists &&
3523 static bool combined_mgs_mds(char *fsname)
3528 rc = cfs_get_param_paths(&path, "mdt/%s-MDT0000", fsname);
3530 cfs_free_param_data(&path);
3532 if (get_mgs_device() > 0 && !rc)
3539 * if pool is NULL, search ostname in target_obd
3540 * if pool is not NULL:
3541 * - if pool not found returns errno < 0
3542 * - if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
3543 * - if ostname is not NULL, returns 1 if OST is in pool and 0 if not
3545 int lctl_search_ost(char *fsname, char *poolname, char *ostname,
3546 enum lcfg_command_type cmd)
3548 char logname[MAX_OBD_NAME] = {'\0'};
3551 if (fsname && fsname[0] == '\0')
3556 if (combined_mgs_mds(fsname))
3557 return llapi_search_ost(fsname, poolname, ostname);
3559 /* fetch the last_index of llog record */
3560 snprintf(logname, sizeof(logname), "%s-client", fsname);
3561 last_index = llog_last_index(logname);
3565 /* if pool is NULL, search ostname in target_obd */
3566 if (!poolname && ostname)
3567 return llog_search_ost(logname, last_index, ostname);
3569 return llog_search_pool(logname, last_index, fsname, poolname,
3573 static int check_pool_cmd(enum lcfg_command_type cmd, char *fsname,
3574 char *poolname, char *ostname)
3578 rc = lctl_search_ost(fsname, poolname, ostname, cmd);
3579 if (rc < 0 && (cmd != LCFG_POOL_NEW)) {
3580 fprintf(stderr, "Pool %s.%s not found\n",
3586 case LCFG_POOL_NEW: {
3591 fprintf(stderr, "Pool %s.%s already exists\n",
3597 case LCFG_POOL_DEL: {
3603 "Pool %s.%s not empty, please remove all members\n",
3609 case LCFG_POOL_ADD: {
3611 fprintf(stderr, "OST %s is already in pool %s.%s\n",
3612 ostname, fsname, poolname);
3615 rc = lctl_search_ost(fsname, NULL, ostname, cmd);
3617 fprintf(stderr, "OST %s is not part of the '%s' fs.\n",
3623 case LCFG_POOL_REM: {
3625 fprintf(stderr, "OST %s not found in pool %s.%s\n",
3626 ostname, fsname, poolname);
3638 * This check only verifies that the changes have been "pushed out" to
3639 * the client successfully. This involves waiting for a config update,
3640 * and so may fail because of problems in that code or post-command
3641 * network loss. So reporting a warning is appropriate, but not a failure.
3643 static int check_pool_cmd_result(enum lcfg_command_type cmd, char *fsname,
3644 char *poolname, char *ostname)
3650 case LCFG_POOL_NEW: {
3652 rc = lctl_search_ost(fsname, poolname, NULL, cmd);
3658 } while ((rc < 0) && (cpt > 0));
3660 fprintf(stderr, "Pool %s.%s created\n",
3665 fprintf(stderr, "Warning, pool %s.%s not found\n", fsname,
3669 case LCFG_POOL_DEL: {
3671 rc = lctl_search_ost(fsname, poolname, NULL, cmd);
3677 } while ((rc >= 0) && (cpt > 0));
3679 fprintf(stderr, "Pool %s.%s destroyed\n",
3684 fprintf(stderr, "Warning, pool %s.%s still found\n", fsname,
3688 case LCFG_POOL_ADD: {
3690 rc = lctl_search_ost(fsname, poolname, ostname, cmd);
3696 } while ((rc != 1) && (cpt > 0));
3698 fprintf(stderr, "OST %s added to pool %s.%s\n",
3699 ostname, fsname, poolname);
3702 fprintf(stderr, "Warning, OST %s not found in pool %s.%s\n",
3703 ostname, fsname, poolname);
3706 case LCFG_POOL_REM: {
3708 rc = lctl_search_ost(fsname, poolname, ostname, cmd);
3714 } while ((rc == 1) && (cpt > 0));
3716 fprintf(stderr, "OST %s removed from pool %s.%s\n",
3717 ostname, fsname, poolname);
3720 fprintf(stderr, "Warning, OST %s still found in pool %s.%s\n",
3721 ostname, fsname, poolname);
3730 static int check_and_complete_ostname(char *fsname, char *ostname)
3733 char real_ostname[MAX_OBD_NAME + 1];
3736 /* if OST name does not start with fsname, we add it */
3737 /* if not check if the fsname is the right one */
3738 ptr = strchr(ostname, '-');
3740 sprintf(real_ostname, "%s-%s", fsname, ostname);
3741 } else if (strncmp(ostname, fsname, strlen(fsname)) != 0) {
3742 fprintf(stderr, "%s does not start with fsname %s\n",
3746 if (strlen(ostname) > sizeof(real_ostname) - 1)
3749 strncpy(real_ostname, ostname, sizeof(real_ostname));
3752 /* real_ostname is fsname-????? */
3753 ptr = real_ostname + strlen(fsname) + 1;
3754 if (strncmp(ptr, "OST", 3) != 0) {
3755 fprintf(stderr, "%s does not start by %s-OST nor OST\n",
3759 /* real_ostname is fsname-OST????? */
3761 for (i = 0; i < 4; i++) {
3762 if (!isxdigit(*ptr)) {
3764 "ost's index in %s is not an hexa number\n",
3770 /* real_ostname is fsname-OSTXXXX????? */
3771 /* if OST name does not end with _UUID, we add it */
3773 strcat(real_ostname, "_UUID");
3774 } else if (strcmp(ptr, "_UUID") != 0) {
3776 "ostname %s does not end with _UUID\n", ostname);
3779 /* real_ostname is fsname-OSTXXXX_UUID */
3780 strcpy(ostname, real_ostname);
3784 /* returns 0 or -errno */
3785 static int pool_cmd(enum lcfg_command_type cmd, char *cmdname,
3786 char *fullpoolname, char *fsname, char *poolname,
3790 struct obd_ioctl_data data;
3791 struct lustre_cfg_bufs bufs;
3792 struct lustre_cfg *lcfg;
3793 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
3795 rc = check_pool_cmd(cmd, fsname, poolname, ostname);
3798 "Can't verify pool command since there is no local MDT or client, proceeding anyhow...\n");
3802 lustre_cfg_bufs_reset(&bufs, NULL);
3803 lustre_cfg_bufs_set_string(&bufs, 0, cmdname);
3804 lustre_cfg_bufs_set_string(&bufs, 1, fullpoolname);
3806 lustre_cfg_bufs_set_string(&bufs, 2, ostname);
3808 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
3811 lustre_cfg_init(lcfg, cmd, &bufs);
3813 memset(&data, 0, sizeof(data));
3814 rc = data.ioc_dev = get_mgs_device();
3818 data.ioc_type = LUSTRE_CFG_TYPE;
3819 data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
3820 lcfg->lcfg_buflens);
3821 data.ioc_pbuf1 = (void *)lcfg;
3823 memset(buf, 0, sizeof(rawbuf));
3824 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3826 fprintf(stderr, "error: %s: invalid ioctl\n",
3827 jt_cmdname(cmdname));
3831 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_POOL, buf);
3838 "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",
3839 jt_cmdname(cmdname), LOV_MAXPOOLNAME, LUSTRE_MAXFSNAME);
3843 "error: %s can contain only alphanumeric characters, underscores, and dashes besides the required '.'\n",
3844 jt_cmdname(cmdname));
3853 * Format and send the ioctl to the MGS.
3855 * \param cmd IOCTL to send
3856 * \param ret_data void pointer to return anything from
3858 * \param num_args number of arguments to pack into the
3860 * \param argv[] variable number of string arguments
3862 * \retval 0 on success
3864 static int nodemap_cmd(enum lcfg_command_type cmd, void *ret_data,
3865 unsigned int ret_size, ...)
3870 struct lustre_cfg_bufs bufs;
3871 struct obd_ioctl_data data;
3872 struct lustre_cfg *lcfg;
3873 char rawbuf[MAX_IOC_BUFLEN];
3877 lustre_cfg_bufs_reset(&bufs, NULL);
3879 va_start(ap, ret_size);
3880 arg = va_arg(ap, char *);
3882 lustre_cfg_bufs_set_string(&bufs, i, arg);
3884 arg = va_arg(ap, char *);
3888 lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
3891 lustre_cfg_init(lcfg, cmd, &bufs);
3893 memset(&data, 0, sizeof(data));
3894 rc = data.ioc_dev = get_mgs_device();
3898 data.ioc_type = LUSTRE_CFG_TYPE;
3899 data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
3900 lcfg->lcfg_buflens);
3901 data.ioc_pbuf1 = (void *)lcfg;
3903 memset(buf, 0, sizeof(rawbuf));
3904 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3907 "error: invalid ioctl: %08x errno: %d with rc=%d\n",
3912 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_NODEMAP, buf);
3915 "error: invalid ioctl: %08x errno: %d with rc=%d\n",
3921 rc = llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
3925 if (ret_size > data.ioc_plen1)
3926 ret_size = data.ioc_plen1;
3928 memcpy(ret_data, data.ioc_pbuf1, ret_size);
3937 * activate nodemap functions
3939 * \param argc number of args
3940 * \param argv[] variable string arguments
3942 * argv[0] 1 for activate or 0 for deactivate
3944 * \retval 0 on success
3946 int jt_nodemap_activate(int argc, char **argv)
3950 rc = nodemap_cmd(LCFG_NODEMAP_ACTIVATE, NULL, 0, argv[0], argv[1],
3964 * \param argc number of args
3965 * \param argv[] variable string arguments
3967 * argv[0] nodemap name
3969 * \retval 0 on success
3971 int jt_nodemap_add(int argc, char **argv)
3975 rc = llapi_nodemap_exists(argv[1]);
3977 fprintf(stderr, "error: %s existing nodemap name\n", argv[1]);
3981 rc = nodemap_cmd(LCFG_NODEMAP_ADD, NULL, 0, argv[0], argv[1], NULL);
3994 * \param argc number of args
3995 * \param argv[] variable string arguments
3997 * argv[0] nodemap name
3999 * \retval 0 on success
4001 int jt_nodemap_del(int argc, char **argv)
4005 rc = llapi_nodemap_exists(argv[1]);
4007 fprintf(stderr, "error: %s not existing nodemap name\n",
4011 rc = nodemap_cmd(LCFG_NODEMAP_DEL, NULL, 0, argv[0], argv[1], NULL);
4022 * test a nid for nodemap membership
4024 * \param argc number of args
4025 * \param argv[] variable string arguments
4027 * argv[0] properly formatted nid
4029 * \retval 0 on success
4031 int jt_nodemap_test_nid(int argc, char **argv)
4033 char rawbuf[MAX_IOC_BUFLEN];
4036 rc = nodemap_cmd(LCFG_NODEMAP_TEST_NID, &rawbuf, sizeof(rawbuf),
4037 argv[0], argv[1], NULL);
4039 printf("%s\n", (char *)rawbuf);
4045 * test a nodemap id pair for mapping
4047 * \param argc number of args
4048 * \param argv[[] variable string arguments
4050 * \retval 0 on success
4052 * The argv array should contain the nodemap name, the id
4053 * to checking the mapping on, and the id type (UID or GID)
4056 int jt_nodemap_test_id(int argc, char **argv)
4058 char rawbuf[MAX_IOC_BUFLEN];
4059 char *nidstr = NULL;
4061 char *typestr = NULL;
4065 static struct option long_opts[] = {
4066 { .val = 'i', .name = "id", .has_arg = required_argument },
4067 { .val = 'n', .name = "nid", .has_arg = required_argument },
4068 { .val = 't', .name = "idtype",
4069 .has_arg = required_argument },
4072 while ((c = getopt_long(argc, argv, "n:t:i:",
4073 long_opts, NULL)) != -1) {
4087 if (!nidstr || !typestr || !idstr) {
4089 "usage: nodemap_test_id --nid <nid> --idtype [uid|gid] --id <id>\n");
4093 rc = nodemap_cmd(LCFG_NODEMAP_TEST_ID, &rawbuf, sizeof(rawbuf),
4094 argv[0], nidstr, typestr, idstr);
4096 printf("%s\n", (char *)rawbuf);
4104 * \param nodemap_range --range string
4105 * \param nid_range nid range string, min_nid:max_nid
4107 * \retval 0 on success
4109 static int parse_nid_range(char *nodemap_range, char *nid_range, int range_len)
4111 char min_nid[LNET_NIDSTR_SIZE + 1];
4112 char max_nid[LNET_NIDSTR_SIZE + 1];
4113 struct list_head nidlist;
4116 INIT_LIST_HEAD(&nidlist);
4118 if (cfs_parse_nidlist(nodemap_range, strlen(nodemap_range),
4121 "error: nodemap_xxx_range: can't parse nid range: %s\n",
4126 rc = cfs_nidrange_find_min_max(&nidlist, &min_nid[0], &max_nid[0],
4131 "error: nodemap_xxx_range: nid range uses currently unsupported features\n");
4132 else if (rc == -ERANGE)
4134 "error: nodemap_xxx_range: nodemap ranges must be contiguous\n");
4139 snprintf(nid_range, range_len, "%s:%s", min_nid, max_nid);
4145 * add an nid range to a nodemap
4147 * \param argc number of args
4148 * \param argv[] variable string arguments
4150 * --name nodemap name
4151 * --range properly formatted nid range
4153 * \retval 0 on success
4155 int jt_nodemap_add_range(int argc, char **argv)
4157 char *nodemap_name = NULL;
4158 char *nodemap_range = NULL;
4159 char nid_range[2 * LNET_NIDSTR_SIZE + 2];
4163 static struct option long_opts[] = {
4164 { .val = 'n', .name = "name", .has_arg = required_argument },
4165 { .val = 'r', .name = "range", .has_arg = required_argument },
4168 while ((c = getopt_long(argc, argv, "n:r:",
4169 long_opts, NULL)) != -1) {
4172 nodemap_name = optarg;
4175 nodemap_range = optarg;
4180 if (!nodemap_name || !nodemap_range) {
4182 "usage: nodemap_add_range --name <name> --range <range>\n");
4186 rc = parse_nid_range(nodemap_range, nid_range, sizeof(nid_range));
4191 rc = nodemap_cmd(LCFG_NODEMAP_ADD_RANGE, NULL, 0, argv[0],
4192 nodemap_name, nid_range, NULL);
4196 "error: %s: cannot add range '%s' to nodemap '%s': rc = %d\n",
4197 jt_cmdname(argv[0]), nodemap_range, nodemap_name, rc);
4204 * delete an nid range to a nodemap
4206 * \param argc number of args
4207 * \param argv[] variable string arguments
4209 * --name nodemap name
4210 * --range properly formatted nid range
4212 * \retval 0 on success
4214 int jt_nodemap_del_range(int argc, char **argv)
4216 char *nodemap_name = NULL;
4217 char *nodemap_range = NULL;
4218 char nid_range[2 * LNET_NIDSTR_SIZE + 2];
4222 static struct option long_opts[] = {
4223 { .val = 'n', .name = "name", .has_arg = required_argument },
4224 { .val = 'r', .name = "range", .has_arg = required_argument },
4227 while ((c = getopt_long(argc, argv, "n:r:",
4228 long_opts, NULL)) != -1) {
4231 nodemap_name = optarg;
4234 nodemap_range = optarg;
4239 if (!nodemap_name || !nodemap_range) {
4241 "usage: nodemap_del_range --name <name> --range <range>\n");
4245 rc = parse_nid_range(nodemap_range, nid_range, sizeof(nid_range));
4250 rc = nodemap_cmd(LCFG_NODEMAP_DEL_RANGE, NULL, 0, argv[0],
4251 nodemap_name, nid_range, NULL);
4255 "error: %s: cannot delete range '%s' to nodemap '%s': rc = %d\n",
4256 jt_cmdname(argv[0]), nodemap_range, nodemap_name, rc);
4263 * set a fileset on a nodemap
4265 * \param argc number of args
4266 * \param argv[] variable string arguments
4268 * --name nodemap name
4269 * --fileset fileset name
4271 * \retval 0 on success
4273 int jt_nodemap_set_fileset(int argc, char **argv)
4275 char *nodemap_name = NULL;
4276 char *fileset_name = NULL;
4280 static struct option long_opts[] = {
4281 { .val = 'f', .name = "fileset", .has_arg = required_argument },
4282 { .val = 'n', .name = "name", .has_arg = required_argument },
4285 while ((c = getopt_long(argc, argv, "n:f:",
4286 long_opts, NULL)) != -1) {
4289 nodemap_name = optarg;
4292 fileset_name = optarg;
4297 if (!nodemap_name || !fileset_name) {
4299 "usage: nodemap_set_fileset --name <name> --fileset <fileset>\n");
4303 rc = nodemap_cmd(LCFG_NODEMAP_SET_FILESET, NULL, 0, argv[0],
4304 nodemap_name, fileset_name, NULL);
4308 "error: %s: cannot set fileset '%s' on nodemap '%s': rc = %d\n",
4309 jt_cmdname(argv[0]), fileset_name, nodemap_name, rc);
4316 * set SELinux policy info on a nodemap
4318 * \param argc number of args
4319 * \param argv[] variable string arguments
4321 * --name nodemap name
4322 * --sepol SELinux policy info
4324 * \retval 0 on success
4326 int jt_nodemap_set_sepol(int argc, char **argv)
4328 char *nodemap_name = NULL;
4333 static struct option long_options[] = {
4336 .has_arg = required_argument,
4341 .has_arg = required_argument,
4349 while ((c = getopt_long(argc, argv, "n:s:",
4350 long_options, NULL)) != -1) {
4353 nodemap_name = optarg;
4361 if (!nodemap_name || !sepol) {
4363 "usage: nodemap_set_sepol --name <name> --sepol <sepol>\n");
4367 rc = nodemap_cmd(LCFG_NODEMAP_SET_SEPOL, NULL, 0, argv[0],
4368 nodemap_name, sepol, NULL);
4372 "error: %s: cannot set sepol '%s' on nodemap '%s': rc = %d\n",
4373 jt_cmdname(argv[0]), sepol, nodemap_name, rc);
4380 * modify a nodemap's behavior
4382 * \param argc number of args
4383 * \param argv[] variable string arguments
4385 * --name nodemap name
4386 * --property nodemap property to change
4387 * admin, trusted, squash_uid, squash_gid)
4388 * --value value to set property
4390 * \retval 0 on success
4392 int jt_nodemap_modify(int argc, char **argv)
4396 enum lcfg_command_type cmd = 0;
4397 char *nodemap_name = NULL;
4401 static struct option long_opts[] = {
4402 { .val = 'n', .name = "name", .has_arg = required_argument },
4403 { .val = 'p', .name = "property", .has_arg = required_argument },
4404 { .val = 'v', .name = "value", .has_arg = required_argument },
4407 while ((c = getopt_long(argc, argv, "n:p:v:",
4408 long_opts, NULL)) != -1) {
4411 nodemap_name = optarg;
4422 if (!nodemap_name || !param || !value) {
4424 "usage: nodemap_modify --name <nodemap_name> --property <property_name> --value <value>\n");
4426 "valid properties: admin trusted map_mode squash_uid squash_gid deny_unknown audit_mode forbid_encryption\n");
4430 if (strcmp("admin", param) == 0) {
4431 cmd = LCFG_NODEMAP_ADMIN;
4432 } else if (strcmp("trusted", param) == 0) {
4433 cmd = LCFG_NODEMAP_TRUSTED;
4434 } else if (strcmp("deny_unknown", param) == 0) {
4435 cmd = LCFG_NODEMAP_DENY_UNKNOWN;
4436 } else if (strcmp("squash_uid", param) == 0) {
4437 cmd = LCFG_NODEMAP_SQUASH_UID;
4438 } else if (strcmp("squash_gid", param) == 0) {
4439 cmd = LCFG_NODEMAP_SQUASH_GID;
4440 } else if (strcmp("map_mode", param) == 0) {
4441 cmd = LCFG_NODEMAP_MAP_MODE;
4442 } else if (strcmp("audit_mode", param) == 0) {
4443 cmd = LCFG_NODEMAP_AUDIT_MODE;
4444 } else if (strcmp("forbid_encryption", param) == 0) {
4445 cmd = LCFG_NODEMAP_FORBID_ENCRYPT;
4448 "error: %s: nodemap_modify invalid subcommand: %s\n",
4449 jt_cmdname(argv[0]), param);
4453 rc = nodemap_cmd(cmd, NULL, 0, argv[0], nodemap_name, param,
4458 "error: %s: cannot modify nodemap '%s' to param '%s': value '%s': rc = %d\n",
4459 jt_cmdname(argv[0]), nodemap_name, param, value, rc);
4465 int jt_nodemap_add_idmap(int argc, char **argv)
4468 enum lcfg_command_type cmd = 0;
4469 char *nodemap_name = NULL;
4471 char *idtype = NULL;
4474 static struct option long_opts[] = {
4475 { .val = 'i', .name = "idtype", .has_arg = required_argument },
4476 { .val = 'm', .name = "idmap", .has_arg = required_argument },
4477 { .val = 'n', .name = "name", .has_arg = required_argument },
4480 while ((c = getopt_long(argc, argv, "n:m:i:",
4481 long_opts, NULL)) != -1) {
4484 nodemap_name = optarg;
4495 if (!nodemap_name || !idmap || !idtype) {
4497 "usage: %s --name <name> --idtype [uid | gid] --idmap <client id>:<filesystem id>\n",
4502 if (strcmp("uid", idtype) == 0) {
4503 cmd = LCFG_NODEMAP_ADD_UIDMAP;
4504 } else if (strcmp("gid", idtype) == 0) {
4505 cmd = LCFG_NODEMAP_ADD_GIDMAP;
4508 "usage: %s --name <name> --idtype [uid | gid] --idmap <client id>:<filesystem id>\n",
4513 rc = nodemap_cmd(cmd, NULL, 0, argv[0], nodemap_name, idmap, NULL);
4517 "cannot add %smap '%s' to nodemap '%s': rc = %d\n",
4518 idtype, idmap, nodemap_name, rc);
4524 int jt_nodemap_del_idmap(int argc, char **argv)
4527 enum lcfg_command_type cmd = 0;
4528 char *nodemap_name = NULL;
4530 char *idtype = NULL;
4533 static struct option long_opts[] = {
4534 { .val = 'i', .name = "idtype", .has_arg = required_argument },
4535 { .val = 'm', .name = "idmap", .has_arg = required_argument },
4536 { .val = 'n', .name = "name", .has_arg = required_argument },
4539 while ((c = getopt_long(argc, argv, "n:m:i:",
4540 long_opts, NULL)) != -1) {
4543 nodemap_name = optarg;
4554 if (!nodemap_name || !idmap || !idtype) {
4556 "usage: %s --name <name> --idtype [uid | gid] --idmap <client id>:<filesystem id>\n",
4561 if (strcmp("uid", idtype) == 0)
4562 cmd = LCFG_NODEMAP_DEL_UIDMAP;
4564 cmd = LCFG_NODEMAP_DEL_GIDMAP;
4566 rc = nodemap_cmd(cmd, NULL, 0, argv[0], nodemap_name, idmap, NULL);
4570 "cannot delete %smap '%s' from nodemap '%s': rc = %d\n",
4571 idtype, idmap, nodemap_name, rc);
4578 * this function tranforms a rule [start-end/step] into an array
4579 * of matching numbers
4580 * supported forms are:
4581 * [start] : just this number
4582 * [start-end] : all numbers from start to end
4583 * [start-end/step] : numbers from start to end with increment of step
4584 * on return, format contains a printf format string which can be used
4585 * to generate all the strings
4587 static int get_array_idx(char *rule, char *format, int **array)
4589 char *start, *end, *ptr;
4590 unsigned int lo, hi, step;
4595 start = strchr(rule, '[');
4596 end = strchr(rule, ']');
4597 if ((!start) || (!end)) {
4598 *array = malloc(sizeof(int));
4601 strcpy(format, rule);
4609 /* put in format the printf format (the rule without the range) */
4610 sprintf(format, "%s%%.4x%s", rule, end);
4615 /* loop on , separator */
4617 /* extract the 3 fields */
4618 rc = sscanf(start, "%x-%x/%u", &lo, &hi, &step);
4626 tmp = realloc(*array, array_sz * sizeof(int));
4630 (*array)[array_idx] = lo;
4636 /* do not break to share code with case 3: */
4641 if ((hi < lo) || (step == 0))
4643 array_sz += (hi - lo) / step + 1;
4644 tmp = realloc(*array, array_sz * sizeof(int));
4648 for (i = lo; i <= hi; i += step, array_idx++)
4649 (*array)[array_idx] = i;
4653 ptr = strchr(start, ',');
4667 struct llog_pool_name {
4668 char lpn_name[UUID_MAX];
4669 struct list_head lpn_list;
4672 struct llog_pool_list_data {
4673 char lpld_fsname[LUSTRE_MAXFSNAME + 1];
4674 char lpld_poolname[LOV_MAXPOOLNAME + 1];
4676 struct list_head lpld_list_head;
4680 * Callback to list pool information in llog
4681 * - { index: 74, event: new_pool, device: tfs-clilov, fsname: tfs, pool: tmp }
4682 * - { index: 77, event: add_pool, device: tfs-clilov, fsname: tfs, pool: tmp,
4683 * ost: tfs-OST0000_UUID }
4684 * - { index: 224, event: remove_pool, device: tfs-clilov, fsname: tfs,
4685 * pool: tmp, ost: tfs-OST0003_UUID }
4686 * - { index: 227, event: del_pool, device: tfs-clilov, fsname: tfs, pool: tmp }
4688 * \param record[in] pointer to llog record
4689 * \param data[in] pointer to struct llog_pool_list_data
4691 * \retval 0 on success
4694 static int llog_poollist_cb(const char *record, void *data)
4696 struct llog_pool_list_data *lpld = data;
4697 char pool_filter[MAX_STRING_SIZE] = "";
4698 char *new_record, *del_record, *del_pool, *found;
4700 int filter_len, rc = 0;
4702 filter_len = snprintf(pool_filter, sizeof(pool_filter), " fsname: %s,",
4704 if (lpld->lpld_poolname[0] == '\0') {
4705 new_record = get_event_filter(LCFG_POOL_NEW);
4706 del_record = get_event_filter(LCFG_POOL_DEL);
4707 strncpy(type, " pool: ", sizeof(type));
4709 filter_len += snprintf(pool_filter + filter_len,
4710 sizeof(pool_filter) - filter_len,
4711 " pool: %s", lpld->lpld_poolname);
4712 new_record = get_event_filter(LCFG_POOL_ADD);
4713 del_record = get_event_filter(LCFG_POOL_REM);
4714 strncpy(type, " ost: ", sizeof(type));
4716 del_pool = get_event_filter(LCFG_POOL_DEL);
4718 if (!new_record || !del_record || !del_pool) {
4723 found = strstr(record, pool_filter);
4725 (found[filter_len] == ' ' || found[filter_len] == ',')) {
4726 struct llog_pool_name *tmp = NULL;
4727 struct list_head *head = &lpld->lpld_list_head;
4729 int name_len, type_len = strlen(type);
4731 lpld->lpld_exists = true;
4732 if (strstr(record, new_record)) {
4733 name = strstr(record, type);
4734 /* 2 bytes for " }" */
4735 name_len = strlen(name) - type_len - 2;
4736 if (name_len <= 0 || name_len > sizeof(tmp->lpn_name))
4738 tmp = malloc(sizeof(struct llog_pool_name));
4743 memset(tmp, 0, sizeof(struct llog_pool_name));
4744 strncpy(tmp->lpn_name, name + type_len, name_len);
4745 list_add_tail(&tmp->lpn_list, &lpld->lpld_list_head);
4746 } else if (strstr(record, del_record)) {
4747 name = strstr(record, type);
4748 name_len = strlen(name) - type_len - 2;
4749 list_for_each_entry(tmp, head, lpn_list) {
4750 if (strncmp(tmp->lpn_name, name + type_len,
4752 tmp->lpn_name[name_len] == '\0') {
4753 list_del(&tmp->lpn_list);
4759 /* verify if the specified pool still exists */
4760 if (lpld->lpld_poolname[0] && strstr(record, del_pool))
4761 lpld->lpld_exists = false;
4775 * List pool information by config log
4777 * \param fsname[in] pointer to filesystem name
4778 * \param poolname[in] pointer to pool name
4780 * \retval 0 on success
4783 int llog_poollist(char *fsname, char *poolname)
4785 char logname[MAX_OBD_NAME] = {'\0'};
4786 struct llog_pool_list_data lpld;
4787 struct llog_pool_name *tmp;
4788 struct list_head *head;
4791 if (fsname && fsname[0] == '\0')
4796 memset(&lpld, 0, sizeof(lpld));
4797 INIT_LIST_HEAD(&lpld.lpld_list_head);
4798 lpld.lpld_exists = false;
4799 strncpy(lpld.lpld_fsname, fsname, sizeof(lpld.lpld_fsname) - 1);
4800 if (poolname && poolname[0])
4801 strncpy(lpld.lpld_poolname, poolname,
4802 sizeof(lpld.lpld_poolname) - 1);
4803 snprintf(logname, sizeof(logname), "%s-client", fsname);
4804 rc = jt_llog_print_iter(logname, 0, -1, llog_poollist_cb, &lpld, false);
4806 if (poolname && poolname[0])
4807 printf("Pool: %s.%s\n", fsname, poolname);
4809 printf("Pools from %s:\n", fsname);
4811 head = &lpld.lpld_list_head;
4812 if (poolname && poolname[0] && !lpld.lpld_exists && list_empty(head))
4815 list_for_each_entry(tmp, head, lpn_list) {
4816 if (poolname && poolname[0])
4817 printf("%s\n", tmp->lpn_name);
4819 printf("%s.%s\n", fsname, tmp->lpn_name);
4820 list_del(&tmp->lpn_list);
4827 static bool get_pools_path(char *fsname)
4832 rc = cfs_get_param_paths(&path, "lov/%s-*/pools", fsname);
4834 cfs_free_param_data(&path);
4839 static int extract_fsname_poolname(char **argv, char *fsname,
4842 char *cmd = argv[0], *param = argv[1];
4846 snprintf(fsname, PATH_MAX + 1, "%s", param);
4847 ptr = strchr(fsname, '.');
4849 if (strcmp(cmd, "pool_list") == 0) {
4853 fprintf(stderr, ". is missing in %s\n", fsname);
4858 if ((ptr - fsname) == 0) {
4859 fprintf(stderr, "fsname is empty\n");
4867 if (strlen(ptr) == 0) {
4868 fprintf(stderr, "poolname is empty\n");
4873 strncpy(poolname, ptr, LOV_MAXPOOLNAME);
4874 poolname[LOV_MAXPOOLNAME] = '\0';
4876 if (strncmp(poolname, "none", LOV_MAXPOOLNAME) == 0) {
4877 fprintf(stderr, "poolname cannot be 'none'\n");
4884 fprintf(stderr, "argument %s must be <fsname>.<poolname>\n", param);
4888 int jt_pool_cmd(int argc, char **argv)
4890 enum lcfg_command_type cmd;
4891 char fsname[PATH_MAX + 1];
4892 char poolname[LOV_MAXPOOLNAME + 1];
4893 char *ostnames_buf = NULL;
4895 int *array = NULL, array_sz;
4903 case 1: return CMD_HELP;
4905 rc = extract_fsname_poolname(argv, fsname, poolname);
4909 if (strcmp("pool_new", argv[0]) == 0) {
4910 cmd = LCFG_POOL_NEW;
4911 } else if (strcmp("pool_destroy", argv[0]) == 0) {
4912 cmd = LCFG_POOL_DEL;
4913 } else if (strcmp("pool_list", argv[0]) == 0) {
4914 if (get_pools_path(fsname))
4915 return llapi_poollist(argv[1]);
4916 if (get_mgs_device() > 0)
4917 return llog_poollist(fsname, poolname);
4919 "Cannot run pool_list command since there is no local MGS/MDT or client\n");
4925 rc = pool_cmd(cmd, argv[0], argv[1], fsname, poolname, NULL);
4929 check_pool_cmd_result(cmd, fsname, poolname, NULL);
4933 char format[2 * MAX_OBD_NAME];
4935 if (strcmp("pool_remove", argv[0]) == 0)
4936 cmd = LCFG_POOL_REM;
4937 else if (strcmp("pool_add", argv[0]) == 0)
4938 cmd = LCFG_POOL_ADD;
4942 rc = extract_fsname_poolname(argv, fsname, poolname);
4946 for (i = 2; i < argc; i++) {
4949 array_sz = get_array_idx(argv[i], format, &array);
4953 cmds = malloc(array_sz * sizeof(cmds[0]));
4955 ostnames_buf = malloc(array_sz *
4956 (MAX_OBD_NAME + 1));
4963 for (j = 0; j < array_sz; j++) {
4964 char ostname[MAX_OBD_NAME + 1];
4966 snprintf(ostname, MAX_OBD_NAME, format,
4968 ostname[MAX_OBD_NAME] = '\0';
4970 rc = check_and_complete_ostname(fsname,
4981 &ostnames_buf[(MAX_OBD_NAME + 1) * j];
4982 strcpy(cmds[j].ostname, ostname);
4984 cmds[j].ostname = NULL;
4986 cmds[j].rc = pool_cmd(cmd, argv[0], argv[1],
4989 /* Return an err if any of the add/dels fail */
4993 for (j = 0; j < array_sz; j++) {
4995 char ostname[MAX_OBD_NAME + 1];
4997 if (!cmds[j].ostname) {
4998 snprintf(ostname, MAX_OBD_NAME,
5000 ostname[MAX_OBD_NAME] = '\0';
5001 check_and_complete_ostname(
5007 check_pool_cmd_result(cmd, fsname,
5032 #ifdef HAVE_SERVER_SUPPORT
5033 static const char *barrier_status2name(enum barrier_status status)
5038 case BS_FREEZING_P1:
5039 return "freezing_p1";
5040 case BS_FREEZING_P2:
5041 return "freezing_p2";
5059 int jt_barrier_freeze(int argc, char **argv)
5061 struct obd_ioctl_data data;
5062 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
5063 struct barrier_ctl bc;
5066 if (argc < 2 || argc > 3)
5069 memset(&data, 0, sizeof(data));
5070 rc = data.ioc_dev = get_mgs_device();
5074 memset(&bc, 0, sizeof(bc));
5075 bc.bc_version = BARRIER_VERSION_V1;
5076 bc.bc_cmd = BC_FREEZE;
5078 bc.bc_timeout = atoi(argv[2]);
5079 if (bc.bc_timeout == 0)
5080 bc.bc_timeout = BARRIER_TIMEOUT_DEFAULT;
5082 if (strlen(argv[1]) > 8) {
5084 "%s: fsname name %s is too long. It should not exceed 8.\n",
5089 strncpy(bc.bc_name, argv[1], sizeof(bc.bc_name));
5090 data.ioc_inlbuf1 = (char *)&bc;
5091 data.ioc_inllen1 = sizeof(bc);
5092 memset(buf, 0, sizeof(rawbuf));
5093 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5095 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
5099 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_BARRIER, buf);
5101 fprintf(stderr, "Fail to freeze barrier for %s: %s\n",
5102 argv[1], strerror(errno));
5107 int jt_barrier_thaw(int argc, char **argv)
5109 struct obd_ioctl_data data;
5110 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
5111 struct barrier_ctl bc;
5117 memset(&data, 0, sizeof(data));
5118 rc = data.ioc_dev = get_mgs_device();
5122 memset(&bc, 0, sizeof(bc));
5123 bc.bc_version = BARRIER_VERSION_V1;
5124 bc.bc_cmd = BC_THAW;
5126 if (strlen(argv[1]) > 8) {
5128 "fsname name %s is too long. It should not exceed 8.\n",
5133 strncpy(bc.bc_name, argv[1], sizeof(bc.bc_name));
5134 data.ioc_inlbuf1 = (char *)&bc;
5135 data.ioc_inllen1 = sizeof(bc);
5136 memset(buf, 0, sizeof(rawbuf));
5137 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5139 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
5143 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_BARRIER, buf);
5145 fprintf(stderr, "Fail to thaw barrier for %s: %s\n",
5146 argv[1], strerror(errno));
5151 int __jt_barrier_stat(const char *fsname, struct barrier_ctl *bc)
5153 struct obd_ioctl_data data;
5154 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
5157 memset(&data, 0, sizeof(data));
5158 rc = data.ioc_dev = get_mgs_device();
5162 memset(bc, 0, sizeof(*bc));
5163 bc->bc_version = BARRIER_VERSION_V1;
5164 bc->bc_cmd = BC_STAT;
5165 strncpy(bc->bc_name, fsname, sizeof(bc->bc_name) - 1);
5166 data.ioc_inlbuf1 = (char *)bc;
5167 data.ioc_inllen1 = sizeof(*bc);
5168 memset(buf, 0, sizeof(rawbuf));
5169 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5171 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
5175 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_BARRIER, buf);
5177 fprintf(stderr, "Fail to query barrier for %s: %s\n",
5178 fsname, strerror(errno));
5180 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
5185 int jt_barrier_stat(int argc, char **argv)
5187 struct barrier_ctl bc;
5188 static struct option long_opt_barrier_stat[] = {
5192 .has_arg = no_argument,
5196 .has_arg = no_argument,
5207 bool timeout = false;
5209 while ((opt = getopt_long(argc, argv, "st", long_opt_barrier_stat,
5226 name = argv[optind];
5227 if (strlen(name) > 8) {
5229 "fsname name %s is too long. It should not exceed 8.\n",
5234 rc = __jt_barrier_stat(name, &bc);
5236 if (state && !timeout)
5237 printf("%s\n", barrier_status2name(bc.bc_status));
5238 else if (timeout && !state)
5240 (bc.bc_status == BS_FREEZING_P1 ||
5241 bc.bc_status == BS_FREEZING_P2 ||
5242 bc.bc_status == BS_FROZEN) ?
5245 printf("state: %s\ntimeout: %d seconds\n",
5246 barrier_status2name(bc.bc_status),
5247 (bc.bc_status == BS_FREEZING_P1 ||
5248 bc.bc_status == BS_FREEZING_P2 ||
5249 bc.bc_status == BS_FROZEN) ?
5256 int jt_barrier_rescan(int argc, char **argv)
5258 struct obd_ioctl_data data;
5259 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
5260 struct barrier_ctl bc;
5263 if (argc < 2 || argc > 3)
5266 memset(&data, 0, sizeof(data));
5267 rc = data.ioc_dev = get_mgs_device();
5271 memset(&bc, 0, sizeof(bc));
5272 bc.bc_version = BARRIER_VERSION_V1;
5273 bc.bc_cmd = BC_RESCAN;
5275 bc.bc_timeout = atoi(argv[2]);
5276 if (bc.bc_timeout == 0)
5277 bc.bc_timeout = BARRIER_TIMEOUT_DEFAULT;
5279 if (strlen(argv[1]) > 8) {
5281 "fsname name %s is too long. It should not exceed 8.\n",
5286 strncpy(bc.bc_name, argv[1], sizeof(bc.bc_name));
5287 data.ioc_inlbuf1 = (char *)&bc;
5288 data.ioc_inllen1 = sizeof(bc);
5289 memset(buf, 0, sizeof(rawbuf));
5290 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5292 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
5296 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_BARRIER, buf);
5298 fprintf(stderr, "Fail to rescan barrier bitmap for %s: %s\n",
5299 argv[1], strerror(errno));
5301 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
5302 printf("%u of %u MDT(s) in the filesystem %s are inactive\n",
5303 bc.bc_absence, bc.bc_total, argv[1]);
5308 #endif /* HAVE_SERVER_SUPPORT */
5310 int jt_get_obj_version(int argc, char **argv)
5313 struct obd_ioctl_data data;
5314 __u64 version, id = ULLONG_MAX, group = ULLONG_MAX;
5315 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf, *fidstr;
5318 while ((c = getopt(argc, argv, "i:g:")) != -1) {
5321 id = strtoull(optarg, NULL, 0);
5324 group = strtoull(optarg, NULL, 0);
5332 fidstr = *(argv + optind);
5334 if (!(id != ULLONG_MAX && group != ULLONG_MAX && argc == 0) &&
5335 !(id == ULLONG_MAX && group == ULLONG_MAX && argc == 1))
5338 memset(&data, 0, sizeof(data));
5339 data.ioc_dev = cur_device;
5341 rc = llapi_fid_parse(fidstr, &fid, NULL);
5343 fprintf(stderr, "%s: error parsing FID '%s': %s\n",
5344 jt_cmdname(argv[0]), fidstr, strerror(-rc));
5348 data.ioc_inlbuf1 = (char *)&fid;
5349 data.ioc_inllen1 = sizeof(fid);
5351 data.ioc_inlbuf3 = (char *)&id;
5352 data.ioc_inllen3 = sizeof(id);
5353 data.ioc_inlbuf4 = (char *)&group;
5354 data.ioc_inllen4 = sizeof(group);
5356 data.ioc_inlbuf2 = (char *)&version;
5357 data.ioc_inllen2 = sizeof(version);
5359 memset(buf, 0, sizeof(*buf));
5360 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5362 fprintf(stderr, "error: %s: packing ioctl arguments: %s\n",
5363 jt_cmdname(argv[0]), strerror(-rc));
5367 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_GET_OBJ_VERSION, buf);
5369 fprintf(stderr, "error: %s: ioctl: %s\n",
5370 jt_cmdname(argv[0]), strerror(errno));
5374 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
5375 printf("%#jx\n", (uintmax_t)version);
5380 int jt_changelog_register(int argc, char **argv)
5382 struct obd_ioctl_data data = { 0 };
5383 char rawbuf[MAX_IOC_BUFLEN] = "";
5385 char *device = lcfg_get_devname();
5386 bool print_name_only = false;
5393 while ((c = getopt(argc, argv, "hn")) >= 0) {
5396 print_name_only = true;
5404 if (cur_device < 0 || !device)
5407 data.ioc_dev = cur_device;
5409 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5411 fprintf(stderr, "error: %s: cannot pack ioctl: %s\n",
5412 jt_cmdname(argv[0]), strerror(-rc));
5416 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CHANGELOG_REG, buf);
5419 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
5424 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
5426 if (data.ioc_u32_1 == 0) {
5427 fprintf(stderr, "received invalid userid!\n");
5431 if (print_name_only)
5432 printf("%s%u\n", CHANGELOG_USER_PREFIX, data.ioc_u32_1);
5434 printf("%s: Registered changelog userid '%s%u'\n",
5435 device, CHANGELOG_USER_PREFIX, data.ioc_u32_1);
5440 int jt_changelog_deregister(int argc, char **argv)
5442 struct obd_ioctl_data data = { 0 };
5443 char rawbuf[MAX_IOC_BUFLEN] = "";
5445 char *device = lcfg_get_devname();
5449 if (argc != 2 || cur_device < 0 || !device)
5452 rc = sscanf(argv[1], CHANGELOG_USER_PREFIX"%d", &id);
5453 if (rc != 1 || id <= 0) {
5455 "error: %s: expected id of the form %s<num> got '%s'\n",
5456 jt_cmdname(argv[0]), CHANGELOG_USER_PREFIX, argv[1]);
5460 data.ioc_dev = cur_device;
5461 data.ioc_u32_1 = id;
5463 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
5465 fprintf(stderr, "error: %s: invalid ioctl\n",
5466 jt_cmdname(argv[0]));
5470 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CHANGELOG_DEREG, buf);
5472 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
5473 strerror(rc = errno));
5477 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
5478 printf("%s: Deregistered changelog user '%s%u'\n",
5479 device, CHANGELOG_USER_PREFIX, data.ioc_u32_1);
5484 int jt_pcc_add(int argc, char **argv)
5486 struct option long_opts[] = {
5487 { .val = 'p', .name = "param", .has_arg = required_argument },
5489 const char *mntpath;
5490 const char *pccpath;
5496 while ((rc = getopt_long(argc, argv, "p:", long_opts, NULL)) != -1) {
5507 fprintf(stderr, "%s: must specify the config param for PCC\n",
5508 jt_cmdname(argv[0]));
5512 if (optind + 2 != argc) {
5514 "%s: must specify mount path and PCC path %d:%d\n",
5515 jt_cmdname(argv[0]), optind, argc);
5519 mntpath = argv[optind++];
5520 pccpath = argv[optind];
5522 snprintf(cmd, PATH_MAX, "add %s %s", pccpath, param);
5523 rc = llapi_pccdev_set(mntpath, cmd);
5525 fprintf(stderr, "%s: failed to run '%s' on %s\n",
5526 jt_cmdname(argv[0]), cmd, mntpath);
5531 int jt_pcc_del(int argc, char **argv)
5533 const char *mntpath;
5534 const char *pccpath;
5540 fprintf(stderr, "%s: require 3 arguments\n",
5541 jt_cmdname(argv[0]));
5545 mntpath = argv[optind++];
5546 pccpath = argv[optind++];
5548 snprintf(cmd, PATH_MAX, "del %s", pccpath);
5549 rc = llapi_pccdev_set(mntpath, cmd);
5551 fprintf(stderr, "%s: failed to run '%s' on %s\n",
5552 jt_cmdname(argv[0]), cmd, mntpath);
5557 int jt_pcc_clear(int argc, char **argv)
5559 const char *mntpath;
5564 fprintf(stderr, "%s: require 2 arguments\n",
5565 jt_cmdname(argv[0]));
5569 mntpath = argv[optind];
5570 rc = llapi_pccdev_set(mntpath, "clear");
5572 fprintf(stderr, "%s: failed to run 'clear' on %s\n",
5573 jt_cmdname(argv[0]), mntpath);
5578 int jt_pcc_list(int argc, char **argv)
5580 const char *mntpath;
5585 fprintf(stderr, "%s: require 2 arguments\n",
5586 jt_cmdname(argv[0]));
5590 mntpath = argv[optind];
5591 rc = llapi_pccdev_get(mntpath);
5593 fprintf(stderr, "%s: failed to run 'pcc list' on %s\n",
5594 jt_cmdname(argv[0]), mntpath);