btrfs: prop: fix vanished compression property after failed set [Linux 3.16.72]

This Linux kernel change "btrfs: prop: fix vanished compression property after failed set" is included in the Linux 3.16.72 release. This change is authored by Anand Jain <anand.jain [at]> on Tue Apr 2 18:07:40 2019 +0800. The commit for this change in Linux stable tree is 7eaca00 (patch) which is from upstream commit 272e532. The same Linux upstream change may have been applied to various maintained Linux releases and you can find all Linux releases containing changes from upstream 272e532.

btrfs: prop: fix vanished compression property after failed set

commit 272e5326c7837697882ce3162029ba893059b616 upstream.

The compression property resets to NULL, instead of the old value if we
fail to set the new compression parameter.

  $ btrfs prop get /btrfs compression
  $ btrfs prop set /btrfs compression zli
    ERROR: failed to set compression for /btrfs: Invalid argument
  $ btrfs prop get /btrfs compression

This is because the compression property ->validate() is successful for
'zli' as the strncmp() used the length passed from the userspace.

Fix it by using the expected string length in strncmp().

Fixes: 63541927c8d1 ("Btrfs: add support for inode properties")
Fixes: 5c1aab1dd544 ("btrfs: Add zstd support")
Reviewed-by: Nikolay Borisov <>
Signed-off-by: Anand Jain <>
Reviewed-by: David Sterba <>
Signed-off-by: David Sterba <>
[bwh: Backported to 3.16: "zstd" is not supported]
Signed-off-by: Ben Hutchings <>

There are 4 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 fs/btrfs/props.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/props.c b/fs/btrfs/props.c
index 129b1dd..93b7f3e 100644
--- a/fs/btrfs/props.c
+++ b/fs/btrfs/props.c
@@ -378,9 +378,9 @@ int btrfs_subvol_inherit_props(struct btrfs_trans_handle *trans,

 static int prop_compression_validate(const char *value, size_t len)
-   if (!strncmp("lzo", value, len))
+   if (!strncmp("lzo", value, 3))
        return 0;
-   else if (!strncmp("zlib", value, len))
+   else if (!strncmp("zlib", value, 4))
        return 0;

    return -EINVAL;

Leave a Reply

Your email address will not be published. Required fields are marked *