btrfs: skip compression property for anything other than files and dirs
authorFilipe Manana <fdmanana@suse.com>
Thu, 21 Apr 2022 10:01:22 +0000 (11:01 +0100)
committerDavid Sterba <dsterba@suse.com>
Wed, 27 Apr 2022 20:20:21 +0000 (22:20 +0200)
The compression property only has effect on regular files and directories
(so that it's propagated to files and subdirectories created inside a
directory). For any other inode type (symlink, fifo, device, socket),
it's pointless to set the compression property because it does nothing
and ends up unnecessarily wasting leaf space due to the pointless xattr
(75 or 76 bytes, depending on the compression value). Symlinks in
particular are very common (for example, I have almost 10k symlinks under
/etc, /usr and /var alone) and therefore it's worth to avoid wasting
leaf space with the compression xattr.

For example, the compression property can end up on a symlink or character
device implicitly, through inheritance from a parent directory

  $ mkdir /mnt/testdir
  $ btrfs property set /mnt/testdir compression lzo

  $ ln -s yadayada /mnt/testdir/lnk
  $ mknod /mnt/testdir/dev c 0 0

Or explicitly like this:

  $ ln -s yadayda /mnt/lnk
  $ setfattr -h -n btrfs.compression -v lzo /mnt/lnk

So skip the compression property on inodes that are neither a regular
file nor a directory.

CC: stable@vger.kernel.org # 5.4+
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/props.c
fs/btrfs/props.h
fs/btrfs/xattr.c

index 5a6f87744c282909e987536bfe7ff498c20b1bde..1b31481f9e72c35fda104449ff6108ac5eee0872 100644 (file)
@@ -21,6 +21,7 @@ struct prop_handler {
                        size_t len);
        int (*apply)(struct inode *inode, const char *value, size_t len);
        const char *(*extract)(struct inode *inode);
+       bool (*ignore)(const struct btrfs_inode *inode);
        int inheritable;
 };
 
@@ -74,6 +75,28 @@ int btrfs_validate_prop(const struct btrfs_inode *inode, const char *name,
        return handler->validate(inode, value, value_len);
 }
 
+/*
+ * Check if a property should be ignored (not set) for an inode.
+ *
+ * @inode:     The target inode.
+ * @name:      The property's name.
+ *
+ * The caller must be sure the given property name is valid, for example by
+ * having previously called btrfs_validate_prop().
+ *
+ * Returns:    true if the property should be ignored for the given inode
+ *             false if the property must not be ignored for the given inode
+ */
+bool btrfs_ignore_prop(const struct btrfs_inode *inode, const char *name)
+{
+       const struct prop_handler *handler;
+
+       handler = find_prop_handler(name, NULL);
+       ASSERT(handler != NULL);
+
+       return handler->ignore(inode);
+}
+
 int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode,
                   const char *name, const char *value, size_t value_len,
                   int flags)
@@ -316,6 +339,22 @@ static int prop_compression_apply(struct inode *inode, const char *value,
        return 0;
 }
 
+static bool prop_compression_ignore(const struct btrfs_inode *inode)
+{
+       /*
+        * Compression only has effect for regular files, and for directories
+        * we set it just to propagate it to new files created inside them.
+        * Everything else (symlinks, devices, sockets, fifos) is pointless as
+        * it will do nothing, so don't waste metadata space on a compression
+        * xattr for anything that is neither a file nor a directory.
+        */
+       if (!S_ISREG(inode->vfs_inode.i_mode) &&
+           !S_ISDIR(inode->vfs_inode.i_mode))
+               return true;
+
+       return false;
+}
+
 static const char *prop_compression_extract(struct inode *inode)
 {
        switch (BTRFS_I(inode)->prop_compress) {
@@ -336,6 +375,7 @@ static struct prop_handler prop_handlers[] = {
                .validate = prop_compression_validate,
                .apply = prop_compression_apply,
                .extract = prop_compression_extract,
+               .ignore = prop_compression_ignore,
                .inheritable = 1
        },
 };
@@ -362,6 +402,9 @@ static int inherit_props(struct btrfs_trans_handle *trans,
                if (!h->inheritable)
                        continue;
 
+               if (h->ignore(BTRFS_I(inode)))
+                       continue;
+
                value = h->extract(parent);
                if (!value)
                        continue;
index 2b2ac15ab7882ea22d95436b9b76e403917bf8b5..59bea741cfcf433925faa316a4a9095a4cc973a7 100644 (file)
@@ -15,6 +15,7 @@ int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode,
                   int flags);
 int btrfs_validate_prop(const struct btrfs_inode *inode, const char *name,
                        const char *value, size_t value_len);
+bool btrfs_ignore_prop(const struct btrfs_inode *inode, const char *name);
 
 int btrfs_load_inode_props(struct inode *inode, struct btrfs_path *path);
 
index 367bb87b66df394394b275f83b89ecbbe722e7cc..85691dc2232fa60d6bd123c7bb7cf0ab8da549c5 100644 (file)
@@ -408,6 +408,9 @@ static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
        if (ret)
                return ret;
 
+       if (btrfs_ignore_prop(BTRFS_I(inode), name))
+               return 0;
+
        trans = btrfs_start_transaction(root, 2);
        if (IS_ERR(trans))
                return PTR_ERR(trans);