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_disk.h>
76 #include <linux/lustre/lustre_ioctl.h>
77 #include <linux/lustre/lustre_ostid.h>
78 #include <linux/lustre/lustre_param.h>
79 #include <linux/lustre/lustre_ver.h>
81 #include <lustre/lustreapi.h>
83 #define MAX_STRING_SIZE 128
90 #define MAX_THREADS 4096
91 #define MAX_BASE_ID 0xffffffff
92 #define NIDSTRING_LENGTH 64
94 pthread_mutex_t mutex;
98 __u64 counters[MAX_THREADS];
99 __u64 offsets[MAX_THREADS];
103 struct timeval start_time;
104 struct timeval end_time;
108 static struct shared_data *shared_data;
109 static __u64 counter_snapshot[2][MAX_THREADS];
110 static int prev_valid;
111 static struct timeval prev_time;
116 const int nthreads = 1;
119 static int cur_device = -1;
121 int lcfg_ioctl(char *func, int dev_id, struct lustre_cfg *lcfg)
123 struct obd_ioctl_data data;
124 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
127 memset(&data, 0, sizeof(data));
128 data.ioc_dev = cur_device;
129 data.ioc_type = LUSTRE_CFG_TYPE;
130 data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
132 data.ioc_pbuf1 = (void *)lcfg;
133 memset(buf, 0, sizeof(rawbuf));
134 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
136 fprintf(stderr, "error: %s: invalid ioctl\n", jt_cmdname(func));
140 rc = l_ioctl(dev_id, OBD_IOC_PROCESS_CFG, buf);
145 static int do_device(char *func, char *devname);
147 static int get_mgs_device(void)
150 static int mgs_device = -1;
152 if (mgs_device == -1) {
155 do_disconnect(NULL, 1);
156 rc = do_device("mgsioc", mgs);
159 "This command must be run on the MGS.\n");
163 mgs_device = cur_device;
168 /* Returns -1 on error with errno set */
169 int lcfg_mgs_ioctl(char *func, int dev_id, struct lustre_cfg *lcfg)
171 struct obd_ioctl_data data;
172 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
175 memset(&data, 0, sizeof(data));
176 rc = data.ioc_dev = get_mgs_device();
179 data.ioc_type = LUSTRE_CFG_TYPE;
180 data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
182 data.ioc_pbuf1 = (void *)lcfg;
183 memset(buf, 0, sizeof(rawbuf));
184 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
186 fprintf(stderr, "error: %s: invalid ioctl\n", jt_cmdname(func));
190 rc = l_ioctl(dev_id, OBD_IOC_PARAM, buf);
192 if (rc && errno == ENOSYS)
193 fprintf(stderr, "Make sure cfg_device is set first.\n");
198 char *obdo_print(struct obdo *obd)
202 snprintf(buf, sizeof(buf), "id: %#jx\ngrp: %#jx\natime: %ju\n"
203 "mtime: %ju\nctime: %ju\nsize: %ju\nblocks: %ju"
204 "\nblksize: %u\nmode: %o\nuid: %d\ngid: %d\nflags: %x\n"
205 "misc: %x\nnlink: %d,\nvalid %#jx\n",
206 (uintmax_t)ostid_id(&obd->o_oi),
207 (uintmax_t)ostid_seq(&obd->o_oi),
208 (uintmax_t)obd->o_atime, (uintmax_t)obd->o_mtime,
209 (uintmax_t)obd->o_ctime, (uintmax_t)obd->o_size,
210 (uintmax_t)obd->o_blocks, obd->o_blksize, obd->o_mode,
211 obd->o_uid, obd->o_gid, obd->o_flags, obd->o_misc,
212 obd->o_nlink, (uintmax_t)obd->o_valid);
216 #define BAD_VERBOSE (-999999999)
218 #define N2D_OFF 0x100 /* So we can tell between error codes and devices */
220 static int do_name2dev(char *func, char *name)
222 struct obd_ioctl_data data;
223 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
226 memset(&data, 0, sizeof(data));
227 data.ioc_dev = cur_device;
228 data.ioc_inllen1 = strlen(name) + 1;
229 data.ioc_inlbuf1 = name;
231 memset(buf, 0, sizeof(rawbuf));
232 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
234 fprintf(stderr, "error: %s: invalid ioctl\n", jt_cmdname(func));
237 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_NAME2DEV, buf);
240 rc = llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
242 fprintf(stderr, "error: %s: invalid reply\n", jt_cmdname(func));
246 return data.ioc_dev + N2D_OFF;
250 * resolve a device name to a device number.
251 * supports a number, $name or %uuid.
253 int parse_devname(char *func, char *name)
262 try_digit = isdigit(name[0]);
264 if (name[0] == '$' || name[0] == '%')
267 rc = do_name2dev(func, name);
272 ret = strtoul(name, NULL, 0);
274 fprintf(stderr, "No device found for name %s: %s\n",
279 char *jt_cmdname(char *func)
281 static char buf[512];
284 sprintf(buf, "%s-%d", func, thread);
291 #define difftime(a, b) \
292 ((a)->tv_sec - (b)->tv_sec + \
293 ((a)->tv_usec - (b)->tv_usec) / 1000000.0)
295 static int be_verbose(int verbose, struct timeval *next_time,
296 __u64 num, __u64 *next_num, int num_total)
304 gettimeofday(&now, NULL);
306 /* A positive verbosity means to print every X iterations */
307 if (verbose > 0 && (num >= *next_num || num >= num_total)) {
308 *next_num += verbose;
310 next_time->tv_sec = now.tv_sec - verbose;
311 next_time->tv_usec = now.tv_usec;
316 /* A negative verbosity means to print at most each X seconds */
317 if (verbose < 0 && next_time && difftime(&now, next_time) >= 0.0) {
318 next_time->tv_sec = now.tv_sec - verbose;
319 next_time->tv_usec = now.tv_usec;
327 static int get_verbose(char *func, const char *arg)
332 if (!arg || arg[0] == 'v') {
334 } else if (arg[0] == 's' || arg[0] == 'q') {
337 verbose = (int)strtoul(arg, &end, 0);
339 fprintf(stderr, "error: %s: bad verbose option '%s'\n",
340 jt_cmdname(func), arg);
346 printf("Print status every %d seconds\n", -verbose);
347 else if (verbose == 1)
348 printf("Print status every operation\n");
349 else if (verbose > 1)
350 printf("Print status every %d operations\n", verbose);
355 int do_disconnect(char *func, int verbose)
357 lcfg_set_devname(NULL);
363 static int shmem_setup(void)
365 pthread_mutexattr_t mattr;
366 pthread_condattr_t cattr;
370 /* Create new segment */
371 shmid = shmget(IPC_PRIVATE, sizeof(*shared_data), 0600);
373 fprintf(stderr, "Can't create shared data: %s\n",
378 /* Attatch to new segment */
379 shared_data = (struct shared_data *)shmat(shmid, NULL, 0);
381 if (shared_data == (struct shared_data *)(-1)) {
382 fprintf(stderr, "Can't attach shared data: %s\n",
389 * Mark segment as destroyed, so it will disappear when we exit.
390 * Forks will inherit attached segments, so we should be OK.
392 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
393 fprintf(stderr, "Can't destroy shared data: %s\n",
398 pthread_mutexattr_init(&mattr);
399 pthread_condattr_init(&cattr);
401 rc = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
403 fprintf(stderr, "Can't set shared mutex attr\n");
407 rc = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
409 fprintf(stderr, "Can't set shared cond attr\n");
413 pthread_mutex_init(&shared_data->mutex, &mattr);
414 pthread_cond_init(&shared_data->cond, &cattr);
416 pthread_mutexattr_destroy(&mattr);
417 pthread_condattr_destroy(&cattr);
422 static inline void shmem_lock(void)
424 pthread_mutex_lock(&shared_data->mutex);
427 static inline void shmem_unlock(void)
429 pthread_mutex_unlock(&shared_data->mutex);
432 static inline void shmem_wait(void)
434 pthread_cond_wait(&shared_data->cond, &shared_data->mutex);
437 static inline void shmem_wakeup_all(void)
439 pthread_cond_broadcast(&shared_data->cond);
442 static inline void shmem_reset(int total_threads)
447 memset(&shared_data->body, 0, sizeof(shared_data->body));
448 memset(counter_snapshot, 0, sizeof(counter_snapshot));
450 shared_data->stopping = 0;
451 shared_data->body.start_barrier = total_threads;
452 shared_data->body.stop_barrier = total_threads;
455 static inline void shmem_bump(__u32 counter)
457 static bool running_not_bumped = true;
459 if (!shared_data || thread <= 0 || thread > MAX_THREADS)
463 shared_data->body.counters[thread - 1] += counter;
464 if (running_not_bumped) {
465 shared_data->body.thr_running++;
466 running_not_bumped = false;
471 static void shmem_total(int total_threads)
477 if (!shared_data || total_threads > MAX_THREADS)
481 for (i = 0; i < total_threads; i++)
482 total += shared_data->body.counters[i];
484 secs = difftime(&shared_data->body.end_time,
485 &shared_data->body.start_time);
488 printf("Total: total %ju threads %d sec %f %f/second\n",
489 (uintmax_t)total, total_threads, secs, total / secs);
492 static void shmem_snap(int total_threads, int live_threads)
494 struct timeval this_time;
501 if (!shared_data || total_threads > MAX_THREADS)
505 memcpy(counter_snapshot[0], shared_data->body.counters,
506 total_threads * sizeof(counter_snapshot[0][0]));
507 running = shared_data->body.thr_running;
510 gettimeofday(&this_time, NULL);
512 for (i = 0; i < total_threads; i++) {
513 long long this_count =
514 counter_snapshot[0][i] - counter_snapshot[1][i];
516 if (this_count != 0) {
522 secs = difftime(&this_time, &prev_time);
523 if (prev_valid && secs > 1.0) { /* someone screwed with the time? */
524 printf("%d/%d Total: %f/second\n", non_zero, total_threads,
527 memcpy(counter_snapshot[1], counter_snapshot[0],
528 total_threads * sizeof(counter_snapshot[0][0]));
529 prev_time = this_time;
531 if (!prev_valid && running == total_threads) {
533 /* drop counters when all threads were started */
534 memcpy(counter_snapshot[1], counter_snapshot[0],
535 total_threads * sizeof(counter_snapshot[0][0]));
536 prev_time = this_time;
540 static void shmem_stop(void)
545 shared_data->stopping = 1;
548 static void shmem_cleanup(void)
555 pthread_mutex_destroy(&shared_data->mutex);
556 pthread_cond_destroy(&shared_data->cond);
559 static int shmem_running(void)
561 return (!shared_data || !shared_data->stopping);
564 static void shmem_end_time_locked(void)
566 shared_data->body.stop_barrier--;
567 if (shared_data->body.stop_barrier == 0)
568 gettimeofday(&shared_data->body.end_time, NULL);
571 static void shmem_start_time_locked(void)
573 shared_data->body.start_barrier--;
574 if (shared_data->body.start_barrier == 0) {
576 gettimeofday(&shared_data->body.start_time, NULL);
583 static int shmem_setup(void)
588 static inline void shmem_reset(int total_threads)
592 static inline void shmem_bump(__u32 counters)
596 static void shmem_lock(void)
600 static void shmem_unlock(void)
604 static void shmem_cleanup(void)
608 static int shmem_running(void)
614 extern command_t cmdlist[];
616 static int do_device(char *func, char *devname)
620 dev = parse_devname(func, devname);
624 lcfg_set_devname(devname);
629 int jt_obd_get_device(void)
634 int jt_obd_device(int argc, char **argv)
642 printf("current device is %d - %s\n",
643 cur_device, lcfg_get_devname() ? : "not set");
646 rc = do_device("device", argv[1]);
650 int jt_opt_device(int argc, char **argv)
658 rc = do_device("device", argv[1]);
661 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
663 ret = do_disconnect(argv[0], 0);
671 static void parent_sighandler(int sig)
675 int jt_opt_threads(int argc, char **argv)
677 static char cmdstr[129];
680 struct sigaction sigact;
681 struct sigaction saveact1;
682 struct sigaction saveact2;
683 unsigned long threads;
687 int report_count = -1;
694 threads = strtoul(argv[1], &end, 0);
697 report_count = strtoul(end + 1, &end, 0);
699 if (*end || threads > MAX_THREADS) {
700 fprintf(stderr, "error: %s: invalid thread count '%s'\n",
701 jt_cmdname(argv[0]), argv[1]);
705 verbose = get_verbose(argv[0], argv[2]);
706 if (verbose == BAD_VERBOSE)
710 snprintf(cmdstr, sizeof(cmdstr), "%s", argv[4]);
711 for (i = 5; i < argc; i++)
712 snprintf(cmdstr + strlen(cmdstr),
713 sizeof(cmdstr) - strlen(cmdstr),
716 printf("%s: starting %ld threads on device %s running %s\n",
717 argv[0], threads, argv[3], cmdstr);
720 shmem_reset(threads);
722 sigemptyset(&sigset);
723 sigaddset(&sigset, SIGALRM);
724 sigaddset(&sigset, SIGCHLD);
725 sigprocmask(SIG_BLOCK, &sigset, &saveset);
729 for (i = 1, next_thread = verbose; i <= threads; i++) {
732 fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
733 strerror(rc = errno));
735 } else if (rc == 0) {
736 sigprocmask(SIG_SETMASK, &saveset, NULL);
739 argv[2] = "--device";
740 exit(jt_opt_device(argc - 2, argv + 2));
741 } else if (be_verbose(verbose, NULL, i, &next_thread, threads))
742 printf("%s: thread #%d (PID %d) started\n",
747 if (!thread) { /* parent process */
748 int live_threads = threads;
750 sigemptyset(&sigset);
751 sigemptyset(&sigact.sa_mask);
752 sigact.sa_handler = parent_sighandler;
755 sigaction(SIGALRM, &sigact, &saveact1);
756 sigaction(SIGCHLD, &sigact, &saveact2);
758 while (live_threads > 0) {
762 if (verbose < 0) /* periodic stats */
768 while (live_threads > 0) {
769 ret = waitpid(0, &status, WNOHANG);
775 "error: %s: wait - %s\n",
776 argv[0], strerror(errno));
782 * This is a hack. We _should_ be able
783 * to use WIFEXITED(status) to see if
784 * there was an error, but it appears
785 * to be broken and it always returns 1
788 int err = WEXITSTATUS(status);
790 if (err || WIFSIGNALED(status))
792 "%s: PID %d had rc=%d\n",
801 /* Show stats while all threads running */
803 shmem_snap(threads, live_threads);
804 if (report_count > 0 && --report_count == 0)
808 sigaction(SIGCHLD, &saveact2, NULL);
809 sigaction(SIGALRM, &saveact1, NULL);
812 shmem_total(threads);
813 sigprocmask(SIG_SETMASK, &saveset, NULL);
818 int jt_opt_threads(int argc, char **argv)
820 fprintf(stderr, "%s not-supported in a single-threaded runtime\n",
821 jt_cmdname(argv[0]));
826 int jt_opt_net(int argc, char **argv)
837 rc = jt_ptl_network(2, arg2);
840 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
845 #ifdef HAVE_SERVER_SUPPORT
847 * Place this here so we can build tools that work with
848 * older Lustre versions
850 #ifndef OBD_IOC_NO_TRANSNO
851 #define OBD_IOC_NO_TRANSNO _IOW('f', 140, OBD_IOC_DATA_TYPE)
854 int jt_obd_no_transno(int argc, char **argv)
856 struct obd_ioctl_data data;
857 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
866 memset(buf, 0, sizeof(rawbuf));
868 rc = cfs_get_param_paths(&path, "no_transno");
872 fd = open(path.gl_pathv[0], O_WRONLY);
874 cfs_free_param_data(&path);
878 snprintf(rawbuf, sizeof(rawbuf), "%d", cur_device);
880 count = write(fd, rawbuf, strlen(rawbuf));
884 cfs_free_param_data(&path);
892 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
893 memset(&data, 0, sizeof(data));
894 data.ioc_dev = cur_device;
896 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
898 fprintf(stderr, "error: %s: invalid ioctl\n",
899 jt_cmdname(argv[0]));
902 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_NO_TRANSNO, buf);
904 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
905 strerror(rc = errno));
909 #endif /* HAVE_SERVER_SUPPORT */
911 int jt_obd_set_readonly(int argc, char **argv)
913 struct obd_ioctl_data data;
914 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
917 memset(&data, 0, sizeof(data));
918 data.ioc_dev = cur_device;
923 memset(buf, 0, sizeof(rawbuf));
924 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
926 fprintf(stderr, "error: %s: invalid ioctl\n",
927 jt_cmdname(argv[0]));
930 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_SET_READONLY, buf);
932 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
933 strerror(rc = errno));
938 static int obd_abort_recovery(char *cmd, enum obd_abort_recovery_flags flags)
940 struct obd_ioctl_data data = {
941 .ioc_dev = cur_device,
944 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
947 memset(buf, 0, sizeof(rawbuf));
948 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
950 fprintf(stderr, "error: %s: invalid ioctl\n",
954 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_ABORT_RECOVERY, buf);
956 fprintf(stderr, "error: %s: %s\n", jt_cmdname(cmd),
957 strerror(rc = errno));
962 int jt_obd_abort_recovery(int argc, char **argv)
967 return obd_abort_recovery(argv[0], OBD_FLG_ABORT_RECOV_OST);
970 int jt_obd_abort_recovery_mdt(int argc, char **argv)
975 return obd_abort_recovery(argv[0], OBD_FLG_ABORT_RECOV_MDT);
978 int jt_get_version(int argc, char **argv)
986 rc = llapi_get_version_string(version, sizeof(version));
988 printf("Lustre version: %s\n", LUSTRE_VERSION_STRING);
990 printf("Lustre version: %s\n", version);
995 static void print_obd_line(char *s)
997 const char *param = "osc/%s/ost_conn_uuid";
998 char buf[MAX_STRING_SIZE];
999 char obd_name[MAX_OBD_NAME];
1004 /* obd device type is the first 3 characters of param name */
1005 snprintf(buf, sizeof(buf), " %%*d %%*s %.3s %%%zus %%*s %%*d ",
1006 param, sizeof(obd_name) - 1);
1007 if (sscanf(s, buf, obd_name) == 0)
1009 if (cfs_get_param_paths(&path, param, obd_name) != 0)
1011 fp = fopen(path.gl_pathv[0], "r");
1013 /* need to free path data before retry */
1014 cfs_free_param_data(&path);
1016 if (param[0] == 'o') { /* failed with osc, try mdc */
1017 param = "mdc/%s/mds_conn_uuid";
1024 /* should not ignore fgets(3)'s return value */
1025 if (!fgets(buf, sizeof(buf), fp)) {
1026 fprintf(stderr, "reading from %s: %s", buf, strerror(errno));
1032 cfs_free_param_data(&path);
1034 /* trim trailing newlines */
1035 ptr = strrchr(buf, '\n');
1039 ptr = strrchr(s, '\n');
1042 printf("%s%s%s\n", s, buf[0] ? " " : "", buf);
1045 /* get device list by ioctl */
1046 int jt_obd_list_ioctl(int argc, char **argv)
1049 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1050 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buf;
1054 /* Just ignore a -t option. Only supported with /proc. */
1055 else if (argc == 2 && strcmp(argv[1], "-t") != 0)
1058 for (index = 0;; index++) {
1059 memset(buf, 0, sizeof(rawbuf));
1060 data->ioc_version = OBD_IOCTL_VERSION;
1062 sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(*data), 8);
1063 data->ioc_inlbuf1 = buf + __ALIGN_KERNEL(sizeof(*data), 8);
1064 data->ioc_len = obd_ioctl_packlen(data);
1065 data->ioc_count = index;
1067 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_GETDEVICE, buf);
1070 printf("%s\n", (char *)data->ioc_bulk);
1073 if (errno == ENOENT)
1074 /* no device or the last device */
1078 "Error getting device list: %s: check dmesg\n",
1084 int jt_obd_list(int argc, char **argv)
1086 char buf[MAX_STRING_SIZE];
1095 if (strcmp(argv[1], "-t") == 0)
1101 if (cfs_get_param_paths(&path, "devices") ||
1102 !(fp = fopen(path.gl_pathv[0], "r"))) {
1103 cfs_free_param_data(&path);
1105 return jt_obd_list_ioctl(argc, argv);
1108 while (fgets(buf, sizeof(buf), fp) != NULL)
1110 print_obd_line(buf);
1114 cfs_free_param_data(&path);
1119 struct jt_fid_space {
1125 int jt_obd_alloc_fids(struct jt_fid_space *space, struct lu_fid *fid,
1130 if (space->jt_seq == 0 || space->jt_id == space->jt_width) {
1131 struct obd_ioctl_data data;
1132 char rawbuf[MAX_IOC_BUFLEN];
1137 memset(&data, 0, sizeof(data));
1138 data.ioc_dev = cur_device;
1140 data.ioc_pbuf1 = (char *)&seqnr;
1141 data.ioc_plen1 = sizeof(seqnr);
1143 data.ioc_pbuf2 = (char *)&max_count;
1144 data.ioc_plen2 = sizeof(max_count);
1146 memset(buf, 0, sizeof(rawbuf));
1147 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1149 fprintf(stderr, "error: invalid ioctl rc = %d\n", rc);
1153 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_ECHO_ALLOC_SEQ, buf);
1155 fprintf(stderr, "ioctl error: rc = %d\n", rc);
1159 space->jt_seq = *(__u64 *)data.ioc_pbuf1;
1160 space->jt_width = *(int *)data.ioc_pbuf2;
1163 fid->f_seq = space->jt_seq;
1164 fid->f_oid = space->jt_id;
1167 space->jt_id = space->jt_id + *count;
1168 if (space->jt_id > space->jt_width)
1169 space->jt_id = space->jt_width;
1171 *count = space->jt_id - fid->f_oid;
1175 #define MD_STEP_COUNT 1000
1176 int jt_obd_md_common(int argc, char **argv, int cmd)
1178 struct obd_ioctl_data data;
1179 struct timeval start;
1180 struct timeval end_time;
1181 char rawbuf[MAX_IOC_BUFLEN];
1186 char *parent_basedir = NULL;
1188 int parent_base_id = 0;
1189 int parent_count = 1;
1190 __u64 child_base_id = -1;
1191 int stripe_count = 0;
1192 int stripe_index = -1;
1198 __u64 total_count = 0;
1200 struct jt_fid_space fid_space = {0};
1202 struct option long_opts[] = {
1203 { .val = 'b', .name = "child_base_id",
1204 .has_arg = required_argument },
1205 { .val = 'c', .name = "stripe_count",
1206 .has_arg = required_argument },
1207 { .val = 'd', .name = "parent_basedir",
1208 .has_arg = required_argument },
1209 { .val = 'D', .name = "parent_dircount",
1210 .has_arg = required_argument },
1211 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
1212 { .val = 'm', .name = "mode", .has_arg = required_argument },
1213 { .val = 'n', .name = "count", .has_arg = required_argument },
1214 { .val = 't', .name = "time", .has_arg = required_argument },
1215 { .val = 'v', .name = "version", .has_arg = no_argument },
1218 while ((c = getopt_long(argc, argv, "b:c:d:D:m:n:t:v",
1219 long_opts, NULL)) >= 0) {
1222 child_base_id = strtoull(optarg, &end, 0);
1225 "error: %s: bad child_base_id '%s'\n",
1226 jt_cmdname(argv[0]), optarg);
1231 stripe_count = strtoul(optarg, &end, 0);
1234 "error: %s: bad stripe count '%s'\n",
1235 jt_cmdname(argv[0]), optarg);
1240 parent_basedir = optarg;
1243 parent_count = strtoul(optarg, &end, 0);
1246 "error: %s: bad parent count '%s'\n",
1247 jt_cmdname(argv[0]), optarg);
1252 stripe_index = strtoul(optarg, &end, 0);
1255 "error: %s: bad stripe index '%s'\n",
1256 jt_cmdname(argv[0]), optarg);
1261 mode = strtoul(optarg, &end, 0);
1263 fprintf(stderr, "error: %s: bad mode '%s'\n",
1264 jt_cmdname(argv[0]), optarg);
1269 total_count = strtoul(optarg, &end, 0);
1270 if (*end || total_count == 0) {
1271 fprintf(stderr, "%s: bad child count '%s'\n",
1272 jt_cmdname(argv[0]), optarg);
1277 seconds = strtoull(optarg, &end, 0);
1279 fprintf(stderr, "error: %s: seconds '%s'\n",
1280 jt_cmdname(argv[0]), optarg);
1289 "error: %s: option '%s' unrecognized\n",
1290 argv[0], argv[optind - 1]);
1295 memset(&data, 0, sizeof(data));
1296 data.ioc_dev = cur_device;
1297 if (child_base_id == -1) {
1300 name = argv[optind];
1303 if (optind < argc) {
1305 "child_base_id and name can not specified at the same time\n");
1310 if (stripe_count == 0 && stripe_index != -1) {
1312 "If stripe_count is 0, stripe_index can not be specified\n");
1316 if (total_count == 0 && seconds == 0) {
1317 fprintf(stderr, "count or seconds needs to be indicated\n");
1321 if (parent_count <= 0) {
1322 fprintf(stderr, "parent count must < 0\n");
1329 /* threads interleave */
1330 if (parent_base_id != -1)
1331 parent_base_id += (thread - 1) % parent_count;
1333 if (child_base_id != -1)
1334 child_base_id += (thread - 1) *
1335 (MAX_BASE_ID / nthreads);
1337 shmem_start_time_locked();
1342 * If parent directory is not specified, try to get the directory
1345 if (!parent_basedir) {
1350 "parent_basedir or name must be indicated!\n");
1353 /*Get directory and name from name*/
1354 last_lash = strrchr(name, '/');
1355 if (!last_lash || name[0] != '/') {
1356 fprintf(stderr, "Can not locate %s\n", name);
1360 if (last_lash == name) {
1361 sprintf(dirname, "%s", "/");
1364 int namelen = (unsigned long)last_lash -
1365 (unsigned long)name + 1;
1366 snprintf(dirname, namelen, "%s", name);
1367 name = last_lash + 1;
1370 data.ioc_pbuf1 = dirname;
1371 data.ioc_plen1 = strlen(dirname);
1373 data.ioc_pbuf2 = name;
1374 data.ioc_plen2 = strlen(name);
1377 data.ioc_pbuf2 = name;
1378 data.ioc_plen2 = strlen(name);
1380 if (parent_base_id > 0)
1381 sprintf(dirname, "%s%d", parent_basedir,
1384 sprintf(dirname, "%s", parent_basedir);
1385 data.ioc_pbuf1 = dirname;
1386 data.ioc_plen1 = strlen(dirname);
1389 if (cmd == ECHO_MD_MKDIR || cmd == ECHO_MD_RMDIR)
1390 create_mode = S_IFDIR;
1392 create_mode = S_IFREG;
1394 data.ioc_obdo1.o_mode = mode | S_IFDIR;
1395 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
1396 OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1397 data.ioc_command = cmd;
1399 gettimeofday(&start, NULL);
1400 while (shmem_running()) {
1401 struct lu_fid fid = { 0 };
1403 if (child_base_id != -1)
1404 data.ioc_obdo2.o_oi.oi.oi_id = child_base_id;
1405 data.ioc_obdo2.o_mode = mode | create_mode;
1406 data.ioc_obdo2.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
1407 OBD_MD_FLMODE | OBD_MD_FLFLAGS |
1409 data.ioc_obdo2.o_misc = stripe_count;
1410 data.ioc_obdo2.o_stripe_idx = stripe_index;
1412 if (total_count > 0) {
1413 if ((total_count - count) > MD_STEP_COUNT)
1414 data.ioc_count = MD_STEP_COUNT;
1416 data.ioc_count = total_count - count;
1418 data.ioc_count = MD_STEP_COUNT;
1421 if (cmd == ECHO_MD_CREATE || cmd == ECHO_MD_MKDIR) {
1422 /*Allocate fids for the create */
1423 rc = jt_obd_alloc_fids(&fid_space, &fid,
1426 fprintf(stderr, "Allocate fids error %d.\n",
1430 data.ioc_obdo1.o_oi.oi_fid = fid;
1433 child_base_id += data.ioc_count;
1434 count += data.ioc_count;
1436 memset(buf, 0, sizeof(rawbuf));
1437 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1439 fprintf(stderr, "error: %s: invalid ioctl %d\n",
1440 jt_cmdname(argv[0]), rc);
1444 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_ECHO_MD, buf);
1446 fprintf(stderr, "error: %s: %s\n",
1447 jt_cmdname(argv[0]), strerror(rc = errno));
1450 shmem_bump(data.ioc_count);
1452 gettimeofday(&end_time, NULL);
1453 diff = difftime(&end_time, &start);
1454 if (seconds > 0 && (__u64)diff > seconds)
1457 if (count >= total_count && total_count > 0)
1461 if (count > 0 && version) {
1462 gettimeofday(&end_time, NULL);
1463 diff = difftime(&end_time, &start);
1464 printf("%s: %d in %.3fs (%.3f /s): %s",
1465 jt_cmdname(argv[0]), count, diff,
1466 (double)count / diff, ctime(&end_time.tv_sec));
1472 shmem_end_time_locked();
1479 int jt_obd_test_create(int argc, char **argv)
1481 return jt_obd_md_common(argc, argv, ECHO_MD_CREATE);
1484 int jt_obd_test_mkdir(int argc, char **argv)
1486 return jt_obd_md_common(argc, argv, ECHO_MD_MKDIR);
1489 int jt_obd_test_destroy(int argc, char **argv)
1491 return jt_obd_md_common(argc, argv, ECHO_MD_DESTROY);
1494 int jt_obd_test_rmdir(int argc, char **argv)
1496 return jt_obd_md_common(argc, argv, ECHO_MD_RMDIR);
1499 int jt_obd_test_lookup(int argc, char **argv)
1501 return jt_obd_md_common(argc, argv, ECHO_MD_LOOKUP);
1504 int jt_obd_test_setxattr(int argc, char **argv)
1506 return jt_obd_md_common(argc, argv, ECHO_MD_SETATTR);
1509 int jt_obd_test_md_getattr(int argc, char **argv)
1511 return jt_obd_md_common(argc, argv, ECHO_MD_GETATTR);
1514 int jt_obd_create(int argc, char **argv)
1516 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1517 struct obd_ioctl_data data;
1518 struct timeval next_time;
1519 __u64 count = 1, next_count, base_id = 1;
1520 int verbose = 1, mode = 0100644, rc = 0, i;
1523 memset(&data, 0, sizeof(data));
1524 data.ioc_dev = cur_device;
1525 if (argc < 2 || argc > 4)
1528 count = strtoull(argv[1], &end, 0);
1530 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1531 jt_cmdname(argv[0]), argv[1]);
1536 mode = strtoul(argv[2], &end, 0);
1538 fprintf(stderr, "error: %s: invalid mode '%s'\n",
1539 jt_cmdname(argv[0]), argv[2]);
1542 if (!(mode & S_IFMT))
1547 verbose = get_verbose(argv[0], argv[3]);
1548 if (verbose == BAD_VERBOSE)
1552 printf("%s: %jd objects\n", jt_cmdname(argv[0]), (uintmax_t)count);
1553 gettimeofday(&next_time, NULL);
1554 next_time.tv_sec -= verbose;
1556 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1557 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1559 * base_id is 1 so we don't need to worry about it being
1560 * greater than OBIF_MAX_OID
1562 data.ioc_obdo1.o_oi.oi_fid.f_oid = base_id;
1563 data.ioc_obdo1.o_mode = mode;
1564 data.ioc_obdo1.o_uid = 0;
1565 data.ioc_obdo1.o_gid = 0;
1566 data.ioc_obdo1.o_projid = 0;
1567 data.ioc_obdo1.o_valid = OBD_MD_FLTYPE | OBD_MD_FLMODE |
1568 OBD_MD_FLID | OBD_MD_FLUID |
1569 OBD_MD_FLGID | OBD_MD_FLGROUP |
1572 memset(buf, 0, sizeof(rawbuf));
1573 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1575 fprintf(stderr, "error: %s: invalid ioctl\n",
1576 jt_cmdname(argv[0]));
1579 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CREATE, buf);
1580 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
1583 fprintf(stderr, "error: %s: #%d - %s\n",
1584 jt_cmdname(argv[0]), i, strerror(rc = errno));
1587 if (!(data.ioc_obdo1.o_valid & OBD_MD_FLID)) {
1588 fprintf(stderr, "error: %s: oid not valid #%d:%#jx\n",
1589 jt_cmdname(argv[0]), i,
1590 (uintmax_t)data.ioc_obdo1.o_valid);
1595 if (be_verbose(verbose, &next_time, i, &next_count, count))
1596 printf("%s: #%d is object id %#jx\n",
1597 jt_cmdname(argv[0]), i,
1598 (uintmax_t)ostid_id(&data.ioc_obdo1.o_oi));
1604 int jt_obd_setattr(int argc, char **argv)
1606 struct obd_ioctl_data data;
1607 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1613 memset(&data, 0, sizeof(data));
1614 data.ioc_dev = cur_device;
1618 objid = strtoull(argv[1], &end, 0);
1620 fprintf(stderr, "error: %s: objid '%s' is not a number\n",
1621 jt_cmdname(argv[0]), argv[1]);
1625 if (objid >= OBIF_MAX_OID) {
1626 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1627 jt_cmdname(argv[0]), argv[1]);
1631 mode = strtoul(argv[2], &end, 0);
1633 fprintf(stderr, "error: %s: invalid mode '%s'\n",
1634 jt_cmdname(argv[0]), argv[2]);
1638 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1639 data.ioc_obdo1.o_mode = S_IFREG | mode;
1640 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1641 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
1643 memset(buf, 0, sizeof(rawbuf));
1644 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1646 fprintf(stderr, "error: %s: invalid ioctl\n",
1647 jt_cmdname(argv[0]));
1650 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, buf);
1652 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1653 strerror(rc = errno));
1658 int jt_obd_test_setattr(int argc, char **argv)
1660 struct obd_ioctl_data data;
1661 struct timeval start, next_time;
1662 __u64 i, count, next_count;
1663 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1669 if (argc < 2 || argc > 4)
1672 memset(&data, 0, sizeof(data));
1673 data.ioc_dev = cur_device;
1674 count = strtoull(argv[1], &end, 0);
1676 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1677 jt_cmdname(argv[0]), argv[1]);
1682 verbose = get_verbose(argv[0], argv[2]);
1683 if (verbose == BAD_VERBOSE)
1688 if (argv[3][0] == 't') {
1689 objid = strtoull(argv[3] + 1, &end, 0);
1691 objid += thread - 1;
1693 objid = strtoull(argv[3], &end, 0);
1696 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1697 jt_cmdname(argv[0]), argv[3]);
1702 gettimeofday(&start, NULL);
1703 next_time.tv_sec = start.tv_sec - verbose;
1704 next_time.tv_usec = start.tv_usec;
1706 printf("%s: setting %jd attrs (objid %#jx): %s",
1707 jt_cmdname(argv[0]), (uintmax_t)count,
1708 (uintmax_t)objid, ctime(&start.tv_sec));
1710 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1711 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1712 if (objid >= OBIF_MAX_OID) {
1713 fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
1714 jt_cmdname(argv[0]), (unsigned long long)objid);
1718 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1719 data.ioc_obdo1.o_mode = S_IFREG;
1720 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
1722 memset(buf, 0, sizeof(rawbuf));
1723 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1725 fprintf(stderr, "error: %s: invalid ioctl\n",
1726 jt_cmdname(argv[0]));
1729 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, &data);
1732 fprintf(stderr, "error: %s: #%jd - %d:%s\n",
1733 jt_cmdname(argv[0]), (uintmax_t)i,
1734 errno, strerror(rc = errno));
1737 if (be_verbose(verbose, &next_time, i, &next_count, count))
1738 printf("%s: set attr #%jd\n",
1739 jt_cmdname(argv[0]), (uintmax_t)i);
1746 gettimeofday(&end, NULL);
1748 diff = difftime(&end, &start);
1752 printf("%s: %jd attrs in %.3fs (%.3f attr/s): %s",
1753 jt_cmdname(argv[0]), (uintmax_t)i, diff,
1754 i / diff, ctime(&end.tv_sec));
1759 int jt_obd_destroy(int argc, char **argv)
1761 struct obd_ioctl_data data;
1762 struct timeval next_time;
1763 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1764 __u64 count = 1, next_count;
1770 memset(&data, 0, sizeof(data));
1771 data.ioc_dev = cur_device;
1772 if (argc < 2 || argc > 4)
1776 id = strtoull(argv[1], &end, 0);
1777 if (*end || id == 0 || errno != 0) {
1778 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1779 jt_cmdname(argv[0]), argv[1]);
1783 count = strtoull(argv[2], &end, 0);
1786 "error: %s: invalid iteration count '%s'\n",
1787 jt_cmdname(argv[0]), argv[2]);
1793 verbose = get_verbose(argv[0], argv[3]);
1794 if (verbose == BAD_VERBOSE)
1798 printf("%s: %jd objects\n", jt_cmdname(argv[0]), (uintmax_t)count);
1799 gettimeofday(&next_time, NULL);
1800 next_time.tv_sec -= verbose;
1802 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1803 for (i = 1, next_count = verbose; i <= count && shmem_running();
1805 if (id >= OBIF_MAX_OID) {
1806 fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
1807 jt_cmdname(argv[0]), (unsigned long long)id);
1811 data.ioc_obdo1.o_oi.oi_fid.f_oid = id;
1812 data.ioc_obdo1.o_mode = S_IFREG | 0644;
1813 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLMODE;
1815 memset(buf, 0, sizeof(rawbuf));
1816 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1818 fprintf(stderr, "error: %s: invalid ioctl\n",
1819 jt_cmdname(argv[0]));
1822 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_DESTROY, buf);
1823 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
1826 fprintf(stderr, "error: %s: objid %#jx: %s\n",
1827 jt_cmdname(argv[0]), (uintmax_t)id,
1828 strerror(rc = errno));
1832 if (be_verbose(verbose, &next_time, i, &next_count, count))
1833 printf("%s: #%d is object id %#jx\n",
1834 jt_cmdname(argv[0]), i, (uintmax_t)id);
1840 int jt_obd_getattr(int argc, char **argv)
1842 struct obd_ioctl_data data;
1843 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1851 objid = strtoull(argv[1], &end, 0);
1853 fprintf(stderr, "error: %s: objid '%s' is not a number\n",
1854 jt_cmdname(argv[0]), argv[1]);
1858 if (objid >= OBIF_MAX_OID) {
1859 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1860 jt_cmdname(argv[0]), argv[1]);
1864 memset(&data, 0, sizeof(data));
1865 data.ioc_dev = cur_device;
1866 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1867 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1868 /* to help obd filter */
1869 data.ioc_obdo1.o_mode = 0100644;
1870 data.ioc_obdo1.o_valid = 0xffffffff;
1871 printf("%s: object id %#jx\n", jt_cmdname(argv[0]),
1872 (uintmax_t)ostid_id(&data.ioc_obdo1.o_oi));
1874 memset(buf, 0, sizeof(rawbuf));
1875 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1877 fprintf(stderr, "error: %s: invalid ioctl\n",
1878 jt_cmdname(argv[0]));
1881 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, buf);
1882 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
1884 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1885 strerror(rc = errno));
1887 printf("%s: object id %ju, mode %o\n", jt_cmdname(argv[0]),
1888 (uintmax_t)ostid_id(&data.ioc_obdo1.o_oi),
1889 data.ioc_obdo1.o_mode);
1894 int jt_obd_test_getattr(int argc, char **argv)
1896 struct obd_ioctl_data data;
1897 struct timeval start, next_time;
1898 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
1899 __u64 i, count, next_count;
1905 if (argc < 2 || argc > 4)
1908 memset(&data, 0, sizeof(data));
1909 data.ioc_dev = cur_device;
1910 count = strtoull(argv[1], &end, 0);
1912 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1913 jt_cmdname(argv[0]), argv[1]);
1918 verbose = get_verbose(argv[0], argv[2]);
1919 if (verbose == BAD_VERBOSE)
1924 if (argv[3][0] == 't') {
1925 objid = strtoull(argv[3] + 1, &end, 0);
1927 objid += thread - 1;
1929 objid = strtoull(argv[3], &end, 0);
1932 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1933 jt_cmdname(argv[0]), argv[3]);
1938 gettimeofday(&start, NULL);
1939 next_time.tv_sec = start.tv_sec - verbose;
1940 next_time.tv_usec = start.tv_usec;
1942 printf("%s: getting %jd attrs (objid %#jx): %s",
1943 jt_cmdname(argv[0]), (uintmax_t)count,
1944 (uintmax_t)objid, ctime(&start.tv_sec));
1946 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
1947 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1948 if (objid >= OBIF_MAX_OID) {
1949 fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
1950 jt_cmdname(argv[0]), (unsigned long long)objid);
1954 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
1955 data.ioc_obdo1.o_mode = S_IFREG;
1956 data.ioc_obdo1.o_valid = 0xffffffff;
1957 memset(buf, 0, sizeof(rawbuf));
1958 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1960 fprintf(stderr, "error: %s: invalid ioctl\n",
1961 jt_cmdname(argv[0]));
1964 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, &data);
1967 fprintf(stderr, "error: %s: #%jd - %d:%s\n",
1968 jt_cmdname(argv[0]), (uintmax_t)i,
1969 errno, strerror(rc = errno));
1972 if (be_verbose(verbose, &next_time, i, &next_count, count))
1973 printf("%s: got attr #%jd\n",
1974 jt_cmdname(argv[0]), (uintmax_t)i);
1981 gettimeofday(&end, NULL);
1983 diff = difftime(&end, &start);
1987 printf("%s: %jd attrs in %.3fs (%.3f attr/s): %s",
1988 jt_cmdname(argv[0]), (uintmax_t)i, diff,
1989 i / diff, ctime(&end.tv_sec));
1996 * test_brw <cnt> count
1997 * <r|w[r(repeat)x(noverify)]> mode
1998 * <q|v|#(print interval)> verbosity
1999 * <npages[+offset]> blocksize
2000 * <[[<interleave_threads>]t(inc obj by thread#)]obj> object
2003 int jt_obd_test_brw(int argc, char **argv)
2005 struct obd_ioctl_data data;
2006 struct timeval start, next_time;
2007 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2008 __u64 count, next_count, len, stride, thr_offset = 0, objid = 3;
2009 int write = 0, verbose = 1, cmd, i, rc = 0, pages = 1;
2010 int offset_pages = 0;
2012 int repeat_offset = 0;
2013 unsigned long long ull;
2014 int nthr_per_obj = 0;
2019 if (argc < 2 || argc > 7) {
2020 fprintf(stderr, "error: %s: bad number of arguments: %d\n",
2021 jt_cmdname(argv[0]), argc);
2025 count = strtoull(argv[1], &end, 0);
2027 fprintf(stderr, "error: %s: bad iteration count '%s'\n",
2028 jt_cmdname(argv[0]), argv[1]);
2033 if (argv[2][0] == 'w' || argv[2][0] == '1')
2035 /* else it's a read */
2037 if (argv[2][0] != 0)
2038 for (i = 1; argv[2][i] != 0; i++)
2039 switch (argv[2][i]) {
2050 "Can't parse cmd '%s'\n",
2057 verbose = get_verbose(argv[0], argv[3]);
2058 if (verbose == BAD_VERBOSE)
2063 pages = strtoul(argv[4], &end, 0);
2066 offset_pages = strtoul(end + 1, &end, 0);
2068 if (*end != 0 || offset_pages < 0 || offset_pages >= pages) {
2069 fprintf(stderr, "error: %s: bad npages[+offset] parameter '%s'\n",
2070 jt_cmdname(argv[0]), argv[4]);
2076 if (thread && (n = strtol(argv[5], &end, 0)) > 0 &&
2077 *end == 't' && (ull = strtoull(end + 1, &end, 0)) > 0 &&
2081 } else if (thread && argv[5][0] == 't') {
2083 objid = strtoull(argv[5] + 1, &end, 0);
2086 objid = strtoull(argv[5], &end, 0);
2089 fprintf(stderr, "error: %s: bad objid '%s'\n",
2090 jt_cmdname(argv[0]), argv[5]);
2095 memset(&data, 0, sizeof(data));
2096 data.ioc_dev = cur_device;
2099 * communicate the 'type' of brw test and batching to echo_client.
2100 * don't start. we'd love to refactor this lctl->echo_client
2103 data.ioc_pbuf1 = (void *)1;
2107 switch (argv[6][0]) {
2108 case 'g': /* plug and unplug */
2109 data.ioc_pbuf1 = (void *)2;
2110 data.ioc_plen1 = strtoull(argv[6] + 1, &end, 0);
2112 case 'p': /* prep and commit */
2113 data.ioc_pbuf1 = (void *)3;
2114 data.ioc_plen1 = strtoull(argv[6] + 1, &end, 0);
2118 "error: %s: batching '%s' needs to specify 'p' or 'g'\n",
2119 jt_cmdname(argv[0]), argv[6]);
2124 fprintf(stderr, "error: %s: bad batching '%s'\n",
2125 jt_cmdname(argv[0]), argv[6]);
2128 data.ioc_plen1 *= getpagesize();
2131 len = pages * getpagesize();
2132 thr_offset = offset_pages * getpagesize();
2138 if (nthr_per_obj != 0) {
2139 /* threads interleave */
2140 obj_idx = (thread - 1) / nthr_per_obj;
2142 stride *= nthr_per_obj;
2143 if ((thread - 1) % nthr_per_obj == 0) {
2144 shared_data->body.offsets[obj_idx] =
2145 stride + thr_offset;
2147 thr_offset += ((thread - 1) % nthr_per_obj) * len;
2149 /* threads disjoint */
2150 thr_offset += (thread - 1) * len;
2153 shmem_start_time_locked();
2158 ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
2159 if (objid >= OBIF_MAX_OID) {
2160 fprintf(stderr, "errr: %s: invalid objid '%llu'\n",
2161 jt_cmdname(argv[0]), (unsigned long long)objid);
2165 data.ioc_obdo1.o_oi.oi_fid.f_oid = objid;
2166 data.ioc_obdo1.o_mode = S_IFREG;
2167 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
2168 OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
2169 data.ioc_obdo1.o_flags = (verify ? OBD_FL_DEBUG_CHECK : 0);
2170 data.ioc_count = len;
2171 data.ioc_offset = (repeat_offset ? 0 : thr_offset);
2173 gettimeofday(&start, NULL);
2174 next_time.tv_sec = start.tv_sec - verbose;
2175 next_time.tv_usec = start.tv_usec;
2178 printf("%s: %s %jux%d pages (obj %#jx, off %ju): %s",
2179 jt_cmdname(argv[0]), write ? "writing" : "reading",
2180 (uintmax_t)count, pages, (uintmax_t)objid,
2181 (uintmax_t)data.ioc_offset, ctime(&start.tv_sec));
2183 cmd = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
2184 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
2185 data.ioc_obdo1.o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
2186 memset(buf, 0, sizeof(rawbuf));
2187 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2189 fprintf(stderr, "error: %s: invalid ioctl\n",
2190 jt_cmdname(argv[0]));
2193 rc = l_ioctl(OBD_DEV_ID, cmd, buf);
2196 fprintf(stderr, "error: %s: #%d - %s on %s\n",
2197 jt_cmdname(argv[0]), i, strerror(rc = errno),
2198 write ? "write" : "read");
2200 } else if (be_verbose(verbose, &next_time, i,
2201 &next_count, count)) {
2203 printf("%s: %s number %d @ %jd:%ju for %d\n",
2204 jt_cmdname(argv[0]), write ? "write" : "read", i,
2205 (uintmax_t)ostid_id(&data.ioc_obdo1.o_oi),
2206 (uintmax_t)data.ioc_offset,
2207 (int)(pages * getpagesize()));
2211 if (!repeat_offset) {
2213 if (stride == len) {
2214 data.ioc_offset += stride;
2215 } else if (i < count) {
2218 shared_data->body.offsets[obj_idx];
2219 shared_data->body.offsets[obj_idx] += len;
2223 data.ioc_offset += len;
2224 obj_idx = 0; /* avoids an unused var warning */
2233 gettimeofday(&end, NULL);
2235 diff = difftime(&end, &start);
2239 printf("%s: %s %dx%d pages in %.3fs (%.3f MB/s): %s",
2240 jt_cmdname(argv[0]), write ? "wrote" : "read",
2242 ((double)i * pages * getpagesize()) /
2243 (diff * 1048576.0), ctime(&end.tv_sec));
2249 shmem_end_time_locked();
2256 int jt_obd_lov_getconfig(int argc, char **argv)
2258 struct obd_ioctl_data data;
2259 struct lov_desc desc;
2260 struct obd_uuid *uuidarray;
2261 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2266 memset(&data, 0, sizeof(data));
2267 data.ioc_dev = cur_device;
2273 fd = open(path, O_RDONLY);
2275 fprintf(stderr, "open \"%s\" failed: %s\n", path,
2280 memset(&desc, 0, sizeof(desc));
2281 obd_str2uuid(&desc.ld_uuid, argv[1]);
2282 desc.ld_tgt_count = ((MAX_IOC_BUFLEN - sizeof(data) - sizeof(desc)) /
2283 (sizeof(*uuidarray) + sizeof(*obdgens)));
2286 uuidarray = calloc(desc.ld_tgt_count, sizeof(*uuidarray));
2288 fprintf(stderr, "error: %s: no memory for %d uuid's\n",
2289 jt_cmdname(argv[0]), desc.ld_tgt_count);
2293 obdgens = calloc(desc.ld_tgt_count, sizeof(*obdgens));
2295 fprintf(stderr, "error: %s: no memory for %d generation #'s\n",
2296 jt_cmdname(argv[0]), desc.ld_tgt_count);
2301 memset(buf, 0, sizeof(rawbuf));
2302 data.ioc_inllen1 = sizeof(desc);
2303 data.ioc_inlbuf1 = (char *)&desc;
2304 data.ioc_inllen2 = desc.ld_tgt_count * sizeof(*uuidarray);
2305 data.ioc_inlbuf2 = (char *)uuidarray;
2306 data.ioc_inllen3 = desc.ld_tgt_count * sizeof(*obdgens);
2307 data.ioc_inlbuf3 = (char *)obdgens;
2309 if (llapi_ioctl_pack(&data, &buf, sizeof(rawbuf))) {
2310 fprintf(stderr, "error: %s: invalid ioctl\n",
2311 jt_cmdname(argv[0]));
2315 rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
2316 if (rc == -ENOSPC) {
2321 fprintf(stderr, "error: %s: ioctl error: %s\n",
2322 jt_cmdname(argv[0]), strerror(rc = errno));
2324 struct obd_uuid *uuidp;
2328 if (llapi_ioctl_unpack(&data, buf, sizeof(rawbuf))) {
2329 fprintf(stderr, "error: %s: invalid reply\n",
2330 jt_cmdname(argv[0]));
2334 if (desc.ld_default_stripe_count == (__u32)-1)
2335 printf("default_stripe_count: %d\n", -1);
2337 printf("default_stripe_count: %u\n",
2338 desc.ld_default_stripe_count);
2339 printf("default_stripe_size: %ju\n",
2340 (uintmax_t)desc.ld_default_stripe_size);
2341 printf("default_stripe_offset: %jd\n",
2342 (uintmax_t)desc.ld_default_stripe_offset);
2343 printf("default_stripe_pattern: %u\n", desc.ld_pattern);
2344 printf("obd_count: %u\n", desc.ld_tgt_count);
2345 printf("OBDS:\tobdidx\t\tobdgen\t\t obduuid\n");
2348 for (i = 0; i < desc.ld_tgt_count; i++, uuidp++, genp++)
2349 printf("\t%6u\t%14u\t\t %s\n", i, *genp, (char *)uuidp);
2360 static int do_activate(int argc, char **argv, int flag)
2362 struct obd_ioctl_data data;
2363 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2366 memset(&data, 0, sizeof(data));
2367 data.ioc_dev = cur_device;
2371 /* reuse offset for 'active' */
2372 data.ioc_offset = flag;
2374 memset(buf, 0, sizeof(rawbuf));
2375 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2377 fprintf(stderr, "error: %s: invalid ioctl\n",
2378 jt_cmdname(argv[0]));
2381 rc = l_ioctl(OBD_DEV_ID, IOC_OSC_SET_ACTIVE, buf);
2383 fprintf(stderr, "error: %s: failed: %s\n",
2384 jt_cmdname(argv[0]), strerror(rc = errno));
2390 * Replace nids for given device.
2391 * lctl replace_nids <devicename> <nid1>[,nid2,nid3]
2392 * Command should be started on MGS server.
2393 * Only MGS server should be started (command execution
2394 * returns error in another cases). Command mount
2395 * -t lustre <MDT partition> -o nosvc <mount point>
2396 * can be used for that.
2398 * llogs for MDTs and clients are processed. All
2399 * records copied as is except add_uuid and setup. This records
2400 * are skipped and recorded with new nids and uuid.
2402 * \see mgs_replace_nids
2403 * \see mgs_replace_log
2404 * \see mgs_replace_nids_handler
2406 int jt_replace_nids(int argc, char **argv)
2409 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2410 struct obd_ioctl_data data;
2412 memset(&data, 0, sizeof(data));
2413 data.ioc_dev = get_mgs_device();
2417 data.ioc_inllen1 = strlen(argv[1]) + 1;
2418 data.ioc_inlbuf1 = argv[1];
2420 data.ioc_inllen2 = strlen(argv[2]) + 1;
2421 data.ioc_inlbuf2 = argv[2];
2422 memset(buf, 0, sizeof(rawbuf));
2423 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2425 fprintf(stderr, "error: %s: invalid ioctl\n",
2426 jt_cmdname(argv[0]));
2430 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_REPLACE_NIDS, buf);
2432 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
2433 strerror(rc = errno));
2440 * Clear config logs for given device or filesystem.
2441 * lctl clear_conf <devicename|fsname>
2442 * Command has to be run on MGS node having MGS device mounted with -o
2445 * Configuration logs for filesystem or one particular log is
2446 * processed. New log is created, original log is read, its records
2447 * marked SKIP do not get copied to new log. Others are copied as-is.
2448 * Original file is renamed to log.${time}.bak.
2450 * \see mgs_clear_configs
2451 * \see mgs_replace_log
2452 * \see mgs_clear_config_handler
2454 int jt_lcfg_clear(int argc, char **argv)
2457 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2458 struct obd_ioctl_data data;
2460 memset(&data, 0, sizeof(data));
2461 data.ioc_dev = get_mgs_device();
2465 data.ioc_inllen1 = strlen(argv[1]) + 1;
2466 data.ioc_inlbuf1 = argv[1];
2468 memset(buf, 0, sizeof(rawbuf));
2469 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2471 fprintf(stderr, "error: %s: invalid ioctl\n",
2472 jt_cmdname(argv[0]));
2476 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CLEAR_CONFIGS, buf);
2478 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
2479 strerror(rc = errno));
2485 int jt_obd_deactivate(int argc, char **argv)
2487 return do_activate(argc, argv, 0);
2490 int jt_obd_activate(int argc, char **argv)
2492 return do_activate(argc, argv, 1);
2495 int jt_obd_recover(int argc, char **argv)
2497 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2498 struct obd_ioctl_data data;
2501 memset(&data, 0, sizeof(data));
2502 data.ioc_dev = cur_device;
2507 data.ioc_inllen1 = strlen(argv[1]) + 1;
2508 data.ioc_inlbuf1 = argv[1];
2511 memset(buf, 0, sizeof(rawbuf));
2512 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2514 fprintf(stderr, "error: %s: invalid ioctl\n",
2515 jt_cmdname(argv[0]));
2518 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CLIENT_RECOVER, buf);
2520 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
2521 strerror(rc = errno));
2527 int jt_obd_mdc_lookup(int argc, char **argv)
2529 struct obd_ioctl_data data;
2530 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2531 char *parent, *child;
2532 int rc, fd, verbose = 1;
2534 if (argc < 3 || argc > 4)
2540 verbose = get_verbose(argv[0], argv[3]);
2542 memset(&data, 0, sizeof(data));
2543 data.ioc_dev = cur_device;
2545 data.ioc_inllen1 = strlen(child) + 1;
2546 data.ioc_inlbuf1 = child;
2548 memset(buf, 0, sizeof(rawbuf));
2549 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2551 fprintf(stderr, "error: %s: invalid ioctl\n",
2552 jt_cmdname(argv[0]));
2556 fd = open(parent, O_RDONLY);
2558 fprintf(stderr, "open \"%s\" failed: %s\n", parent,
2563 rc = ioctl(fd, IOC_MDC_LOOKUP, buf);
2565 fprintf(stderr, "error: %s: ioctl error: %s\n",
2566 jt_cmdname(argv[0]), strerror(rc = errno));
2571 rc = llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
2573 fprintf(stderr, "error: %s: invalid reply\n",
2574 jt_cmdname(argv[0]));
2577 printf("%s: mode %o uid %d gid %d\n",
2578 child, data.ioc_obdo1.o_mode, data.ioc_obdo1.o_uid,
2579 data.ioc_obdo1.o_gid);
2585 int jt_lcfg_fork(int argc, char **argv)
2587 struct obd_ioctl_data data;
2588 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2594 memset(&data, 0, sizeof(data));
2595 data.ioc_dev = get_mgs_device();
2596 data.ioc_inllen1 = strlen(argv[1]) + 1;
2597 data.ioc_inlbuf1 = argv[1];
2598 data.ioc_inllen2 = strlen(argv[2]) + 1;
2599 data.ioc_inlbuf2 = argv[2];
2601 memset(buf, 0, sizeof(rawbuf));
2602 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2604 fprintf(stderr, "error: %s: invalid ioctl\n",
2605 jt_cmdname(argv[0]));
2609 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LCFG_FORK, buf);
2611 fprintf(stderr, "error: %s: OBD_IOC_LCFG_FORK failed: %s\n",
2612 jt_cmdname(argv[0]), strerror(errno));
2617 int jt_lcfg_erase(int argc, char **argv)
2619 struct obd_ioctl_data data;
2620 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2624 if (strncmp(argv[2], "-q", strlen("-q")) != 0 &&
2625 strncmp(argv[2], "--quiet", strlen("--quiet")) != 0)
2627 } else if (argc != 2) {
2631 memset(&data, 0, sizeof(data));
2632 data.ioc_dev = get_mgs_device();
2633 data.ioc_inllen1 = strlen(argv[1]) + 1;
2634 data.ioc_inlbuf1 = argv[1];
2636 memset(buf, 0, sizeof(rawbuf));
2637 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2639 fprintf(stderr, "error: %s: invalid ioctl\n",
2640 jt_cmdname(argv[0]));
2644 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LCFG_ERASE, buf);
2646 fprintf(stderr, "error: %s: OBD_IOC_LCFG_ERASE failed: %s\n",
2647 jt_cmdname(argv[0]), strerror(errno));
2652 enum llog_default_dev_op {
2653 LLOG_DFLT_MGS_SET = 0,
2657 static int llog_default_device(enum llog_default_dev_op op)
2660 static int dflt_dev = -1;
2662 if (op == LLOG_DFLT_MGS_SET && (cur_device == -1)) {
2663 char mgs[] = "$MGS";
2665 rc = do_device("llog_default_device", mgs);
2666 dflt_dev = cur_device;
2668 } else if (op == LLOG_DFLT_DEV_RESET && (dflt_dev != -1)) {
2669 do_disconnect(NULL, 1);
2676 int jt_llog_catlist(int argc, char **argv)
2678 struct obd_ioctl_data data;
2679 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2687 if (llog_default_device(LLOG_DFLT_MGS_SET))
2688 return CMD_INCOMPLETE;
2691 memset(&data, 0, sizeof(data));
2692 data.ioc_dev = cur_device;
2693 data.ioc_inllen1 = sizeof(rawbuf) -
2694 __ALIGN_KERNEL(sizeof(data), 8);
2695 data.ioc_count = start;
2696 memset(buf, 0, sizeof(rawbuf));
2697 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2699 fprintf(stderr, "error: %s: invalid ioctl\n",
2700 jt_cmdname(argv[0]));
2703 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CATLOGLIST, buf);
2706 tmp = ((struct obd_ioctl_data *)buf)->ioc_bulk;
2707 if (strlen(tmp) > 0)
2708 fprintf(stdout, "%s", tmp);
2711 start = ((struct obd_ioctl_data *)buf)->ioc_count;
2715 fprintf(stderr, "OBD_IOC_CATLOGLIST failed: %s\n",
2719 llog_default_device(LLOG_DFLT_DEV_RESET);
2724 int jt_llog_info(int argc, char **argv)
2726 const struct option long_opts[] = {
2727 /* Allow optional "--catalog" for compatibility with llog commands. */
2728 { .val = 'c', .name = "catalog", .has_arg = required_argument },
2729 { .val = 'h', .name = "help", .has_arg = no_argument },
2731 struct obd_ioctl_data data = { 0 };
2732 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
2733 char *cmd = argv[0];
2734 char *catalog = NULL;
2737 while ((c = getopt_long(argc, argv, "c:h", long_opts, NULL)) != -1) {
2749 /* support "logname" positional parameter */
2753 "%s: catalog is set, unknown argument '%s'\n",
2758 } else if (!catalog || argc > 1) {
2762 /* Manage default device */
2763 if (llog_default_device(LLOG_DFLT_MGS_SET))
2764 return CMD_INCOMPLETE;
2766 data.ioc_dev = cur_device;
2767 data.ioc_inllen1 = strlen(catalog) + 1;
2768 data.ioc_inlbuf1 = catalog;
2769 data.ioc_inllen2 = sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(data), 8) -
2770 __ALIGN_KERNEL(data.ioc_inllen1, 8);
2771 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2773 fprintf(stderr, "%s: ioctl_pack failed for catalog '%s': %s\n",
2774 jt_cmdname(cmd), catalog, strerror(-rc));
2778 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_INFO, buf);
2780 fprintf(stdout, "%s", ((struct obd_ioctl_data *)buf)->ioc_bulk);
2782 fprintf(stderr, "%s: OBD_IOC_LLOG_INFO failed: %s\n",
2783 jt_cmdname(cmd), strerror(errno));
2786 llog_default_device(LLOG_DFLT_DEV_RESET);
2791 int jt_llog_print_cb(const char *record, void *private)
2793 printf("%s\n", record);
2799 llog_process_records(int (record_cb)(const char *record, void *private),
2800 const char *record, void *private, bool reverse)
2808 ptr = strchr(record, '\n');
2811 rc = record_cb(record, private);
2816 } while (ptr && *(ptr + 1));
2818 tmp = (char *)record;
2820 ptr = strrchr(record, '\n');
2825 while ((ptr = strrchr(record, '\n'))) {
2828 rc = record_cb(tmp, private);
2832 rc = record_cb(record, private);
2841 * Iterate over llog records, typically YAML-formatted configuration logs
2843 * \param logname[in] name of llog file or FID
2844 * \param start[in] first record to process
2845 * \param end[in] last record to process (inclusive)
2846 * \param cb[in] callback for records. Return -ve error, or +ve abort.
2847 * \param private[in,out] private data passed to the \a record_cb function
2848 * \param reverse[in] print the llog records from the beginning or the end
2850 * \retval 0 on success
2851 * others handled by the caller
2853 int jt_llog_print_iter(char *logname, long start, long end,
2854 int (record_cb)(const char *record, void *private),
2855 void *private, bool reverse)
2857 struct obd_ioctl_data data = { 0 };
2858 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
2859 char startbuf[16], endbuf[16];
2860 static long inc = sizeof(rawbuf) / 128;
2867 data.ioc_dev = cur_device;
2868 data.ioc_inlbuf1 = logname;
2869 data.ioc_inllen1 = strlen(logname) + 1;
2872 * Estimate about 128 characters per configuration record. Not all
2873 * records will be printed in any case, so they should easily fit. If
2874 * not, the kernel will return -EOVERFLOW and ask for fewer records.
2876 * We don't want to request records from the kernel one-at-a-time, as
2877 * it restarts the config llog iteration from the beginning, so we
2878 * fetch multiple records from the kernel per call and split locally.
2880 for (rec = start; rec < end; rec += inc) {
2881 char *record = ((struct obd_ioctl_data *)buf)->ioc_bulk;
2884 snprintf(startbuf, sizeof(startbuf), "%lu", rec);
2885 snprintf(endbuf, sizeof(endbuf), "%lu",
2886 end < rec + inc - 1 ? end : rec + inc - 1);
2888 /* start and end record numbers are passed as ASCII digits */
2889 data.ioc_inlbuf2 = startbuf;
2890 data.ioc_inllen2 = strlen(startbuf) + 1;
2891 data.ioc_inlbuf3 = endbuf;
2892 data.ioc_inllen3 = strlen(endbuf) + 1;
2894 data.ioc_inllen4 = sizeof(rawbuf) -
2895 __ALIGN_KERNEL(sizeof(data), 8) -
2896 __ALIGN_KERNEL(data.ioc_inllen1, 8) -
2897 __ALIGN_KERNEL(data.ioc_inllen2, 8) -
2898 __ALIGN_KERNEL(data.ioc_inllen3, 8);
2899 memset(buf, 0, sizeof(rawbuf));
2900 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
2902 fprintf(stderr, "%s: invalid ioctl data\n", logname);
2906 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_PRINT, buf);
2907 if (rc == -EOVERFLOW && inc > 2) {
2912 fprintf(stderr, "%s: OBD_IOC_LLOG_PRINT failed: %s\n",
2913 logname, strerror(errno));
2918 /* There is no "end of list" marker, record was not modified */
2919 if (strcmp(record, logname) == 0)
2922 rc = llog_process_records(record_cb, record, private, reverse);
2931 static int llog_parse_catalog_start_end(int *argc, char **argv[],
2932 char **catalog, long *start, long *end)
2934 const struct option long_opts[] = {
2935 /* the --catalog option is not required, just for consistency */
2936 { .val = 'c', .name = "catalog", .has_arg = required_argument },
2937 { .val = 'e', .name = "end", .has_arg = required_argument },
2938 { .val = 'h', .name = "help", .has_arg = no_argument },
2939 { .val = 's', .name = "start", .has_arg = required_argument },
2941 char *cmd = (*argv)[0];
2945 if (!catalog || !start || !end)
2948 /* now process command line arguments*/
2949 while ((c = getopt_long(*argc, *argv, "c:e:hs:",
2950 long_opts, NULL)) != -1) {
2956 *end = strtol(optarg, &endp, 0);
2957 if (*endp != '\0') {
2958 fprintf(stderr, "%s: bad end value '%s'\n",
2964 *start = strtol(optarg, &endp, 0);
2965 if (*endp != '\0') {
2966 fprintf(stderr, "%s: bad start value '%s'\n",
2980 * support old optional positional parameters only if they were
2981 * not already specified with named arguments: logname [start [end]]
2986 "%s: logname is set, unknown argument '%s'\n",
2990 *catalog = (*argv)[0];
2995 if (*catalog == NULL) {
2996 fprintf(stderr, "%s: no logname specified\n", cmd);
3003 "%s: --start is set, unknown argument '%s'\n",
3008 *start = strtol((*argv)[0], &endp, 0);
3009 if (*endp != '\0') {
3010 fprintf(stderr, "%s: bad start value '%s'\n",
3020 "%s: --end is set, unknown argument '%s'\n",
3025 *end = strtol((*argv)[0], &endp, 0);
3026 if (*endp != '\0') {
3027 fprintf(stderr, "%s: bad end value '%s'\n",
3035 fprintf(stderr, "%s: unknown argument '%s'\n", cmd, (*argv)[0]);
3039 if (*end != -1 && *end < *start) {
3040 fprintf(stderr, "%s: end '%lu' less than than start '%lu'\n",
3048 int jt_llog_print(int argc, char **argv)
3050 char *catalog = NULL;
3051 long start = 1, end = -1;
3054 rc = llog_parse_catalog_start_end(&argc, &argv, &catalog, &start, &end);
3058 if (llog_default_device(LLOG_DFLT_MGS_SET))
3059 return CMD_INCOMPLETE;
3061 rc = jt_llog_print_iter(catalog, start, end, jt_llog_print_cb,
3064 llog_default_device(LLOG_DFLT_DEV_RESET);
3070 * Parse catalog, log ID, and optionally a log index with either optional
3071 * arguments or positional arguments. Only the initial catalog argument
3072 * may be positional with other optional arguments.
3074 * The positional arguments option should eventually be phased out.
3076 static int llog_parse_catalog_log_idx(int *argc, char ***argv, const char *opts,
3077 int max_args, struct obd_ioctl_data *data)
3079 const struct option long_opts[] = {
3080 /* the --catalog option is not required, just for consistency */
3081 { .val = 'c', .name = "catalog", .has_arg = required_argument },
3082 { .val = 'h', .name = "help", .has_arg = no_argument },
3083 { .val = 'i', .name = "log_idx", .has_arg = required_argument },
3084 { .val = 'l', .name = "log_id", .has_arg = required_argument },
3089 if (!data || *argc <= 1)
3092 data->ioc_dev = cur_device;
3094 /* now process command line arguments*/
3095 while ((c = getopt_long(*argc, *argv, opts, long_opts, NULL)) != -1) {
3098 data->ioc_inllen1 = strlen(optarg) + 1;
3099 data->ioc_inlbuf1 = optarg;
3102 data->ioc_inllen3 = strlen(optarg) + 1;
3103 data->ioc_inlbuf3 = optarg;
3105 case 'l': /* The log_id option isn't currently needed for
3106 * cancel as mdt_iocontrol() handles IOC_LLOG_CANCEL,
3107 * but we may as well keep it for now.
3109 data->ioc_inllen2 = strlen(optarg) + 1;
3110 data->ioc_inlbuf2 = optarg;
3121 /* Allow catalog to be specified as first option without --catalog */
3122 if (!data->ioc_inlbuf1 && *argc > 0) {
3123 data->ioc_inlbuf1 = (*argv)[0];
3124 data->ioc_inllen1 = strlen((*argv)[0]) + 1;
3132 int jt_llog_cancel(int argc, char **argv)
3134 struct obd_ioctl_data data = { 0 };
3135 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
3136 char *cmd = argv[0];
3139 /* Manage default device */
3140 if (llog_default_device(LLOG_DFLT_MGS_SET))
3141 return CMD_INCOMPLETE;
3143 /* Parse catalog file (in inlbuf1) and named parameters */
3144 rc = llog_parse_catalog_log_idx(&argc, &argv, "c:hi:l:", 3, &data);
3147 * Handle old positional parameters if not using named parameters,
3148 * either "<catalog> <log_idx>" or "<catalog> <log_id> <log_idx>".
3149 * It was "inlbuf3 = log_idx", and "inlbuf2 = log_id" (ignored by
3150 * config log cancel), and shows why I hate positional parameters.
3153 data.ioc_inllen3 = strlen(argv[0]) + 1;
3154 data.ioc_inlbuf3 = argv[0];
3155 } else if (argc == 2) {
3156 data.ioc_inllen2 = strlen(argv[0]) + 1;
3157 data.ioc_inlbuf2 = argv[0];
3158 data.ioc_inllen3 = strlen(argv[1]) + 1;
3159 data.ioc_inlbuf3 = argv[1];
3162 if (!data.ioc_inlbuf1 || !data.ioc_inlbuf3) {
3163 /* missing mandatory parameters */
3168 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3170 fprintf(stderr, "%s: ioctl_pack for catalog '%s' failed: %s\n",
3171 jt_cmdname(cmd), data.ioc_inlbuf1, strerror(-rc));
3175 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CANCEL, buf);
3177 fprintf(stderr, "%s: cancel catalog '%s:%s' failed: %s\n",
3178 jt_cmdname(cmd), data.ioc_inlbuf1, data.ioc_inlbuf3,
3182 llog_default_device(LLOG_DFLT_DEV_RESET);
3186 int jt_llog_check(int argc, char **argv)
3188 struct obd_ioctl_data data = { 0 };
3189 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
3190 char *catalog = NULL;
3191 char startbuf[16], endbuf[16];
3192 long start = 1, end = -1;
3193 char *cmd = argv[0];
3196 rc = llog_parse_catalog_start_end(&argc, &argv, &catalog, &start, &end);
3200 if (llog_default_device(LLOG_DFLT_MGS_SET))
3201 return CMD_INCOMPLETE;
3206 data.ioc_dev = cur_device;
3207 data.ioc_inllen1 = strlen(catalog) + 1;
3208 data.ioc_inlbuf1 = catalog;
3210 snprintf(startbuf, sizeof(startbuf), "%lu", start);
3211 snprintf(endbuf, sizeof(endbuf), "%lu", end);
3212 /* start and end record numbers are passed as ASCII digits */
3213 data.ioc_inllen2 = strlen(startbuf) + 1;
3214 data.ioc_inlbuf2 = startbuf;
3215 data.ioc_inllen3 = strlen(endbuf) + 1;
3216 data.ioc_inlbuf3 = endbuf;
3218 data.ioc_inllen4 = sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(data), 8) -
3219 __ALIGN_KERNEL(data.ioc_inllen1, 8) -
3220 __ALIGN_KERNEL(data.ioc_inllen2, 8) -
3221 __ALIGN_KERNEL(data.ioc_inllen3, 8);
3222 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3224 fprintf(stderr, "%s: ioctl_pack failed for catalog '%s': %s\n",
3225 jt_cmdname(cmd), data.ioc_inlbuf1, strerror(-rc));
3229 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CHECK, buf);
3231 fprintf(stdout, "%s", ((struct obd_ioctl_data *)buf)->ioc_bulk);
3233 fprintf(stderr, "%s: OBD_IOC_LLOG_CHECK failed: %s\n",
3234 jt_cmdname(cmd), strerror(errno));
3236 llog_default_device(LLOG_DFLT_DEV_RESET);
3240 int jt_llog_remove(int argc, char **argv)
3242 struct obd_ioctl_data data = { 0 };
3243 char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
3244 char *cmd = argv[0];
3247 if (llog_default_device(LLOG_DFLT_MGS_SET))
3248 return CMD_INCOMPLETE;
3250 rc = llog_parse_catalog_log_idx(&argc, &argv, "c:hl:", 2, &data);
3255 if (data.ioc_inlbuf2) {
3257 "%s: --log_id is set, unknown argument '%s'\n",
3258 jt_cmdname(cmd), argv[0]);
3263 data.ioc_inllen2 = strlen(argv[0]) + 1;
3264 data.ioc_inlbuf2 = argv[0];
3267 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3269 fprintf(stderr, "%s: ioctl_pack for catalog '%s' failed: %s\n",
3270 jt_cmdname(cmd), data.ioc_inlbuf1, strerror(-rc));
3274 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_REMOVE, buf);
3276 fprintf(stderr, "%s: cancel catalog '%s:%s' failed: %s\n",
3277 jt_cmdname(cmd), data.ioc_inlbuf1, data.ioc_inlbuf2,
3281 llog_default_device(LLOG_DFLT_DEV_RESET);
3285 static void signal_server(int sig)
3287 if (sig == SIGINT) {
3288 do_disconnect("sigint", 1);
3291 fprintf(stderr, "%s: got signal %d\n", jt_cmdname("sigint"),
3296 int obd_initialize(int argc, char **argv)
3298 if (shmem_setup() != 0)
3301 register_ioc_dev(OBD_DEV_ID, OBD_DEV_PATH);
3306 void obd_finalize(int argc, char **argv)
3308 struct sigaction sigact;
3310 /* sigact initialization */
3311 sigact.sa_handler = signal_server;
3312 sigfillset(&sigact.sa_mask);
3313 sigact.sa_flags = SA_RESTART;
3314 /* coverity[uninit_use_in_call] */
3315 sigaction(SIGINT, &sigact, NULL);
3318 do_disconnect(argv[0], 1);