+int jt_llog_catlist(int argc, char **argv)
+{
+ struct obd_ioctl_data data;
+ char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
+ char *tmp = NULL;
+ int start = 0;
+ int rc;
+
+ if (argc != 1)
+ return CMD_HELP;
+
+ if (llog_default_device(LLOG_DFLT_MGS_SET))
+ return CMD_INCOMPLETE;
+
+ do {
+ memset(&data, 0, sizeof(data));
+ data.ioc_dev = cur_device;
+ data.ioc_inllen1 = sizeof(rawbuf) -
+ __ALIGN_KERNEL(sizeof(data), 8);
+ data.ioc_count = start;
+ memset(buf, 0, sizeof(rawbuf));
+ rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
+ if (rc) {
+ fprintf(stderr, "error: %s: invalid ioctl\n",
+ jt_cmdname(argv[0]));
+ goto err;
+ }
+ rc = l_ioctl(OBD_DEV_ID, OBD_IOC_CATLOGLIST, buf);
+ if (rc < 0)
+ break;
+ tmp = ((struct obd_ioctl_data *)buf)->ioc_bulk;
+ if (strlen(tmp) > 0)
+ fprintf(stdout, "%s", tmp);
+ else
+ break;
+ start = ((struct obd_ioctl_data *)buf)->ioc_count;
+ } while (start);
+
+ if (rc < 0)
+ fprintf(stderr, "OBD_IOC_CATLOGLIST failed: %s\n",
+ strerror(errno));
+
+err:
+ llog_default_device(LLOG_DFLT_DEV_RESET);
+
+ return rc;
+}
+
+int jt_llog_info(int argc, char **argv)
+{
+ const struct option long_opts[] = {
+ /* Allow optional "--catalog" for compatibility with llog commands. */
+ { .val = 'c', .name = "catalog", .has_arg = required_argument },
+ { .val = 'h', .name = "help", .has_arg = no_argument },
+ { .name = NULL } };
+ struct obd_ioctl_data data = { 0 };
+ char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
+ char *cmd = argv[0];
+ char *catalog = NULL;
+ int rc, c;
+
+ while ((c = getopt_long(argc, argv, "c:h", long_opts, NULL)) != -1) {
+ switch (c) {
+ case 'c':
+ catalog = optarg;
+ break;
+ case 'h':
+ default:
+ return CMD_HELP;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+ /* support "logname" positional parameter */
+ if (argc == 1) {
+ if (catalog) {
+ fprintf(stderr,
+ "%s: catalog is set, unknown argument '%s'\n",
+ cmd, optarg);
+ return CMD_HELP;
+ }
+ catalog = argv[0];
+ } else if (!catalog || argc > 1) {
+ return CMD_HELP;
+ }
+
+ /* Manage default device */
+ if (llog_default_device(LLOG_DFLT_MGS_SET))
+ return CMD_INCOMPLETE;
+
+ data.ioc_dev = cur_device;
+ data.ioc_inllen1 = strlen(catalog) + 1;
+ data.ioc_inlbuf1 = catalog;
+ data.ioc_inllen2 = sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(data), 8) -
+ __ALIGN_KERNEL(data.ioc_inllen1, 8);
+ rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
+ if (rc) {
+ fprintf(stderr, "%s: ioctl_pack failed for catalog '%s': %s\n",
+ jt_cmdname(cmd), catalog, strerror(-rc));
+ goto err;
+ }
+
+ rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_INFO, buf);
+ if (rc == 0)
+ fprintf(stdout, "%s", ((struct obd_ioctl_data *)buf)->ioc_bulk);
+ else
+ fprintf(stderr, "%s: OBD_IOC_LLOG_INFO failed: %s\n",
+ jt_cmdname(cmd), strerror(errno));
+
+err:
+ llog_default_device(LLOG_DFLT_DEV_RESET);
+
+ return rc;
+}
+
+int jt_llog_print_cb(const char *record, void *private)
+{
+ printf("%s\n", record);
+
+ return 0;
+}
+
+static int
+llog_process_records(int (record_cb)(const char *record, void *private),
+ const char *record, void *private, bool reverse)
+{
+ char *ptr = NULL;
+ char *tmp = NULL;
+ int rc = 0;
+
+ if (!reverse) {
+ do {
+ ptr = strchr(record, '\n');
+ if (ptr)
+ *ptr = '\0';
+ rc = record_cb(record, private);
+ if (rc)
+ goto out;
+ if (ptr)
+ record = ptr + 1;
+ } while (ptr && *(ptr + 1));
+ } else {
+ tmp = (char *)record;
+
+ ptr = strrchr(record, '\n');
+ if (ptr)
+ *ptr = '\0';
+ else
+ goto out;
+ while ((ptr = strrchr(record, '\n'))) {
+ tmp = ptr + 1;
+ *ptr = '\0';
+ rc = record_cb(tmp, private);
+ if (rc)
+ goto out;
+ };
+ rc = record_cb(record, private);
+ if (rc)
+ goto out;
+ }
+out:
+ return rc;
+}
+
+/**
+ * Iterate over llog records, typically YAML-formatted configuration logs
+ *
+ * \param logname[in] name of llog file or FID
+ * \param start[in] first record to process
+ * \param end[in] last record to process (inclusive)
+ * \param cb[in] callback for records. Return -ve error, or +ve abort.
+ * \param private[in,out] private data passed to the \a record_cb function
+ * \param reverse[in] print the llog records from the beginning or the end
+ *
+ * \retval 0 on success
+ * others handled by the caller
+ */
+int jt_llog_print_iter(char *logname, long start, long end,
+ int (record_cb)(const char *record, void *private),
+ void *private, bool reverse)
+{
+ struct obd_ioctl_data data = { 0 };
+ char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
+ char startbuf[16], endbuf[16];
+ static long inc = sizeof(rawbuf) / 128;
+ long rec;
+ int rc = 0;
+
+ if (end == -1)
+ end = 0x7fffffff;
+
+ data.ioc_dev = cur_device;
+ data.ioc_inlbuf1 = logname;
+ data.ioc_inllen1 = strlen(logname) + 1;
+
+ /*
+ * Estimate about 128 characters per configuration record. Not all
+ * records will be printed in any case, so they should easily fit. If
+ * not, the kernel will return -EOVERFLOW and ask for fewer records.
+ *
+ * We don't want to request records from the kernel one-at-a-time, as
+ * it restarts the config llog iteration from the beginning, so we
+ * fetch multiple records from the kernel per call and split locally.
+ */
+ for (rec = start; rec < end; rec += inc) {
+ char *record = ((struct obd_ioctl_data *)buf)->ioc_bulk;
+
+retry:
+ snprintf(startbuf, sizeof(startbuf), "%lu", rec);
+ snprintf(endbuf, sizeof(endbuf), "%lu",
+ end < rec + inc - 1 ? end : rec + inc - 1);
+
+ /* start and end record numbers are passed as ASCII digits */
+ data.ioc_inlbuf2 = startbuf;
+ data.ioc_inllen2 = strlen(startbuf) + 1;
+ data.ioc_inlbuf3 = endbuf;
+ data.ioc_inllen3 = strlen(endbuf) + 1;
+
+ data.ioc_inllen4 = sizeof(rawbuf) -
+ __ALIGN_KERNEL(sizeof(data), 8) -
+ __ALIGN_KERNEL(data.ioc_inllen1, 8) -
+ __ALIGN_KERNEL(data.ioc_inllen2, 8) -
+ __ALIGN_KERNEL(data.ioc_inllen3, 8);
+ memset(buf, 0, sizeof(rawbuf));
+ rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
+ if (rc) {
+ fprintf(stderr, "%s: invalid ioctl data\n", logname);
+ goto out;
+ }
+
+ rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_PRINT, buf);
+ if (rc == -EOVERFLOW && inc > 2) {
+ inc /= 2;
+ goto retry;
+ }
+ if (rc) {
+ fprintf(stderr, "%s: OBD_IOC_LLOG_PRINT failed: %s\n",
+ logname, strerror(errno));
+ rc = -errno;
+ goto out;
+ }
+
+ /* There is no "end of list" marker, record was not modified */
+ if (strcmp(record, logname) == 0)
+ break;
+
+ rc = llog_process_records(record_cb, record, private, reverse);
+ if (rc)
+ goto out;
+ }
+
+out:
+ return rc;
+}
+
+static int llog_parse_catalog_start_end(int *argc, char **argv[],
+ char **catalog, long *start, long *end)
+{
+ const struct option long_opts[] = {
+ /* the --catalog option is not required, just for consistency */
+ { .val = 'c', .name = "catalog", .has_arg = required_argument },
+ { .val = 'e', .name = "end", .has_arg = required_argument },
+ { .val = 'h', .name = "help", .has_arg = no_argument },
+ { .val = 's', .name = "start", .has_arg = required_argument },
+ { .name = NULL } };
+ char *cmd = (*argv)[0];
+ char *endp;
+ int c;
+
+ if (!catalog || !start || !end)
+ return -EINVAL;
+
+ /* now process command line arguments*/
+ while ((c = getopt_long(*argc, *argv, "c:e:hs:",
+ long_opts, NULL)) != -1) {
+ switch (c) {
+ case 'c':
+ *catalog = optarg;
+ break;
+ case 'e':
+ *end = strtol(optarg, &endp, 0);
+ if (*endp != '\0') {
+ fprintf(stderr, "%s: bad end value '%s'\n",
+ cmd, optarg);
+ return CMD_HELP;
+ }
+ break;
+ case 's':
+ *start = strtol(optarg, &endp, 0);
+ if (*endp != '\0') {
+ fprintf(stderr, "%s: bad start value '%s'\n",
+ cmd, optarg);
+ return CMD_HELP;
+ }
+ break;
+ case 'h':
+ default:
+ return CMD_HELP;
+ }
+ }
+ *argc -= optind;
+ *argv += optind;
+
+ /*
+ * support old optional positional parameters only if they were
+ * not already specified with named arguments: logname [start [end]]
+ */
+ if (*argc >= 1) {
+ if (*catalog) {
+ fprintf(stderr,
+ "%s: logname is set, unknown argument '%s'\n",
+ cmd, (*argv)[0]);
+ return CMD_HELP;
+ }
+ *catalog = (*argv)[0];
+ (*argc)--;
+ (*argv)++;
+ }
+
+ if (*catalog == NULL) {
+ fprintf(stderr, "%s: no logname specified\n", cmd);
+ return CMD_HELP;
+ }
+
+ if (*argc >= 1) {
+ if (*start != 1) {
+ fprintf(stderr,
+ "%s: --start is set, unknown argument '%s'\n",
+ cmd, (*argv)[0]);
+ return CMD_HELP;
+ }
+
+ *start = strtol((*argv)[0], &endp, 0);
+ if (*endp != '\0') {
+ fprintf(stderr, "%s: bad start value '%s'\n",
+ cmd, (*argv)[0]);
+ return CMD_HELP;
+ }
+ (*argc)--;
+ (*argv)++;
+ }
+ if (*argc >= 1) {
+ if (*end != -1) {
+ fprintf(stderr,
+ "%s: --end is set, unknown argument '%s'\n",
+ cmd, (*argv)[0]);
+ return CMD_HELP;
+ }
+
+ *end = strtol((*argv)[0], &endp, 0);
+ if (*endp != '\0') {
+ fprintf(stderr, "%s: bad end value '%s'\n",
+ cmd, (*argv)[0]);
+ return CMD_HELP;
+ }
+ (*argc)--;
+ (*argv)++;
+ }
+ if (*argc > 1) {
+ fprintf(stderr, "%s: unknown argument '%s'\n", cmd, (*argv)[0]);
+ return CMD_HELP;
+ }
+
+ if (*end != -1 && *end < *start) {
+ fprintf(stderr, "%s: end '%lu' less than than start '%lu'\n",
+ cmd, *end, *start);
+ return CMD_HELP;
+ }
+
+ return 0;
+}
+
+int jt_llog_print(int argc, char **argv)
+{
+ char *catalog = NULL;
+ long start = 1, end = -1;
+ int rc;
+
+ rc = llog_parse_catalog_start_end(&argc, &argv, &catalog, &start, &end);
+ if (rc)
+ return rc;
+
+ if (llog_default_device(LLOG_DFLT_MGS_SET))
+ return CMD_INCOMPLETE;
+
+ rc = jt_llog_print_iter(catalog, start, end, jt_llog_print_cb,
+ NULL, false);
+
+ llog_default_device(LLOG_DFLT_DEV_RESET);
+
+ return rc;
+}
+
+/*
+ * Parse catalog, log ID, and optionally a log index with either optional
+ * arguments or positional arguments. Only the initial catalog argument
+ * may be positional with other optional arguments.
+ *
+ * The positional arguments option should eventually be phased out.
+ */
+static int llog_parse_catalog_log_idx(int *argc, char ***argv, const char *opts,
+ int max_args, struct obd_ioctl_data *data)
+{
+ const struct option long_opts[] = {
+ /* the --catalog option is not required, just for consistency */
+ { .val = 'c', .name = "catalog", .has_arg = required_argument },
+ { .val = 'h', .name = "help", .has_arg = no_argument },
+ { .val = 'i', .name = "log_idx", .has_arg = required_argument },
+ { .val = 'l', .name = "log_id", .has_arg = required_argument },
+ { .name = NULL } };
+ int c;
+
+ /* sanity check */
+ if (!data || *argc <= 1)
+ return -1;
+
+ data->ioc_dev = cur_device;
+
+ /* now process command line arguments*/
+ while ((c = getopt_long(*argc, *argv, opts, long_opts, NULL)) != -1) {
+ switch (c) {
+ case 'c':
+ data->ioc_inllen1 = strlen(optarg) + 1;
+ data->ioc_inlbuf1 = optarg;
+ break;
+ case 'i':
+ data->ioc_inllen3 = strlen(optarg) + 1;
+ data->ioc_inlbuf3 = optarg;
+ break;
+ case 'l': /* The log_id option isn't currently needed for
+ * cancel as mdt_iocontrol() handles IOC_LLOG_CANCEL,
+ * but we may as well keep it for now.
+ */
+ data->ioc_inllen2 = strlen(optarg) + 1;
+ data->ioc_inlbuf2 = optarg;
+ break;
+ case 'h':
+ default:
+ return CMD_HELP;
+ }
+ }
+
+ *argc -= optind;
+ *argv += optind;
+
+ /* Allow catalog to be specified as first option without --catalog */
+ if (!data->ioc_inlbuf1 && *argc > 0) {
+ data->ioc_inlbuf1 = (*argv)[0];
+ data->ioc_inllen1 = strlen((*argv)[0]) + 1;
+ (*argc)--;
+ (*argv)++;
+ }
+
+ return 0;
+}
+
+int jt_llog_cancel(int argc, char **argv)
+{
+ struct obd_ioctl_data data = { 0 };
+ char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
+ char *cmd = argv[0];
+ int rc;
+
+ /* Manage default device */
+ if (llog_default_device(LLOG_DFLT_MGS_SET))
+ return CMD_INCOMPLETE;
+
+ /* Parse catalog file (in inlbuf1) and named parameters */
+ rc = llog_parse_catalog_log_idx(&argc, &argv, "c:hi:l:", 3, &data);
+
+ /*
+ * Handle old positional parameters if not using named parameters,
+ * either "<catalog> <log_idx>" or "<catalog> <log_id> <log_idx>".
+ * It was "inlbuf3 = log_idx", and "inlbuf2 = log_id" (ignored by
+ * config log cancel), and shows why I hate positional parameters.
+ */
+ if (argc == 1) {
+ data.ioc_inllen3 = strlen(argv[0]) + 1;
+ data.ioc_inlbuf3 = argv[0];
+ } else if (argc == 2) {
+ data.ioc_inllen2 = strlen(argv[0]) + 1;
+ data.ioc_inlbuf2 = argv[0];
+ data.ioc_inllen3 = strlen(argv[1]) + 1;
+ data.ioc_inlbuf3 = argv[1];
+ }
+
+ if (!data.ioc_inlbuf1 || !data.ioc_inlbuf3) {
+ /* missing mandatory parameters */
+ rc = CMD_HELP;
+ goto err;
+ }
+
+ rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
+ if (rc) {
+ fprintf(stderr, "%s: ioctl_pack for catalog '%s' failed: %s\n",
+ jt_cmdname(cmd), data.ioc_inlbuf1, strerror(-rc));
+ goto err;
+ }
+
+ rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CANCEL, buf);
+ if (rc)
+ fprintf(stderr, "%s: cancel catalog '%s:%s' failed: %s\n",
+ jt_cmdname(cmd), data.ioc_inlbuf1, data.ioc_inlbuf3,
+ strerror(errno));
+
+err:
+ llog_default_device(LLOG_DFLT_DEV_RESET);
+ return rc;
+}
+
+int jt_llog_check(int argc, char **argv)
+{
+ struct obd_ioctl_data data = { 0 };
+ char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
+ char *catalog = NULL;
+ char startbuf[16], endbuf[16];
+ long start = 1, end = -1;
+ char *cmd = argv[0];
+ int rc;
+
+ rc = llog_parse_catalog_start_end(&argc, &argv, &catalog, &start, &end);
+ if (rc)
+ return rc;
+
+ if (llog_default_device(LLOG_DFLT_MGS_SET))
+ return CMD_INCOMPLETE;
+
+ if (end == -1)
+ end = 0x7fffffff;
+
+ data.ioc_dev = cur_device;
+ data.ioc_inllen1 = strlen(catalog) + 1;
+ data.ioc_inlbuf1 = catalog;
+
+ snprintf(startbuf, sizeof(startbuf), "%lu", start);
+ snprintf(endbuf, sizeof(endbuf), "%lu", end);
+ /* start and end record numbers are passed as ASCII digits */
+ data.ioc_inllen2 = strlen(startbuf) + 1;
+ data.ioc_inlbuf2 = startbuf;
+ data.ioc_inllen3 = strlen(endbuf) + 1;
+ data.ioc_inlbuf3 = endbuf;
+
+ data.ioc_inllen4 = sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(data), 8) -
+ __ALIGN_KERNEL(data.ioc_inllen1, 8) -
+ __ALIGN_KERNEL(data.ioc_inllen2, 8) -
+ __ALIGN_KERNEL(data.ioc_inllen3, 8);
+ rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
+ if (rc) {
+ fprintf(stderr, "%s: ioctl_pack failed for catalog '%s': %s\n",
+ jt_cmdname(cmd), data.ioc_inlbuf1, strerror(-rc));
+ goto err;
+ }
+
+ rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_CHECK, buf);
+ if (rc == 0)
+ fprintf(stdout, "%s", ((struct obd_ioctl_data *)buf)->ioc_bulk);
+ else
+ fprintf(stderr, "%s: OBD_IOC_LLOG_CHECK failed: %s\n",
+ jt_cmdname(cmd), strerror(errno));
+err:
+ llog_default_device(LLOG_DFLT_DEV_RESET);
+ return rc;
+}
+
+int jt_llog_remove(int argc, char **argv)
+{
+ struct obd_ioctl_data data = { 0 };
+ char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
+ char *cmd = argv[0];
+ int rc;
+
+ if (llog_default_device(LLOG_DFLT_MGS_SET))
+ return CMD_INCOMPLETE;
+
+ rc = llog_parse_catalog_log_idx(&argc, &argv, "c:hl:", 2, &data);
+ if (rc)
+ goto err;
+
+ if (argc == 1) {
+ if (data.ioc_inlbuf2) {
+ fprintf(stderr,
+ "%s: --log_id is set, unknown argument '%s'\n",
+ jt_cmdname(cmd), argv[0]);
+ rc = CMD_HELP;
+ goto err;
+ }
+
+ data.ioc_inllen2 = strlen(argv[0]) + 1;
+ data.ioc_inlbuf2 = argv[0];
+ }
+
+ rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
+ if (rc) {
+ fprintf(stderr, "%s: ioctl_pack for catalog '%s' failed: %s\n",
+ jt_cmdname(cmd), data.ioc_inlbuf1, strerror(-rc));
+ goto err;
+ }
+
+ rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_REMOVE, buf);
+ if (rc)
+ fprintf(stderr, "%s: cancel catalog '%s:%s' failed: %s\n",
+ jt_cmdname(cmd), data.ioc_inlbuf1, data.ioc_inlbuf2,
+ strerror(-rc));
+
+err:
+ llog_default_device(LLOG_DFLT_DEV_RESET);
+ return rc;
+}
+
+static void signal_server(int sig)
+{
+ if (sig == SIGINT) {
+ do_disconnect("sigint", 1);
+ exit(1);
+ } else {
+ fprintf(stderr, "%s: got signal %d\n", jt_cmdname("sigint"),
+ sig);
+ }
+}
+
+int obd_initialize(int argc, char **argv)
+{
+ if (shmem_setup() != 0)
+ return -1;
+
+ register_ioc_dev(OBD_DEV_ID, OBD_DEV_PATH);
+
+ return 0;
+}
+
+void obd_finalize(int argc, char **argv)
+{
+ struct sigaction sigact;
+
+ /* sigact initialization */
+ sigact.sa_handler = signal_server;
+ sigfillset(&sigact.sa_mask);
+ sigact.sa_flags = SA_RESTART;
+ /* coverity[uninit_use_in_call] */
+ sigaction(SIGINT, &sigact, NULL);
+
+ shmem_cleanup();
+ do_disconnect(argv[0], 1);
+}
+
+/**
+ * Get the index of the last llog record
+ *
+ * logid: [0x3:0xa:0x0]:0
+ * flags: 4 (plain)
+ * records_count: 57
+ * last_index: 57
+ *
+ * \param logname[in] pointer to config log name
+ *
+ * \retval > 0 on success
+ * <= 0 on error
+ */
+static long llog_last_index(char *logname)
+{
+ struct obd_ioctl_data data = { 0 };
+ char rawbuf[MAX_IOC_BUFLEN] = "", *buf = rawbuf;
+ char *last_index;
+ long rc;
+
+ data.ioc_dev = cur_device;
+ data.ioc_inllen1 = strlen(logname) + 1;
+ data.ioc_inlbuf1 = logname;
+ data.ioc_inllen2 = sizeof(rawbuf) - __ALIGN_KERNEL(sizeof(data), 8) -
+ __ALIGN_KERNEL(data.ioc_inllen1, 8);
+ rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
+ if (rc) {
+ fprintf(stderr, "%s: ioctl_pack failed for catalog '%s': %s\n",
+ __func__, logname, strerror(-rc));
+ return rc;
+ }
+
+ rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LLOG_INFO, buf);
+ if (rc == 0) {
+ last_index = strstr(((struct obd_ioctl_data *)buf)->ioc_bulk,
+ "last_index:");
+ return strtol(last_index + 11, NULL, 10);
+ }
+
+ rc = -errno;
+
+ return rc;
+}
+
+static char *get_llog_event_name(__u32 cmd)
+{
+ struct lcfg_type_data *data;
+
+ data = lcfg_cmd2data(cmd);
+ if (data)
+ return data->ltd_name;
+ return NULL;
+}
+
+static char *get_event_filter(__u32 cmd)
+{
+ char *event_name;
+ char *filter = NULL;
+ int len;
+
+ event_name = get_llog_event_name(cmd);
+ if (event_name) {
+ /* 9 bytes for "event: , " */
+ len = 9 + strlen(event_name);
+ filter = malloc(len + 1);
+ if (!filter)
+ return NULL;
+ memset(filter, 0, len + 1);
+ snprintf(filter, len, "event: %s, ", event_name);
+ return filter;
+ }
+
+ return NULL;
+}
+
+/**
+ * Callback to search ostname in llog
+ * - { index: 23, event: attach, device: lustre-OST0000-osc, type: osc,
+ * UUID: lustre-clilov_UUID }
+ * - { index: 24, event: setup, device: lustre-OST0000-osc,
+ * UUID: lustre-OST0000_UUID, node: 192.168.0.120@tcp }
+ * - { index: 25, event: add_osc, device: lustre-clilov,
+ * ost: lustre-OST0000_UUID, index: 0, gen: 1 }
+ *
+ * \param record[in] pointer to llog record
+ * \param data[in] pointer to ostname
+ *
+ * \retval 1 if ostname is found
+ * 0 if ostname is not found
+ * -ENOENT if ostname is deleted
+ */
+static int llog_search_ost_cb(const char *record, void *data)
+{
+ char *ostname = data;
+ char ost_filter[MAX_STRING_SIZE] = {'\0'};
+ char *add_osc, *del_osc, *setup, *cleanup;
+
+ add_osc = get_event_filter(LCFG_LOV_ADD_OBD);
+ del_osc = get_event_filter(LCFG_LOV_DEL_OBD);
+ setup = get_event_filter(LCFG_SETUP);
+ cleanup = get_event_filter(LCFG_CLEANUP);
+ if (!add_osc || !del_osc || !setup || !cleanup)
+ return -ENOMEM;
+
+ if (ostname && ostname[0])
+ snprintf(ost_filter, sizeof(ost_filter), " %s,", ostname);
+
+ if (strstr(record, ost_filter)) {
+ if (strstr(record, add_osc) || strstr(record, setup))
+ return 1;
+ if (strstr(record, del_osc) || strstr(record, cleanup))
+ return -ENOENT;
+ }
+
+ free(add_osc);
+ free(del_osc);
+ free(setup);
+ free(cleanup);
+
+ return 0;
+}
+
+/**
+ * Search ost in llog
+ *
+ * \param logname[in] pointer to config log name
+ * \param last_index[in] the index of the last llog record
+ * \param ostname[in] pointer to ost name
+ *
+ * \retval 1 if ostname is found
+ * 0 if ostname is not found
+ */
+static int llog_search_ost(char *logname, long last_index, char *ostname)
+{
+ long start, end, inc = MAX_IOC_BUFLEN / 128;
+ int rc = 0;
+
+ for (end = last_index; end > 1; end -= inc) {
+ start = end - inc > 0 ? end - inc : 1;
+ rc = jt_llog_print_iter(logname, start, end, llog_search_ost_cb,
+ ostname, true);
+ if (rc)
+ break;
+ }
+
+ return (rc == 1 ? 1 : 0);
+}
+
+struct llog_pool_data {
+ char lpd_fsname[LUSTRE_MAXFSNAME + 1];
+ char lpd_poolname[LOV_MAXPOOLNAME + 1];
+ char lpd_ostname[MAX_OBD_NAME + 1];
+ enum lcfg_command_type lpd_cmd_type;
+ bool lpd_pool_exists;
+ int lpd_ost_num;
+};
+
+/**
+ * Called for each formatted line in the config log (within range).
+ *
+ * - { index: 74, event: new_pool, device: tfs-clilov, fsname: tfs, pool: tmp }
+ * - { index: 77, event: add_pool, device: tfs-clilov, fsname: tfs, pool: tmp,
+ * ost: tfs-OST0000_UUID }
+ * - { index: 224, event: remove_pool, device: tfs-clilov, fsname: tfs,
+ * pool: tmp, ost: tfs-OST0003_UUID }
+ * - { index: 227, event: del_pool, device: tfs-clilov, fsname: tfs, pool: tmp }
+ *
+ * \param record[in] pointer to llog record
+ * \param data[in] pointer to llog_pool_data
+ *
+ * \retval 1 if pool or OST is found
+ * 0 if pool or OST is not found
+ * -ENOENT if pool or OST is removed
+ */
+static int llog_search_pool_cb(const char *record, void *data)