Commit Graph

249 Commits

Author SHA1 Message Date
David Sterba
c7fce0e6ef btrfs-progs: document 2K sector size
Update help text of mkfs and documentation mentioning that 2K size is
allowed in experimental mode.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-03-19 23:17:28 +01:00
Qu Wenruo
d421c85ea5 btrfs-progs: mkfs: support 2K block size
Since btrfs only supports block size 4K and PAGE_SIZE, on x86_64 it
means we can not test subpage block size easily.

With the recent kernel change to support 2K block size for debug builds,
also add 2K block size support for btrfs-progs, so that we can do proper
subpage block size testing on x86_64, without acquiring an aarch64
machine.

There is a limitation:

- No support for 2K node size
  The limitation is from the initial mkfs tree root, which can only have
  a single leaf to contain all root items.
  But 2K leaf cannot handle all the root items, thus we have to disable
  it.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2025-03-19 22:28:03 +01:00
Sebastian Pucilowski
fdb81b170b btrfs-progs: mkfs: fix typo in default-ro help text
Fix a option typo in the mkfs help (`mkfs.btrfs -h`) introduced in the
most recent public release: `defalut-ro` instead of `default-ro`.

Pull-request: #958
Signed-off-by: David Sterba <dsterba@suse.com>
2025-03-11 22:15:09 +01:00
Mark Harmstone
aaf0cf3805 btrfs-progs: mkfs: set incompat bit BIG_METADATA if nodesize > PAGE_SIZE
Follow the kernel by setting the BIG_METADATA incompat flag if nodesize
is greater than the page size.

This flag was introduced with commit 727011e07cbdf8 ("Btrfs: allow
metadata blocks larger than the page size") in 2010, as kernels before
2.6.36 would crash due to a buggy page cache implementation.
The flag has no real meaning anymore but we can at least set it at mkfs
time.

Signed-off-by: Mark Harmstone <maharmstone@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-15 00:08:32 +01:00
David Sterba
4bfcadabdf btrfs-progs: mkfs: print type of subvolume in the listing
Print indicators in the summary if the subvolume is read-write,
read-only or default:

$ mkfs.btrfs --subvol ro:subvolro --subvol rw:subvolrw --subvol default-ro:defaultro --rootdir /rootdir/path img
...
Rootdir from:       /rootdir/path
  Compress:         no
  Subvolume (rw):   subvolrw
  Subvolume (ro):   subvolro
  Subvolume (dro):  defaultro
...

The path is relative to the rootdir path and may not be a subvolume in
the source directory so drop the rootdir as this may be confusing.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-15 00:08:32 +01:00
David Sterba
6a5e20ebde btrfs-progs: mkfs: fix printing of subvolumes in the summary
The subvolumes created during mkfs are not printed in the summary
because btrfs_mkfs_fill_dir() deletes them from the list as they get
created.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-15 00:08:32 +01:00
David Sterba
260ddff355 btrfs-progs: mkfs: print errno string instead of numeric values
The preferred error message should have a prefix with problem
description and then the errno description as we use the negative errno
convention almost everywhere.

- drop additional %d in the message if %m is present
- replace %d with %m

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-15 00:08:32 +01:00
David Sterba
6afa173ce9 btrfs-progs: mkfs: update subvolume related error messages
Unify the error message style a bit, print the relevant path if missing.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-15 00:08:22 +01:00
David Sterba
e5d3ba75ab btrfs-progs: mkfs: factor out parsing of option --subovlume
Keep the case code to minimum and move the parsing to a helper.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-11 20:43:28 +01:00
David Sterba
2e118b1fc3 btrfs-progs: mkfs: print builtin features in --version output
For parity with 'btrfs', also print the builtin features as the
compression is now available.

  $ mkfs.btrfs --version
  mkfs.btrfs, part of btrfs-progs v6.12
  +EXPERIMENTAL -INJECT -STATIC +LZO +ZSTD +UDEV +FSVERITY +ZONED CRYPTO=builtin

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-11 20:43:28 +01:00
David Sterba
6e52a4e6be btrfs-progs: mkfs: use verbosity helpers
The message helpers can be used in some places instead of raw printf or
open coded level checks.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-11 20:43:28 +01:00
David Sterba
abc2497465 btrfs-progs: mkfs: completely remove --leafsize option
This has been deprecated since 4.0 and mkfs fails since 6.0 with that
option. No need to keep it around anymore.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-11 20:43:28 +01:00
David Sterba
b5e7734220 btrfs-progs: mkfs: update help text
Enhance information in the help text where some interesting information
was not missing and would require looking up the documentation.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-11 20:43:28 +01:00
David Sterba
451bf7390f btrfs-progs: mkfs: print in summary if compression is used
$ mkfs.btrfs --compress zlib:9 --rootdir Documentation img
  ...
  Rootdir from:       /path/Documentation
    Compress:         zlib:9
    Shrink:           no
  ...

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-11 20:43:28 +01:00
David Sterba
97127657b0 btrfs-progs: mkfs: add --compress and --rootdir constraints
It does not make sense to pass only the compression option when there
are no files being added by --rootdir.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-11 20:43:28 +01:00
David Sterba
1285676253 btrfs-progs: mkfs: document level ranges for zlib and zstd in help
Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-11 20:43:28 +01:00
David Sterba
765cda8ca6 btrfs-progs: mkfs: validate compression levels while parsing options
Report invalid compression specification while parsing the options. Now
an ivalid level won't be silently accepted and capped when processing
the files. Other checks regarding conditional support of LZO and ZSTD
are left in place.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-11 20:43:28 +01:00
David Sterba
65aa8a16c7 btrfs-progs: mkfs: change compression level type to 32bit type
The level is a small number, no need to pass it around as u64.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-11 20:43:28 +01:00
David Sterba
c36a428d6c btrfs-progs: mkfs: factor out compression option parsing
Put option parsing to its own helper to keep the switch/case smaller.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-11 20:43:28 +01:00
David Sterba
d23cfb860f btrfs-progs: mkfs: print which compression algos are compiled in
The compression support is optional, eg. also in 'btrfs-restore', so
print the support in help text.

usage: mkfs.btrfs [options] <dev> [<dev...>]

...
    --compress ALGO[:LEVEL]   compress files by algorithm and level, ALGO can be 'no' (default), zstd, lzo, zlib
                              Built-in:
                              - ZSTD: yes
                              - LZO: yes
                              - ZLIB: yes
...

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-11 20:43:28 +01:00
David Sterba
4f4b173269 btrfs-progs: mkfs: update compression help and documentation
Move the option documentation next to --rootdir, reword documentation.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-11 20:43:23 +01:00
David Sterba
52379d2dcd btrfs-progs: mkfs: insert newline between sections in help
Improve readability and add space between sections.

usage: mkfs.btrfs [options] <dev> [<dev...>]

    Create a BTRFS filesystem on a device or multiple devices

    Allocation profiles:
    -d|--data PROFILE         data profile, raid0, raid1, raid1c3, raid1c4, raid5, raid6, raid10, dup or single
    -m|--metadata PROFILE     metadata profile, values like for data profile
    -M|--mixed                mix metadata and data together

    Features:
    --csum TYPE
    --checksum TYPE           checksum algorithm to use, crc32c (default), xxhash, sha256, blake2
    -n|--nodesize SIZE        size of btree nodes
    -s|--sectorsize SIZE      data block size (may not be mountable by current kernel)
    -O|--features LIST        comma separated list of filesystem features (use '-O list-all' to list features)
    -L|--label LABEL          set the filesystem label
    -U|--uuid UUID            specify the filesystem UUID (must be unique for a filesystem with multiple devices)
    --device-uuid UUID        Specify the filesystem device UUID (a.k.a sub-uuid) (for single device filesystem
                              only)
...

Signed-off-by: David Sterba <dsterba@suse.com>
2025-01-22 20:25:48 +01:00
David Sterba
abad680f5f btrfs-progs: clean up includes
Remove unused includes, add ones that are missing.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-01-16 19:13:26 +01:00
Mark Harmstone
c6d24a363d btrfs-progs: mkfs: add lzo to --compress option
Allow --compress to work with lzo.

Signed-off-by: Mark Harmstone <maharmstone@fb.com>
[ Add extra handling when LZO support is not compiled in ]
Signed-off-by: Qu Wenruo <wqu@suse.com>
2025-01-06 14:19:28 +01:00
Mark Harmstone
bad629f78b btrfs-progs: mkfs: add zstd to --compress option
Allow --compress to work with zstd, when compiled in.

Signed-off-by: Mark Harmstone <maharmstone@fb.com>
2025-01-06 14:19:28 +01:00
Mark Harmstone
0134e755c1 btrfs-progs: mkfs: add --compress option
Add an option --compress to mkfs.btrfs, to allow creating files
using zlib when using --rootdir.

Signed-off-by: Mark Harmstone <maharmstone@fb.com>
2025-01-06 14:19:28 +01:00
Mark Harmstone
9d1f706703 btrfs-progs: mkfs: avoid dynamic allocation when doing --subvol
Reworks mkfs.btrfs --subvol so that dir and full_path in struct
rootdir_subvol are stored as arrays rather than pointers.

Signed-off-by: Mark Harmstone <maharmstone@fb.com>
2024-11-26 21:05:34 +01:00
Mark Harmstone
8ba72e45c8 btrfs-progs: mkfs: rework format of --subvol option value
Change mkfs.btrfs --subvol so that instead of being of the form --subvol
DIR:FLAGS, it's instead --subvol MODIFIER:DIR, with MODIFIER being ro,
rw, default, or ro-default.

Signed-off-by: Mark Harmstone <maharmstone@fb.com>
2024-11-26 21:05:34 +01:00
Mark Harmstone
ec8a6b1536 btrfs-progs: mkfs: add ro flag to --subvol
Adds a flag to mkfs.btrfs --subvol to allow subvolumes to be created
readonly.

Signed-off-by: Mark Harmstone <maharmstone@fb.com>
2024-09-17 17:00:03 +02:00
Mark Harmstone
fa70df7e78 btrfs-progs: mkfs: add default flag to --subvol
Change --subvol that it can accept flags, and add a "default" flag that
allows you to mark a subvolume as the default.

Signed-off-by: Mark Harmstone <maharmstone@fb.com>
2024-09-17 17:00:03 +02:00
Mark Harmstone
e844ffcaad btrfs-progs: mkfs: add new option --subvol
Add a new option --subvol, which tells mkfs.btrfs to create the
specified directories as subvolumes when used with --rootdir.

Given a populated directory dir, the command

  $ mkfs.btrfs --rootdir dir --subvol usr --subvol home --subvol home/username img

will create subvolumes 'usr' and 'home' within the toplevel subvolume,
and subvolume 'username' within the 'home' subvolume. It will fail if
any of the directories do not yet exist.

Pull-request: #868
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Mark Harmstone <maharmstone@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-09-17 17:00:03 +02:00
Qu Wenruo
18ecbfd3dd btrfs-progs: open the devices exclusively for writes
There is an internal report that, during btrfs-convert to block-group
tree, by accident some systemd events triggered the mount of the target
fs.

This leads to double mount (one by kernel and one by the btrfs-progs),
which seems to cause quite some problems.

To avoid such accident, exclusively opens all devices if btrfs-progs is
doing write operations.

Pull-request: #888
Reported-by: pandada8 <pandada8@gmail.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
2024-09-17 14:33:22 +02:00
David Sterba
e5aeb39033 btrfs-progs: mkfs: use correct ctree.h include
Commit 14ac1a6051 ("btrfs-progs: mkfs: add support for squota")
mistakenly added ctree.h from libbtrfs/ but this is not supposed to be
used outside of the library. Moreover the correct ctree.h was already
there.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-08-14 23:58:42 +02:00
Mark Harmstone
e8c729743a btrfs-progs: mkfs: simplify mkfs_main() cleanup
mkfs_main() is a main-like function, meaning that return and exit are
equivalent. Deduplicate our cleanup code by moving the error label.

Signed-off-by: Mark Harmstone <maharmstone@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-07-30 20:04:38 +02:00
Qu Wenruo
d3cf350e21 btrfs-progs: introduce btrfs_rebuild_uuid_tree() for mkfs and btrfs-convert
Currently mkfs uses its own create_uuid_tree(), but that function is
only handling FS_TREE.  This means for btrfs-convert we do not generate
the uuid tree, nor add the UUID of the image subvolume.  This can be a
problem if we're going to support multiple subvolumes during mkfs time.

To address this, introduce a new helper, btrfs_rebuild_uuid_tree():

- Create a new uuid tree if there is not one

- Remove all the existing items from uuid tree

- Iterate through all subvolumes
  * If the subvolume has no valid UUID, regenerate one
  * Add the uuid entry for the subvolume UUID
  * If the subvolume has received UUID, also add it to UUID tree

By this, this new helper can handle all the uuid tree generation needs for:

- Current mkfs
  Only one uuid entry for FS_TREE

- Current btrfs-convert
  Only FS_TREE and the image subvolume

- Future multi-subvolume mkfs
  As we do the scan for all subvolumes.

- Future "btrfs rescue rebuild-uuid-tree"

Signed-off-by: Qu Wenruo <wqu@suse.com>
2024-07-30 20:02:56 +02:00
Qu Wenruo
99dc37bcfe btrfs-progs: cross-port btrfs_uuid_tree_add() from kernel
The modification is minimal:

- Replace WARN_ON() with UASSERT()

- Remove the @trans parameter for btrfs_extend_item() and
  btrfs_mark_buffer_dirty()
  As progs version doesn't need a transaction handler.

- Remove the btrfs_uuid_tree_add() in mkfs/main.c

Signed-off-by: Qu Wenruo <wqu@suse.com>
2024-07-30 20:02:42 +02:00
Qu Wenruo
8efa8092aa btrfs-progs: move uuid-tree definitions to kernel-shared/uuid-tree.h
Currently we already have a kernel-shared/uuid-tree.c, which is mostly
shared with kernel.

Kernel also has a uuid-tree.h, but we are still using ctree.h for the
header.

Move all the uuid-tree related definitions to kernel-shared/uuid-tree.h,
making future code sync easier.

Signed-off-by: Qu Wenruo <wqu@suse.com>
2024-07-30 20:01:59 +02:00
Qu Wenruo
3c555beabf btrfs-progs: introduce btrfs_make_subvolume()
There are two different subvolume/data reloc tree creation routines:

- create_subvol() from convert/main.c
  * calls btrfs_copy_root() to create an empty root
    This is not safe, as it relies on the source root to be empty.
  * calls btrfs_read_fs_root() to add it to the cache and trace it
    properly
  * calls btrfs_make_root_dir() to initialize the empty new root

- create_data_reloc_tree() from mkfs/main.c
  * calls btrfs_create_tree() to create an empty root
  * Manually add the root to fs_root cache
    This is only safe for data reloc tree as it's never updated
    inside btrfs-progs.
    But not safe for other subvolume trees.
  * manually setup the root dir

Both have their good and bad aspects, so here we introduce a new helper,
btrfs_make_subvolume():

- Calls btrfs_create_tree() to create an empty root
- Calls btrfs_read_fs_root() to setup the cache and tracking properly
- Calls btrfs_make_root_dir() to initialize the root dir
- Calls btrfs_update_root() to reflect the rootdir change

So this new helper can replace both create_subvol() and
create_data_reloc_tree().

Signed-off-by: Qu Wenruo <wqu@suse.com>
2024-07-30 19:54:04 +02:00
Qu Wenruo
9b74d80919 btrfs-progs: remove fs_info parameter from btrfs_create_tree()
The @fs_info parameter can be easily extracted from @trans, and kernel
has already remove the parameter.

Signed-off-by: Qu Wenruo <wqu@suse.com>
2024-07-30 19:54:00 +02:00
David Sterba
4db925911c btrfs-progs: use strncpy_null everywhere
Use the safe version of strncpy that makes sure the string is
terminated.

To be noted:

- the conversion in scrub path handling was skipped
- sizes of device paths in some ioctl related structures is
  BTRFS_DEVICE_PATH_NAME_MAX + 1

Recently gcc 13.3 started to detect problems with our use of strncpy
potentially lacking the null terminator, warnings like:

cmds/inspect.c: In function ‘cmd_inspect_logical_resolve’:
cmds/inspect.c:294:33: warning: ‘__builtin_strncpy’ specified bound 4096 equals destination size [-Wstringop-truncation]
  294 |                                 strncpy(mount_path, mounted, PATH_MAX);
      |                                 ^

Signed-off-by: David Sterba <dsterba@suse.com>
2024-06-24 19:18:48 +02:00
Qu Wenruo
e0bf3943a4 btrfs-progs: move RST back to experimental
Currently raid-stripe-tree feature is still experimental as it requires
a BTRFS_DEBUG kernel to recognize it.  To avoid confusion move it back
to experimental so regular users won't incorrectly set it.

And since RST is no longer supported by default, also change the RST
profile detection so that for non-experimental build we won't enable RST
according to the data profiles.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-06-24 19:18:32 +02:00
Naohiro Aota
970c1a543c btrfs-progs: mkfs: align byte_count with sectorsize and zone size
While "byte_count" is eventually rounded down to sectorsize at make_btrfs()
or btrfs_add_to_fs_id(), it would be better round it down first and do the
size checks not to confuse the things.

Also, on a zoned device, creating a filesystem whose size is not aligned
to the zone boundary can be confusing. Round it down further to the zone
boundary.

The size calculation with a source directory is also tweaked to be aligned.
device_get_partition_size_fd_stat() must be aligned down not to exceed the
device size. And, btrfs_mkfs_size_dir() should have return sectorsize aligned
size. So, add an UASSERT for it.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-06-03 21:17:34 +02:00
Naohiro Aota
2190d77a1c btrfs-progs: mkfs: unify zoned mode minimum size calc into btrfs_min_dev_size()
We are going to implement a better minimum size calculation for the zoned
mode. Move the current logic to btrfs_min_dev_size() and unify the size
checking path.

Also, convert "int mixed" to "bool mixed" while at it.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-06-03 21:15:26 +02:00
Naohiro Aota
5ff2e9c069 btrfs-progs: mkfs: remove duplicated device size check
test_minimum_size() already checks if each device can host the initial
block groups. There is no need to check if the first device can host the
initial system chunk again.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-06-03 21:14:34 +02:00
Naohiro Aota
d76d74c344 btrfs-progs: rename block_count to byte_count
block_count and dev_block_count are counting the size in bytes. And,
comparing them with e.g, "min_dev_size" is confusing. Rename them to
represent the unit better.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-06-03 21:12:47 +02:00
David Sterba
3172d27fb9 btrfs-progs: mkfs: make transaction start and commit errors verbose
Use the templated error message for transaction failures, use the same
pattern assigning the ret and errno.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-03-22 01:19:47 +01:00
David Sterba
a80d717db2 btrfs-progs: minor source sync with kernel 6.8
Sync a few more file on the source level with kernel 6.8.

- type cleanups
- defines and enums
- comments
- parameter updates
- error handling

Signed-off-by: David Sterba <dsterba@suse.com>
2024-03-12 21:22:56 +01:00
David Sterba
3d1a3c8c5a btrfs-progs: mkfs: print incompat and runtime features on one line
We've deprecated the -R option and runtime feature distinction, there's
only one option -O recommended so let it also print on the same line.
Incompat/compat/runtime status of a feature shall be consulted with the
documentation.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-13 07:15:37 +01:00
David Sterba
e406abb157 btrfs-progs: mkfs: warn if page does not match sectorsize
Be verbose about the potential compatibility problems with the
sectorsize and page size. Also print the page size on the overview.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-13 07:15:33 +01:00
Neal Gompa
e3232c2abb btrfs-progs: mkfs: make 4k sectorsize default
We have had working subpage support in Btrfs for many cycles now.
Generally, we do not want people creating filesystems by default
with non-4k sectorsizes since it creates portability problems.

As the subpage has stabilized it seems to be safe to do the switch.
This may still affect users that relying on the previous behaviour.

Issue: #604
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: Eric Curtin <ecurtin@redhat.com>
Signed-off-by: Neal Gompa <neal@gompa.dev>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-01-18 02:37:45 +01:00