From: Andreas Dilger Date: Tue, 31 Mar 2020 22:45:40 +0000 (-0600) Subject: LU-13404 utils: fix lfs mirror duplicate file check X-Git-Tag: 2.13.54~194 X-Git-Url: https://git.whamcloud.com/?p=fs%2Flustre-release.git;a=commitdiff_plain;h=8befc64e5aabe9b9bdca49f51830f25b3f747019 LU-13404 utils: fix lfs mirror duplicate file check Fix the check_same_file() check to work if the output file does not exist. Since the file is opened with O_CREAT|O_EXCL then definitely it cannot be the same as the input file. Add test_0j to verify "lfs mirror read -o" and "lfs mirror write -i". Test-Parameters: trivial testlist=sanity-flr env=ONLY=0j Signed-off-by: Andreas Dilger Change-Id: Ide929b53a528bd0cc77e1f997d3c561c67729179 Reviewed-on: https://review.whamcloud.com/38108 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Jian Yu Reviewed-by: James Nunez Reviewed-by: Olaf Faaland-LLNL Reviewed-by: Oleg Drokin --- diff --git a/lustre/tests/sanity-flr.sh b/lustre/tests/sanity-flr.sh index d76e2da..07e52af 100644 --- a/lustre/tests/sanity-flr.sh +++ b/lustre/tests/sanity-flr.sh @@ -767,6 +767,24 @@ test_0h() { } run_test 0h "set, clear and test flags for FLR files" +test_0j() { + $LFS mirror create -N2 $DIR/$tfile || error "create $DIR/$tfile failed" + + cp /etc/hosts $DIR/$tfile || error "write to $DIR/$tfile failed" + $LFS mirror resync $DIR/$tfile || error "resync $DIR/$tfile failed" + cmp /etc/hosts $DIR/$tfile || error "cmp with /etc/hosts failed" + + $LFS mirror read -N2 -o $TMP/$tfile $DIR/$tfile || "read mirror failed" + stack_trap "rm -f $TMP/$tfile" + cmp $TMP/$tfile $DIR/$tfile || error "cmp with $TMP/$tfile failed" + $LFS mirror write -N2 -i /etc/passwd $DIR/$tfile || "write failed" + $LFS setstripe --comp-set -I 65537 --comp-flags=stale $DIR/$tfile || + error "set component 1 stale failed" + $LFS mirror resync $DIR/$tfile || error "resync $DIR/$tfile failed" + cmp /etc/passwd $DIR/$tfile || error "cmp with /etc/passwd failed" +} +run_test 0j "test lfs mirror read/write commands" + test_1() { local tf=$DIR/$tfile local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX diff --git a/lustre/utils/lfs.c b/lustre/utils/lfs.c index a1198e0..44f2653 100644 --- a/lustre/utils/lfs.c +++ b/lustre/utils/lfs.c @@ -9458,22 +9458,16 @@ static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id) * \retval 1 not the same file * \retval <0 error code */ -static inline int check_same_file(const char *f1, const char *f2) +static inline int check_same_file(int fd, const char *f2) { struct stat stbuf1; struct stat stbuf2; - if (stat(f1, &stbuf1) < 0) { - fprintf(stderr, "%s: cannot stat file '%s': %s\n", - progname, f1, strerror(errno)); + if (fstat(fd, &stbuf1) < 0) return -errno; - } - if (stat(f2, &stbuf2) < 0) { - fprintf(stderr, "%s: cannot stat file '%s': %s\n", - progname, f2, strerror(errno)); - return -errno; - } + if (stat(f2, &stbuf2) < 0) + return 1; if (stbuf1.st_rdev == stbuf2.st_rdev && stbuf1.st_ino == stbuf2.st_ino) @@ -9539,19 +9533,6 @@ static inline int lfs_mirror_read(int argc, char **argv) /* open mirror file */ fname = argv[optind]; - - if (outfile) { - rc = check_same_file(fname, outfile); - if (rc == 0) { - fprintf(stderr, - "%s %s: output file cannot be the mirrored file\n", - progname, argv[0]); - return -EINVAL; - } - if (rc < 0) - return rc; - } - fd = open(fname, O_DIRECT | O_RDONLY); if (fd < 0) { fprintf(stderr, "%s %s: cannot open '%s': %s\n", @@ -9568,7 +9549,7 @@ static inline int lfs_mirror_read(int argc, char **argv) goto close_fd; } - /* open output file */ + /* open output file - O_EXCL ensures output is not the same as input */ if (outfile) { outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644); if (outfd < 0) { @@ -9708,19 +9689,6 @@ static inline int lfs_mirror_write(int argc, char **argv) /* open mirror file */ fname = argv[optind]; - - if (inputfile) { - rc = check_same_file(fname, inputfile); - if (rc == 0) { - fprintf(stderr, - "%s %s: input file cannot be the mirrored file\n", - progname, argv[0]); - return -EINVAL; - } - if (rc < 0) - return rc; - } - fd = open(fname, O_DIRECT | O_WRONLY); if (fd < 0) { fprintf(stderr, "%s %s: cannot open '%s': %s\n", @@ -9739,6 +9707,16 @@ static inline int lfs_mirror_write(int argc, char **argv) /* open input file */ if (inputfile) { + rc = check_same_file(fd, inputfile); + if (rc == 0) { + fprintf(stderr, + "%s %s: input file cannot be the mirrored file\n", + progname, argv[0]); + goto close_fd; + } + if (rc < 0) + goto close_fd; + inputfd = open(inputfile, O_RDONLY, 0644); if (inputfd < 0) { fprintf(stderr, "%s %s: cannot open file '%s': %s\n",