From: Frederick Dilger Date: Thu, 12 Dec 2024 08:11:10 +0000 (-0700) Subject: LU-16446 utils: fix issues with llapi_get_lmm_from_path() X-Git-Tag: 2.16.52~98 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=bed10bb93399971f27295069d82a046cb0995b9a;p=fs%2Flustre-release.git LU-16446 utils: fix issues with llapi_get_lmm_from_path() size_t was being used for lmmlen when it should have been ssize_t so that it can hold a negative return value. comp_v1 was returned by llapi_get_lmm_from_path() but not freed. lmmbuf was allocated in llapi_get_lmm_from_path() but leaked on error. file p was opened in llapi_get_lmm_from_path() but leaked on error. Test-Parameters: trivial CoverityID: 451724 ("Unsigned compared against 0") CoverityID: 451710 ("Resource leak") CoverityID: 451707 ("Resource leak") Fixes: a796b10fa2 ("LU-16446 utils: specify total count for mirror extend") Signed-off-by: Frederick Dilger Change-Id: I729d2c2abb8789a82d61858b034dcadf733a4197 Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/57391 Reviewed-by: Andreas Dilger Reviewed-by: Timothy Day Reviewed-by: Oleg Drokin Tested-by: jenkins Tested-by: Maloo --- diff --git a/lustre/utils/lfs.c b/lustre/utils/lfs.c index 4a55370e..ff0633a 100755 --- a/lustre/utils/lfs.c +++ b/lustre/utils/lfs.c @@ -4206,27 +4206,30 @@ create_mirror: } if (mirror_total_mode) { - char *path = argv[argc-1]; + char *path = argv[argc - 1]; struct lov_comp_md_v1 *comp_v1; + int have_mirrors; - result = llapi_get_lmm_from_path(path, (struct lov_user_md_v1 **)&comp_v1); + result = llapi_get_lmm_from_path(path, + (struct lov_user_md_v1 **)&comp_v1); if (result) { fprintf(stderr, "error: %s: cannot get layout from %s: %s\n", progname, path, strerror(-result)); goto error; } + have_mirrors = comp_v1->lcm_mirror_count; + free(comp_v1); - if (comp_v1->lcm_mirror_count >= mirror_count) + if (have_mirrors >= mirror_count) mirror_count = 0; else - mirror_count -= comp_v1->lcm_mirror_count; + mirror_count -= have_mirrors; - if (!mirror_count) { + if (mirror_count == 0) { fprintf(stderr, - "warning: the file '%s' already has %d mirrors. No new mirrors will be created\n", - path, - comp_v1->lcm_mirror_count); + "warning: '%s' already has %d mirrors, no new mirrors will be created\n", + path, have_mirrors); break; } } diff --git a/lustre/utils/liblustreapi.c b/lustre/utils/liblustreapi.c index 60206f5..27f2de7 100644 --- a/lustre/utils/liblustreapi.c +++ b/lustre/utils/liblustreapi.c @@ -1963,7 +1963,7 @@ retry_getfileinfo: */ int llapi_get_lmm_from_path(const char *path, struct lov_user_md_v1 **lmmbuf) { - size_t lmmlen; + ssize_t lmmlen; int p = -1; int rc = 0; @@ -1972,14 +1972,23 @@ int llapi_get_lmm_from_path(const char *path, struct lov_user_md_v1 **lmmbuf) return -EINVAL; p = open_parent(path); + if (p < 0) + return -errno; *lmmbuf = calloc(1, lmmlen); - if (*lmmbuf == NULL) - return -errno; + if (*lmmbuf == NULL) { + rc = -errno; + goto out_close; + } rc = get_lmd_info_fd(path, p, 0, *lmmbuf, lmmlen, GET_LMD_STRIPE); - if (p != -1) - close(p); + if (rc < 0) { + free(*lmmbuf); + *lmmbuf = NULL; + } +out_close: + close(p); + return rc; }