Skip to contentSkip to conversation

xfs: test delalloc quota leak when chprojid fails

PATCH In review Darrick J. Wong 3 participants 5 messages Feb 2, 2021 – Feb 3, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: test delalloc quota leak when chprojid fails

From: Darrick J. Wong <[email protected]>

This is a regression test for a bug in the XFS implementation of FSSETXATTR. When we try to change a file's project id, the quota reservation code will update the incore quota reservations for delayed allocation blocks. Unfortunately, it does this before we finish validating all the FSSETXATTR parameters, which means that if we decide to bail out, we also fail to undo the incore changes.

---
 .gitignore          |    1 +
 src/Makefile        |    3 +-
 src/chprojid_fail.c |   86 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/xfs/765       |   63 +++++++++++++++++++++++++++++++++++++
 tests/xfs/765.out   |    3 ++
 tests/xfs/group     |    1 +
 6 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 src/chprojid_fail.c
 create mode 100755 tests/xfs/765
 create mode 100644 tests/xfs/765.out
.gitignore +1 -0
index f988a44a..6d5c4ba6 100644
@@ -58,6 +58,7 @@
 /src/bulkstat_null_ocount
 /src/bulkstat_unlink_test
 /src/bulkstat_unlink_test_modified
+/src/chprojid_fail
 /src/cloner
 /src/dbtest
 /src/devzero
src/Makefile +2 -1
index 28789915..435c1e8a 100644
@@ -29,7 +29,8 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	attr-list-by-handle-cursor-test listxattr dio-interleaved t_dir_type \
 	dio-invalidate-cache stat_test t_encrypted_d_revalidate \
 	attr_replace_test swapon mkswap t_attr_corruption t_open_tmpfiles \
-	fscrypt-crypt-util bulkstat_null_ocount splice-test immutable_write
+	fscrypt-crypt-util bulkstat_null_ocount splice-test immutable_write \
+	chprojid_fail
 
 SUBDIRS = log-writes perf
 
src/chprojid_fail.c new file +86 -0

86 lines not on this page · raw message

tests/xfs/765 new file +63 -0

63 lines not on this page · raw message

tests/xfs/765.out new file +3 -0
new file mode 100644
index 00000000..f44ba43e
@@ -0,0 +1,3 @@
+QA output created by 765
+Format filesystem
+Run test program
tests/xfs/group +1 -0
index f406a9b9..fb78b0d7 100644
@@ -544,6 +544,7 @@
 762 auto quick rw scrub realtime
 763 auto quick rw realtime
 764 auto quick repair
+765 auto quick quota
 908 auto quick bigtime
 909 auto quick bigtime quota
 910 auto quick inobtcount
#1 lore raw
Quoted 11 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

This is a regression test for a bug in the XFS implementation of FSSETXATTR. When we try to change a file's project id, the quota reservation code will update the incore quota reservations for delayed allocation blocks. Unfortunately, it does this before we finish validating all the FSSETXATTR parameters, which means that if we decide to bail out, we also fail to undo the incore changes.

---

Looks like this patch comes from djwong-devel :) It can't be merged into xfstests-dev mainline directly.

Applying: xfs: test delalloc quota leak when chprojid fails
error: patch failed: src/Makefile:29
error: src/Makefile: patch does not apply
error: patch failed: tests/xfs/group:544
error: tests/xfs/group: patch does not apply

Anyway, I think Eryu can solve it. I just have another question below.

Quoted 11 lines
 .gitignore          |    1 +
 src/Makefile        |    3 +-
 src/chprojid_fail.c |   86 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/xfs/765       |   63 +++++++++++++++++++++++++++++++++++++
 tests/xfs/765.out   |    3 ++
 tests/xfs/group     |    1 +
 6 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 src/chprojid_fail.c
 create mode 100755 tests/xfs/765
 create mode 100644 tests/xfs/765.out

[snip]

Quoted 21 lines
tests/xfs/765 new file +21 -0
+# FS QA Test No. 765
+#
+# Regression test for failing to undo delalloc quota reservations when changing
+# project id but we fail some other part of FSSETXATTR validation.  If we fail
+# the test, we trip debugging assertions in dmesg.
+#
+# The appropriate XFS patch is:
+# xfs: fix chown leaking delalloc quota blocks when fssetxattr fails
+
+seq=`basename $0`
+seqres=$RESULT_DIR/$seq
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1    # failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+_cleanup()
+{
+	cd /

tests/xfs/765 line 35

Quoted 10 lines
tests/xfs/765 new file +10 -0
+	rm -f $tmp.*
+}
+
+# get standard environment, filters and checks
+. ./common/rc
+. ./common/quota
+
+# real QA test starts here
+_supported_fs xfs
+_require_xfs_debug

Why CONFIG_XFS_DEBUG=y is necessary for this case? Maybe I miss something, but I didn't see any debug injection in this case. If a kernel is only built with CONFIG_XFS_WARN=y, I still can reproduce this bug [1].

Even if the XFS_WARN and XFS_DEUBG are all unset, I think this case can be run without any failures. So why not just let it run? We could recommand using debug xfs kernel, but general kernel don't need to skip this test.

Thanks,
Zorro

[1]

# ./check xfs/765
FSTYP         -- xfs (non-debug)
PLATFORM      -- Linux/x86_64 hp-dl380pg8-01 4.18.0 #1 SMP Sat Jan 23 14:15:14 EST 2021
MKFS_OPTIONS  -- -f -m reflink=1,rmapbt=1 /dev/mapper/xfscratch
MOUNT_OPTIONS -- -o context=system_u:object_r:root_t:s0 /dev/mapper/xfscratch /mnt/scratch

xfs/765 _check_dmesg: something found in dmesg (see /home/xfstests-dev/results//xfs/765.dmesg)

Ran: xfs/765
Failures: xfs/765
Failed 1 of 1 tests

[root@hp-dl380pg8-01 xfstests-dev]# less /home/xfstests-dev/results//xfs/765.dmesg

[30020.559403] run fstests xfs/765 at 2021-02-03 10:36:02
...
[30028.411797] XFS: Assertion failed: dqp->q_res_bcount >= be64_to_cpu(dqp->q_core.d_bcount), file: fs/xfs/xfs_trans_dquot.c, line: 471
Quoted 50 lines
tests/xfs/765 new file +28 -0
+_require_command "$FILEFRAG_PROG" filefrag
+_require_test_program "chprojid_fail"
+_require_quota
+_require_scratch
+
+rm -f $seqres.full
+
+echo "Format filesystem" | tee -a $seqres.full
+_scratch_mkfs > $seqres.full
+_qmount_option 'prjquota'
+_qmount
+_require_prjquota $SCRATCH_DEV
+
+echo "Run test program"
+$XFS_QUOTA_PROG -f -x -c 'report -ap' $SCRATCH_MNT >> $seqres.full
+$here/src/chprojid_fail $SCRATCH_MNT/blah >> $seqres.full
+res=$?
+if [ $res -ne 0 ]; then
+	echo "chprojid_fail returned $res, expected 0"
+fi
+$XFS_QUOTA_PROG -f -x -c 'report -ap' $SCRATCH_MNT >> $seqres.full
+$FILEFRAG_PROG -v $SCRATCH_MNT/blah >> $seqres.full
+$FILEFRAG_PROG -v $SCRATCH_MNT/blah 2>&1 | grep -q delalloc || \
+	echo "file didn't get delalloc extents?"
+
+# success, all done
+status=0
+exit
tests/xfs/765.out new file +3 -0
new file mode 100644
index 00000000..f44ba43e
@@ -0,0 +1,3 @@
+QA output created by 765
+Format filesystem
+Run test program
tests/xfs/group +1 -0
index f406a9b9..fb78b0d7 100644
@@ -544,6 +544,7 @@
 762 auto quick rw scrub realtime
 763 auto quick rw realtime
 764 auto quick repair
+765 auto quick quota
 908 auto quick bigtime
 909 auto quick bigtime quota
 910 auto quick inobtcount
#2 lore raw
Quoted 10 lines · Zorro Lang
Quoted 9 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

This is a regression test for a bug in the XFS implementation of FSSETXATTR. When we try to change a file's project id, the quota reservation code will update the incore quota reservations for delayed allocation blocks. Unfortunately, it does this before we finish validating all the FSSETXATTR parameters, which means that if we decide to bail out, we also fail to undo the incore changes.

Quoted 5 lines
Quoted 2 lines

---

Looks like this patch comes from djwong-devel :) It can't be merged into xfstests-dev mainline directly.

Yeah, applying patches to fstests is annoying like that. :(

Quoted 49 lines

Applying: xfs: test delalloc quota leak when chprojid fails
error: patch failed: src/Makefile:29
error: src/Makefile: patch does not apply
error: patch failed: tests/xfs/group:544
error: tests/xfs/group: patch does not apply

Anyway, I think Eryu can solve it. I just have another question below.

Quoted 11 lines
 .gitignore          |    1 +
 src/Makefile        |    3 +-
 src/chprojid_fail.c |   86 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/xfs/765       |   63 +++++++++++++++++++++++++++++++++++++
 tests/xfs/765.out   |    3 ++
 tests/xfs/group     |    1 +
 6 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 src/chprojid_fail.c
 create mode 100755 tests/xfs/765
 create mode 100644 tests/xfs/765.out

[snip]

Quoted 27 lines
tests/xfs/765 new file +27 -0
+# FS QA Test No. 765
+#
+# Regression test for failing to undo delalloc quota reservations when changing
+# project id but we fail some other part of FSSETXATTR validation.  If we fail
+# the test, we trip debugging assertions in dmesg.
+#
+# The appropriate XFS patch is:
+# xfs: fix chown leaking delalloc quota blocks when fssetxattr fails
+
+seq=`basename $0`
+seqres=$RESULT_DIR/$seq
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1    # failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+_cleanup()
+{
+	cd /
+	rm -f $tmp.*
+}
+
+# get standard environment, filters and checks
+. ./common/rc
+. ./common/quota
Quoted 8 lines

tests/xfs/765 line 35

Quoted 4 lines
tests/xfs/765 new file +4 -0
+
+# real QA test starts here
+_supported_fs xfs
+_require_xfs_debug

Why CONFIG_XFS_DEBUG=y is necessary for this case? Maybe I miss something, but I didn't see any debug injection in this case. If a kernel is only built with CONFIG_XFS_WARN=y, I still can reproduce this bug [1].

Heh, I forgot that asserts can trigger even if debug is disabled.

Quoted 3 lines

Even if the XFS_WARN and XFS_DEUBG are all unset, I think this case can be run without any failures. So why not just let it run? We could recommand using debug xfs kernel, but general kernel don't need to skip this test.

Ok. I'll get rid of it then.

--D

Quoted 72 lines

Thanks,
Zorro

[1]

# ./check xfs/765
FSTYP         -- xfs (non-debug)
PLATFORM      -- Linux/x86_64 hp-dl380pg8-01 4.18.0 #1 SMP Sat Jan 23 14:15:14 EST 2021
MKFS_OPTIONS  -- -f -m reflink=1,rmapbt=1 /dev/mapper/xfscratch
MOUNT_OPTIONS -- -o context=system_u:object_r:root_t:s0 /dev/mapper/xfscratch /mnt/scratch

xfs/765 _check_dmesg: something found in dmesg (see /home/xfstests-dev/results//xfs/765.dmesg)

Ran: xfs/765
Failures: xfs/765
Failed 1 of 1 tests

[root@hp-dl380pg8-01 xfstests-dev]# less /home/xfstests-dev/results//xfs/765.dmesg

[30020.559403] run fstests xfs/765 at 2021-02-03 10:36:02
...
[30028.411797] XFS: Assertion failed: dqp->q_res_bcount >= be64_to_cpu(dqp->q_core.d_bcount), file: fs/xfs/xfs_trans_dquot.c, line: 471
Quoted 50 lines
tests/xfs/765 new file +28 -0
+_require_command "$FILEFRAG_PROG" filefrag
+_require_test_program "chprojid_fail"
+_require_quota
+_require_scratch
+
+rm -f $seqres.full
+
+echo "Format filesystem" | tee -a $seqres.full
+_scratch_mkfs > $seqres.full
+_qmount_option 'prjquota'
+_qmount
+_require_prjquota $SCRATCH_DEV
+
+echo "Run test program"
+$XFS_QUOTA_PROG -f -x -c 'report -ap' $SCRATCH_MNT >> $seqres.full
+$here/src/chprojid_fail $SCRATCH_MNT/blah >> $seqres.full
+res=$?
+if [ $res -ne 0 ]; then
+	echo "chprojid_fail returned $res, expected 0"
+fi
+$XFS_QUOTA_PROG -f -x -c 'report -ap' $SCRATCH_MNT >> $seqres.full
+$FILEFRAG_PROG -v $SCRATCH_MNT/blah >> $seqres.full
+$FILEFRAG_PROG -v $SCRATCH_MNT/blah 2>&1 | grep -q delalloc || \
+	echo "file didn't get delalloc extents?"
+
+# success, all done
+status=0
+exit
tests/xfs/765.out new file +3 -0
new file mode 100644
index 00000000..f44ba43e
@@ -0,0 +1,3 @@
+QA output created by 765
+Format filesystem
+Run test program
tests/xfs/group +1 -0
index f406a9b9..fb78b0d7 100644
@@ -544,6 +544,7 @@
 762 auto quick rw scrub realtime
 763 auto quick rw realtime
 764 auto quick repair
+765 auto quick quota
 908 auto quick bigtime
 909 auto quick bigtime quota
 910 auto quick inobtcount
#3 lore raw
In reply to Darrick J. Wong
Quoted 22 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

This is a regression test for a bug in the XFS implementation of FSSETXATTR. When we try to change a file's project id, the quota reservation code will update the incore quota reservations for delayed allocation blocks. Unfortunately, it does this before we finish validating all the FSSETXATTR parameters, which means that if we decide to bail out, we also fail to undo the incore changes.

---
 .gitignore          |    1 +
 src/Makefile        |    3 +-
 src/chprojid_fail.c |   86 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/xfs/765       |   63 +++++++++++++++++++++++++++++++++++++
 tests/xfs/765.out   |    3 ++
 tests/xfs/group     |    1 +
 6 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 src/chprojid_fail.c
 create mode 100755 tests/xfs/765
 create mode 100644 tests/xfs/765.out

...

Quoted 63 lines
src/chprojid_fail.c new file +57 -0
new file mode 100644
index 00000000..e7467372
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Oracle.  All Rights Reserved.
+ * Author: Darrick J. Wong <[email protected]>
+ *
+ * Regression test for failing to undo delalloc quota reservations when
+ * changing project id and we fail some other FSSETXATTR validation.
+ */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <linux/fs.h>
+
+static char zerobuf[65536];
+
+int
+main(
+	int		argc,
+	char		*argv[])
+{
+	struct fsxattr	fa;
+	ssize_t		sz;
+	int		fd, ret;
+
+	if (argc < 2) {
+		printf("Usage: %s filename\n", argv[0]);
+		return 1;
+	}
+
+	fd = open(argv[1], O_CREAT | O_TRUNC | O_RDWR, 0600);
+	if (fd < 0) {
+		perror(argv[1]);
+		return 2;
+	}
+
+	/* Zero the project id and the extent size hint. */
+	ret = ioctl(fd, FS_IOC_FSGETXATTR, &fa);
+	if (ret) {
+		perror("FSGETXATTR check file");
+		return 2;
+	}
+
+	if (fa.fsx_projid != 0 || fa.fsx_extsize != 0) {
+		fa.fsx_projid = 0;
+		fa.fsx_extsize = 0;
+		ret = ioctl(fd, FS_IOC_FSSETXATTR, &fa);
+		if (ret) {
+			perror("FSSETXATTR zeroing");
+			return 2;
+		}
+	}
+

src/chprojid_fail.c line 67

Quoted 10 lines
src/chprojid_fail.c new file +10 -0
+	/* Dirty a few kb of a file to create delalloc extents. */
+	sz = write(fd, zerobuf, sizeof(zerobuf));
+	if (sz != sizeof(zerobuf)) {
+		perror("delalloc write");
+		return 2;
+	}
+
+	/*
+	 * Fail to chprojid and set an extent size hint after we wrote the file.
+	 */

Might be helpful to document exactly why this command should fail.

Quoted 50 lines
src/chprojid_fail.c new file +19 -0
+	ret = ioctl(fd, FS_IOC_FSGETXATTR, &fa);
+	if (ret) {
+		perror("FSGETXATTR");
+		return 2;
+	}
+
+	fa.fsx_projid = 23652;
+	fa.fsx_extsize = 2;
+	fa.fsx_xflags |= FS_XFLAG_EXTSIZE;
+
+	ret = ioctl(fd, FS_IOC_FSSETXATTR, &fa);
+	if (ret) {
+		printf("FSSETXATTRR should fail: %s\n", strerror(errno));
+		return 0;
+	}
+
+	/* Uhoh, that FSSETXATTR call should have failed! */
+	return 3;
+}
tests/xfs/765 new file +25 -0
new file mode 100755
index 00000000..769b545b
@@ -0,0 +1,63 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2021 Oracle.  All Rights Reserved.
+#
+# FS QA Test No. 765
+#
+# Regression test for failing to undo delalloc quota reservations when changing
+# project id but we fail some other part of FSSETXATTR validation.  If we fail
+# the test, we trip debugging assertions in dmesg.
+#
+# The appropriate XFS patch is:
+# xfs: fix chown leaking delalloc quota blocks when fssetxattr fails
+
+seq=`basename $0`
+seqres=$RESULT_DIR/$seq
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1    # failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+_cleanup()
+{
+	cd /

tests/xfs/765 line 35

Quoted 10 lines
tests/xfs/765 new file +10 -0
+	rm -f $tmp.*
+}
+
+# get standard environment, filters and checks
+. ./common/rc
+. ./common/quota
+
+# real QA test starts here
+_supported_fs xfs
+_require_xfs_debug

I agree with Zorro that we might as well just let this run with or without debug mode..

Quoted 10 lines
tests/xfs/765 new file +10 -0
+_require_command "$FILEFRAG_PROG" filefrag
+_require_test_program "chprojid_fail"
+_require_quota
+_require_scratch
+
+rm -f $seqres.full
+
+echo "Format filesystem" | tee -a $seqres.full
+_scratch_mkfs > $seqres.full
+_qmount_option 'prjquota'

tests/xfs/765 line 55

Quoted 10 lines
tests/xfs/765 new file +10 -0
+_qmount
+_require_prjquota $SCRATCH_DEV
+
+echo "Run test program"
+$XFS_QUOTA_PROG -f -x -c 'report -ap' $SCRATCH_MNT >> $seqres.full
+$here/src/chprojid_fail $SCRATCH_MNT/blah >> $seqres.full
+res=$?
+if [ $res -ne 0 ]; then
+	echo "chprojid_fail returned $res, expected 0"
+fi

Can we just use the output from the test program as the golden test output?

tests/xfs/765 line 59

Quoted 4 lines
tests/xfs/765 new file +4 -0
+$XFS_QUOTA_PROG -f -x -c 'report -ap' $SCRATCH_MNT >> $seqres.full
+$FILEFRAG_PROG -v $SCRATCH_MNT/blah >> $seqres.full
+$FILEFRAG_PROG -v $SCRATCH_MNT/blah 2>&1 | grep -q delalloc || \
+	echo "file didn't get delalloc extents?"

This could technically cause a failure even if the test did the right thing if writeback happens to occur, right? I suppose that's exceedingly unlikely, but might at least be worth documenting in the event that somebody has to deal with that scenario sometime in the future.

Brian

Quoted 26 lines
tests/xfs/765 new file +4 -0
+
+# success, all done
+status=0
+exit
tests/xfs/765.out new file +3 -0
new file mode 100644
index 00000000..f44ba43e
@@ -0,0 +1,3 @@
+QA output created by 765
+Format filesystem
+Run test program
tests/xfs/group +1 -0
index f406a9b9..fb78b0d7 100644
@@ -544,6 +544,7 @@
 762 auto quick rw scrub realtime
 763 auto quick rw realtime
 764 auto quick repair
+765 auto quick quota
 908 auto quick bigtime
 909 auto quick bigtime quota
 910 auto quick inobtcount
#4 lore raw
Quoted 91 lines · Brian Foster
Quoted 22 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

This is a regression test for a bug in the XFS implementation of FSSETXATTR. When we try to change a file's project id, the quota reservation code will update the incore quota reservations for delayed allocation blocks. Unfortunately, it does this before we finish validating all the FSSETXATTR parameters, which means that if we decide to bail out, we also fail to undo the incore changes.

---
 .gitignore          |    1 +
 src/Makefile        |    3 +-
 src/chprojid_fail.c |   86 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/xfs/765       |   63 +++++++++++++++++++++++++++++++++++++
 tests/xfs/765.out   |    3 ++
 tests/xfs/group     |    1 +
 6 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 src/chprojid_fail.c
 create mode 100755 tests/xfs/765
 create mode 100644 tests/xfs/765.out

...

Quoted 67 lines
src/chprojid_fail.c new file +61 -0
new file mode 100644
index 00000000..e7467372
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Oracle.  All Rights Reserved.
+ * Author: Darrick J. Wong <[email protected]>
+ *
+ * Regression test for failing to undo delalloc quota reservations when
+ * changing project id and we fail some other FSSETXATTR validation.
+ */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <linux/fs.h>
+
+static char zerobuf[65536];
+
+int
+main(
+	int		argc,
+	char		*argv[])
+{
+	struct fsxattr	fa;
+	ssize_t		sz;
+	int		fd, ret;
+
+	if (argc < 2) {
+		printf("Usage: %s filename\n", argv[0]);
+		return 1;
+	}
+
+	fd = open(argv[1], O_CREAT | O_TRUNC | O_RDWR, 0600);
+	if (fd < 0) {
+		perror(argv[1]);
+		return 2;
+	}
+
+	/* Zero the project id and the extent size hint. */
+	ret = ioctl(fd, FS_IOC_FSGETXATTR, &fa);
+	if (ret) {
+		perror("FSGETXATTR check file");
+		return 2;
+	}
+
+	if (fa.fsx_projid != 0 || fa.fsx_extsize != 0) {
+		fa.fsx_projid = 0;
+		fa.fsx_extsize = 0;
+		ret = ioctl(fd, FS_IOC_FSSETXATTR, &fa);
+		if (ret) {
+			perror("FSSETXATTR zeroing");
+			return 2;
+		}
+	}
+
+	/* Dirty a few kb of a file to create delalloc extents. */
+	sz = write(fd, zerobuf, sizeof(zerobuf));
+	if (sz != sizeof(zerobuf)) {
+		perror("delalloc write");
Quoted 8 lines

src/chprojid_fail.c line 67

Quoted 6 lines
src/chprojid_fail.c new file +6 -0
+		return 2;
+	}
+
+	/*
+	 * Fail to chprojid and set an extent size hint after we wrote the file.
+	 */

Might be helpful to document exactly why this command should fail.

Fixed.

Quoted 55 lines
Quoted 55 lines
src/chprojid_fail.c new file +19 -0
+	ret = ioctl(fd, FS_IOC_FSGETXATTR, &fa);
+	if (ret) {
+		perror("FSGETXATTR");
+		return 2;
+	}
+
+	fa.fsx_projid = 23652;
+	fa.fsx_extsize = 2;
+	fa.fsx_xflags |= FS_XFLAG_EXTSIZE;
+
+	ret = ioctl(fd, FS_IOC_FSSETXATTR, &fa);
+	if (ret) {
+		printf("FSSETXATTRR should fail: %s\n", strerror(errno));
+		return 0;
+	}
+
+	/* Uhoh, that FSSETXATTR call should have failed! */
+	return 3;
+}
tests/xfs/765 new file +30 -0
new file mode 100755
index 00000000..769b545b
@@ -0,0 +1,63 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2021 Oracle.  All Rights Reserved.
+#
+# FS QA Test No. 765
+#
+# Regression test for failing to undo delalloc quota reservations when changing
+# project id but we fail some other part of FSSETXATTR validation.  If we fail
+# the test, we trip debugging assertions in dmesg.
+#
+# The appropriate XFS patch is:
+# xfs: fix chown leaking delalloc quota blocks when fssetxattr fails
+
+seq=`basename $0`
+seqres=$RESULT_DIR/$seq
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1    # failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+_cleanup()
+{
+	cd /
+	rm -f $tmp.*
+}
+
+# get standard environment, filters and checks
+. ./common/rc
Quoted 8 lines

tests/xfs/765 line 35

Quoted 5 lines
tests/xfs/765 new file +5 -0
+. ./common/quota
+
+# real QA test starts here
+_supported_fs xfs
+_require_xfs_debug

I agree with Zorro that we might as well just let this run with or without debug mode..

<nod> Fixed.

Quoted 15 lines
Quoted 15 lines
tests/xfs/765 new file +15 -0
+_require_command "$FILEFRAG_PROG" filefrag
+_require_test_program "chprojid_fail"
+_require_quota
+_require_scratch
+
+rm -f $seqres.full
+
+echo "Format filesystem" | tee -a $seqres.full
+_scratch_mkfs > $seqres.full
+_qmount_option 'prjquota'
+_qmount
+_require_prjquota $SCRATCH_DEV
+
+echo "Run test program"
+$XFS_QUOTA_PROG -f -x -c 'report -ap' $SCRATCH_MNT >> $seqres.full
Quoted 8 lines

tests/xfs/765 line 55

Quoted 5 lines
tests/xfs/765 new file +5 -0
+$here/src/chprojid_fail $SCRATCH_MNT/blah >> $seqres.full
+res=$?
+if [ $res -ne 0 ]; then
+	echo "chprojid_fail returned $res, expected 0"
+fi

Can we just use the output from the test program as the golden test output?

Hm, I guess so? Not sure I want to encode the exact failure in the test, but I don't see why not.

Admittedly it's done that way because originally I was going to wire this up as a generic test, but then noticed that not all the other fssetxattr implementations actually error out on nonzero extent size and decided to pull this back into tests/xfs/.

Quoted 9 lines

tests/xfs/765 line 59

Quoted 4 lines
tests/xfs/765 new file +4 -0
+$XFS_QUOTA_PROG -f -x -c 'report -ap' $SCRATCH_MNT >> $seqres.full
+$FILEFRAG_PROG -v $SCRATCH_MNT/blah >> $seqres.full
+$FILEFRAG_PROG -v $SCRATCH_MNT/blah 2>&1 | grep -q delalloc || \
+	echo "file didn't get delalloc extents?"

This could technically cause a failure even if the test did the right thing if writeback happens to occur, right? I suppose that's exceedingly unlikely, but might at least be worth documenting in the event that somebody has to deal with that scenario sometime in the future.

Yeah. I'll add a comment explaining that the delalloc grep is to check that we even set up the test correctly.

--D

Quoted 30 lines

Brian

Quoted 26 lines
tests/xfs/765 new file +4 -0
+
+# success, all done
+status=0
+exit
tests/xfs/765.out new file +3 -0
new file mode 100644
index 00000000..f44ba43e
@@ -0,0 +1,3 @@
+QA output created by 765
+Format filesystem
+Run test program
tests/xfs/group +1 -0
index f406a9b9..fb78b0d7 100644
@@ -544,6 +544,7 @@
 762 auto quick rw scrub realtime
 763 auto quick rw realtime
 764 auto quick repair
+765 auto quick quota
 908 auto quick bigtime
 909 auto quick bigtime quota
 910 auto quick inobtcount