1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Peter J. Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
40 * Author: Andreas Dilger <adilger@clusterfs.com>
41 * Author: Robert Read <rread@clusterfs.com>
45 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <sys/types.h>
51 #include <sys/param.h>
59 #include <obd.h> /* for struct lov_stripe_md */
60 #include <lustre/lustre_build_version.h>
69 #include <lustre/liblustreapi.h>
71 #ifdef HAVE_ASM_PAGE_H
72 #include <asm/page.h> /* needed for PAGE_SIZE - rread */
75 #include <obd_class.h>
76 #include <lnet/lnetctl.h>
81 #define MAX_STRING_SIZE 128
82 #define DEVICES_LIST "/proc/fs/lustre/devices"
89 #define MAX_THREADS 1024
92 __u64 counters[MAX_THREADS];
93 __u64 offsets[MAX_THREADS];
101 static struct shared_data *shared_data;
102 static __u64 counter_snapshot[2][MAX_THREADS];
103 static int prev_valid;
104 static struct timeval prev_time;
108 const int thread = 0;
109 const int nthreads = 1;
112 static char rawbuf[8192];
113 static char *buf = rawbuf;
114 static int max = sizeof(rawbuf);
116 static int cur_device = -1;
119 #define MAX_STRIPES 170
120 struct lov_oinfo lov_oinfos[MAX_STRIPES];
123 struct lov_stripe_md lsm;
124 struct lov_oinfo *ptrs[MAX_STRIPES];
127 static int l2_ioctl(int dev_id, unsigned int opc, void *buf)
129 return l_ioctl(dev_id, opc, buf);
132 #define IOC_INIT(data) \
134 memset(&data, 0, sizeof(data)); \
135 data.ioc_dev = cur_device; \
138 #define IOC_PACK(func, data) \
140 memset(buf, 0, sizeof(rawbuf)); \
141 if (obd_ioctl_pack(&data, &buf, max)) { \
142 fprintf(stderr, "error: %s: invalid ioctl\n", \
148 #define IOC_UNPACK(func, data) \
150 if (obd_ioctl_unpack(&data, buf, max)) { \
151 fprintf(stderr, "error: %s: invalid reply\n", \
157 int lcfg_ioctl(char * func, int dev_id, struct lustre_cfg *lcfg)
159 struct obd_ioctl_data data;
163 data.ioc_type = LUSTRE_CFG_TYPE;
164 data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
166 data.ioc_pbuf1 = (void *)lcfg;
167 IOC_PACK(func, data);
169 rc = l_ioctl(dev_id, OBD_IOC_PROCESS_CFG, buf);
174 static int do_device(char *func, char *devname);
176 static int get_mgs_device()
179 static int mgs_device = -1;
181 if (mgs_device == -1) {
183 do_disconnect(NULL, 1);
184 rc = do_device("mgsioc", mgs);
187 "This command must be run on the MGS.\n");
191 mgs_device = cur_device;
196 /* Returns -1 on error with errno set */
197 int lcfg_mgs_ioctl(char *func, int dev_id, struct lustre_cfg *lcfg)
199 struct obd_ioctl_data data;
203 rc = data.ioc_dev = get_mgs_device();
206 data.ioc_type = LUSTRE_CFG_TYPE;
207 data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
209 data.ioc_pbuf1 = (void *)lcfg;
210 IOC_PACK(func, data);
212 rc = l_ioctl(dev_id, OBD_IOC_PARAM, buf);
216 fprintf(stderr, "Make sure cfg_device is set first.\n");
218 fprintf(stderr, "cfg_device should be of the form "
219 "'lustre-MDT0000'\n");
224 char *obdo_print(struct obdo *obd)
228 sprintf(buf, "id: "LPX64"\ngrp: "LPX64"\natime: "LPU64"\nmtime: "LPU64
229 "\nctime: "LPU64"\nsize: "LPU64"\nblocks: "LPU64
230 "\nblksize: %u\nmode: %o\nuid: %d\ngid: %d\nflags: %x\n"
231 "misc: %x\nnlink: %d,\nvalid "LPX64"\n",
232 obd->o_id, obd->o_gr, obd->o_atime, obd->o_mtime, obd->o_ctime,
233 obd->o_size, obd->o_blocks, obd->o_blksize, obd->o_mode,
234 obd->o_uid, obd->o_gid, obd->o_flags, obd->o_misc,
235 obd->o_nlink, obd->o_valid);
240 #define BAD_VERBOSE (-999999999)
242 #define N2D_OFF 0x100 /* So we can tell between error codes and devices */
244 static int do_name2dev(char *func, char *name)
246 struct obd_ioctl_data data;
251 data.ioc_inllen1 = strlen(name) + 1;
252 data.ioc_inlbuf1 = name;
254 IOC_PACK(func, data);
255 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_NAME2DEV, buf);
258 IOC_UNPACK(func, data);
260 return data.ioc_dev + N2D_OFF;
264 * resolve a device name to a device number.
265 * supports a number, $name or %uuid.
267 int parse_devname(char *func, char *name)
274 if (isdigit(name[0])) {
275 ret = strtoul(name, NULL, 0);
277 if (name[0] == '$' || name[0] == '%')
279 rc = do_name2dev(func, name);
282 // printf("Name %s is device %d\n", name, ret);
284 fprintf(stderr, "No device found for name %s: %s\n",
292 reset_lsmb (struct lsm_buffer *lsmb)
294 memset (&lsmb->lsm, 0, sizeof (lsmb->lsm));
295 memset(lov_oinfos, 0, sizeof(lov_oinfos));
296 lsmb->lsm.lsm_magic = LOV_MAGIC;
300 parse_lsm (struct lsm_buffer *lsmb, char *string)
302 struct lov_stripe_md *lsm = &lsmb->lsm;
307 * object_id[=size#count[@offset:id]*]
312 lsm->lsm_object_id = strtoull (string, &end, 0);
324 lsm->lsm_stripe_size = strtoul (string, &end, 0);
333 lsm->lsm_stripe_count = strtoul (string, &end, 0);
338 if (*string == 0) /* don't have to specify obj ids */
341 for (i = 0; i < lsm->lsm_stripe_count; i++) {
345 lsm->lsm_oinfo[i]->loi_ost_idx = strtoul(string, &end, 0);
349 lsm->lsm_oinfo[i]->loi_id = strtoull(string, &end, 0);
359 char *jt_cmdname(char *func)
361 static char buf[512];
364 sprintf(buf, "%s-%d", func, thread);
371 #define difftime(a, b) \
372 ((a)->tv_sec - (b)->tv_sec + \
373 ((a)->tv_usec - (b)->tv_usec) / 1000000.0)
375 static int be_verbose(int verbose, struct timeval *next_time,
376 __u64 num, __u64 *next_num, int num_total)
383 if (next_time != NULL)
384 gettimeofday(&now, NULL);
386 /* A positive verbosity means to print every X iterations */
387 if (verbose > 0 && (num >= *next_num || num >= num_total)) {
388 *next_num += verbose;
390 next_time->tv_sec = now.tv_sec - verbose;
391 next_time->tv_usec = now.tv_usec;
396 /* A negative verbosity means to print at most each X seconds */
397 if (verbose < 0 && next_time != NULL &&
398 difftime(&now, next_time) >= 0.0){
399 next_time->tv_sec = now.tv_sec - verbose;
400 next_time->tv_usec = now.tv_usec;
408 static int get_verbose(char *func, const char *arg)
413 if (!arg || arg[0] == 'v')
415 else if (arg[0] == 's' || arg[0] == 'q')
418 verbose = (int)strtoul(arg, &end, 0);
420 fprintf(stderr, "error: %s: bad verbose option '%s'\n",
421 jt_cmdname(func), arg);
427 printf("Print status every %d seconds\n", -verbose);
428 else if (verbose == 1)
429 printf("Print status every operation\n");
430 else if (verbose > 1)
431 printf("Print status every %d operations\n", verbose);
436 int do_disconnect(char *func, int verbose)
438 lcfg_set_devname(NULL);
444 static void shmem_setup(void)
446 /* Create new segment */
447 int shmid = shmget(IPC_PRIVATE, sizeof(*shared_data), 0600);
450 fprintf(stderr, "Can't create shared data: %s\n",
455 /* Attatch to new segment */
456 shared_data = (struct shared_data *)shmat(shmid, NULL, 0);
458 if (shared_data == (struct shared_data *)(-1)) {
459 fprintf(stderr, "Can't attach shared data: %s\n",
465 /* Mark segment as destroyed, so it will disappear when we exit.
466 * Forks will inherit attached segments, so we should be OK.
468 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
469 fprintf(stderr, "Can't destroy shared data: %s\n",
474 static inline void shmem_lock(void)
476 l_mutex_lock(&shared_data->mutex);
479 static inline void shmem_unlock(void)
481 l_mutex_unlock(&shared_data->mutex);
484 static inline void shmem_reset(int total_threads)
486 if (shared_data == NULL)
489 memset(shared_data, 0, sizeof(*shared_data));
490 l_mutex_init(&shared_data->mutex);
491 l_cond_init(&shared_data->cond);
492 memset(counter_snapshot, 0, sizeof(counter_snapshot));
494 shared_data->barrier = total_threads;
497 static inline void shmem_bump(void)
499 static int bumped_running;
501 if (shared_data == NULL || thread <= 0 || thread > MAX_THREADS)
505 shared_data->counters[thread - 1]++;
507 shared_data->running++;
512 static void shmem_snap(int total_threads, int live_threads)
514 struct timeval this_time;
521 if (shared_data == NULL || total_threads > MAX_THREADS)
525 memcpy(counter_snapshot[0], shared_data->counters,
526 total_threads * sizeof(counter_snapshot[0][0]));
527 running = shared_data->running;
530 gettimeofday(&this_time, NULL);
532 for (i = 0; i < total_threads; i++) {
533 long long this_count =
534 counter_snapshot[0][i] - counter_snapshot[1][i];
536 if (this_count != 0) {
542 secs = (this_time.tv_sec + this_time.tv_usec / 1000000.0) -
543 (prev_time.tv_sec + prev_time.tv_usec / 1000000.0);
546 live_threads == total_threads &&
547 secs > 0.0) /* someone screwed with the time? */
548 printf("%d/%d Total: %f/second\n", non_zero, total_threads, total / secs);
550 memcpy(counter_snapshot[1], counter_snapshot[0],
551 total_threads * sizeof(counter_snapshot[0][0]));
552 prev_time = this_time;
554 running == total_threads)
558 static void shmem_stop(void)
560 if (shared_data == NULL)
563 shared_data->stop = 1;
566 static int shmem_running(void)
568 return (shared_data == NULL ||
572 static void shmem_setup(void)
576 static inline void shmem_reset(int total_threads)
580 static inline void shmem_bump(void)
584 static void shmem_lock()
588 static void shmem_unlock()
592 static void shmem_stop(void)
596 static int shmem_running(void)
602 extern command_t cmdlist[];
604 static int do_device(char *func, char *devname)
608 dev = parse_devname(func, devname);
612 lcfg_set_devname(devname);
617 int jt_obd_get_device()
622 int jt_obd_device(int argc, char **argv)
630 printf("current device is %d - %s\n",
631 cur_device, lcfg_get_devname() ? : "not set");
634 rc = do_device("device", argv[1]);
638 int jt_opt_device(int argc, char **argv)
646 rc = do_device("device", argv[1]);
649 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
651 ret = do_disconnect(argv[0], 0);
659 static void parent_sighandler (int sig)
664 int jt_opt_threads(int argc, char **argv)
666 static char cmdstr[128];
669 struct sigaction sigact;
670 struct sigaction saveact1;
671 struct sigaction saveact2;
672 unsigned long threads;
676 int report_count = -1;
683 threads = strtoul(argv[1], &end, 0);
686 report_count = strtoul(end + 1, &end, 0);
688 if (*end || threads > MAX_THREADS) {
689 fprintf(stderr, "error: %s: invalid thread count '%s'\n",
690 jt_cmdname(argv[0]), argv[1]);
694 verbose = get_verbose(argv[0], argv[2]);
695 if (verbose == BAD_VERBOSE)
699 snprintf(cmdstr, sizeof(cmdstr), "%s", argv[4]);
700 for (i = 5; i < argc; i++)
701 snprintf(cmdstr + strlen(cmdstr), sizeof(cmdstr),
704 printf("%s: starting %ld threads on device %s running %s\n",
705 argv[0], threads, argv[3], cmdstr);
708 shmem_reset(threads);
710 sigemptyset(&sigset);
711 sigaddset(&sigset, SIGALRM);
712 sigaddset(&sigset, SIGCHLD);
713 sigprocmask(SIG_BLOCK, &sigset, &saveset);
717 for (i = 1, next_thread = verbose; i <= threads; i++) {
720 fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
721 strerror(rc = errno));
723 } else if (rc == 0) {
724 sigprocmask(SIG_SETMASK, &saveset, NULL);
727 argv[2] = "--device";
728 return jt_opt_device(argc - 2, argv + 2);
729 } else if (be_verbose(verbose, NULL, i, &next_thread, threads))
730 printf("%s: thread #%d (PID %d) started\n",
735 if (!thread) { /* parent process */
736 int live_threads = threads;
738 sigemptyset(&sigset);
739 sigemptyset(&sigact.sa_mask);
740 sigact.sa_handler = parent_sighandler;
743 sigaction(SIGALRM, &sigact, &saveact1);
744 sigaction(SIGCHLD, &sigact, &saveact2);
746 while (live_threads > 0) {
750 if (verbose < 0) /* periodic stats */
756 while (live_threads > 0) {
757 ret = waitpid(0, &status, WNOHANG);
762 fprintf(stderr, "error: %s: wait - %s\n",
763 argv[0], strerror(errno));
769 * This is a hack. We _should_ be able
770 * to use WIFEXITED(status) to see if
771 * there was an error, but it appears
772 * to be broken and it always returns 1
775 int err = WEXITSTATUS(status);
776 if (err || WIFSIGNALED(status))
778 "%s: PID %d had rc=%d\n",
787 /* Show stats while all threads running */
789 shmem_snap(threads, live_threads);
790 if (report_count > 0 && --report_count == 0)
794 sigaction(SIGCHLD, &saveact2, NULL);
795 sigaction(SIGALRM, &saveact1, NULL);
798 sigprocmask(SIG_SETMASK, &saveset, NULL);
803 int jt_opt_threads(int argc, char **argv)
805 fprintf(stderr, "%s not-supported in a single-threaded runtime\n",
806 jt_cmdname(argv[0]));
811 int jt_opt_net(int argc, char **argv)
822 rc = jt_ptl_network (2, arg2);
825 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
830 int jt_obd_no_transno(int argc, char **argv)
832 struct obd_ioctl_data data;
840 IOC_PACK(argv[0], data);
841 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_NO_TRANSNO, buf);
843 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
844 strerror(rc = errno));
849 int jt_obd_set_readonly(int argc, char **argv)
851 struct obd_ioctl_data data;
859 IOC_PACK(argv[0], data);
860 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_SET_READONLY, buf);
862 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
863 strerror(rc = errno));
868 int jt_obd_abort_recovery(int argc, char **argv)
870 struct obd_ioctl_data data;
878 IOC_PACK(argv[0], data);
879 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_ABORT_RECOVERY, buf);
881 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
882 strerror(rc = errno));
887 int jt_get_version(int argc, char **argv)
891 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buf;
896 memset(buf, 0, sizeof(buf));
897 data->ioc_version = OBD_IOCTL_VERSION;
898 data->ioc_inllen1 = sizeof(buf) - size_round(sizeof(*data));
899 data->ioc_inlbuf1 = buf + size_round(sizeof(*data));
900 data->ioc_len = obd_ioctl_packlen(data);
902 rc = l2_ioctl(OBD_DEV_ID, OBD_GET_VERSION, buf);
904 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
905 strerror(rc = errno));
907 printf("Lustre version: %s\n", data->ioc_bulk);
910 printf("lctl version: %s\n", BUILD_VERSION);
915 * Print an obd device line with the ost_conn_uuid inserted, if the obd
918 static void print_obd_line(char *s)
920 char buf[MAX_STRING_SIZE];
921 char obd_name[MAX_OBD_NAME];
925 if (sscanf(s, " %*d %*s osc %s %*s %*d ", obd_name) == 0)
927 snprintf(buf, sizeof(buf),
928 "/proc/fs/lustre/osc/%s/ost_conn_uuid", obd_name);
929 if ((fp = fopen(buf, "r")) == NULL)
934 if (sscanf(s, " %*d %*s mdc %s %*s %*d ", obd_name) == 0)
936 snprintf(buf, sizeof(buf),
937 "/proc/fs/lustre/mdc/%s/mds_conn_uuid", obd_name);
938 if ((fp = fopen(buf, "r")) == NULL)
942 fgets(buf, sizeof(buf), fp);
945 /* trim trailing newlines */
946 ptr = strrchr(buf, '\n');
947 if (ptr) *ptr = '\0';
948 ptr = strrchr(s, '\n');
949 if (ptr) *ptr = '\0';
951 printf("%s %s\n", s, buf);
959 /* get device list by ioctl */
960 int jt_obd_list_ioctl(int argc, char **argv)
964 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buf;
968 /* Just ignore a -t option. Only supported with /proc. */
969 else if (argc == 2 && strcmp(argv[1], "-t") != 0)
972 for (index = 0;; index++) {
973 memset(buf, 0, sizeof(buf));
974 data->ioc_version = OBD_IOCTL_VERSION;
975 data->ioc_inllen1 = sizeof(buf) - size_round(sizeof(*data));
976 data->ioc_inlbuf1 = buf + size_round(sizeof(*data));
977 data->ioc_len = obd_ioctl_packlen(data);
978 data->ioc_count = index;
980 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_GETDEVICE, buf);
983 printf("%s\n", (char *)data->ioc_bulk);
987 /* no device or the last device */
990 fprintf(stderr, "Error getting device list: %s: "
997 int jt_obd_list(int argc, char **argv)
1000 char buf[MAX_STRING_SIZE];
1006 else if (argc == 2) {
1007 if (strcmp(argv[1], "-t") == 0)
1013 fp = fopen(DEVICES_LIST, "r");
1015 fprintf(stderr, "error: %s: %s opening "DEVICES_LIST"\n",
1016 jt_cmdname(argv[0]), strerror(rc = errno));
1017 return jt_obd_list_ioctl(argc, argv);
1020 while (fgets(buf, sizeof(buf), fp) != NULL)
1022 print_obd_line(buf);
1033 /* Create one or more objects, arg[4] may describe stripe meta-data. If
1034 * not, defaults assumed. This echo-client instance stashes the stripe
1035 * object ids. Use get_stripe on this node to print full lsm and
1036 * set_stripe on another node to cut/paste between nodes.
1038 /* create <count> [<file_create_mode>] [q|v|# verbosity] [striping] */
1039 int jt_obd_create(int argc, char **argv)
1041 struct obd_ioctl_data data;
1042 struct timeval next_time;
1043 __u64 count = 1, next_count, base_id = 0;
1044 int verbose = 1, mode = 0100644, rc = 0, i, valid_lsm = 0;
1048 if (argc < 2 || argc > 5)
1051 count = strtoull(argv[1], &end, 0);
1053 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1054 jt_cmdname(argv[0]), argv[1]);
1059 mode = strtoul(argv[2], &end, 0);
1061 fprintf(stderr, "error: %s: invalid mode '%s'\n",
1062 jt_cmdname(argv[0]), argv[2]);
1065 if (!(mode & S_IFMT))
1070 verbose = get_verbose(argv[0], argv[3]);
1071 if (verbose == BAD_VERBOSE)
1076 reset_lsmb (&lsm_buffer); /* will set default */
1078 rc = parse_lsm (&lsm_buffer, argv[4]);
1080 fprintf(stderr, "error: %s: invalid lsm '%s'\n",
1081 jt_cmdname(argv[0]), argv[4]);
1084 base_id = lsm_buffer.lsm.lsm_object_id;
1088 printf("%s: "LPD64" objects\n", jt_cmdname(argv[0]), count);
1089 gettimeofday(&next_time, NULL);
1090 next_time.tv_sec -= verbose;
1092 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1093 data.ioc_obdo1.o_mode = mode;
1094 data.ioc_obdo1.o_id = base_id;
1095 data.ioc_obdo1.o_uid = 0;
1096 data.ioc_obdo1.o_gid = 0;
1097 data.ioc_obdo1.o_valid = OBD_MD_FLTYPE | OBD_MD_FLMODE |
1098 OBD_MD_FLID | OBD_MD_FLUID | OBD_MD_FLGID;
1101 data.ioc_plen1 = sizeof lsm_buffer;
1102 data.ioc_pbuf1 = (char *)&lsm_buffer;
1105 IOC_PACK(argv[0], data);
1106 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_CREATE, buf);
1107 IOC_UNPACK(argv[0], data);
1110 fprintf(stderr, "error: %s: #%d - %s\n",
1111 jt_cmdname(argv[0]), i, strerror(rc = errno));
1114 if (!(data.ioc_obdo1.o_valid & OBD_MD_FLID)) {
1115 fprintf(stderr,"error: %s: oid not valid #%d:"LPX64"\n",
1116 jt_cmdname(argv[0]), i, data.ioc_obdo1.o_valid);
1121 if (be_verbose(verbose, &next_time, i, &next_count, count))
1122 printf("%s: #%d is object id "LPX64"\n",
1123 jt_cmdname(argv[0]), i, data.ioc_obdo1.o_id);
1128 int jt_obd_setattr(int argc, char **argv)
1130 struct obd_ioctl_data data;
1138 data.ioc_obdo1.o_id = strtoull(argv[1], &end, 0);
1140 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1141 jt_cmdname(argv[0]), argv[1]);
1144 data.ioc_obdo1.o_mode = S_IFREG | strtoul(argv[2], &end, 0);
1146 fprintf(stderr, "error: %s: invalid mode '%s'\n",
1147 jt_cmdname(argv[0]), argv[2]);
1150 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
1152 IOC_PACK(argv[0], data);
1153 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, buf);
1155 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1156 strerror(rc = errno));
1161 int jt_obd_test_setattr(int argc, char **argv)
1163 struct obd_ioctl_data data;
1164 struct timeval start, next_time;
1165 __u64 i, count, next_count;
1171 if (argc < 2 || argc > 4)
1175 count = strtoull(argv[1], &end, 0);
1177 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1178 jt_cmdname(argv[0]), argv[1]);
1183 verbose = get_verbose(argv[0], argv[2]);
1184 if (verbose == BAD_VERBOSE)
1189 if (argv[3][0] == 't') {
1190 objid = strtoull(argv[3] + 1, &end, 0);
1192 objid += thread - 1;
1194 objid = strtoull(argv[3], &end, 0);
1196 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1197 jt_cmdname(argv[0]), argv[3]);
1202 gettimeofday(&start, NULL);
1203 next_time.tv_sec = start.tv_sec - verbose;
1204 next_time.tv_usec = start.tv_usec;
1206 printf("%s: setting "LPD64" attrs (objid "LPX64"): %s",
1207 jt_cmdname(argv[0]), count, objid, ctime(&start.tv_sec));
1209 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1210 data.ioc_obdo1.o_id = objid;
1211 data.ioc_obdo1.o_mode = S_IFREG;
1212 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
1213 IOC_PACK(argv[0], data);
1214 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_SETATTR, &data);
1217 fprintf(stderr, "error: %s: #"LPD64" - %d:%s\n",
1218 jt_cmdname(argv[0]), i, errno, strerror(rc = errno));
1222 (verbose, &next_time, i, &next_count, count))
1223 printf("%s: set attr #"LPD64"\n",
1224 jt_cmdname(argv[0]), i);
1232 gettimeofday(&end, NULL);
1234 diff = difftime(&end, &start);
1238 printf("%s: "LPD64" attrs in %.3fs (%.3f attr/s): %s",
1239 jt_cmdname(argv[0]), i, diff, i / diff,
1240 ctime(&end.tv_sec));
1245 int jt_obd_destroy(int argc, char **argv)
1247 struct obd_ioctl_data data;
1248 struct timeval next_time;
1249 __u64 count = 1, next_count;
1256 if (argc < 2 || argc > 4)
1259 id = strtoull(argv[1], &end, 0);
1260 if (*end || id == 0 || errno != 0) {
1261 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1262 jt_cmdname(argv[0]), argv[1]);
1266 count = strtoull(argv[2], &end, 0);
1269 "error: %s: invalid iteration count '%s'\n",
1270 jt_cmdname(argv[0]), argv[2]);
1276 verbose = get_verbose(argv[0], argv[3]);
1277 if (verbose == BAD_VERBOSE)
1281 printf("%s: "LPD64" objects\n", jt_cmdname(argv[0]), count);
1282 gettimeofday(&next_time, NULL);
1283 next_time.tv_sec -= verbose;
1285 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++, id++) {
1286 data.ioc_obdo1.o_id = id;
1287 data.ioc_obdo1.o_mode = S_IFREG | 0644;
1288 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLMODE;
1290 IOC_PACK(argv[0], data);
1291 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_DESTROY, buf);
1292 IOC_UNPACK(argv[0], data);
1295 fprintf(stderr, "error: %s: objid "LPX64": %s\n",
1296 jt_cmdname(argv[0]), id, strerror(rc = errno));
1300 if (be_verbose(verbose, &next_time, i, &next_count, count))
1301 printf("%s: #%d is object id "LPX64"\n",
1302 jt_cmdname(argv[0]), i, id);
1308 int jt_obd_getattr(int argc, char **argv)
1310 struct obd_ioctl_data data;
1318 data.ioc_obdo1.o_id = strtoull(argv[1], &end, 0);
1320 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1321 jt_cmdname(argv[0]), argv[1]);
1324 /* to help obd filter */
1325 data.ioc_obdo1.o_mode = 0100644;
1326 data.ioc_obdo1.o_valid = 0xffffffff;
1327 printf("%s: object id "LPX64"\n", jt_cmdname(argv[0]),data.ioc_obdo1.o_id);
1329 IOC_PACK(argv[0], data);
1330 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, buf);
1331 IOC_UNPACK(argv[0], data);
1333 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1334 strerror(rc = errno));
1336 printf("%s: object id "LPX64", mode %o\n", jt_cmdname(argv[0]),
1337 data.ioc_obdo1.o_id, data.ioc_obdo1.o_mode);
1342 int jt_obd_test_getattr(int argc, char **argv)
1344 struct obd_ioctl_data data;
1345 struct timeval start, next_time;
1346 __u64 i, count, next_count;
1352 if (argc < 2 || argc > 4)
1356 count = strtoull(argv[1], &end, 0);
1358 fprintf(stderr, "error: %s: invalid iteration count '%s'\n",
1359 jt_cmdname(argv[0]), argv[1]);
1364 verbose = get_verbose(argv[0], argv[2]);
1365 if (verbose == BAD_VERBOSE)
1370 if (argv[3][0] == 't') {
1371 objid = strtoull(argv[3] + 1, &end, 0);
1373 objid += thread - 1;
1375 objid = strtoull(argv[3], &end, 0);
1377 fprintf(stderr, "error: %s: invalid objid '%s'\n",
1378 jt_cmdname(argv[0]), argv[3]);
1383 gettimeofday(&start, NULL);
1384 next_time.tv_sec = start.tv_sec - verbose;
1385 next_time.tv_usec = start.tv_usec;
1387 printf("%s: getting "LPD64" attrs (objid "LPX64"): %s",
1388 jt_cmdname(argv[0]), count, objid, ctime(&start.tv_sec));
1390 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1391 data.ioc_obdo1.o_id = objid;
1392 data.ioc_obdo1.o_mode = S_IFREG;
1393 data.ioc_obdo1.o_valid = 0xffffffff;
1394 IOC_PACK(argv[0], data);
1395 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_GETATTR, &data);
1398 fprintf(stderr, "error: %s: #"LPD64" - %d:%s\n",
1399 jt_cmdname(argv[0]), i, errno, strerror(rc = errno));
1403 (verbose, &next_time, i, &next_count, count))
1404 printf("%s: got attr #"LPD64"\n",
1405 jt_cmdname(argv[0]), i);
1413 gettimeofday(&end, NULL);
1415 diff = difftime(&end, &start);
1419 printf("%s: "LPD64" attrs in %.3fs (%.3f attr/s): %s",
1420 jt_cmdname(argv[0]), i, diff, i / diff,
1421 ctime(&end.tv_sec));
1426 /* test_brw <cnt> count
1427 <r|w[r(repeat)x(noverify)]> mode
1428 <q|v|#(print interval)> verbosity
1429 <npages[+offset]> blocksize
1430 <[[<interleave_threads>]t(inc obj by thread#)]obj> object
1431 [p|g<args>] batch */
1432 int jt_obd_test_brw(int argc, char **argv)
1434 struct obd_ioctl_data data;
1435 struct timeval start, next_time;
1436 __u64 count, next_count, len, stride, thr_offset = 0, objid = 3;
1437 int write = 0, verbose = 1, cmd, i, rc = 0, pages = 1;
1438 int offset_pages = 0;
1440 int repeat_offset = 0;
1441 unsigned long long ull;
1442 int nthr_per_obj = 0;
1447 if (argc < 2 || argc > 7) {
1448 fprintf(stderr, "error: %s: bad number of arguments: %d\n",
1449 jt_cmdname(argv[0]), argc);
1453 count = strtoull(argv[1], &end, 0);
1455 fprintf(stderr, "error: %s: bad iteration count '%s'\n",
1456 jt_cmdname(argv[0]), argv[1]);
1461 if (argv[2][0] == 'w' || argv[2][0] == '1')
1463 /* else it's a read */
1465 if (argv[2][0] != 0)
1466 for (i = 1; argv[2][i] != 0; i++)
1467 switch (argv[2][i]) {
1477 fprintf (stderr, "Can't parse cmd '%s'\n",
1484 verbose = get_verbose(argv[0], argv[3]);
1485 if (verbose == BAD_VERBOSE)
1490 pages = strtoul(argv[4], &end, 0);
1493 offset_pages = strtoul(end + 1, &end, 0);
1496 offset_pages < 0 || offset_pages >= pages) {
1497 fprintf(stderr, "error: %s: bad npages[+offset] parameter '%s'\n",
1498 jt_cmdname(argv[0]), argv[4]);
1505 (n = strtol(argv[5], &end, 0)) > 0 &&
1507 (ull = strtoull(end + 1, &end, 0)) > 0 &&
1511 } else if (thread &&
1512 argv[5][0] == 't') {
1514 objid = strtoull(argv[5] + 1, &end, 0);
1517 objid = strtoull(argv[5], &end, 0);
1520 fprintf(stderr, "error: %s: bad objid '%s'\n",
1521 jt_cmdname(argv[0]), argv[5]);
1528 /* communicate the 'type' of brw test and batching to echo_client.
1529 * don't start. we'd love to refactor this lctl->echo_client
1531 data.ioc_pbuf1 = (void *)1;
1535 switch(argv[6][0]) {
1536 case 'g': /* plug and unplug */
1537 data.ioc_pbuf1 = (void *)2;
1538 data.ioc_plen1 = strtoull(argv[6] + 1, &end,
1541 case 'p': /* prep and commit */
1542 data.ioc_pbuf1 = (void *)3;
1543 data.ioc_plen1 = strtoull(argv[6] + 1, &end,
1547 fprintf(stderr, "error: %s: batching '%s' "
1548 "needs to specify 'p' or 'g'\n",
1549 jt_cmdname(argv[0]), argv[6]);
1554 fprintf(stderr, "error: %s: bad batching '%s'\n",
1555 jt_cmdname(argv[0]), argv[6]);
1558 data.ioc_plen1 *= getpagesize();
1561 len = pages * getpagesize();
1562 thr_offset = offset_pages * getpagesize();
1568 if (nthr_per_obj != 0) {
1569 /* threads interleave */
1570 obj_idx = (thread - 1)/nthr_per_obj;
1572 stride *= nthr_per_obj;
1574 shared_data->offsets[obj_idx] = stride + thr_offset;
1575 thr_offset += ((thread - 1) % nthr_per_obj) * len;
1577 /* threads disjoint */
1578 thr_offset += (thread - 1) * len;
1581 shared_data->barrier--;
1582 if (shared_data->barrier == 0)
1583 l_cond_broadcast(&shared_data->cond);
1585 l_cond_wait(&shared_data->cond,
1586 &shared_data->mutex);
1592 data.ioc_obdo1.o_id = objid;
1593 data.ioc_obdo1.o_mode = S_IFREG;
1594 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLFLAGS;
1595 data.ioc_obdo1.o_flags = (verify ? OBD_FL_DEBUG_CHECK : 0);
1596 data.ioc_count = len;
1597 data.ioc_offset = (repeat_offset ? 0 : thr_offset);
1599 gettimeofday(&start, NULL);
1600 next_time.tv_sec = start.tv_sec - verbose;
1601 next_time.tv_usec = start.tv_usec;
1604 printf("%s: %s "LPU64"x%d pages (obj "LPX64", off "LPU64"): %s",
1605 jt_cmdname(argv[0]), write ? "writing" : "reading", count,
1606 pages, objid, data.ioc_offset, ctime(&start.tv_sec));
1608 cmd = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
1609 for (i = 1, next_count = verbose; i <= count && shmem_running(); i++) {
1610 data.ioc_obdo1.o_valid &= ~(OBD_MD_FLBLOCKS|OBD_MD_FLGRANT);
1611 IOC_PACK(argv[0], data);
1612 rc = l2_ioctl(OBD_DEV_ID, cmd, buf);
1615 fprintf(stderr, "error: %s: #%d - %s on %s\n",
1616 jt_cmdname(argv[0]), i, strerror(rc = errno),
1617 write ? "write" : "read");
1619 } else if (be_verbose(verbose, &next_time,i, &next_count,count)) {
1621 printf("%s: %s number %d @ "LPD64":"LPU64" for %d\n",
1622 jt_cmdname(argv[0]), write ? "write" : "read", i,
1623 data.ioc_obdo1.o_id, data.ioc_offset,
1624 (int)(pages * getpagesize()));
1628 if (!repeat_offset) {
1630 if (stride == len) {
1631 data.ioc_offset += stride;
1632 } else if (i < count) {
1634 data.ioc_offset = shared_data->offsets[obj_idx];
1635 shared_data->offsets[obj_idx] += len;
1639 data.ioc_offset += len;
1640 obj_idx = 0; /* avoids an unused var warning */
1649 gettimeofday(&end, NULL);
1651 diff = difftime(&end, &start);
1655 printf("%s: %s %dx%d pages in %.3fs (%.3f MB/s): %s",
1656 jt_cmdname(argv[0]), write ? "wrote" : "read",
1658 ((double)i * pages * getpagesize()) /
1660 ctime(&end.tv_sec));
1666 int jt_obd_lov_getconfig(int argc, char **argv)
1668 struct obd_ioctl_data data;
1669 struct lov_desc desc;
1670 struct obd_uuid *uuidarray;
1681 fd = open(path, O_RDONLY);
1683 fprintf(stderr, "open \"%s\" failed: %s\n", path,
1688 memset(&desc, 0, sizeof(desc));
1689 obd_str2uuid(&desc.ld_uuid, argv[1]);
1690 desc.ld_tgt_count = ((OBD_MAX_IOCTL_BUFFER-sizeof(data)-sizeof(desc)) /
1691 (sizeof(*uuidarray) + sizeof(*obdgens)));
1695 uuidarray = calloc(desc.ld_tgt_count, sizeof(*uuidarray));
1697 fprintf(stderr, "error: %s: no memory for %d uuid's\n",
1698 jt_cmdname(argv[0]), desc.ld_tgt_count);
1702 obdgens = calloc(desc.ld_tgt_count, sizeof(*obdgens));
1704 fprintf(stderr, "error: %s: no memory for %d generation #'s\n",
1705 jt_cmdname(argv[0]), desc.ld_tgt_count);
1710 data.ioc_inllen1 = sizeof(desc);
1711 data.ioc_inlbuf1 = (char *)&desc;
1712 data.ioc_inllen2 = desc.ld_tgt_count * sizeof(*uuidarray);
1713 data.ioc_inlbuf2 = (char *)uuidarray;
1714 data.ioc_inllen3 = desc.ld_tgt_count * sizeof(*obdgens);
1715 data.ioc_inlbuf3 = (char *)obdgens;
1717 if (obd_ioctl_pack(&data, &buf, max)) {
1718 fprintf(stderr, "error: %s: invalid ioctl\n",
1719 jt_cmdname(argv[0]));
1723 rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
1724 if (rc == -ENOSPC) {
1729 fprintf(stderr, "error: %s: ioctl error: %s\n",
1730 jt_cmdname(argv[0]), strerror(rc = errno));
1732 struct obd_uuid *uuidp;
1736 if (obd_ioctl_unpack(&data, buf, max)) {
1737 fprintf(stderr, "error: %s: invalid reply\n",
1738 jt_cmdname(argv[0]));
1742 if (desc.ld_default_stripe_count == (__u16)-1)
1743 printf("default_stripe_count: %d\n", -1);
1745 printf("default_stripe_count: %u\n",
1746 desc.ld_default_stripe_count);
1747 printf("default_stripe_size: "LPU64"\n",
1748 desc.ld_default_stripe_size);
1749 printf("default_stripe_offset: "LPU64"\n",
1750 desc.ld_default_stripe_offset);
1751 printf("default_stripe_pattern: %u\n", desc.ld_pattern);
1752 printf("obd_count: %u\n", desc.ld_tgt_count);
1753 printf("OBDS:\tobdidx\t\tobdgen\t\t obduuid\n");
1756 for (i = 0; i < desc.ld_tgt_count; i++, uuidp++, genp++)
1757 printf("\t%6u\t%14u\t\t %s\n", i, *genp, (char *)uuidp);
1768 int jt_obd_ldlm_regress_start(int argc, char **argv)
1771 struct obd_ioctl_data data;
1772 char argstring[200];
1773 int i, count = sizeof(argstring) - 1;
1779 argstring[0] = '\0';
1780 for (i = 1; i < argc; i++) {
1781 strncat(argstring, " ", count);
1783 strncat(argstring, argv[i], count);
1784 count -= strlen(argv[i]);
1787 if (strlen(argstring)) {
1788 data.ioc_inlbuf1 = argstring;
1789 data.ioc_inllen1 = strlen(argstring) + 1;
1792 IOC_PACK(argv[0], data);
1793 rc = l2_ioctl(OBD_DEV_ID, IOC_LDLM_REGRESS_START, buf);
1795 fprintf(stderr, "error: %s: test failed: %s\n",
1796 jt_cmdname(argv[0]), strerror(rc = errno));
1801 int jt_obd_ldlm_regress_stop(int argc, char **argv)
1804 struct obd_ioctl_data data;
1810 IOC_PACK(argv[0], data);
1811 rc = l2_ioctl(OBD_DEV_ID, IOC_LDLM_REGRESS_STOP, buf);
1814 fprintf(stderr, "error: %s: test failed: %s\n",
1815 jt_cmdname(argv[0]), strerror(rc = errno));
1819 static int do_activate(int argc, char **argv, int flag)
1821 struct obd_ioctl_data data;
1828 /* reuse offset for 'active' */
1829 data.ioc_offset = flag;
1831 IOC_PACK(argv[0], data);
1832 rc = l2_ioctl(OBD_DEV_ID, IOC_OSC_SET_ACTIVE, buf);
1834 fprintf(stderr, "error: %s: failed: %s\n",
1835 jt_cmdname(argv[0]), strerror(rc = errno));
1840 int jt_obd_deactivate(int argc, char **argv)
1842 return do_activate(argc, argv, 0);
1845 int jt_obd_activate(int argc, char **argv)
1847 return do_activate(argc, argv, 1);
1850 int jt_obd_recover(int argc, char **argv)
1853 struct obd_ioctl_data data;
1860 data.ioc_inllen1 = strlen(argv[1]) + 1;
1861 data.ioc_inlbuf1 = argv[1];
1864 IOC_PACK(argv[0], data);
1865 rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_CLIENT_RECOVER, buf);
1867 fprintf(stderr, "error: %s: %s\n", jt_cmdname(argv[0]),
1868 strerror(rc = errno));
1874 int jt_obd_mdc_lookup(int argc, char **argv)
1876 struct obd_ioctl_data data;
1877 char *parent, *child;
1878 int rc, fd, verbose = 1;
1880 if (argc < 3 || argc > 4)
1886 verbose = get_verbose(argv[0], argv[3]);
1890 data.ioc_inllen1 = strlen(child) + 1;
1891 data.ioc_inlbuf1 = child;
1893 IOC_PACK(argv[0], data);
1895 fd = open(parent, O_RDONLY);
1897 fprintf(stderr, "open \"%s\" failed: %s\n", parent,
1902 rc = ioctl(fd, IOC_MDC_LOOKUP, buf);
1904 fprintf(stderr, "error: %s: ioctl error: %s\n",
1905 jt_cmdname(argv[0]), strerror(rc = errno));
1910 IOC_UNPACK(argv[0], data);
1911 printf("%s: mode %o uid %d gid %d\n", child,
1912 data.ioc_obdo1.o_mode, data.ioc_obdo1.o_uid,
1913 data.ioc_obdo1.o_gid);
1919 int jt_cfg_dump_log(int argc, char **argv)
1921 struct obd_ioctl_data data;
1929 data.ioc_inllen1 = strlen(argv[1]) + 1;
1930 data.ioc_inlbuf1 = argv[1];
1932 IOC_PACK(argv[0], data);
1933 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_DUMP_LOG, buf);
1935 fprintf(stderr, "OBD_IOC_DUMP_LOG failed: %s\n",
1941 int jt_llog_catlist(int argc, char **argv)
1943 struct obd_ioctl_data data;
1950 data.ioc_inllen1 = max - size_round(sizeof(data));
1951 IOC_PACK(argv[0], data);
1953 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CATLOGLIST, buf);
1955 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
1957 fprintf(stderr, "OBD_IOC_CATLOGLIST failed: %s\n",
1963 int jt_llog_info(int argc, char **argv)
1965 struct obd_ioctl_data data;
1972 data.ioc_inllen1 = strlen(argv[1]) + 1;
1973 data.ioc_inlbuf1 = argv[1];
1974 data.ioc_inllen2 = max - size_round(sizeof(data)) -
1975 size_round(data.ioc_inllen1);
1976 IOC_PACK(argv[0], data);
1978 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_INFO, buf);
1980 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
1982 fprintf(stderr, "OBD_IOC_LLOG_INFO failed: %s\n",
1988 int jt_llog_print(int argc, char **argv)
1990 struct obd_ioctl_data data;
1993 if (argc != 2 && argc != 4)
1997 data.ioc_inllen1 = strlen(argv[1]) + 1;
1998 data.ioc_inlbuf1 = argv[1];
2000 data.ioc_inllen2 = strlen(argv[2]) + 1;
2001 data.ioc_inlbuf2 = argv[2];
2002 data.ioc_inllen3 = strlen(argv[3]) + 1;
2003 data.ioc_inlbuf3 = argv[3];
2005 char from[2] = "1", to[3] = "-1";
2006 data.ioc_inllen2 = strlen(from) + 1;
2007 data.ioc_inlbuf2 = from;
2008 data.ioc_inllen3 = strlen(to) + 1;
2009 data.ioc_inlbuf3 = to;
2011 data.ioc_inllen4 = max - size_round(sizeof(data)) -
2012 size_round(data.ioc_inllen1) -
2013 size_round(data.ioc_inllen2) -
2014 size_round(data.ioc_inllen3);
2015 IOC_PACK(argv[0], data);
2017 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_PRINT, buf);
2019 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2021 fprintf(stderr, "OBD_IOC_LLOG_PRINT failed: %s\n",
2027 int jt_llog_cancel(int argc, char **argv)
2029 struct obd_ioctl_data data;
2036 data.ioc_inllen1 = strlen(argv[1]) + 1;
2037 data.ioc_inlbuf1 = argv[1];
2038 data.ioc_inllen2 = strlen(argv[2]) + 1;
2039 data.ioc_inlbuf2 = argv[2];
2040 data.ioc_inllen3 = strlen(argv[3]) + 1;
2041 data.ioc_inlbuf3 = argv[3];
2042 IOC_PACK(argv[0], data);
2044 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CANCEL, buf);
2046 fprintf(stdout, "index %s be canceled.\n", argv[3]);
2048 fprintf(stderr, "OBD_IOC_LLOG_CANCEL failed: %s\n",
2054 int jt_llog_check(int argc, char **argv)
2056 struct obd_ioctl_data data;
2059 if (argc != 2 && argc != 4)
2063 data.ioc_inllen1 = strlen(argv[1]) + 1;
2064 data.ioc_inlbuf1 = argv[1];
2066 data.ioc_inllen2 = strlen(argv[2]) + 1;
2067 data.ioc_inlbuf2 = argv[2];
2068 data.ioc_inllen3 = strlen(argv[3]) + 1;
2069 data.ioc_inlbuf3 = argv[3];
2071 char from[2] = "1", to[3] = "-1";
2072 data.ioc_inllen2 = strlen(from) + 1;
2073 data.ioc_inlbuf2 = from;
2074 data.ioc_inllen3 = strlen(to) + 1;
2075 data.ioc_inlbuf3 = to;
2077 data.ioc_inllen4 = max - size_round(sizeof(data)) -
2078 size_round(data.ioc_inllen1) -
2079 size_round(data.ioc_inllen2) -
2080 size_round(data.ioc_inllen3);
2081 IOC_PACK(argv[0], data);
2083 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CHECK, buf);
2085 fprintf(stdout, "%s", ((struct obd_ioctl_data*)buf)->ioc_bulk);
2087 fprintf(stderr, "OBD_IOC_LLOG_CHECK failed: %s\n",
2092 int jt_llog_remove(int argc, char **argv)
2094 struct obd_ioctl_data data;
2097 if (argc != 3 && argc != 2)
2101 data.ioc_inllen1 = strlen(argv[1]) + 1;
2102 data.ioc_inlbuf1 = argv[1];
2104 data.ioc_inllen2 = strlen(argv[2]) + 1;
2105 data.ioc_inlbuf2 = argv[2];
2107 IOC_PACK(argv[0], data);
2109 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_REMOVE, buf);
2112 fprintf(stdout, "log %s are removed.\n", argv[2]);
2114 fprintf(stdout, "the log in catalog %s are removed. \n", argv[1]);
2116 fprintf(stderr, "OBD_IOC_LLOG_REMOVE failed: %s\n",
2122 /* attach a regular file to virtual block device.
2125 * 1: error, it always means the command run failed
2128 static int jt_blockdev_run_process(const char *file, char *argv[])
2134 if (pid == 0) { /* child process */
2135 /* don't print error messages */
2137 (void)execvp(file, argv);
2139 } else if (pid > 0) {
2142 rc = waitpid(pid, &status, 0);
2143 if (rc < 0 || !WIFEXITED(status))
2146 return WEXITSTATUS(status);
2152 static int jt_blockdev_find_module(const char *module)
2158 fp = fopen("/proc/modules", "r");
2162 while (fscanf(fp, "%s %*s %*s %*s %*s %*s", modname) == 1) {
2163 if (strcmp(module, modname) == 0) {
2173 static int jt_blockdev_probe_module(const char *module)
2179 if (jt_blockdev_find_module(module) == 1)
2182 /* run modprobe first */
2184 argv[c++] = "/sbin/modprobe";
2186 argv[c++] = (char *)module;
2188 rc = jt_blockdev_run_process("modprobe", argv);
2192 /* cannot find the module in default directory ... */
2193 sprintf(buf, "../llite/%s.ko", module);
2195 argv[c++] = "/sbin/insmod";
2198 rc = jt_blockdev_run_process("insmod", argv);
2202 int jt_blockdev_attach(int argc, char **argv)
2206 char *filename, *devname;
2212 if (jt_blockdev_probe_module("llite_lloop") < 0) {
2213 fprintf(stderr, "error: cannot find module llite_lloop.(k)o\n");
2220 fd = open(filename, O_RDWR);
2222 fprintf(stderr, "file %s can't be opened(%s)\n\n",
2223 filename, strerror(errno));
2227 rc = ioctl(fd, LL_IOC_LLOOP_ATTACH, &dev);
2230 fprintf(stderr, "attach error(%s)\n", strerror(rc));
2234 rc = stat(devname, &st);
2235 if (rc == 0 && (!S_ISBLK(st.st_mode) || st.st_rdev != dev)) {
2237 } else if (rc < 0) {
2238 if (errno == ENOENT &&
2239 !mknod(devname, S_IFBLK|S_IRUSR|S_IWUSR, dev))
2246 fprintf(stderr, "error: the file %s could be attached to block "
2247 "device %X but creating %s failed: %s\n"
2248 "now detaching the block device..",
2249 filename, (int)dev, devname, strerror(rc));
2251 (void)ioctl(fd, LL_IOC_LLOOP_DETACH_BYDEV, dev);
2252 fprintf(stderr, "%s\n", strerror(errno));
2259 int jt_blockdev_detach(int argc, char **argv)
2268 fd = open(filename, O_RDONLY);
2270 fprintf(stderr, "cannot open file %s error %s\n",
2271 filename, strerror(errno));
2275 rc = ioctl(fd, LL_IOC_LLOOP_DETACH, 0);
2278 fprintf(stderr, "detach error(%s)\n", strerror(rc));
2280 (void)unlink(filename);
2287 int jt_blockdev_info(int argc, char **argv)
2297 fd = open(filename, O_RDONLY);
2299 fprintf(stderr, "cannot open file %s error: %s\n",
2300 filename, strerror(errno));
2304 rc = ioctl(fd, LL_IOC_LLOOP_INFO, &ino);
2307 fprintf(stderr, "error: %s\n", strerror(errno));
2310 fprintf(stdout, "lloop device info: ");
2312 fprintf(stdout, "Not attached\n");
2314 fprintf(stdout, "attached to inode "LPU64"\n", ino);
2320 static void signal_server(int sig)
2322 if (sig == SIGINT) {
2323 do_disconnect("sigint", 1);
2326 fprintf(stderr, "%s: got signal %d\n", jt_cmdname("sigint"), sig);
2329 int obd_initialize(int argc, char **argv)
2333 for (i = 0; i < MAX_STRIPES; i++)
2334 lsm_buffer.lsm.lsm_oinfo[i] = lov_oinfos + i;
2337 register_ioc_dev(OBD_DEV_ID, OBD_DEV_PATH,
2338 OBD_DEV_MAJOR, OBD_DEV_MINOR);
2343 void obd_finalize(int argc, char **argv)
2345 struct sigaction sigact;
2347 sigact.sa_handler = signal_server;
2348 sigfillset(&sigact.sa_mask);
2349 sigact.sa_flags = SA_RESTART;
2350 sigaction(SIGINT, &sigact, NULL);
2353 do_disconnect(argv[0], 1);
2356 static int find_target_obdpath(char *fsname, char *path)
2359 char pattern[MAXPATHLEN + 1];
2362 snprintf(pattern, MAXPATHLEN,
2363 "/proc/fs/lustre/lov/%s-*/target_obd",
2365 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
2369 if (glob_info.gl_pathc == 0) {
2370 globfree(&glob_info);
2374 strcpy(path, glob_info.gl_pathv[0]);
2378 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
2381 char pattern[MAXPATHLEN + 1];
2384 snprintf(pattern, MAXPATHLEN,
2385 "/proc/fs/lustre/lov/%s-*/pools/%s",
2387 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
2391 if (glob_info.gl_pathc == 0) {
2392 globfree(&glob_info);
2396 strcpy(poolpath, glob_info.gl_pathv[0]);
2401 * if pool is NULL, search ostname in target_obd
2402 * if pool is no NULL
2403 * if pool not found returns < 0
2404 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
2405 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
2407 static int search_ost(char *fsname, char *poolname, char *ostname)
2410 char buffer[MAXPATHLEN + 1];
2413 if (ostname != NULL)
2414 len = strlen(ostname);
2416 if (poolname == NULL)
2417 rc = find_target_obdpath(fsname, buffer);
2419 rc = find_poolpath(fsname, poolname, buffer);
2423 if ((fd = fopen(buffer, "r")) == NULL)
2426 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
2427 if (poolname == NULL) {
2428 /* we search ostname in target_obd */
2429 if (strncmp(buffer + 3, ostname, len) == 0) {
2434 /* we search a non empty pool or
2435 an ostname in a pool */
2436 if ((ostname == NULL) ||
2437 (strncmp(buffer, ostname, len) == 0)) {
2447 static int check_pool_cmd(enum lcfg_command_type cmd,
2448 char *fsname, char *poolname,
2454 case LCFG_POOL_NEW: {
2455 if (search_ost(fsname, poolname, NULL) >= 0) {
2456 fprintf(stderr, "Pool %s.%s already exists\n",
2462 case LCFG_POOL_DEL: {
2463 rc = search_ost(fsname, poolname, NULL);
2465 fprintf(stderr, "Pool %s.%s not found\n",
2470 fprintf(stderr, "Pool %s.%s not empty, "
2471 "please remove all members\n",
2477 case LCFG_POOL_ADD: {
2478 rc = search_ost(fsname, NULL, ostname);
2480 fprintf(stderr, "OST %s not found in lov of %s\n",
2484 rc = search_ost(fsname, poolname, ostname);
2486 fprintf(stderr, "Pool %s.%s not found\n",
2491 fprintf(stderr, "OST %s already in pool %s.%s\n",
2492 ostname, fsname, poolname);
2497 case LCFG_POOL_REM: {
2498 rc = search_ost(fsname, poolname, ostname);
2500 fprintf(stderr, "Pool %s.%s not found\n",
2505 fprintf(stderr, "OST %s not found in pool %s.%s\n",
2506 ostname, fsname, poolname);
2517 static void check_pool_cmd_result(enum lcfg_command_type cmd,
2518 char *fsname, char *poolname,
2525 case LCFG_POOL_NEW: {
2527 rc = search_ost(fsname, poolname, NULL);
2531 } while ((rc < 0) && (cpt > 0));
2533 fprintf(stderr, "Pool %s.%s created\n",
2536 fprintf(stderr, "Warning, pool %s.%s not found\n",
2540 case LCFG_POOL_DEL: {
2542 rc = search_ost(fsname, poolname, NULL);
2546 } while ((rc >= 0) && (cpt > 0));
2548 fprintf(stderr, "Pool %s.%s destroyed\n",
2551 fprintf(stderr, "Warning, pool %s.%s still found\n",
2555 case LCFG_POOL_ADD: {
2557 rc = search_ost(fsname, poolname, ostname);
2561 } while ((rc != 1) && (cpt > 0));
2563 fprintf(stderr, "OST %s added to pool %s.%s\n",
2564 ostname, fsname, poolname);
2566 fprintf(stderr, "Warning, OST %s not found in pool %s.%s\n",
2567 ostname, fsname, poolname);
2570 case LCFG_POOL_REM: {
2572 rc = search_ost(fsname, poolname, ostname);
2576 } while ((rc == 1) && (cpt > 0));
2578 fprintf(stderr, "OST %s removed from pool %s.%s\n",
2579 ostname, fsname, poolname);
2581 fprintf(stderr, "Warning, OST %s still found in pool %s.%s\n",
2582 ostname, fsname, poolname);
2590 static int check_and_complete_ostname(char *fsname, char *ostname)
2593 char real_ostname[MAX_OBD_NAME + 1];
2596 /* if OST name does not start with fsname, we add it */
2597 /* if not check if the fsname is the right one */
2598 ptr = strchr(ostname, '-');
2600 sprintf(real_ostname, "%s-%s", fsname, ostname);
2601 } else if (strncmp(ostname, fsname, strlen(fsname)) != 0) {
2602 fprintf(stderr, "%s does not start with fsname %s\n",
2606 strcpy(real_ostname, ostname);
2608 /* real_ostname is fsname-????? */
2609 ptr = real_ostname + strlen(fsname) + 1;
2610 if (strncmp(ptr, "OST", 3) != 0) {
2611 fprintf(stderr, "%s does not start by %s-OST nor OST\n",
2615 /* real_ostname is fsname-OST????? */
2617 for (i = 0; i < 4; i++) {
2618 if (!isxdigit(*ptr)) {
2620 "ost's index in %s is not an hexa number\n",
2626 /* real_ostname is fsname-OSTXXXX????? */
2627 /* if OST name does not end with _UUID, we add it */
2629 strcat(real_ostname, "_UUID");
2630 } else if (strcmp(ptr, "_UUID") != 0) {
2632 "ostname %s does not end with _UUID\n", ostname);
2635 /* real_ostname is fsname-OSTXXXX_UUID */
2636 strcpy(ostname, real_ostname);
2640 /* returns 0 or -errno */
2641 static int pool_cmd(enum lcfg_command_type cmd,
2642 char *cmdname, char *fullpoolname,
2643 char *fsname, char *poolname, char *ostname)
2646 struct obd_ioctl_data data;
2647 struct lustre_cfg_bufs bufs;
2648 struct lustre_cfg *lcfg;
2650 rc = check_pool_cmd(cmd, fsname, poolname, ostname);
2654 lustre_cfg_bufs_reset(&bufs, NULL);
2655 lustre_cfg_bufs_set_string(&bufs, 0, cmdname);
2656 lustre_cfg_bufs_set_string(&bufs, 1, fullpoolname);
2657 if (ostname != NULL)
2658 lustre_cfg_bufs_set_string(&bufs, 2, ostname);
2660 lcfg = lustre_cfg_new(cmd, &bufs);
2667 rc = data.ioc_dev = get_mgs_device();
2671 data.ioc_type = LUSTRE_CFG_TYPE;
2672 data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
2673 lcfg->lcfg_buflens);
2674 data.ioc_pbuf1 = (void *)lcfg;
2675 IOC_PACK(cmdname, data);
2677 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_POOL, buf);
2681 lustre_cfg_free(lcfg);
2686 * this function tranforms a rule [start-end/step] into an array
2687 * of matching numbers
2688 * supported forms are:
2689 * [start] : just this number
2690 * [start-end] : all numbers from start to end
2691 * [start-end/step] : numbers from start to end with increment of step
2692 * on return, format contains a printf format string which can be used
2693 * to generate all the strings
2695 static int get_array_idx(char *rule, char *format, int **array)
2697 char *start, *end, *ptr;
2698 unsigned int lo, hi, step;
2703 start = strchr(rule, '[');
2704 end = strchr(rule, ']');
2705 if ((start == NULL) || (end == NULL)) {
2706 *array = malloc(sizeof(int));
2709 strcpy(format, rule);
2717 /* put in format the printf format (the rule without the range) */
2718 sprintf(format, "%s%%.4d%s", rule, end);
2723 /* loop on , separator */
2725 /* extract the 3 fields */
2726 rc = sscanf(start, "%u-%u/%u", &lo, &hi, &step);
2733 *array = realloc(*array, array_sz * sizeof(int));
2736 (*array)[array_idx] = lo;
2742 /* do not break to share code with case 3: */
2745 if ((hi < lo) || (step == 0))
2747 array_sz += (hi - lo) / step + 1;
2748 *array = realloc(*array, sizeof(int) * array_sz);
2751 for (i = lo; i <= hi; i+=step, array_idx++)
2752 (*array)[array_idx] = i;
2756 ptr = strchr(start, ',');
2760 } while (ptr != NULL);
2764 static int extract_fsname_poolname(char *arg, char *fsname, char *poolname)
2770 strcpy(fsname, arg);
2771 ptr = strchr(fsname, '.');
2773 fprintf(stderr, ". is missing in %s\n", fsname);
2780 fprintf(stderr, "fsname is empty\n");
2785 len = strlen(ptr + 1);
2787 fprintf(stderr, "poolname is empty\n");
2791 if (len > LOV_MAXPOOLNAME) {
2793 "poolname %s is too long (length is %d max is %d)\n",
2794 ptr + 1, len, LOV_MAXPOOLNAME);
2798 strncpy(poolname, ptr + 1, LOV_MAXPOOLNAME);
2799 poolname[LOV_MAXPOOLNAME] = '\0';
2804 fprintf(stderr, "argument %s must be <fsname>.<poolname>\n", arg);
2808 int jt_pool_cmd(int argc, char **argv)
2810 enum lcfg_command_type cmd;
2811 char fsname[MAXPATHLEN + 1];
2812 char poolname[LOV_MAXPOOLNAME + 1];
2813 char *ostnames_buf = NULL;
2815 int *array = NULL, array_sz;
2823 case 1: return CMD_HELP;
2825 if (strcmp("pool_new", argv[0]) == 0)
2826 cmd = LCFG_POOL_NEW;
2827 else if (strcmp("pool_destroy", argv[0]) == 0)
2828 cmd = LCFG_POOL_DEL;
2829 else if (strcmp("pool_list", argv[0]) == 0)
2830 return llapi_poollist(argv[1]);
2831 else return CMD_HELP;
2833 rc = extract_fsname_poolname(argv[1], fsname, poolname);
2837 rc = pool_cmd(cmd, argv[0], argv[1],
2838 fsname, poolname, NULL);
2842 check_pool_cmd_result(cmd, fsname, poolname, NULL);
2846 char format[2*MAX_OBD_NAME];
2848 if (strcmp("pool_remove", argv[0]) == 0) {
2849 cmd = LCFG_POOL_REM;
2850 } else if (strcmp("pool_add", argv[0]) == 0) {
2851 cmd = LCFG_POOL_ADD;
2856 rc = extract_fsname_poolname(argv[1], fsname, poolname);
2860 for (i = 2; i < argc; i++) {
2863 array_sz = get_array_idx(argv[i], format, &array);
2867 cmds = malloc(array_sz * sizeof(cmds[0]));
2869 ostnames_buf = malloc(array_sz *
2870 (MAX_OBD_NAME + 1));
2877 for (j = 0; j < array_sz; j++) {
2878 char ostname[MAX_OBD_NAME + 1];
2880 snprintf(ostname, MAX_OBD_NAME, format,
2882 ostname[MAX_OBD_NAME] = '\0';
2884 rc = check_and_complete_ostname(fsname,ostname);
2892 if (ostnames_buf != NULL) {
2894 &ostnames_buf[(MAX_OBD_NAME + 1) * j];
2895 strcpy(cmds[j].ostname, ostname);
2897 cmds[j].ostname = NULL;
2899 cmds[j].rc = pool_cmd(cmd, argv[0], argv[1],
2903 for (j = 0; j < array_sz; j++) {
2905 char ostname[MAX_OBD_NAME + 1];
2907 if (!cmds[j].ostname) {
2908 snprintf(ostname, MAX_OBD_NAME,
2910 ostname[MAX_OBD_NAME] = '\0';
2911 check_and_complete_ostname(
2917 check_pool_cmd_result(cmd, fsname,
2934 if ((rc == -EINVAL) || (rc == -ENOENT))
2935 fprintf(stderr, "Does the fs, pool or ost exist?\n");
2944 void llapi_ping_target(char *obd_type, char *obd_name,
2945 char *obd_uuid, void *args)
2948 struct obd_ioctl_data data;
2950 memset(&data, 0, sizeof(data));
2951 data.ioc_inlbuf4 = obd_name;
2952 data.ioc_inllen4 = strlen(obd_name) + 1;
2953 data.ioc_dev = OBD_DEV_BY_DEVNAME;
2954 memset(buf, 0, sizeof(rawbuf));
2955 if (obd_ioctl_pack(&data, &buf, max)) {
2956 fprintf(stderr, "error: invalid ioctl\n");
2959 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_PING_TARGET, buf);
2962 if (rc == ENOTCONN || rc == ESHUTDOWN) {
2963 printf("%s inactive.\n", obd_name);
2965 fprintf(stderr, "error: check '%s' %s\n",
2966 obd_name, strerror(errno));
2968 printf("%s active.\n", obd_name);