+/* This check only verifies that the changes have been "pushed out" to
+ the client successfully. This involves waiting for a config update,
+ and so may fail because of problems in that code or post-command
+ network loss. So reporting a warning is appropriate, but not a failure.
+*/
+static int check_pool_cmd_result(enum lcfg_command_type cmd,
+ char *fsname, char *poolname,
+ char *ostname)
+{
+ int cpt = 10;
+ int rc = 0;
+
+ switch (cmd) {
+ case LCFG_POOL_NEW: {
+ do {
+ rc = lctl_search_ost(fsname, poolname, NULL, cmd);
+ if (rc == -ENODEV)
+ return rc;
+ if (rc < 0)
+ sleep(2);
+ cpt--;
+ } while ((rc < 0) && (cpt > 0));
+ if (rc >= 0) {
+ fprintf(stderr, "Pool %s.%s created\n",
+ fsname, poolname);
+ return 0;
+ } else {
+ fprintf(stderr, "Warning, pool %s.%s not found\n",
+ fsname, poolname);
+ return -ENOENT;
+ }
+ }
+ case LCFG_POOL_DEL: {
+ do {
+ rc = lctl_search_ost(fsname, poolname, NULL, cmd);
+ if (rc == -ENODEV)
+ return rc;
+ if (rc >= 0)
+ sleep(2);
+ cpt--;
+ } while ((rc >= 0) && (cpt > 0));
+ if (rc < 0) {
+ fprintf(stderr, "Pool %s.%s destroyed\n",
+ fsname, poolname);
+ return 0;
+ } else {
+ fprintf(stderr, "Warning, pool %s.%s still found\n",
+ fsname, poolname);
+ return -EEXIST;
+ }
+ }
+ case LCFG_POOL_ADD: {
+ do {
+ rc = lctl_search_ost(fsname, poolname, ostname, cmd);
+ if (rc == -ENODEV)
+ return rc;
+ if (rc != 1)
+ sleep(2);
+ cpt--;
+ } while ((rc != 1) && (cpt > 0));
+ if (rc == 1) {
+ fprintf(stderr, "OST %s added to pool %s.%s\n",
+ ostname, fsname, poolname);
+ return 0;
+ } else {
+ fprintf(stderr, "Warning, OST %s not found in pool %s.%s\n",
+ ostname, fsname, poolname);
+ return -ENOENT;
+ }
+ }
+ case LCFG_POOL_REM: {
+ do {
+ rc = lctl_search_ost(fsname, poolname, ostname, cmd);
+ if (rc == -ENODEV)
+ return rc;
+ if (rc == 1)
+ sleep(2);
+ cpt--;
+ } while ((rc == 1) && (cpt > 0));
+ if (rc != 1) {
+ fprintf(stderr, "OST %s removed from pool %s.%s\n",
+ ostname, fsname, poolname);
+ return 0;
+ } else {
+ fprintf(stderr, "Warning, OST %s still found in pool %s.%s\n",
+ ostname, fsname, poolname);
+ return -EEXIST;
+ }
+ }
+ default:
+ break;
+ }
+ return -EINVAL;
+}
+
+static int check_and_complete_ostname(char *fsname, char *ostname)
+{
+ char *ptr;
+ char real_ostname[MAX_OBD_NAME + 1];
+ char i;
+
+ /* if OST name does not start with fsname, we add it */
+ /* if not check if the fsname is the right one */
+ ptr = strchr(ostname, '-');
+ if (ptr == NULL) {
+ sprintf(real_ostname, "%s-%s", fsname, ostname);
+ } else if (strncmp(ostname, fsname, strlen(fsname)) != 0) {
+ fprintf(stderr, "%s does not start with fsname %s\n",
+ ostname, fsname);
+ return -EINVAL;
+ } else {
+ if (strlen(ostname) > sizeof(real_ostname)-1)
+ return -E2BIG;
+ strncpy(real_ostname, ostname, sizeof(real_ostname));
+ }
+ /* real_ostname is fsname-????? */
+ ptr = real_ostname + strlen(fsname) + 1;
+ if (strncmp(ptr, "OST", 3) != 0) {
+ fprintf(stderr, "%s does not start by %s-OST nor OST\n",
+ ostname, fsname);
+ return -EINVAL;
+ }
+ /* real_ostname is fsname-OST????? */
+ ptr += 3;
+ for (i = 0; i < 4; i++) {
+ if (!isxdigit(*ptr)) {
+ fprintf(stderr,
+ "ost's index in %s is not an hexa number\n",
+ ostname);
+ return -EINVAL;
+ }
+ ptr++;
+ }
+ /* real_ostname is fsname-OSTXXXX????? */
+ /* if OST name does not end with _UUID, we add it */
+ if (*ptr == '\0') {
+ strcat(real_ostname, "_UUID");
+ } else if (strcmp(ptr, "_UUID") != 0) {
+ fprintf(stderr,
+ "ostname %s does not end with _UUID\n", ostname);
+ return -EINVAL;
+ }
+ /* real_ostname is fsname-OSTXXXX_UUID */
+ strcpy(ostname, real_ostname);
+ return 0;
+}
+
+/* returns 0 or -errno */
+static int pool_cmd(enum lcfg_command_type cmd,
+ char *cmdname, char *fullpoolname,
+ char *fsname, char *poolname, char *ostname)
+{
+ int rc = 0;
+ struct obd_ioctl_data data;
+ struct lustre_cfg_bufs bufs;
+ struct lustre_cfg *lcfg;
+ char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
+
+ rc = check_pool_cmd(cmd, fsname, poolname, ostname);
+ if (rc == -ENODEV)
+ fprintf(stderr, "Can't verify pool command since there "
+ "is no local MDT or client, proceeding anyhow...\n");
+ else if (rc)
+ return rc;
+
+ lustre_cfg_bufs_reset(&bufs, NULL);
+ lustre_cfg_bufs_set_string(&bufs, 0, cmdname);
+ lustre_cfg_bufs_set_string(&bufs, 1, fullpoolname);
+ if (ostname != NULL)
+ lustre_cfg_bufs_set_string(&bufs, 2, ostname);
+
+
+ lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
+ if (lcfg == NULL)
+ return -ENOMEM;
+ lustre_cfg_init(lcfg, cmd, &bufs);
+
+ memset(&data, 0, sizeof(data));
+ rc = data.ioc_dev = get_mgs_device();
+ if (rc < 0)
+ goto out;
+
+ data.ioc_type = LUSTRE_CFG_TYPE;
+ data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
+ lcfg->lcfg_buflens);
+ data.ioc_pbuf1 = (void *)lcfg;
+
+ memset(buf, 0, sizeof(rawbuf));
+ rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
+ if (rc) {
+ fprintf(stderr, "error: %s: invalid ioctl\n",
+ jt_cmdname(cmdname));
+ free(lcfg);
+ return rc;
+ }
+ rc = l_ioctl(OBD_DEV_ID, OBD_IOC_POOL, buf);
+out:
+ if (rc)
+ rc = -errno;
+ switch (rc) {
+ case -ENAMETOOLONG:
+ fprintf(stderr, "error: %s: either the pool or file "
+ "system name is too long (max pool name len "
+ "is %d and file system name is %d)\n",
+ jt_cmdname(cmdname), LOV_MAXPOOLNAME,
+ LUSTRE_MAXFSNAME);
+ break;
+ case -EINVAL:
+ fprintf(stderr, "error: %s can contain only "
+ "alphanumeric characters, underscores, and "
+ "dashes besides the required '.'\n",
+ jt_cmdname(cmdname));
+ default:
+ break;
+ }
+ free(lcfg);
+ return rc;
+}
+
+/**
+ * Format and send the ioctl to the MGS.
+ *
+ * \param cmd IOCTL to send
+ * \param ret_data void pointer to return anything from
+ * ioctl
+ * \param num_args number of arguments to pack into the
+ * ioctl buffer
+ * \param argv[] variable number of string arguments
+ *
+ * \retval 0 on success
+ */
+static int nodemap_cmd(enum lcfg_command_type cmd, void *ret_data,
+ unsigned int ret_size, ...)
+{
+ va_list ap;
+ char *arg;
+ int i = 0;
+ struct lustre_cfg_bufs bufs;
+ struct obd_ioctl_data data;
+ struct lustre_cfg *lcfg;
+ char rawbuf[MAX_IOC_BUFLEN];
+ char *buf = rawbuf;
+ int rc = 0;
+
+ lustre_cfg_bufs_reset(&bufs, NULL);
+
+ va_start(ap, ret_size);
+ arg = va_arg(ap, char *);
+ while (arg != NULL) {
+ lustre_cfg_bufs_set_string(&bufs, i, arg);
+ i++;
+ arg = va_arg(ap, char *);
+ }
+ va_end(ap);
+
+ lcfg = malloc(lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
+ if (lcfg == NULL)
+ return -ENOMEM;
+ lustre_cfg_init(lcfg, cmd, &bufs);
+
+ memset(&data, 0, sizeof(data));
+ rc = data.ioc_dev = get_mgs_device();
+ if (rc < 0)
+ goto out;
+
+ data.ioc_type = LUSTRE_CFG_TYPE;
+ data.ioc_plen1 = lustre_cfg_len(lcfg->lcfg_bufcount,
+ lcfg->lcfg_buflens);
+ data.ioc_pbuf1 = (void *)lcfg;
+
+ memset(buf, 0, sizeof(rawbuf));
+ rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
+ if (rc != 0) {
+ fprintf(stderr, "error: invalid ioctl: %08x errno: %d with "
+ "rc=%d\n", cmd, errno, rc);
+ goto out;
+ }
+
+ rc = l_ioctl(OBD_DEV_ID, OBD_IOC_NODEMAP, buf);
+ if (rc != 0) {
+ fprintf(stderr, "error: invalid ioctl: %08x errno: %d with "
+ "rc=%d\n", cmd, errno, rc);
+ goto out;
+ }
+
+ if (ret_data != NULL) {
+ rc = llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
+ if (rc != 0)
+ goto out;
+
+ if (ret_size > data.ioc_plen1)
+ ret_size = data.ioc_plen1;
+
+ memcpy(ret_data, data.ioc_pbuf1, ret_size);
+ }
+out:
+ free(lcfg);
+
+ return rc;
+}
+
+/**
+ * activate nodemap functions
+ *
+ * \param argc number of args
+ * \param argv[] variable string arguments
+ *
+ * argv[0] 1 for activate or 0 for deactivate
+ *
+ * \retval 0 on success
+ */
+int jt_nodemap_activate(int argc, char **argv)
+{
+ int rc;
+
+ rc = nodemap_cmd(LCFG_NODEMAP_ACTIVATE, NULL, 0, argv[0], argv[1],
+ NULL);
+
+ if (rc != 0) {
+ errno = -rc;
+ perror(argv[0]);
+ }
+
+ return rc;
+}
+
+/**
+ * add a nodemap
+ *
+ * \param argc number of args
+ * \param argv[] variable string arguments
+ *
+ * argv[0] nodemap name
+ *
+ * \retval 0 on success
+ */
+int jt_nodemap_add(int argc, char **argv)
+{
+ int rc;
+
+ rc = llapi_nodemap_exists(argv[1]);
+ if (rc == 0) {
+ fprintf(stderr, "error: %s existing nodemap name\n", argv[1]);
+ return 1;
+ }
+
+ rc = nodemap_cmd(LCFG_NODEMAP_ADD, NULL, 0, argv[0], argv[1], NULL);
+
+ if (rc != 0) {
+ errno = -rc;
+ perror(argv[0]);
+ }
+
+ return rc;
+}
+
+/**
+ * delete a nodemap
+ *
+ * \param argc number of args
+ * \param argv[] variable string arguments
+ *
+ * argv[0] nodemap name
+ *
+ * \retval 0 on success
+ */
+int jt_nodemap_del(int argc, char **argv)
+{
+ int rc;
+
+ rc = llapi_nodemap_exists(argv[1]);
+ if (rc != 0) {
+ fprintf(stderr, "error: %s not existing nodemap name\n",
+ argv[1]);
+ return rc;
+ }
+ rc = nodemap_cmd(LCFG_NODEMAP_DEL, NULL, 0, argv[0], argv[1], NULL);
+
+ if (rc != 0) {
+ errno = -rc;
+ perror(argv[0]);
+ }
+
+ return rc;
+}
+
+/**
+ * test a nid for nodemap membership
+ *
+ * \param argc number of args
+ * \param argv[] variable string arguments
+ *
+ * argv[0] properly formatted nid
+ *
+ * \retval 0 on success
+ */
+int jt_nodemap_test_nid(int argc, char **argv)
+{
+
+ char rawbuf[MAX_IOC_BUFLEN];
+ int rc;
+
+ rc = nodemap_cmd(LCFG_NODEMAP_TEST_NID, &rawbuf, sizeof(rawbuf),
+ argv[0], argv[1], NULL);
+ if (rc == 0)
+ printf("%s\n", (char *)rawbuf);
+
+ return rc;
+}
+
+/**
+ * test a nodemap id pair for mapping
+ *
+ * \param argc number of args
+ * \param argv[[] variable string arguments
+ *
+ * \retval 0 on success
+ *
+ * The argv array should contain the nodemap name, the id
+ * to checking the mapping on, and the id type (UID or GID)
+ *
+ */
+int jt_nodemap_test_id(int argc, char **argv)
+{
+ char rawbuf[MAX_IOC_BUFLEN];
+ char *nidstr = NULL;
+ char *idstr = NULL;
+ char *typestr = NULL;
+ int rc = 0;
+ int c;
+
+ static struct option long_opts[] = {
+ { .val = 'i', .name = "id", .has_arg = required_argument },
+ { .val = 'n', .name = "nid", .has_arg = required_argument },
+ { .val = 't', .name = "idtype",
+ .has_arg = required_argument },
+ { .name = NULL } };
+
+ while ((c = getopt_long(argc, argv, "n:t:i:",
+ long_opts, NULL)) != -1) {
+ switch (c) {
+ case 'n':
+ nidstr = optarg;
+ break;
+ case 't':
+ typestr = optarg;
+ break;
+ case 'i':
+ idstr = optarg;
+ break;
+ }
+ }
+
+ if (nidstr == NULL || typestr == NULL || idstr == NULL) {
+ fprintf(stderr, "usage: nodemap_test_id --nid <nid> "
+ "--idtype [uid|gid] --id <id>\n");
+ return -1;
+ }
+
+ rc = nodemap_cmd(LCFG_NODEMAP_TEST_ID, &rawbuf, sizeof(rawbuf),
+ argv[0], nidstr, typestr, idstr);
+ if (rc == 0)
+ printf("%s\n", (char *)rawbuf);
+
+ return rc;
+}
+
+/**
+ * parse nid range
+ *
+ * \param nodemap_range --range string
+ * \param nid_range nid range string, min_nid:max_nid
+ *
+ * \retval 0 on success
+ */
+static int parse_nid_range(char *nodemap_range, char *nid_range, int range_len)
+{
+ char min_nid[LNET_NIDSTR_SIZE + 1];
+ char max_nid[LNET_NIDSTR_SIZE + 1];
+ struct list_head nidlist;
+ int rc = 0;
+
+ INIT_LIST_HEAD(&nidlist);
+
+ if (cfs_parse_nidlist(nodemap_range, strlen(nodemap_range),
+ &nidlist) <= 0) {
+ fprintf(stderr,
+ "error: nodemap_xxx_range: can't parse nid range: %s\n",
+ nodemap_range);
+ return -1;
+ }
+
+ rc = cfs_nidrange_find_min_max(&nidlist, &min_nid[0], &max_nid[0],
+ LNET_NIDSTR_SIZE);
+ if (rc < 0) {
+ if (rc == -EINVAL)
+ fprintf(stderr,
+ "error: nodemap_xxx_range: nid range uses "
+ "currently unsupported features\n");
+ else if (rc == -ERANGE)
+ fprintf(stderr,
+ "error: nodemap_xxx_range: nodemap ranges must "
+ "be contiguous\n");
+
+ return rc;
+ }
+
+ snprintf(nid_range, range_len, "%s:%s", min_nid, max_nid);
+
+ return rc;
+}
+
+/**
+ * add an nid range to a nodemap
+ *
+ * \param argc number of args
+ * \param argv[] variable string arguments
+ *
+ * --name nodemap name
+ * --range properly formatted nid range
+ *
+ * \retval 0 on success
+ */
+int jt_nodemap_add_range(int argc, char **argv)
+{
+ char *nodemap_name = NULL;
+ char *nodemap_range = NULL;
+ char nid_range[2 * LNET_NIDSTR_SIZE + 2];
+ int rc = 0;
+ int c;
+
+ static struct option long_opts[] = {
+ { .val = 'n', .name = "name", .has_arg = required_argument },
+ { .val = 'r', .name = "range", .has_arg = required_argument },
+ { .name = NULL } };
+
+ while ((c = getopt_long(argc, argv, "n:r:",
+ long_opts, NULL)) != -1) {
+ switch (c) {
+ case 'n':
+ nodemap_name = optarg;
+ break;
+ case 'r':
+ nodemap_range = optarg;
+ break;
+ }
+ }
+
+ if (nodemap_name == NULL || nodemap_range == NULL) {
+ fprintf(stderr, "usage: nodemap_add_range --name <name> "
+ "--range <range>\n");
+ return -1;
+ }
+
+ rc = parse_nid_range(nodemap_range, nid_range, sizeof(nid_range));
+ if (rc) {
+ errno = -rc;
+ return rc;
+ }
+ rc = nodemap_cmd(LCFG_NODEMAP_ADD_RANGE, NULL, 0, argv[0],
+ nodemap_name, nid_range, NULL);
+ if (rc != 0) {
+ errno = -rc;
+ fprintf(stderr, "error: %s: cannot add range '%s' to nodemap "
+ "'%s': rc = %d\n",
+ jt_cmdname(argv[0]), nodemap_range, nodemap_name, rc);
+ }
+
+ return rc;
+}
+
+/**
+ * delete an nid range to a nodemap
+ *
+ * \param argc number of args
+ * \param argv[] variable string arguments
+ *
+ * --name nodemap name
+ * --range properly formatted nid range
+ *
+ * \retval 0 on success
+ */
+int jt_nodemap_del_range(int argc, char **argv)
+{
+ char *nodemap_name = NULL;
+ char *nodemap_range = NULL;
+ char nid_range[2 * LNET_NIDSTR_SIZE + 2];
+ int rc = 0;
+ int c;
+
+ static struct option long_opts[] = {
+ { .val = 'n', .name = "name", .has_arg = required_argument },
+ { .val = 'r', .name = "range", .has_arg = required_argument },
+ { .name = NULL } };
+
+ while ((c = getopt_long(argc, argv, "n:r:",
+ long_opts, NULL)) != -1) {
+ switch (c) {
+ case 'n':
+ nodemap_name = optarg;
+ break;
+ case 'r':
+ nodemap_range = optarg;
+ break;
+ }
+ }
+
+ if (nodemap_name == NULL || nodemap_range == NULL) {
+ fprintf(stderr, "usage: nodemap_del_range --name <name> "
+ "--range <range>\n");
+ return -1;
+ }
+
+ rc = parse_nid_range(nodemap_range, nid_range, sizeof(nid_range));
+ if (rc) {
+ errno = -rc;
+ return rc;
+ }
+ rc = nodemap_cmd(LCFG_NODEMAP_DEL_RANGE, NULL, 0, argv[0],
+ nodemap_name, nid_range, NULL);
+ if (rc != 0) {
+ errno = -rc;
+ fprintf(stderr, "error: %s: cannot delete range '%s' to "
+ "nodemap '%s': rc = %d\n",
+ jt_cmdname(argv[0]), nodemap_range, nodemap_name, rc);
+ }
+
+ return rc;
+}
+
+/**
+ * set a fileset on a nodemap
+ *
+ * \param argc number of args
+ * \param argv[] variable string arguments
+ *
+ * --name nodemap name
+ * --fileset fileset name
+ *
+ * \retval 0 on success
+ */
+int jt_nodemap_set_fileset(int argc, char **argv)