Skip to content

btrfs-progs: new --inode-flags option #987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: devel
Choose a base branch
from

Conversation

adam900710
Copy link
Collaborator

The new --inode-flags option allows us to specify certain btrfs specific
flags to each inode.

Currently we only support nodatacow and nodatasum.

But in the future compression flag can also be added, allowing more
accurate per-file compression.

Furthermore child inodes will inherit the flag from their parents,
meaning one only needs to specify the flag to the parent directory, then
all children files/directories will have the flag.

This new option also works well with --subvol, although one has to
note that, the inode flag inheritance does not cross subvolume boundary
(the same as the kernel).

Finally, nodatacow and nodatasum will disable compression, just like the
kernel.

I hope this will helps projects like mkosi and yocto to build their
images with more btrfs specific flags.

The only downside for now is the memory usage, both --subvol and
--inode-flags will take over 8K for each specification.
So one should not specify them too many times.

Inside btrfs-progs (mostly 'mkfs/rootdir.c') new inodes are created in a
different way than the kernel:

- A new orphan inode item is created
  Without knowing the parent inode, thus it will always have the default
  flags (0).

- Link the new inode to a parent

Meanwhile inside the kernel, we know exactly the parent inode at new
inode creation time, and can inherit the inode flags
(NODATACOW/NODATASUM/COMPRESS/etc).

Address the missing ability by:

- Inherit the parent inode flags when linking an orphan inode
  The function btrfs_add_link() is called when linking an inode.

  It can be called to creating the initial link if it's a new and orphan
  inode.
  It can also be called to creating extra hard links.

  If the inode is already orphan, we know it's newly created and should
  inherit the inode flag from the parent.

With this new ability, it will be much easier to implement new per-inode
flags (like NODATACOW/NODATASUM) and get them properly passed down.

Signed-off-by: Qu Wenruo <wqu@suse.com>
NODATACOW or NODATASUM

Currently mkfs.btrfs --rootdir is implying data checksum, but soon we
will support per-inode NODATACOW|NODATASUM flags.

To support per-inode NODATACOW|NODATASUM flags:

- Avoid compression if the inode has either NODATACOW|NODATASUM flag

- Do not generate data checksum if the inode has either
  NODATACOW|NODATASUM flag.

Both behaviors are the following the kernel ones.

Signed-off-by: Qu Wenruo <wqu@suse.com>
This new option allows end users to specify certain per-inode flags for
specified file/directory inside rootdir.

And mkfs will follow the kernel behavior by inheriting the inode flag
from the parent.

For example:

 rootdir
 |- file1
 |- file2
 |- dir1/
 |  |- file3
 |- subv/     << will be created as a subvolume using --subvol option
    |- dir2/
    |  |- file4
    |- file5

When `mkfs.btrfs --rootdir rootdir --subvol subv --inode-flags
nodatacow:dir1 --inode-flags nodatacow:subv", then the following files
and directory will have *nodatacow* flag set:

- dir1
- file3
- subv
- dir2
- file4
- file5

For now only two flags are supported:

- nodatacow
  Disable data COW, implies *nodatasum* for regular files

- nodatasum
  Disable data checksum only.

This also works with --compress option, and files with nodatasum or
nodatacow flag will skip compression.

Signed-off-by: Qu Wenruo <wqu@suse.com>
The simple test will create a layout like the following:

rootdir
|- file1
|- file2
|- subv/		<< Regular subvolume
|  |- file3
|- nocow_subv/		<< NODATACOW subvolume
|  |- file4
|- nocow_dir/		<< NODATACOW directory
|  |- dir2
|  |  |- file5
|  |- file6
|- nocow_file1		<< NODATACOW file

Any files under NODATACOW subvolume/directory should also be NODATACOW.
The explicitly specified single file should also be NODATACOW.

Signed-off-by: Qu Wenruo <wqu@suse.com>
@@ -1686,6 +1764,7 @@

g_trans = trans;
g_subvols = subvols;
g_inode_flags_list = inode_flags_list;

Check warning

Code scanning / CodeQL

Local variable address stored in non-local memory Warning

A stack address which arrived via a
parameter
may be assigned to a non-local variable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant