+struct jt_fid_space {
+ obd_seq jt_seq;
+ obd_id jt_id;
+ int jt_width;
+};
+
+int jt_obd_alloc_fids(struct jt_fid_space *space, struct lu_fid *fid,
+ __u64 *count)
+{
+ int rc;
+
+ if (space->jt_seq == 0 || space->jt_id == space->jt_width) {
+ struct obd_ioctl_data data;
+ char rawbuf[MAX_IOC_BUFLEN];
+ char *buf = rawbuf;
+ __u64 seqnr;
+ int max_count;
+
+ memset(&data, 0, sizeof(data));
+ data.ioc_dev = cur_device;
+
+ data.ioc_pbuf1 = (char *)&seqnr;
+ data.ioc_plen1 = sizeof(seqnr);
+
+ data.ioc_pbuf2 = (char *)&max_count;
+ data.ioc_plen2 = sizeof(max_count);
+
+ memset(buf, 0, sizeof(rawbuf));
+ rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
+ if (rc) {
+ fprintf(stderr, "error: invalid ioctl rc = %d\n", rc);
+ return rc;
+ }
+
+ rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_ECHO_ALLOC_SEQ, buf);
+ if (rc) {
+ fprintf(stderr, "ioctl error: rc = %d\n", rc);
+ return rc;
+ }
+
+ space->jt_seq = *(__u64 *)data.ioc_pbuf1;
+ space->jt_width = *(int *)data.ioc_pbuf2;
+ space->jt_id = 1;
+ }
+ fid->f_seq = space->jt_seq;
+ fid->f_oid = space->jt_id;
+ fid->f_ver = 0;
+
+ space->jt_id = min(space->jt_id + *count, space->jt_width);
+
+ *count = space->jt_id - fid->f_oid;
+ return 0;
+}
+
+#define MD_STEP_COUNT 1000
+int jt_obd_md_common(int argc, char **argv, int cmd)
+{
+ struct obd_ioctl_data data;
+ struct timeval start;
+ struct timeval end_time;
+ char rawbuf[MAX_IOC_BUFLEN];
+ char *buf = rawbuf;
+ int mode = 0000644;
+ int create_mode;
+ int rc = 0;
+ char *parent_basedir = NULL;
+ char dirname[4096];
+ int parent_base_id = 0;
+ int parent_count = 1;
+ __u64 child_base_id = -1;
+ int stripe_count = 0;
+ int stripe_index = -1;
+ int count = 0;
+ char *end;
+ __u64 seconds = 0;
+ double diff;
+ int c;
+ int xattr_size = 0;
+ __u64 total_count = 0;
+ char *name = NULL;
+ struct jt_fid_space fid_space = {0};
+ int version = 0;
+ struct option long_opts[] = {
+ {"child_base_id", required_argument, 0, 'b'},
+ {"stripe_count", required_argument, 0, 'c'},
+ {"parent_basedir", required_argument, 0, 'd'},
+ {"parent_dircount", required_argument, 0, 'D'},
+ {"stripe_index", required_argument, 0, 'i'},
+ {"mode", required_argument, 0, 'm'},
+ {"count", required_argument, 0, 'n'},
+ {"time", required_argument, 0, 't'},
+ {"version", no_argument, 0, 'v'},
+ {"xattr_size", required_argument, 0, 'x'},
+ {0, 0, 0, 0}
+ };
+
+ optind = 0;
+ while ((c = getopt_long(argc, argv, "b:c:d:D:m:n:t:vx:",
+ long_opts, NULL)) >= 0) {
+ switch (c) {
+ case 'b':
+ child_base_id = strtoull(optarg, &end, 0);
+ if (*end) {
+ fprintf(stderr, "error: %s: bad child_base_id"
+ " '%s'\n", jt_cmdname(argv[0]), optarg);
+ return CMD_HELP;
+ }
+ break;
+ case 'c':
+ stripe_count = strtoul(optarg, &end, 0);
+ if (*end) {
+ fprintf(stderr, "error: %s: bad stripe count"
+ " '%s'\n", jt_cmdname(argv[0]), optarg);
+ return CMD_HELP;
+ }
+ break;
+ case 'd':
+ parent_basedir = optarg;
+ break;
+ case 'D':
+ parent_count = strtoul(optarg, &end, 0);
+ if (*end) {
+ fprintf(stderr, "error: %s: bad parent count"
+ " '%s'\n", jt_cmdname(argv[0]), optarg);
+ return CMD_HELP;
+ }
+ break;
+ case 'i':
+ stripe_index = strtoul(optarg, &end, 0);
+ if (*end) {
+ fprintf(stderr, "error: %s: bad stripe index"
+ " '%s'\n", jt_cmdname(argv[0]), optarg);
+ return CMD_HELP;
+ }
+ break;
+ case 'm':
+ mode = strtoul(optarg, &end, 0);
+ if (*end) {
+ fprintf(stderr, "error: %s: bad mode '%s'\n",
+ jt_cmdname(argv[0]), optarg);
+ return CMD_HELP;
+ }
+ break;
+ case 'n':
+ total_count = strtoul(optarg, &end, 0);
+ if (*end || total_count == 0) {
+ fprintf(stderr, "%s: bad child count '%s'\n",
+ jt_cmdname(argv[0]), optarg);
+ return CMD_HELP;
+ }
+ break;
+ case 't':
+ seconds = strtoull(optarg, &end, 0);
+ if (*end) {
+ fprintf(stderr, "error: %s: seconds '%s'\n",
+ jt_cmdname(argv[0]), optarg);
+ return CMD_HELP;
+ }
+ break;
+ case 'v':
+ version = 1;
+ break;
+ case 'x':
+ xattr_size = strtoul(optarg, &end, 0);
+ if (*end) {
+ fprintf(stderr, "error: %s: xattr_size '%s'\n",
+ jt_cmdname(argv[0]), optarg);
+ return CMD_HELP;
+ }
+ SET_BUT_UNUSED(xattr_size);
+ break;
+ default:
+ fprintf(stderr, "error: %s: option '%s' "
+ "unrecognized\n", argv[0], argv[optind - 1]);
+ return CMD_HELP;
+ }
+ }
+
+ memset(&data, 0, sizeof(data));
+ data.ioc_dev = cur_device;
+ if (child_base_id == -1) {
+ if (optind >= argc)
+ return CMD_HELP;
+ name = argv[optind];
+ total_count = 1;
+ } else {
+ if (optind < argc) {
+ fprintf(stderr, "child_base_id and name can not"
+ " specified at the same time\n");
+ return CMD_HELP;
+ }
+ }
+
+ if (stripe_count == 0 && stripe_index != -1) {
+ fprintf(stderr, "If stripe_count is 0, stripe_index can not"
+ "be specified\n");
+ return CMD_HELP;
+ }
+
+ if (total_count == 0 && seconds == 0) {
+ fprintf(stderr, "count or seconds needs to be indicated\n");
+ return CMD_HELP;
+ }
+
+ if (parent_count <= 0) {
+ fprintf(stderr, "parent count must < 0\n");
+ return CMD_HELP;
+ }
+
+#ifdef MAX_THREADS
+ if (thread) {
+ shmem_lock();
+ /* threads interleave */
+ if (parent_base_id != -1)
+ parent_base_id += (thread - 1) % parent_count;
+
+ if (child_base_id != -1)
+ child_base_id += (thread - 1) * \
+ (MAX_BASE_ID / nthreads);
+
+ shmem_start_time_locked();
+ shmem_unlock();
+ }
+#endif
+ /* If parent directory is not specified, try to get the directory
+ * from name */
+ if (parent_basedir == NULL) {
+ char *last_lash;
+ if (name == NULL) {
+ fprintf(stderr, "parent_basedir or name must be"
+ "indicated!\n");
+ return CMD_HELP;
+ }
+ /*Get directory and name from name*/
+ last_lash = strrchr(name, '/');
+ if (last_lash == NULL || name[0] != '/') {
+ fprintf(stderr, "Can not locate %s\n", name);
+ return CMD_HELP;
+ }
+
+ if (last_lash == name) {
+ sprintf(dirname, "%s", "/");
+ name++;
+ } else {
+ int namelen = (unsigned long)last_lash -
+ (unsigned long)name;
+ snprintf(dirname, namelen, "%s", name);
+ }
+
+ data.ioc_pbuf1 = dirname;
+ data.ioc_plen1 = strlen(dirname);
+
+ data.ioc_pbuf2 = name;
+ data.ioc_plen2 = strlen(name);
+ } else {
+ if (name != NULL) {
+ data.ioc_pbuf2 = name;
+ data.ioc_plen2 = strlen(name);
+ } else {
+ if (parent_base_id > 0)
+ sprintf(dirname, "%s%d", parent_basedir,
+ parent_base_id);
+ else
+ sprintf(dirname, "%s", parent_basedir);
+ }
+ data.ioc_pbuf1 = dirname;
+ data.ioc_plen1 = strlen(dirname);
+ }
+
+ if (cmd == ECHO_MD_MKDIR || cmd == ECHO_MD_RMDIR)
+ create_mode = S_IFDIR;
+ else
+ create_mode = S_IFREG;
+
+ data.ioc_obdo1.o_mode = mode | S_IFDIR;
+ data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
+ OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
+ data.ioc_command = cmd;
+
+ gettimeofday(&start, NULL);
+ while (shmem_running()) {
+ struct lu_fid fid = { 0 };
+
+ ostid_set_id(&data.ioc_obdo2.o_oi, child_base_id);
+ data.ioc_obdo2.o_mode = mode | create_mode;
+ data.ioc_obdo2.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
+ OBD_MD_FLMODE | OBD_MD_FLFLAGS |
+ OBD_MD_FLGROUP;
+ data.ioc_obdo2.o_misc = stripe_count;
+ data.ioc_obdo2.o_stripe_idx = stripe_index;
+
+ if (total_count > 0) {
+ if ((total_count - count) > MD_STEP_COUNT)
+ data.ioc_count = MD_STEP_COUNT;
+ else
+ data.ioc_count = total_count - count;
+ } else {
+ data.ioc_count = MD_STEP_COUNT;
+ }
+
+ if (cmd == ECHO_MD_CREATE || cmd == ECHO_MD_MKDIR) {
+ /*Allocate fids for the create */
+ rc = jt_obd_alloc_fids(&fid_space, &fid,
+ &data.ioc_count);
+ if (rc) {
+ fprintf(stderr, "Allocate fids error %d.\n",rc);
+ return rc;
+ }
+ fid_to_ostid(&fid, &data.ioc_obdo1.o_oi);
+ }
+
+ child_base_id += data.ioc_count;
+ count += data.ioc_count;
+
+ memset(buf, 0, sizeof(rawbuf));
+ rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
+ if (rc) {
+ fprintf(stderr, "error: %s: invalid ioctl %d\n",
+ jt_cmdname(argv[0]), rc);
+ return rc;
+ }
+
+ rc = l2_ioctl(OBD_DEV_ID, OBD_IOC_ECHO_MD, buf);
+ if (rc) {
+ fprintf(stderr, "error: %s: %s\n",
+ jt_cmdname(argv[0]), strerror(rc = errno));
+ return rc;
+ }
+ shmem_bump(data.ioc_count);
+
+ gettimeofday(&end_time, NULL);
+ diff = difftime(&end_time, &start);
+ if (seconds > 0 && (__u64)diff > seconds)
+ break;
+
+ if (count >= total_count && total_count > 0)
+ break;
+ }
+
+ if (count > 0 && version) {
+ gettimeofday(&end_time, NULL);
+ diff = difftime(&end_time, &start);
+ printf("%s: %d in %.3fs (%.3f /s): %s",
+ jt_cmdname(argv[0]), count, diff,
+ (double)count/diff, ctime(&end_time.tv_sec));
+ }
+
+#ifdef MAX_THREADS
+ if (thread) {
+ shmem_lock();
+ shmem_end_time_locked();
+ shmem_unlock();
+ }
+#endif
+ return rc;
+}
+
+int jt_obd_test_create(int argc, char **argv)
+{
+ return jt_obd_md_common(argc, argv, ECHO_MD_CREATE);
+}
+
+int jt_obd_test_mkdir(int argc, char **argv)
+{
+ return jt_obd_md_common(argc, argv, ECHO_MD_MKDIR);
+}