Whamcloud - gitweb
LU-10822 utils: stop bogus buffer overflow errors
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
index b3dd226..a5c1347 100644 (file)
@@ -306,22 +306,22 @@ int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
        ptr = overlay->ioc_bulk;
        if (data->ioc_inlbuf1) {
                memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
-               ptr += cfs_size_round(data->ioc_inllen1);
+               ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
        }
 
        if (data->ioc_inlbuf2) {
                memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
-               ptr += cfs_size_round(data->ioc_inllen2);
+               ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
        }
 
        if (data->ioc_inlbuf3) {
                memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
-               ptr += cfs_size_round(data->ioc_inllen3);
+               ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
        }
 
        if (data->ioc_inlbuf4) {
                memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
-               ptr += cfs_size_round(data->ioc_inllen4);
+               ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
        }
 
        return 0;
@@ -348,22 +348,22 @@ int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
        ptr = overlay->ioc_bulk;
        if (data->ioc_inlbuf1) {
                memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
-               ptr += cfs_size_round(data->ioc_inllen1);
+               ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
        }
 
        if (data->ioc_inlbuf2) {
                memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
-               ptr += cfs_size_round(data->ioc_inllen2);
+               ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
        }
 
        if (data->ioc_inlbuf3) {
                memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
-               ptr += cfs_size_round(data->ioc_inllen3);
+               ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
        }
 
        if (data->ioc_inlbuf4) {
                memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
-               ptr += cfs_size_round(data->ioc_inllen4);
+               ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
        }
 
        return 0;
@@ -1644,7 +1644,7 @@ static int common_param_init(struct find_param *param, char *path)
                lum_size = PATH_MAX + 1;
 
        param->fp_lum_size = lum_size;
-       param->fp_lmd = calloc(1, sizeof(lstat_t) + param->fp_lum_size);
+       param->fp_lmd = calloc(1, sizeof(lstat_t) + lum_size);
        if (param->fp_lmd == NULL) {
                llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
                            "error: allocation of %zu bytes for ioctl",
@@ -1747,64 +1747,95 @@ again:
        return ret;
 }
 
-static int get_lmd_info(char *path, DIR *parent, DIR *dir,
-                 struct lov_user_mds_data *lmd, int lumlen)
+int get_lmd_info_fd(char *path, int parent_fd, int dir_fd,
+                   void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
 {
-        lstat_t *st = &lmd->lmd_st;
-        int ret = 0;
+       struct lov_user_mds_data *lmd = lmdbuf;
+       lstat_t *st = &lmd->lmd_st;
+       int ret = 0;
 
-        if (parent == NULL && dir == NULL)
-                return -EINVAL;
+       if (parent_fd < 0 && dir_fd < 0)
+               return -EINVAL;
+       if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
+               return -EINVAL;
 
-        if (dir) {
-                ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
-        } else if (parent) {
+       if (dir_fd >= 0) {
+               /* LL_IOC_MDC_GETINFO operates on the current directory inode
+                * and returns struct lov_user_mds_data, while
+                * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
+                */
+               ret = ioctl(dir_fd, type == GET_LMD_INFO ? LL_IOC_MDC_GETINFO :
+                                                          LL_IOC_LOV_GETSTRIPE,
+                           lmdbuf);
+       } else if (parent_fd >= 0) {
                char *fname = strrchr(path, '/');
 
-               /* To avoid opening, locking, and closing each file on the
-                * client if that is not needed. The GETFILEINFO ioctl can
-                * be done on the patent dir with a single open for all
+               /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
+                * the parent directory) and returns struct lov_user_mds_data,
+                * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
+                *
+                * This avoids opening, locking, and closing each file on the
+                * client if that is not needed. Multiple of these ioctl() can
+                * be done on the parent dir with a single open for all
                 * files in that directory, and it also doesn't pollute the
                 * client dcache with millions of dentries when traversing
-                * a large filesystem.  */
+                * a large filesystem.
+                */
                fname = (fname == NULL ? path : fname + 1);
-               /* retrieve needed file info */
-               snprintf((char *)lmd, lumlen, "%s", fname);
-               ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
-        }
 
-        if (ret) {
-                if (errno == ENOTTY) {
-                        /* ioctl is not supported, it is not a lustre fs.
-                         * Do the regular lstat(2) instead. */
-                        ret = lstat_f(path, st);
-                        if (ret) {
-                                ret = -errno;
-                                llapi_error(LLAPI_MSG_ERROR, ret,
-                                            "error: %s: lstat failed for %s",
-                                            __func__, path);
-                        }
-                } else if (errno == ENOENT) {
-                        ret = -errno;
-                        llapi_error(LLAPI_MSG_WARN, ret,
-                                    "warning: %s: %s does not exist",
-                                    __func__, path);
-                } else if (errno != EISDIR) {
-                        ret = -errno;
-                        llapi_error(LLAPI_MSG_ERROR, ret,
-                                    "%s ioctl failed for %s.",
-                                    dir ? "LL_IOC_MDC_GETINFO" :
-                                    "IOC_MDC_GETFILEINFO", path);
-               } else {
+               ret = snprintf(lmdbuf, lmdlen, "%s", fname);
+               if (ret < 0)
+                       errno = -ret;
+               else if (ret >= lmdlen || ret++ == 0)
+                       errno = EINVAL;
+               else
+                       ret = ioctl(parent_fd, type == GET_LMD_INFO ?
+                                               IOC_MDC_GETFILEINFO :
+                                               IOC_MDC_GETFILESTRIPE, lmdbuf);
+       }
+
+       if (ret && type == GET_LMD_INFO) {
+               if (errno == ENOTTY) {
+                       /* ioctl is not supported, it is not a lustre fs.
+                        * Do the regular lstat(2) instead.
+                        */
+                       ret = lstat_f(path, st);
+                       if (ret) {
+                               ret = -errno;
+                               llapi_error(LLAPI_MSG_ERROR, ret,
+                                           "error: %s: lstat failed for %s",
+                                           __func__, path);
+                       }
+               } else if (errno == ENOENT) {
+                       ret = -errno;
+                       llapi_error(LLAPI_MSG_WARN, ret,
+                                   "warning: %s does not exist", path);
+               } else if (errno != EISDIR && errno != ENODATA) {
                        ret = -errno;
                        llapi_error(LLAPI_MSG_ERROR, ret,
-                                "error: %s: IOC_MDC_GETFILEINFO failed for %s",
-                                  __func__, path);
+                                   "%s ioctl failed for %s.",
+                                   dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
+                                   "IOC_MDC_GETFILEINFO", path);
                }
        }
+
        return ret;
 }
 
+static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
+                       int lmdlen, enum get_lmd_info_type type)
+{
+       int parent_fd = -1;
+       int dir_fd = -1;
+
+       if (parent)
+               parent_fd = dirfd(parent);
+       if (dir)
+               dir_fd = dirfd(dir);
+
+       return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
+}
+
 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
                                   semantic_func_t sem_init,
                                   semantic_func_t sem_fini, void *data,
@@ -1859,7 +1890,7 @@ static int llapi_semantic_traverse(char *path, int size, DIR *parent,
                        lstat_t *st = &param->fp_lmd->lmd_st;
 
                        rc = get_lmd_info(path, d, NULL, param->fp_lmd,
-                                          param->fp_lum_size);
+                                         param->fp_lum_size, GET_LMD_INFO);
                        if (rc == 0)
                                dent->d_type = IFTODT(st->st_mode);
                        else if (ret == 0)
@@ -2580,7 +2611,7 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                                     layout2name(lum->lmm_pattern));
                else
                        llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
-               separator = is_dir ? " " : "\n";
+               separator = (!yaml && is_dir) ? " " : "\n";
        }
 
        if ((verbose & VERBOSE_GENERATION) && !is_dir) {
@@ -2589,7 +2620,7 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                        llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen:    ",
                                     space, prefix);
                llapi_printf(LLAPI_MSG_NORMAL, "%u",
-                            (int)lum->lmm_layout_gen);
+                            skip_objs ? 0 : (int)lum->lmm_layout_gen);
                separator = "\n";
        }
 
@@ -2603,6 +2634,8 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                                     lum->lmm_stripe_offset ==
                                     (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
                                     lum->lmm_stripe_offset);
+               else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
+                       llapi_printf(LLAPI_MSG_NORMAL, "0");
                else
                        llapi_printf(LLAPI_MSG_NORMAL, "%u",
                                     objects[0].l_ost_idx);
@@ -2656,7 +2689,8 @@ void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
                                 flags);
 
        if (!is_dir && !skip_objs && (header & VERBOSE_OBJID) &&
-           !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
+           !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
+             lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
                char *space = "      - ";
 
                if (indent)
@@ -3963,7 +3997,8 @@ static int cb_find_init(char *path, DIR *parent, DIR **dirp,
        if (param->fp_obd_uuid || param->fp_mdt_uuid ||
            param->fp_check_uid || param->fp_check_gid ||
            param->fp_atime || param->fp_mtime || param->fp_ctime ||
-           param->fp_check_size || find_check_lmm_info(param) ||
+           param->fp_check_size || param->fp_check_blocks ||
+           find_check_lmm_info(param) ||
            param->fp_check_mdt_count || param->fp_check_hash_type)
                decision = 0;
 
@@ -3980,7 +4015,7 @@ static int cb_find_init(char *path, DIR *parent, DIR **dirp,
 
                param->fp_lmd->lmd_lmm.lmm_magic = 0;
                ret = get_lmd_info(path, parent, dir, param->fp_lmd,
-                                  param->fp_lum_size);
+                                  param->fp_lum_size, GET_LMD_INFO);
                if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
                    find_check_lmm_info(param)) {
                        struct lov_user_md *lmm = &param->fp_lmd->lmd_lmm;
@@ -4215,10 +4250,8 @@ obd_matches:
            The regular stat is almost of the same speed as some new
            'glimpse-size-ioctl'. */
 
-       if (param->fp_check_size && S_ISREG(st->st_mode) && stripe_count)
-               decision = 0;
-
-       if (param->fp_check_size && S_ISDIR(st->st_mode))
+       if ((param->fp_check_size || param->fp_check_blocks) &&
+           ((S_ISREG(st->st_mode) && stripe_count) || S_ISDIR(st->st_mode)))
                decision = 0;
 
        if (!decision) {
@@ -4259,20 +4292,30 @@ obd_matches:
                        goto decided;
        }
 
-       if (param->fp_check_size)
+       if (param->fp_check_size) {
                decision = find_value_cmp(st->st_size, param->fp_size,
                                          param->fp_size_sign,
                                          param->fp_exclude_size,
                                          param->fp_size_units, 0);
+               if (decision == -1)
+                       goto decided;
+       }
 
-       if (decision != -1) {
-               llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
-               if (param->fp_zero_end)
-                       llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
-               else
-                       llapi_printf(LLAPI_MSG_NORMAL, "\n");
+       if (param->fp_check_blocks) { /* convert st_blocks to bytes */
+               decision = find_value_cmp(st->st_blocks * 512, param->fp_blocks,
+                                         param->fp_blocks_sign,
+                                         param->fp_exclude_blocks,
+                                         param->fp_blocks_units, 0);
+               if (decision == -1)
+                       goto decided;
        }
 
+       llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
+       if (param->fp_zero_end)
+               llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
+       else
+               llapi_printf(LLAPI_MSG_NORMAL, "\n");
+
 decided:
        ret = 0;
        /* Do not get down anymore? */
@@ -4495,26 +4538,14 @@ static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
                        return ret;
        }
 
-       if (d) {
-               if (param->fp_get_lmv || param->fp_get_default_lmv) {
-                       ret = cb_get_dirstripe(path, d, param);
-               } else {
-                       ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
-                                    (void *)&param->fp_lmd->lmd_lmm);
-               }
-
-       } else if (parent && !param->fp_get_lmv && !param->fp_get_default_lmv) {
-               char *fname = strrchr(path, '/');
-               fname = (fname == NULL ? path : fname + 1);
-
-               snprintf((char *)&param->fp_lmd->lmd_lmm, param->fp_lum_size,
-                        "%s", fname);
-
-               ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
-                           (void *)&param->fp_lmd->lmd_lmm);
-       } else {
+       if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
+               ret = cb_get_dirstripe(path, d, param);
+       else if (d ||
+                (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
+               ret = get_lmd_info(path, parent, d, &param->fp_lmd->lmd_lmm,
+                                  param->fp_lum_size, GET_LMD_STRIPE);
+       else
                return 0;
-       }
 
         if (ret) {
                 if (errno == ENODATA && d != NULL) {