From 8d09b3e77744973d10d2029028d7e6f14591bc6d Mon Sep 17 00:00:00 2001 From: Bobi Jam Date: Wed, 10 May 2023 23:15:24 +0800 Subject: [PATCH] LU-16808 lfs: return invalid=-1 if no projid retrieved lfs find --printf "%LP" returns -1 as invalid projid if a special file is found while it does not contain projid. Fix bug where "lfs find --printf %LP" always printed "0". Fixes: 6b8e97b76c ("LU-10378 utils: add formatted printf to lfs find") Signed-off-by: Bobi Jam Change-Id: Ic1b474145212e0ce091f97281440816d9a437a4f Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/50916 Reviewed-by: Andreas Dilger Reviewed-by: Thomas Bertschinger Reviewed-by: Oleg Drokin Tested-by: jenkins Tested-by: Maloo --- lustre/tests/sanity.sh | 12 ++++++++++-- lustre/utils/liblustreapi.c | 25 ++++++++++++++++--------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lustre/tests/sanity.sh b/lustre/tests/sanity.sh index 44a38b9..aa2e05d 100755 --- a/lustre/tests/sanity.sh +++ b/lustre/tests/sanity.sh @@ -7188,15 +7188,23 @@ test_56rd() { local dir=$DIR/$tdir test_mkdir $dir + rm -f $dir/* mkfifo $dir/fifo || error "failed to create fifo file" + $LFS find $dir -t p --printf "%p %y %LP\n" || + error "should not fail even cannot get projid from pipe file" found=$($LFS find $dir -t p --printf "%y") - [[ "p" = $found ]] || error "found $found, expect p" + [[ "p" == $found ]] || error "found $found, expect p" mknod $dir/chardev c 1 5 || error "failed to create character device file" + $LFS find $dir -t c --printf "%p %y %LP\n" || + error "should not fail even cannot get projid from chardev file" found=$($LFS find $dir -t c --printf "%y") - [[ "c" = $found ]] || error "found $found, expect c" + [[ "c" == $found ]] || error "found $found, expect c" + + found=$($LFS find $dir ! -type d --printf "%p %y %LP\n" | wc -l) + (( found == 2 )) || error "unable to list all files" } run_test 56rd "check lfs find --printf special files" diff --git a/lustre/utils/liblustreapi.c b/lustre/utils/liblustreapi.c index a48313d..3c3615d 100644 --- a/lustre/utils/liblustreapi.c +++ b/lustre/utils/liblustreapi.c @@ -85,6 +85,10 @@ #define FORMATTED_BUF_LEN 1024 +#ifndef INVALID_PROJID +#define INVALID_PROJID -1 +#endif + static int llapi_msg_level = LLAPI_MSG_MAX; const char *liblustreapi_cmd; @@ -4761,7 +4765,7 @@ format_done: * as part of the format (0 for an unknown format) */ int printf_format_lustre(char *seq, char *buffer, size_t size, int *wrote, - struct find_param *param, char *path, int projid, + struct find_param *param, char *path, __u32 projid, int d) { struct lmv_user_md *lum; @@ -4796,7 +4800,10 @@ int printf_format_lustre(char *seq, char *buffer, size_t size, int *wrote, *wrote = snprintf(buffer, size, DFID_NOBRACE, PFID(&fid)); goto format_done; case 'P': - *wrote = snprintf(buffer, size, "%d", projid); + if (projid == INVALID_PROJID) + *wrote = snprintf(buffer, size, "-1"); + else + *wrote = snprintf(buffer, size, "%u", projid); goto format_done; } @@ -4958,7 +4965,7 @@ format_done: * as part of the format (0 for an unknown format) */ int printf_format_directive(char *seq, char *buffer, size_t size, int *wrote, - struct find_param *param, char *path, int projid, + struct find_param *param, char *path, __u32 projid, int d) { __u16 mode = param->fp_lmd->lmd_stx.stx_mode; @@ -5054,7 +5061,7 @@ int printf_format_directive(char *seq, char *buffer, size_t size, int *wrote, * non-directory file) */ void printf_format_string(struct find_param *param, char *path, - int projid, int d) + __u32 projid, int d) { char output[FORMATTED_BUF_LEN]; char *fmt_char = param->fp_format_printf_str; @@ -5104,14 +5111,16 @@ void printf_format_string(struct find_param *param, char *path, * by the open fd resides on. * Return 0 and project id on success, or -ve errno. */ -static int fget_projid(int fd, int *projid) +static int fget_projid(int fd, __u32 *projid) { struct fsxattr fsx; int rc; rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx); - if (rc) + if (rc) { + *projid = INVALID_PROJID; return -errno; + } *projid = fsx.fsx_projid; return 0; @@ -5159,7 +5168,7 @@ static int cb_find_init(char *path, int p, int *dp, __u32 stripe_count = 0; __u64 flags; int fd = -2; - int projid = 0; + __u32 projid = INVALID_PROJID; bool gather_all = false; if (p == -1 && d == -1) @@ -5496,8 +5505,6 @@ obd_matches: } if (param->fp_check_projid || gather_all) { - int projid = 0; - if (fd == -2) fd = open(path, O_RDONLY | O_NONBLOCK); -- 1.8.3.1