+ /* See if we can check the file type from the dirent. */
+ if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
+ de->d_type < DT_MAX) {
+ checked_type = 1;
+ if (llapi_dir_filetype_table[de->d_type] == param->type) {
+ if (param->exclude_type)
+ goto decided;
+ } else {
+ if (!param->exclude_type)
+ goto decided;
+ }
+ }
+
+
+ /* If a time or OST should be checked, the decision is not taken yet. */
+ if (param->atime || param->ctime || param->mtime || param->obduuid ||
+ param->size)
+ decision = 0;
+
+ ret = 0;
+ /* Request MDS for the stat info. */
+ if (param->have_fileinfo == 0) {
+ if (dir) {
+ /* retrieve needed file info */
+ ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
+ (void *)param->lmd);
+ } else {
+ char *fname = strrchr(path, '/');
+ fname = (fname == NULL ? path : fname + 1);
+
+ /* retrieve needed file info */
+ strncpy((char *)param->lmd, fname, param->lumlen);
+ ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
+ (void *)param->lmd);
+ }
+ }
+
+ if (ret) {
+ if (errno == ENOTTY) {
+ /* ioctl is not supported, it is not a lustre fs.
+ * Do the regular lstat(2) instead. */
+ lustre_fs = 0;
+ ret = lstat_f(path, st);
+ if (ret) {
+ llapi_err(LLAPI_MSG_ERROR,
+ "error: %s: lstat failed for %s",
+ __FUNCTION__, path);
+ return ret;
+ }
+ } else if (errno == ENOENT) {
+ llapi_err(LLAPI_MSG_WARN,
+ "warning: %s: %s does not exist",
+ __FUNCTION__, path);
+ goto decided;
+ } else {
+ llapi_err(LLAPI_MSG_ERROR, "error: %s: %s failed for %s",
+ __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
+ "IOC_MDC_GETFILEINFO", path);
+ return ret;
+ }
+ }
+
+ if (param->type && !checked_type) {
+ if ((st->st_mode & S_IFMT) == param->type) {
+ if (param->exclude_type)
+ goto decided;
+ } else {
+ if (!param->exclude_type)
+ goto decided;
+ }
+ }
+
+ /* Prepare odb. */
+ if (param->obduuid) {
+ if (lustre_fs && param->got_uuids &&
+ param->st_dev != st->st_dev) {
+ /* A lustre/lustre mount point is crossed. */
+ param->got_uuids = 0;
+ param->obds_printed = 0;
+ param->obdindex = OBD_NOT_FOUND;
+ }
+
+ if (lustre_fs && !param->got_uuids) {
+ ret = setup_obd_indexes(dir ? dir : parent, param);
+ if (ret)
+ return ret;
+
+ param->st_dev = st->st_dev;
+ } else if (!lustre_fs && param->got_uuids) {
+ /* A lustre/non-lustre mount point is crossed. */
+ param->got_uuids = 0;
+ param->obdindex = OBD_NOT_FOUND;
+ }
+ }
+
+ /* If an OBD UUID is specified but no one matches, skip this file. */
+ if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
+ goto decided;
+
+ /* If a OST UUID is given, and some OST matches, check it here. */
+ if (param->obdindex != OBD_NOT_FOUND) {
+ if (!S_ISREG(st->st_mode))
+ goto decided;
+
+ /* Only those files should be accepted, which have a
+ * stripe on the specified OST. */
+ if (!param->lmd->lmd_lmm.lmm_stripe_count) {
+ goto decided;
+ } else {
+ int i, j;
+ for (i = 0;
+ i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
+ for (j = 0; j < param->num_obds; j++) {
+ if (param->obdindexes[j] ==
+ param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
+ goto obd_matches;
+ }
+ }
+
+ if (i == param->lmd->lmd_lmm.lmm_stripe_count)
+ goto decided;
+ }
+ }
+
+ /* Check the time on mds. */
+ if (!decision) {
+ int for_mds;
+
+ for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
+ param->lmd->lmd_lmm.lmm_stripe_count)
+ : 0;
+ decision = find_time_check(st, param, for_mds);
+ }
+
+obd_matches:
+ /* If file still fits the request, ask osd for updated info.
+ The regulat stat is almost of the same speed as some new
+ 'glimpse-size-ioctl'. */
+ if (!decision && S_ISREG(st->st_mode) &&
+ (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
+ if (param->obdindex != OBD_NOT_FOUND) {
+ /* Check whether the obd is active or not, if it is
+ * not active, just print the object affected by this
+ * failed ost
+ * */
+ struct obd_statfs stat_buf;
+ struct obd_uuid uuid_buf;
+
+ memset(&stat_buf, 0, sizeof(struct obd_statfs));
+ memset(&uuid_buf, 0, sizeof(struct obd_uuid));
+ ret = llapi_obd_statfs(path, LL_STATFS_LOV,
+ param->obdindex, &stat_buf,
+ &uuid_buf);
+ if (ret) {
+ if (ret == -ENODATA || ret == -ENODEV
+ || ret == -EIO)
+ errno = EIO;
+ llapi_printf(LLAPI_MSG_NORMAL,
+ "obd_uuid: %s failed %s ",
+ param->obduuid->uuid,
+ strerror(errno));
+ goto print_path;
+ }
+ }
+ if (dir) {
+ ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
+ (void *)param->lmd);
+ } else if (parent) {
+ ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
+ (void *)param->lmd);
+ }
+
+ if (ret) {
+ if (errno == ENOENT) {
+ llapi_err(LLAPI_MSG_ERROR,
+ "warning: %s: %s does not exist",
+ __FUNCTION__, path);
+ goto decided;
+ } else {
+ llapi_err(LLAPI_MSG_ERROR,
+ "%s: IOC_LOV_GETINFO on %s failed",
+ __FUNCTION__, path);
+ return ret;
+ }
+ }
+
+ /* Check the time on osc. */
+ decision = find_time_check(st, param, 0);
+ if (decision == -1)
+ goto decided;
+ }
+
+ if (param->size)
+ decision = find_value_cmp(st->st_size, param->size,
+ param->size_sign, param->size_units,
+ 0);
+
+print_path:
+ if (decision != -1) {
+ llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
+ if (param->zeroend)
+ llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
+ else
+ llapi_printf(LLAPI_MSG_NORMAL, "\n");
+ }
+
+decided:
+ /* Do not get down anymore? */
+ if (param->depth == param->maxdepth)
+ return 1;
+
+ param->depth++;
+ return 0;
+}
+
+static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
+ struct dirent64 *de)
+{
+ struct find_param *param = (struct find_param *)data;
+ param->depth--;
+ return 0;
+}
+
+int llapi_find(char *path, struct find_param *param)
+{
+ char *buf;
+ int ret, len = strlen(path);
+
+ if (len > PATH_MAX) {
+ llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
+ __FUNCTION__, path);
+ return -EINVAL;
+ }
+
+ buf = (char *)malloc(PATH_MAX + 1);
+ if (!buf)
+ return -ENOMEM;
+
+ ret = common_param_init(param);
+ if (ret) {
+ free(buf);
+ return ret;
+ }
+
+ param->depth = 0;
+
+ strncpy(buf, path, PATH_MAX + 1);
+ ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
+ cb_common_fini, param, NULL);
+
+ find_param_fini(param);
+ free(buf);
+ return ret < 0 ? ret : 0;
+}
+
+static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
+ struct dirent64 *de)
+{
+ struct find_param *param = (struct find_param *)data;
+ int ret = 0;
+
+ LASSERT(parent != NULL || d != NULL);
+
+ /* Prepare odb. */
+ if (!param->got_uuids) {
+ ret = setup_obd_uuid(d ? d : parent, path, param);
+ if (ret)
+ return ret;
+ }
+
+ if (d) {
+ ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
+ (void *)¶m->lmd->lmd_lmm);
+ } else if (parent) {
+ char *fname = strrchr(path, '/');
+ fname = (fname == NULL ? path : fname + 1);
+
+ strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
+ ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
+ (void *)¶m->lmd->lmd_lmm);
+ }
+
+ if (ret) {
+ if (errno == ENODATA) {
+ if (!param->obduuid && !param->quiet)
+ llapi_printf(LLAPI_MSG_NORMAL,
+ "%s has no stripe info\n", path);
+ goto out;
+ } else if (errno == ENOTTY) {
+ llapi_err(LLAPI_MSG_ERROR,
+ "%s: '%s' not on a Lustre fs?",
+ __FUNCTION__, path);
+ } else if (errno == ENOENT) {
+ llapi_err(LLAPI_MSG_WARN,
+ "warning: %s: %s does not exist",
+ __FUNCTION__, path);
+ goto out;
+ } else {
+ llapi_err(LLAPI_MSG_ERROR,
+ "error: %s: %s failed for %s",
+ __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
+ "IOC_MDC_GETFILESTRIPE", path);
+ }
+
+ return ret;
+ }
+
+ llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
+out:
+ /* Do not get down anymore? */
+ if (param->depth == param->maxdepth)
+ return 1;
+
+ param->depth++;
+ return 0;
+}
+
+int llapi_getstripe(char *path, struct find_param *param)
+{
+ char *buf;
+ int ret = 0, len = strlen(path);
+
+ if (len > PATH_MAX) {
+ llapi_err(LLAPI_MSG_ERROR,
+ "%s: Path name '%s' is too long",
+ __FUNCTION__, path);
+ return -EINVAL;
+ }
+
+ buf = (char *)malloc(PATH_MAX + 1);
+ if (!buf)
+ return -ENOMEM;
+
+ ret = common_param_init(param);
+ if (ret) {
+ free(buf);
+ return ret;
+ }
+
+ param->depth = 0;
+
+ strncpy(buf, path, PATH_MAX + 1);
+ ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
+ cb_common_fini, param, NULL);
+ find_param_fini(param);
+ free(buf);
+ return ret < 0 ? ret : 0;
+}
+
+int llapi_obd_statfs(char *path, __u32 type, __u32 index,
+ struct obd_statfs *stat_buf,
+ struct obd_uuid *uuid_buf)
+{
+ int fd;
+ char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
+ char *rawbuf = raw;
+ struct obd_ioctl_data data = { 0 };
+ int rc = 0;
+
+ data.ioc_inlbuf1 = (char *)&type;
+ data.ioc_inllen1 = sizeof(__u32);
+ data.ioc_inlbuf2 = (char *)&index;
+ data.ioc_inllen2 = sizeof(__u32);
+ data.ioc_pbuf1 = (char *)stat_buf;
+ data.ioc_plen1 = sizeof(struct obd_statfs);
+ data.ioc_pbuf2 = (char *)uuid_buf;
+ data.ioc_plen2 = sizeof(struct obd_uuid);
+
+ if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
+ llapi_err(LLAPI_MSG_ERROR,
+ "llapi_obd_statfs: error packing ioctl data");
+ return rc;
+ }
+
+ fd = open(path, O_RDONLY);
+ if (errno == EISDIR)
+ fd = open(path, O_DIRECTORY | O_RDONLY);
+
+ if (fd < 0) {
+ rc = errno ? -errno : -EBADF;
+ llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
+ __FUNCTION__, path);
+ return rc;
+ }
+ rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
+ if (rc)
+ rc = errno ? -errno : -EINVAL;
+
+ close(fd);
+ return rc;
+}
+
+#define MAX_STRING_SIZE 128
+#define DEVICES_LIST "/proc/fs/lustre/devices"
+
+int llapi_ping(char *obd_type, char *obd_name)
+{
+ char path[MAX_STRING_SIZE];
+ char buf[1];
+ int rc, fd;
+
+ snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
+ obd_type, obd_name);
+
+ fd = open(path, O_WRONLY);
+ if (fd < 0) {
+ rc = errno;
+ llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
+ return rc;
+ }
+
+ rc = write(fd, buf, 1);
+ close(fd);
+
+ if (rc == 1)
+ return 0;
+ return rc;
+}
+
+int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
+{
+ char buf[MAX_STRING_SIZE];
+ FILE *fp = fopen(DEVICES_LIST, "r");
+ int i, rc = 0;
+
+ if (fp == NULL) {
+ rc = errno;
+ llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
+ return rc;
+ }
+
+ while (fgets(buf, sizeof(buf), fp) != NULL) {