mbox series

[0/5] btrfs: Cleanup BTRFS_INODE_NOCOMPRESS usage and extend

Message ID 1642322897-1739-1-git-send-email-zhanglikernel@gmail.com (mailing list archive)
Headers show
Series btrfs: Cleanup BTRFS_INODE_NOCOMPRESS usage and extend | expand

Message

Li Zhang Jan. 16, 2022, 8:48 a.m. UTC
[RELATED ISSUES]
https://github.com/btrfs/btrfs-todo/issues/26
https://github.com/kdave/btrfs-progs/issues/329

[QUESION DESCRIBTION]
1. There are two ways to set BTRFS_INODE_NOCOMPRESS, 
one is user command setting, chattr +m <file>, etc., 
indicating that the file does not need to be compressed;
the other is to set this flag by file system heuristic algorithm,
the most common case is after compression The file gets bigger.
We need a flag to distinguish the two cases.

2. If the user runs the command "chattr +c <file>"
and then gets the properties of the file via 
"btrfs property get <file>", the user can know the 
compression type and level of the file, but if the 
user runs "chattr +m <file>" to disable compressing
the file, "btrfs property get <file>" will return an
empty string, the user does not know if the file is
set by the property to not compress.

3. The user can set the compression type and level of
the file through the command
"btrfs property set <file> compression <compression_type>:<compression_level>",
but the compression_level does not seem to take effect.
Because the btrfs_inode structure only contains
the member prop_compress to record the compression type,
not the compression level.

4. There is redundant code in super.c and props.c
to parse the compressed string into a system flag,
so some cleanup is required.

[SOLUTION]
1. Introduce memory-only flag BTRFS_INODE_HEURISTIC_NOCOMPRESS,
which will only remain in memory. This flag should be unset
every time filesystem write to disk.

2. If the flag BTRFS_NOCOMPRESS is set,
insert compression "none" to the file xattr.

3. Extend the btrfs_inode struct member prop_compress
to use the lowest 4 bits to record the compression type and
5 - 8 bits to record the compression level.

4. In both the files super.c and props.c there is a function that converts
the compression description string to system flags(btrfs_parse_options for
super.c, prop_compression_apply for props.c), move it to compress.c,
my options are compress.c except for the compression type and level flags,
no Need to know the other flags about filesystem,
so I left the compress none property and the incompat flag in super.c and props.c.

[TEST PROGRAM]
After I add patch, it use following two way to test wether my patch is correctly
1.For BTRFS_INODE_HEURISTIC_NOCOMPRESS, to be honest,
I tested a lot of data, but I didn't find any data to trigger this flag,
because the condition for setting this flag is that the compressed file
is larger than the original after compression,
but I found that this function shannon_entropy has done the filtering Work.

2. Run chattr +m <file>, get the attributes of <file>, and find that
xattr compression=none.

3. First I test all compression types and compression levels,
I write the file to the btrfs system (print the compression type
and level with btrfs_info to make sure both values are valid), then
use diff <btrfs_file> <original file>, it tells me both file is the same.
Also, I compress the file with the btrfs filesystem version before adding the patch,
and then read it through the patched version,
the file can be read without errors and data corruption.

[TODO]
1. Extend btrfs_inode struct member defrag_compress to record compression type and level.

Li Zhang (5):
    btrfs: Introduce memory-only flag BTRFS_INODE_HEURISTIC_NOCOMPRESS.
    btrfs: Introduce helper functions for compression.
    btrfs: Convert compression description strings into system flags.
    btrfs: Synchronize compression flag BTRFS_INODE_NOCOMPRESS with xattr.
    
 fs/btrfs/compression.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/compression.h | 14 ++++++++++++++
 fs/btrfs/ctree.h       | 13 ++++++++++++-
 fs/btrfs/inode.c       | 27 ++++++++++++++++++---------
 fs/btrfs/ioctl.c       | 36 ++++++++++++++++++++++++++----------
 fs/btrfs/props.c       | 52 ++++++++++++++++++++++++++++++++++++----------------
 fs/btrfs/super.c       | 89 ++++++++++++++++++++++++++++++++++++++---------------------------------------------------
 fs/btrfs/tree-log.c    |  8 ++++++--

Yours sincerely
Li Zhang