+static int lfs_pcc_attach(int argc, char **argv)
+{
+ struct option long_opts[] = {
+ { .val = 'i', .name = "id", .has_arg = required_argument },
+ { .name = NULL } };
+ int c;
+ int rc = 0;
+ __u32 archive_id = 0;
+ const char *path;
+ char *end;
+ char fullpath[PATH_MAX];
+ enum lu_pcc_type type = LU_PCC_READWRITE;
+
+ optind = 0;
+ while ((c = getopt_long(argc, argv, "i:",
+ long_opts, NULL)) != -1) {
+ switch (c) {
+ case 'i':
+ archive_id = strtoul(optarg, &end, 0);
+ if (*end != '\0' || archive_id == 0) {
+ fprintf(stderr, "error: %s: bad archive ID "
+ "'%s'\n", argv[0], optarg);
+ return CMD_HELP;
+ }
+ break;
+ case '?':
+ return CMD_HELP;
+ default:
+ fprintf(stderr, "%s: option '%s' unrecognized\n",
+ argv[0], argv[optind - 1]);
+ return CMD_HELP;
+ }
+ }
+
+ if (argc <= optind) {
+ fprintf(stderr, "%s: must specify one or more file names\n",
+ argv[0]);
+ return CMD_HELP;
+ }
+
+ while (optind < argc) {
+ int rc2;
+
+ path = argv[optind++];
+ if (realpath(path, fullpath) == NULL) {
+ fprintf(stderr, "%s: could not find path '%s': %s\n",
+ argv[0], path, strerror(errno));
+ if (rc == 0)
+ rc = -EINVAL;
+ continue;
+ }
+
+ rc2 = llapi_pcc_attach(fullpath, archive_id, type);
+ if (rc2 < 0) {
+ fprintf(stderr, "%s: cannot attach '%s' to PCC "
+ "with archive ID '%u': %s\n", argv[0],
+ path, archive_id, strerror(-rc2));
+ if (rc == 0)
+ rc = rc2;
+ }
+ }
+ return rc;
+}
+
+static int lfs_pcc_attach_fid(int argc, char **argv)
+{
+ struct option long_opts[] = {
+ { .val = 'i', .name = "id", .has_arg = required_argument },
+ { .val = 'm', .name = "mnt", .has_arg = required_argument },
+ { .name = NULL } };
+ char short_opts[] = "i:m:";
+ int c;
+ int rc = 0;
+ __u32 archive_id = 0;
+ char *end;
+ const char *mntpath = NULL;
+ const char *fidstr;
+ enum lu_pcc_type type = LU_PCC_READWRITE;
+
+ optind = 0;
+ while ((c = getopt_long(argc, argv, short_opts,
+ long_opts, NULL)) != -1) {
+ switch (c) {
+ case 'i':
+ archive_id = strtoul(optarg, &end, 0);
+ if (*end != '\0') {
+ fprintf(stderr, "error: %s: bad archive ID "
+ "'%s'\n", argv[0], optarg);
+ return CMD_HELP;
+ }
+ break;
+ case 'm':
+ mntpath = optarg;
+ break;
+ case '?':
+ return CMD_HELP;
+ default:
+ fprintf(stderr, "%s: option '%s' unrecognized\n",
+ argv[0], argv[optind - 1]);
+ return CMD_HELP;
+ }
+ }
+
+ if (archive_id == 0) {
+ fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
+ return CMD_HELP;
+ }
+
+ if (mntpath == NULL) {
+ fprintf(stderr, "%s: must specify Lustre mount point\n",
+ argv[0]);
+ return CMD_HELP;
+ }
+
+ if (argc <= optind) {
+ fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
+ return CMD_HELP;
+ }
+
+ while (optind < argc) {
+ int rc2;
+
+ fidstr = argv[optind++];
+
+ rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
+ archive_id, type);
+ if (rc2 < 0) {
+ fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
+ "with archive ID '%u': %s\n", argv[0],
+ fidstr, mntpath, archive_id, strerror(rc2));
+ }
+ if (rc == 0 && rc2 < 0)
+ rc = rc2;
+ }
+ return rc;
+}
+
+static int lfs_pcc_detach(int argc, char **argv)
+{
+ struct option long_opts[] = {
+ { .val = 'k', .name = "keep", .has_arg = no_argument },
+ { .name = NULL } };
+ char short_opts[] = "k";
+ int c;
+ int rc = 0;
+ const char *path;
+ char fullpath[PATH_MAX];
+ __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
+
+ optind = 0;
+ while ((c = getopt_long(argc, argv, short_opts,
+ long_opts, NULL)) != -1) {
+ switch (c) {
+ case 'k':
+ detach_opt = PCC_DETACH_OPT_NONE;
+ break;
+ case '?':
+ return CMD_HELP;
+ default:
+ fprintf(stderr, "%s: option '%s' unrecognized\n",
+ argv[0], argv[optind - 1]);
+ return CMD_HELP;
+ }
+ }
+
+ while (optind < argc) {
+ int rc2;
+
+ path = argv[optind++];
+ if (realpath(path, fullpath) == NULL) {
+ fprintf(stderr, "%s: could not find path '%s': %s\n",
+ argv[0], path, strerror(errno));
+ if (rc == 0)
+ rc = -EINVAL;
+ continue;
+ }
+
+ rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
+ if (rc2 < 0) {
+ rc2 = -errno;
+ fprintf(stderr, "%s: cannot detach '%s' from PCC: "
+ "%s\n", argv[0], path, strerror(errno));
+ if (rc == 0)
+ rc = rc2;
+ }
+ }
+ return rc;
+}
+
+static int lfs_pcc_detach_fid(int argc, char **argv)
+{
+ struct option long_opts[] = {
+ { .val = 'k', .name = "keep", .has_arg = no_argument },
+ { .name = NULL } };
+ char short_opts[] = "k";
+ int c;
+ int rc = 0;
+ const char *fid;
+ const char *mntpath;
+ __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
+
+ optind = 0;
+ while ((c = getopt_long(argc, argv, short_opts,
+ long_opts, NULL)) != -1) {
+ switch (c) {
+ case 'k':
+ detach_opt = PCC_DETACH_OPT_NONE;
+ break;
+ case '?':
+ return CMD_HELP;
+ default:
+ fprintf(stderr, "%s: option '%s' unrecognized\n",
+ argv[0], argv[optind - 1]);
+ return CMD_HELP;
+ }
+ }
+
+ mntpath = argv[optind++];
+
+ while (optind < argc) {
+ int rc2;
+
+ fid = argv[optind++];
+
+ rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
+ if (rc2 < 0) {
+ fprintf(stderr, "%s: cannot detach '%s' on '%s' "
+ "from PCC: %s\n", argv[0], fid, mntpath,
+ strerror(-rc2));
+ if (rc == 0)
+ rc = rc2;
+ }
+ }
+ return rc;
+}
+
+static int lfs_pcc_state(int argc, char **argv)
+{
+ int rc = 0;
+ const char *path;
+ char fullpath[PATH_MAX];
+ struct lu_pcc_state state;
+
+ optind = 1;
+
+ if (argc <= 1) {
+ fprintf(stderr, "%s: must specify one or more file names\n",
+ argv[0]);
+ return CMD_HELP;
+ }
+
+ while (optind < argc) {
+ int rc2;
+
+ path = argv[optind++];
+ if (realpath(path, fullpath) == NULL) {
+ fprintf(stderr, "%s: could not find path '%s': %s\n",
+ argv[0], path, strerror(errno));
+ if (rc == 0)
+ rc = -EINVAL;
+ continue;
+ }
+
+ rc2 = llapi_pcc_state_get(fullpath, &state);
+ if (rc2 < 0) {
+ if (rc == 0)
+ rc = rc2;
+ fprintf(stderr, "%s: cannot get PCC state of '%s': "
+ "%s\n", argv[0], path, strerror(-rc2));
+ continue;
+ }
+
+ printf("file: %s", path);
+ printf(", type: %s", pcc_type2string(state.pccs_type));
+ if (state.pccs_type == LU_PCC_NONE &&
+ state.pccs_open_count == 0) {
+ printf("\n");
+ continue;
+ }
+
+ printf(", PCC file: %s", state.pccs_path);
+ printf(", user number: %u", state.pccs_open_count);
+ printf(", flags: %x", state.pccs_flags);
+ printf("\n");
+ }
+ return rc;
+}
+
+/**
+ * lfs_pcc_list_commands() - List lfs pcc commands.
+ * @argc: The count of command line arguments.
+ * @argv: Array of strings for command line arguments.
+ *
+ * This function lists lfs pcc commands defined in pcc_cmdlist[].
+ *
+ * Return: 0 on success.
+ */
+static int lfs_pcc_list_commands(int argc, char **argv)
+{
+ char buffer[81] = "";
+
+ Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
+ NULL, 0, 4);
+
+ return 0;
+}
+
+/**
+ * lfs_pcc() - Parse and execute lfs pcc commands.
+ * @argc: The count of lfs pcc command line arguments.
+ * @argv: Array of strings for lfs pcc command line arguments.
+ *
+ * This function parses lfs pcc commands and performs the
+ * corresponding functions specified in pcc_cmdlist[].
+ *
+ * Return: 0 on success or an error code on failure.
+ */
+static int lfs_pcc(int argc, char **argv)
+{
+ char cmd[PATH_MAX];
+ int rc = 0;
+
+ setlinebuf(stdout);
+
+ Parser_init("lfs-pcc > ", pcc_cmdlist);
+
+ snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
+ progname = cmd;
+ program_invocation_short_name = cmd;
+ if (argc > 1)
+ rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
+ else
+ rc = Parser_commands();
+
+ return rc < 0 ? -rc : rc;
+}
+