Commit Graph

352 Commits (master)

Author SHA1 Message Date
Qu Wenruo 47c694b0f4 btrfs-progs: Unify btrfs_leaf_free_space() parameter with kernel
Instead of struct btrfs_root, use struct btrfs_fs_info, since nodesize
is now a per-fs setting, and with the need to pass a @root, caller don't
need to wonder which root should be passed.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-04-24 13:00:11 +02:00
Misono, Tomohiro c074434fb5 btrfs-progs: remove BTRFS_CRC32_SIZE definition
The kernel code no longer has BTRFS_CRC32_SIZE and only uses
btrfs_csum_sizes[]. So, update the progs code as well.

Suggested-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Tomohiro Misono <misono.tomohiro@jp.fujitsu.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-03-30 22:15:55 +02:00
Qu Wenruo 12becfe122 btrfs-progs: Introduce btrfs_raid_array and related infrastructures
As part of the effort to unify code and behavior between btrfs-progs and
kernel, copy the btrfs_raid_array from kernel to btrfs-progs.

So later we can use the btrfs_raid_array[] to get needed raid info other
than manually do if-else branches.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-02-24 01:37:17 +01:00
Gu Jinxiang 4665484f5f btrfs-progs: Cleanup use of root in leaf_data_end
In function leaf_data_end, root is just used to get fs_info, so change
the parameter of this function from btrfs_root to btrfs_fs_info.  And
also make it consistent with kernel.

Add const to parameter leaf of function btrfs_item_offset_nr to keep
type consistent with leaf_data_end.

Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-02-02 16:01:57 +01:00
Gu Jinxiang f1bc621846 btrfs-progs: remove no longer used btrfs_alloc_extent
Function btrfs_alloc_extent is no longer be used.  So let's remove it.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-02-02 16:01:57 +01:00
Gu Jinxiang 8bcac688be btrfs-progs: Use fs_info instead of root for BTRFS_MAX_XATTR_SIZE
Do a cleanup. Also make it consistent with kernel.  Use fs_info instead
of root for BTRFS_MAX_XATTR_SIZE, since maybe in some situation we do
not know root, but just know fs_info.

To be consistent with kernel, change macro to inline function.

Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-02-02 16:01:57 +01:00
Gu Jinxiang c958110785 btrfs-progs: Sync code with kernel for BTRFS_MAX_INLINE_DATA_SIZE
Do a cleanup. Also make it consistent with kernel.  Use fs_info instead
of root for BTRFS_MAX_INLINE_DATA_SIZE, since maybe in some situation we
do not know root, but just know fs_info.

Change macro to inline function to be consistent with kernel.  And
change the function body to match kernel.

Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-02-02 16:01:57 +01:00
Gu Jinxiang 3809b36d85 btrfs-progs: Use fs_info instead of root for BTRFS_NODEPTRS_PER_BLOCK
Do a cleanup. Also make it consistent with kernel.  Use fs_info instead
of root for BTRFS_NODEPTRS_PER_BLOCK, since maybe in some situation we
do not know root, but just know fs_info.

To be consistent with kernel, change macro to inline function.

Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-02-02 16:01:57 +01:00
Gu Jinxiang 26072f584d btrfs-progs: Use fs_info instead of root for BTRFS_LEAF_DATA_SIZE
Do a cleanup. Also make it consistent with kernel.  Use fs_info instead
of root for BTRFS_LEAF_DATA_SIZE, since maybe in some situation we do
not know root, but just know fs_info.

Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-02-02 16:01:56 +01:00
Qu Wenruo 1945854e01 btrfs-progs: Remove unnecessary parameter for btrfs_add_block_group
@chunk_objectid of btrfs_make_block_group() function is always fixed to
BTRFS_FIRST_FREE_OBJECTID, so there is no need to pass it as parameter
explicitly.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-01-31 15:14:03 +01:00
Qu Wenruo 6b5a5c648b btrfs-progs: Use bool parameter to determine if we're allocating data extent
btrfs_reserve_extent() uses int @data to determine if we're allocating
data extent, while reuse the parameter later to pass it as profile
(data/meta/sys).

It's a little confusing, this patch will follow kernel parameter to use
bool @is_data to replace it.
And in btrfs_reserve_extent(), use dedicated u64 @profile.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Su Yue <suy.fnst@cn.fujitsu.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-01-31 15:14:02 +01:00
Gu Jinxiang 50d4ab19e4 btrfs-progs: Remove unused parameter trans
Some parameter of trans is not used indeed.
Let's remove them.

Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-01-08 19:24:47 +01:00
Misono, Tomohiro f23506c8a4 btrfs-progs: dump-tree: print c/o/s/r time of ROOT_ITEM
Currently ctime/otime/stime/rtime of ROOT_ITEM are not printed in
print_root_item().  Fix this and print them if the values are not zero.
The function print_timespec() is moved forward to reuse.

Signed-off-by: Tomohiro Misono <misono.tomohiro@jp.fujitsu.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-01-03 17:08:57 +01:00
Su Yue 3efc459166 btrfs-progs: check: adjustments for further repair
For code reuse, btrfs_insert_dir_item() now calls
inserts_with_overflow() even if the dir_item existed.

Add a parameter @ignore_existed to btrfs_add_link().
If @ignore_existed is not zero, btrfs_add_link() continues to do link.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-10-16 20:33:00 +02:00
Yingyi Luo fe667f6e7d btrfs-progs: add a parameter to btrfs_mksubvol
A convert parameter is added as a flag to indicate if btrfs_mksubvol()
is used for btrfs-convert. The change cascades down to the callchain.

Signed-off-by: Yingyi Luo <yingyil@google.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-10-06 13:23:37 +02:00
Yingyi Luo 8a820b5765 btrfs-progs: convert: move link_subvol out of main
link_subvol() is moved to inode.c and renamed as btrfs_mksubvol().
The change cascades down to the callchain.

Signed-off-by: Yingyi Luo <yingyil@google.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-10-06 13:23:34 +02:00
Nick Terrell 717a8b1e5d btrfs-progs: Add zstd support
Adds zstd support to the btrfs program. An optional dependency on libzstd
>= 1.0.0 is added. Autoconf accepts `--enable-zstd' or `--disable-zstd' and
defaults to detecting if libzstd is present using `pkg-config'.

The patch is also available in my fork of btrfs-progs [1], which passes
Travis-CI with the new tests. The prebuilt binary is available there.

I haven't updated Android.mk.

[1] https://github.com/terrelln/btrfs-progs/tree/devel

Signed-off-by: Nick Terrell <terrelln@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-25 15:17:13 +02:00
Goffredo Baroncelli b5665d66e8 btrfs-progs: fi du: don't call lookup_path_rootid for BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
When ino is BTRFS_EMPTY_SUBVOL_DIR_OBJECTID, the item is not referred to
any file-tree. So lookup_path_rootid() doesn't return any meaningful
value.

As was reported, this can be triggered by

$ btrfs sub create test1
$ btrfs sub create test1/test2
$ btrfs sub snap test1 test1.snap
$ btrfs fi du -s test1
  Total   Exclusive  Set shared  Filename
  0.00B       0.00B       0.00B  test1
$ btrfs fi du -s test1.snap
  Total   Exclusive  Set shared  Filename
ERROR: cannot check space of 'test1.snap': Inappropriate ioctl for device

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-08 16:15:05 +02:00
David Sterba 355a052647 btrfs-progs: start framework for transaction abort
Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-08 16:15:05 +02:00
David Sterba b423d18229 btrfs-progs: switch fs_info::system_allocs to bit
The value really is 0 or 1, the test in btrfs_reserve_extent can be
simplified.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-08 16:15:05 +02:00
Jeff Mahoney 99340c2ef7 btrfs-progs: convert: add support for converting reiserfs
This patch adds support to convert reiserfs file systems in-place to btrfs.

It will convert extended attribute files to btrfs extended attributes,
translate ACLs, coalesce tails that consist of multiple items into one item,
and convert tails that are too big into indirect files.

This requires that libreiserfscore 3.6.27 be available.

Signed-off-by: Jeff Mahoney <jeffm@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-08 16:15:05 +02:00
Qu Wenruo 0544aafcbf btrfs-progs: Refactor chunk creation functions to use btrfs_fs_info
4 functions are involved in this refactor: btrfs_make_block_group()
btrfs_make_block_groups(), btrfs_alloc_chunk, btrfs_alloc_data_chunk().

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-12 17:54:16 +02:00
Qu Wenruo 9f6dec4dd5 btrfs-progs: Refactor read_node_slot function to get rid of btrfs_root
parameter

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
2017-07-03 13:35:11 +02:00
Qu Wenruo fa5fd68021 btrfs-progs: Remove block size members in btrfs_root
Finally, we can get rid of per tree block size members now.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
2017-07-03 13:35:11 +02:00
Qu Wenruo 34bd207932 btrfs-progs: Refactor block sizes users in ctree.c and ctree.h
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
2017-07-03 13:35:10 +02:00
Qu Wenruo c4893f15ce btrfs-progs: Introduce sectorsize nodesize and stripesize members for
btrfs_fs_info

Just like what we do in kernel, since we will not support different
leaf/node/stripe size per tree, there is no need to store these block
sizes in btrfs_root.

This patch will introduce these block size members into btrfs_fs_info
structure, allowing us to convert such usage in later patches.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
2017-07-03 13:35:10 +02:00
Qu Wenruo 3c7a04e592 btrfs-progs: Remove deprecated leafsize usage
Leafsize is deprecated for a long time, and kernel has already updated
ctree.h to rename sb->leafsize to sb->__unused_leafsize.

This patch will remove normal users of leafsize:
1) Remove leafsize member from btrfs_root structure
   Now only root->nodesize and root->sectorisze.
   No longer root->leafsize.

2) Remove @leafsize parameter from btrfs_setup_root() function
   Since no root->leafsize, no need for @leafsize parameter.

The remaining user of leafsize will be:
1) btrfs inspect-internal dump-super
   Reformat the "leafsize" output to "leafsize (deprecated)" and
   use le32_to_cpu() to do the cast manually.

2) mkfs
   We still need to set sb->__unused_leafsize to nodesize.
   Do the manual cast too.

3) convert
   Same as mkfs, these two superblock setup should be merged later

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
2017-07-03 13:35:10 +02:00
David Sterba 87b971d0cc btrfs-progs: library: distribute sizes.h, fix build
The patch "btrfs-progs: Introduce kernel sizes to cleanup large
intermediate number" (a2203246ae) was taken from kernel but not
properly ported so the build breaks because the header linux/sizes.h is
not exported.

The build tests of library do not cover the case when the macro
BTRFS_FLAT_INCLUDES is not defined (ie. an external build).

Reported-by: Holger Hoffstätte <holger@applied-asynchrony.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-16 17:02:44 +01:00
Qu Wenruo 6d881033e1 btrfs-progs: convert: Rework rollback
Rework rollback to a more easy to understand way.

New convert behavior makes us to have a more flex chunk layout, which
only data chunk containing old fs data will be at the same physical
location, while new chunks (data/meta/sys) can be mapped anywhere else.

This behavior makes old rollback behavior can't handle it.
As old behavior assumes all data/meta is mapped in a large chunk, which is
mapped 1:1 on disk.

So rework rollback to handle new convert behavior, enhance the check by
only checking all file extents of convert image, only to check if these
file extents and therir chunks are mapped 1:1.

This new rollback check behavior can handle both new and old convert
behavior, as the new behavior is a superset of old behavior.

Further more, introduce a simple rollback mechanisim:
1) Read reserved data (offset = file offset) from convert image
2) Write reserved data into disk (offset = physical offset)

Since old fs image is a valid fs, and we only need to rollback
superblocks (btrfs reserved ranges), then we just read out data in
reserved range, and write it back.

Due to the fact that all other file extents of converted image is mapped
1:1 on disk, we put the missing piece back, then the fs is as good as
old one.

Then what we do in btrfs is just another dream.

With this new rollback mechanisim, we can open btrfs read-only, so we
won't cause any damage to current btrfs, until the final piece (0~1M,
containing 1st super block) is put back.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
[ port to v4.10 ]
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-16 17:02:44 +01:00
Qu Wenruo 5613d32e75 btrfs-progs: file: Introduce function to read out file content
Introduce a new function, btrfs_read_file(), to read out data of a file
inode.

This function will iterate through EXTENT_DATA items and handle
inline/prealloc/hole file extents.

Compression is not supported yet.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-16 17:02:44 +01:00
David Sterba 029a5608c9 btrfs-progs: remove unused argument from btrfs_lookup_inode_ref
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
David Sterba 4999d033d3 btrfs-progs: remove unused helpers from ctree.h
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
David Sterba 99063d88af btrfs-progs: drop unused argument from btrfs_del_ptr
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
David Sterba c6eae415ee btrfs-progs: drop unused argument from btrfs_extend_item
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
David Sterba 4069aa73d3 btrfs-progs: drop unused argument from btrfs_truncate_item
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:46 +01:00
Qu Wenruo a2203246ae btrfs-progs: Introduce kernel sizes to cleanup large intermediate number
Large numbers like (1024 * 1024 * 1024) may cost reader/reviewer to
waste one second to convert to 1G.

Introduce kernel include/linux/sizes.h to replace any intermediate
number larger than 4096 (not including 4096) to SZ_*.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:45 +01:00
Goldwyn Rodrigues e325c74bb8 btrfs-progs: check: get the highest inode for lost+found
root->highest_inode is not accurate at the time of creating a lost+found
and it fails because the highest_inode+1 is already present. This could be
because of fixes after highest_inode is set. Instead, search
for the highest inode in the tree and use it for lost+found.

This makes root->highest_inode unnecessary and hence deleted.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
Reviewed-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-01-25 09:47:19 +01:00
David Sterba dfbdde35fc btrfs-progs: make incompat bit wrappers more compact
Use the same macro tricks as in kernel code.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:34 +01:00
Omar Sandoval 9814411021 btrfs-progs: add btrfs_clear_free_space_tree() from the kernel
Reviewed-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-23 11:07:05 +01:00
Omar Sandoval 41eaae6d74 btrfs-progs: add the FREE_SPACE_TREE_VALID compat_ro bit definition
This is just the definition; we don't support it yet.

Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-23 11:07:05 +01:00
David Sterba 5b3c976bc2 btrfs-progs: convert bitfield to separate variables in btrfs_path
We don't have that many bitfields that would save space.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-09 13:47:32 +01:00
David Sterba 5a0645afb8 btrfs-progs: reduce size of btrfs_path::lowest_level
Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-09 13:47:31 +01:00
David Sterba f0f40c400e btrfs-progs: reduce size of btrfs_path::reada
We use only a few values, reada accessed not in performance critical
contexts.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-09 13:47:31 +01:00
David Sterba b81edabe79 btrfs-progs: reduce size of btrfs_path, locks are not used
Size of btrfs_path can be reduced by 32 bytes as we don't use the locks
array, down to 112 from 144 bytes.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-09 13:47:31 +01:00
David Sterba 0f4ab474fe btrfs-progs: add dev stats on-disk structure
Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-09 13:47:30 +01:00
David Sterba ad5c75708a btrfs-progs: add balance status structures
Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-09 13:47:30 +01:00
David Sterba 2add123c8e btrfs-progs: switch dev stats item to the permanent item key
[ kernel patch 242e2956e4afde7727fffe21adc0a7b198b77f96 ]
Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-09 13:47:30 +01:00
David Sterba e244550b42 btrfs-progs: introduce key type for persistent permanent items
The number of distinct key types is not that big that we could waste one
for something new we want to store in the tree.

Similar to the temporary items, we'll introduce a new name for an
existing key value and use the objectid for further extension.  The
victim is the BTRFS_DEV_STATS_KEY (248).

The device stats are an example of a permanent item.

[ kernel patch 50c2d5abe64c1726b48d292a2ab04f60e8238933 ]
Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-09 13:47:30 +01:00
David Sterba 6122cb32c6 btrfs-progs: introduce key type for persistent temporary items
The number of distinct key types is not that big that we could waste one
for something new we want to store in the tree. We'll introduce a new
name for an existing key value and use the objectid for further
extension.  The victim is the BTRFS_BALANCE_ITEM_KEY (248).

The nature of the balance status item is a good example of the temporary
item. It exists from beginning of the balance, keeps the status until it
finishes.

[ kernel patch 0bbbccb17fea86818e1a058faf5903aefd20b31a ]
Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-09 13:47:30 +01:00
Qu Wenruo e93f3e7ded btrfs-progs: Copy btrfs inode flags from kernel header
Btrfs-progs header lacks quite a lot inode flags.
Copy them from kernel for later use.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-24 14:57:55 +02:00
David Sterba 890f4a61d7 btrfs-progs: cleanup, kill trivial btrfs_key_type helper
Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-03 11:33:15 +02:00
David Sterba 555743075b btrfs-progs: cleanup, kill trivial btrfs_set_key_type helper
Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-03 11:33:15 +02:00
Omar Sandoval 02a9b6cbef Revert "btrfs-progs: fix compat_ro mask for free space tree"
btrfs-progs can't mount space_cache=v2 filesystems read-write, which is
why the compat bit wasn't added to the supported mask in the first
place. Remove it.

Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-09-21 11:50:09 +02:00
David Sterba d57187703e btrfs-progs: add rest of stack operations for btrfs_dir_item
Signed-off-by: David Sterba <dsterba@suse.com>
2016-08-25 19:02:18 +02:00
David Sterba 2ff04db5f4 btrfs-progs: two staged filesystem creation
The filesystem existence on a device is manifested by the signature,
during the mkfs process we write it first and then create other
structures. Such filesystem is not valid and should not be registered
during device scan nor listed among devices from blkid.

This patch will introduce two staged creation. In the first phase, the
signature is wrong, but recognized as a partially created filesystem (by
open or scan helpers). Once we successfully create and write everything,
we fixup the signature. At this point automated scanning should find
a valid filesystem on all devices.

We can also rely on the partially created filesystem to do better error
handling during creation. We can just bail out and do not need to clean
up.

The partial signature is '!BHRfS_M', can be shown by

  btrfs inspect-internal dump-super -F image

Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-08-24 14:37:31 +02:00
David Sterba 3c350dec06 btrfs-progs: introduce signature for a partially set up filesystem
Currently the superblock is created first, with a valid signaure, but
the rest of the filesystem is missing. When the creation process is
interrupted, the filesystem still might be considered as valid.

To prevent that, create the filesytem with an invalid signature that
would be still recognized during the mkfs process, and finalize at the
end.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-08-24 14:36:58 +02:00
Qu Wenruo bdadea75e4 btrfs-progs: convert: Fix a regression that ext2_save/image is not readonly
The new convert treats the convert image as a normal file, without any
special flags and permissions.

This is different from original code:
1) Permission changed from 0400 to 0600
2) Inode lacks READONLY flag

This makes we can read-write mount the ext2 image and cause rollback
failure.

Follow old code behavior, use 0400 permission and add back READONLY
flag to fix it.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-08-19 13:52:43 +02:00
Lu Fengqi b0d360b541 btrfs-progs: check: introduce function to check data backref in extent tree
Introduce a new function check_data_extent_item() to check if the
corresponding data backref exists in extent tree.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Reviewed-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-08-17 18:51:19 +02:00
Mark Fasheh a804254361 btrfs-progs: check: write corrected qgroup info to disk
Now that we can verify all qgroups, we can write the corrected qgroups out
to disk when '--repair' is specified. The qgroup status item is also updated
to clear any out-of-date state. The repair_ functions were modeled after the
inode repair code in cmds-check.c.

I also renamed the 'scan' member of qgroup_status_item to 'rescan' in order
to keep consistency with the kernel.

Testing this was easy, I just reproduced qgroup inconsistencies via the
usual routes and had btrfsck fix them.

Signed-off-by: Mark Fasheh <mfasheh@suse.de>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-07-13 18:44:26 +02:00
Qu Wenruo cfdcb4fa61 btrfs-progs: Cleanup old btrfs-convert
Cleanup all the old btrfs-convert facilities, including:
1) btrfs_convert_operations->alloc/free/test_extents*
   No need to do non-standard extent allocation.
   After init_btrfs() everything can be done by normal routine.

   Now only 4 functions are needed in btrfs_convert_operations.
   1) open_fs
   2) read_used_space
   3) copy_inodes
   4) close_fs

2) fs_info->extent_ops
   Same as above.

3) Old init_btrfs(), create_image(), create_file_image_range()
   Replaced with newer and cleaner one.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-06-07 18:15:19 +02:00
Qu Wenruo 561f773433 btrfs-progs: convert: Strictly avoid meta or system chunk allocation
Before this patch, btrfs-convert only rely on large enough initial
system/metadata chunk size to ensure no newer system/meta chunk will be
created.

But that's not safe enough. So add two new members in fs_info,
avoid_sys/meta_chunk_alloc flags to prevent any newer system or meta
chunks to be created before init_btrfs_v2().

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-06-07 18:15:19 +02:00
Qu Wenruo a21cc1ca3f btrfs-progs: extent-tree: Enhance btrfs_record_file_extent
Btrfs_record_file_extent() has some small problems like:
1) Can't handle overlapping extents
2) May create extent larger than BTRFS_MAX_EXTENT_SIZE

So enhance it using previously added facilites.
This is used for later btrfs-convert, as for new convert, we create
saved image first, then copy inode.
Which will also cause extent overlapping.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-06-07 18:15:19 +02:00
Qu Wenruo f5e77e4c52 btrfs-progs: extent-tree: Introduce function to find the first overlapping extent
Introduce a new function, btrfs_search_overlap_extent() to find the first
overlapping extent.

It's useful for later btrfs-convert rework.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-06-07 18:15:19 +02:00
Nicholas D Steeves bd2cc320af btrfs-progs: typo review of strings and comments
Signed-off-by: Nicholas D Steeves <nsteeves@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-06-01 14:56:56 +02:00
David Sterba 64a4800875 btrfs-progs: add getters for ioctl search_header
The search header is usually accessed in an unaligned way, we could
trigger errors (SIGBUS) on architectures that do not support that.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-05-11 15:53:16 +02:00
David Sterba 3be6e3e7c9 btrfs-progs: deprecate and stop using btrfs_level_size
Size of a b-tree node is always nodesize, regardless of the level.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-05-02 14:40:23 +02:00
David Sterba 2a796d84af btrfs-progs: replace leafsize with nodesize
Nodesize is used in kernel, the values are always equal. We have to keep
leafsize in headers, similarly the tree setting functions still take and
set leafsize, but it's effectively a no-op.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-05-02 14:40:18 +02:00
Qu Wenruo 43318324d2 btrfs-progs: Allow open_ctree to return fs_info even chunk tree is corrupted
Current open_ctree_fs_info() won't return anything if chunk tree root is
corrupted.
This makes some function, like btrfs-find-root, unable to find any older
chunk tree root, even it is possible to use system_chunk_array in super
block.

And at least two users in mail list has reported such heavily chunk
corruption.
Although we have 'btrfs rescue chunk-recovery' but it's too time
consuming and sometimes not able to cope with a specific filesystem
corruption.

This patch adds a new open ctree flag,
OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR, allowing fs_info to be returned from
open_ctree_fs_info() even there is no valid tree root in it.

Also adds a new close_ctree() variant, close_ctree_fs_info() to handle
possible fs_info without any root.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
[ adjusted error messages ]
Signed-off-by: David Sterba <dsterba@suse.com>
2016-02-26 17:27:58 +01:00
David Sterba d30b0be836 btrfs-progs: fix compat_ro mask for free space tree
The BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE bit is supposed to be in the
COMPAT_RO_SUPP bitmask.

Reported-by: Holger Hoffstätte <holger.hoffstaette@googlemail.com>
Reported-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-25 15:35:30 +01:00
Omar Sandoval e4797df6a9 btrfs-progs: check the free space tree in btrfsck
This reuses the existing code for checking the free space cache, we just
need to load the free space tree. While we do that, we check a couple of
invariants on the free space tree itself. This requires pulling in some
code from the kernel to exclude the super stripes.

Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-12 15:02:54 +01:00
Omar Sandoval d8c537e71f btrfs-progs: add basic awareness of the free space tree
To start, let's tell btrfs-progs to read the free space root and how to
print the on-disk format of the free space tree. However, we're not
adding the FREE_SPACE_TREE read-only compat bit to the set of supported
bits because progs doesn't know how to keep the free space tree
consistent.

Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-12 15:02:54 +01:00
Zhao Lei f248b10786 btrfs-progs: Avoid uninitialized data in output of btrfs-convert
The sequence, transid and reserved fields of inode were writen to disk
with uninitizlized value, this patch fixes it.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-09-09 17:43:05 +02:00
Zhao Lei 537fb08026 btrfs-progs: fix typo in set/get of btrfs_inode_item::sequence
s/generation/sequence/
for BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence, ...)

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-09-09 17:42:22 +02:00
Qu Wenruo c11eeecd78 btrfs-progs: convert: Avoid allocating metadata extent crossing stripe boundary
As convert implement its own alloc extent, avoid such metadata problem
too.

Reported-by: Chris Murphy <lists@colorremedies.com>
Reported-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-08-31 19:25:10 +02:00
Filipe Manana a57606e815 Btrfs-progs: add feature to get mininum size for resizing a fs/device
Currently there is not way for a user to know what is the minimum size a
device of a btrfs filesystem can be resized to. Sometimes the value of
total allocated space (sum of all allocated chunks/device extents), which
can be parsed from 'btrfs filesystem show' and 'btrfs filesystem usage',
works as the minimum size, but sometimes it does not, namely when device
extents have to relocated to holes (unallocated space) within the new
size of the device (the total allocated space sum).

This change adds the ability to reliably compute such minimum value and
extents 'btrfs filesystem resize' with the following syntax to get such
value:

   btrfs filesystem resize [devid:]get_min_size

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-08-31 19:25:03 +02:00
Qu Wenruo 80fc1e0fc6 btrfs-progs: extent-tree: Introduce btrfs_free_block_group function
This function will be used to free a empty chunk.

This provides the basis for later temp chunk cleanup.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-07-10 14:39:39 +02:00
Qu Wenruo 20ce7d3ee6 btrfs-progs: Remove non-exist csum size.
Current btrfs only support CRC32 as checksum algorithm.
But in btrfs_csum_sizes array, we have an extra 0 at tail, causing
csum_type 1 can still be considered as supported csum type.

Fix it by removing the tailing 0.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-05-14 15:41:08 +02:00
Qu Wenruo 0c8b978881 btrfs-progs: Introduce change_header_uuid function
This function is used to change fsid and chunk_tree_uuid of a node/leaf.
The function does it without transaction protection.

This is the basis of offline uuid change.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-05-14 15:41:07 +02:00
Qu Wenruo 2259f03439 btrfs-progs: Add open_ctree check for uuid changing
Now open_ctree will exit if it found the superblock is marked
CHANGING_FSID, except given IGNORE_FSID open ctree flags.

Kernel will do the same thing later.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
[removed the chunk tree flag, reworded the error message]
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-05-14 15:41:07 +02:00
Qu Wenruo 7cc792872a btrfs-progs: Add CHANGING_FSID super flag
Add the super flag to inform kernel not to mount a filesystem wich fsid
change is in progress.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
[removed the chunk tree flag]
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-05-14 15:41:07 +02:00
Josef Bacik 2681e00f00 btrfs-progs: check for matching free space in cache
We have this check in the kernel but not in userspace, which makes fsck
fail when we wouldn't have a problem in the kernel.  This was meant to
catch this case because it really isn't good, unfortunately it will
require a design change to fix in the kernel so in the meantime add this
check so we can be sure our tests only catch real problems.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-04-24 15:42:04 +02:00
Qu Wenruo 0f67b5aa01 btrfs-progs: Add stack get/set functions for btrfs_qgroup_status_item
This provides the basis for later qgroup related changes.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-03-24 00:28:34 +01:00
Qu Wenruo 9922c42701 btrfs-progs: Update qgroup status flags and replace qgroup level/subvid calculation with inline function
Ctree.h of btrfs-progs contains wrong flags for btrfs_qgroup_status.
Update it with the one in kernel.

Also, introduce the inline function btrfs_qgroup_(level/subvid) to get
the level/subvolid of qgroup, to replace the old open-coded bit
operations.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-03-24 00:07:59 +01:00
Qu Wenruo 0c05f623b1 btrfs-progs: Add support to suppress tree block csum error output
Add new open ctree flag OPEN_CTREE_SUPPRESS_CHECK_BLOCK_ERRORS to
suppress tree block csum error output.

Provides the basis for new btrfs-find-root and other enhancement on
btrfs offline tools output.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
[renamed vars and funcs, added comments]
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-02-11 17:14:05 +01:00
Josef Bacik 08a4597223 Btrfs-progs: remove global transaction from fsck
We hold a transaction open for the entirety of fixing extent refs.  This works
out ok most of the time but we can be tight on space and run out of space when
fixing things.  To get around this just push down the transaction starting dance
into the functions that actually fix things.  This keeps us from ending up with
ENOSPC because we pinned everything and allows the code to be a bit simpler.
Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
2015-02-09 14:53:15 -05:00
Josef Bacik 797a937e5d Btrfs-progs: Introduce metadump_v2
The METADUMP super flag makes us skip doing the chunk tree reading which isn't
helpful for the new restore since we have a valid chunk tree.  But we still want
to have a way for the kernel to know that this is a metadump restore so it
doesn't do things like verify data checksums.  We also want to skip some of the
device extent checks in fsck since those will obviously not match.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
2015-02-09 14:53:15 -05:00
Qu Wenruo aae2c8421b btrfs-progs: Add btrfs_get_extent() and btrfs_punch_hole().
Add btrfs_get_extent() and btrfs_punch_hole() for btrfs-progs.

Btrfs_get_extent() will act much like kernel one, return the first
extent that covers the given range.
The difference will be that progs btrfs_get_extent() can't handle
no-holes feature, which means caller should handle it carefully.

Btrfs_punch_hole() will punch a hole in given range of given inode,
however it differs from kernel one since it won't zero any page or drop
any extents if there is any extent in the hole range.

These functions are mainly used for later I_ERR_FILE_EXTENT_DISCOUNT
repair function.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-02-02 14:16:38 +01:00
Qu Wenruo 0ddf63c09f btrfs-progs: Record orphan data extent ref to corresponding root.
Before this patch, when a extent's data ref points to a invalid key in
fs tree, this happens if a leaf/node of fs tree is corrupted, btrfsck
can't do any repair and just exit.

In fact, such problem can be handled in fs tree repair routines, rebuild
the inode item(if missing) and add back the extent data (with some
assumption).

So this patch records such data extent refs for later fs tree recovery
routine.

TODO:
    Restore orphan data extent refs into btrfs_root is not the best
    method. It's best to directly restore it into inode_record, however
    current extent tree and fs tree can't cooperate together, so use
    btrfs_root as a temporary storage until inode_cache is built.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-02-02 14:16:38 +01:00
David Sterba 07ce7005fc btrfs-progs: unify header file inclusion protections
There are missing ifdefs or defines with very generic names.

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-01-21 17:49:26 +01:00
Qu Wenruo 55fea7186c btrfs-progs: Add inode item rebuild function.
Add a basic inode item rebuild function for I_ERR_NO_INODE_ITEM.
The main use case is to repair btrfs which fs root has corrupted leaf,
but it is already working for case if the corrupteed fs root leaf/node
contains no inode extent_data.

The repair needs 3 elements for inode rebuild:
1. inode number
   This is quite easy, existing inode_record codes will detect it quite
   well.

2. inode type
   This is the trick part. The only reliable method is to recovery it from
   parent's dir_index/item.
   The remaining method will search for regular file extent for FILE
   type or child's backref for DIR(todo).
   Fallback will be FILE.

Inode name(inode_ref) will be recoverd by nlink repair function.

This is just a fundamental implement, some advanced recovery can be
improved later with btrfs-progs infrastructure change.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-12-10 13:52:28 +01:00
Qu Wenruo 43c36f3cfd btrfs-progs: Add btrfs_mkdir() function for the incoming 'lost+found' fsck mechanism.
With the previous btrfs inode operations patches, now we can use
btrfs_mkdir() to create the 'lost+found' dir to do some data salvage in
btrfsck.

This patch along with previous ones will make data salvage easier.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-12-10 13:14:26 +01:00
Qu Wenruo 0cc75eddd0 btrfs-progs: Add btrfs_unlink() and btrfs_add_link() functions.
Add btrfs_unlink() and btrfs_add_link() functions in inode.c,
for the incoming btrfs_mkdir() and later inode operations functions.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-12-10 13:13:06 +01:00
Qu Wenruo c0af17d07f btrfs-progs: Import lookup/del_inode_ref() function.
Import lookup/del_inode_ref() function in inode-item.c, as base functions
for the incoming btrfs_add_link() and btrfs_unlink() functions.

Also modify btrfs_insert_inode_ref() and split_leaf() making them able
to deal with EXTENT_IREF incompat flag.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-12-10 11:53:48 +01:00
Qu Wenruo 2606756577 btrfs-progs: Import btrfs_insert/del/lookup_extref() functions.
Import btrfs_insert/del/lookup_extref() functions form kernel for the
incoming btrfs_add_link() and btrfs_unlink() functions.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-12-10 11:53:25 +01:00
Goffredo Baroncelli e12e91e859 btrfs-progs: Enhance the command btrfs filesystem df
Enhance the command "btrfs filesystem df" to show space usage information
for a mount point(s). It shows also an estimation of the space available,
on the basis of the current one used.

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
[code moved under #if 0 instead of deletion]
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-12-04 16:48:09 +01:00
Josef Bacik b25a40651d Btrfs-progs: delete bogus dir indexes
We may run across dir indexes that are corrupt in such a way that it makes them
useless, such as having a bad location key or a bad name.  In this case we can
just delete dir indexes that don't show up properly and then re-create what we
need.  When we delete dir indexes however we need to restart scanning the fs
tree as we could have greated bogus inode recs if the location key was bad, so
set it up so that if we had to delete an dir index we go ahead and free up our
inode recs and return -EAGAIN to check_fs_roots so it knows to restart the loop.
Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-10-14 10:45:03 +02:00
Josef Bacik d90d8d2323 Btrfs-progs: pull back backref.c and fix it up
This patch pulls back backref.c, adds a couple of helpers everywhere that it
needs, and cleans up backref.c to fit in btrfs-progs.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
[removed free_some_buffers after "do not reclaim extent buffer"]
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-10-14 10:39:38 +02:00
David Sterba da5db5596c btrfs-progs: read global reserve size from space infos
Kernels >= 3.15 export the global block reserve as a space info presented
by 'btrfs fi df' but would display 'unknown' instead of some meaningful
string.

Signed-off-by: David Sterba <dsterba@suse.cz>
2014-08-28 02:03:33 +02:00
Gui Hecheng 5f1313a45d btrfs-progs: remove unused flags for btrfs_path
The three flags of @btrfs_path:
	btrfs_path {
		unsigned int keep_locks:1;
		unsigned int skip_locking:1;
		unsigned int leave_spinning:1;
	}
have little meaning, because the userspace @btrfs_search_slot()
is free of locking and no other routines will decide their behavior
on these. So just remove them.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-08-28 02:03:18 +02:00
Gui Hecheng ad70b55b66 btrfs-progs: replace BTRFS_NUM_MIRRORS with BTRFS_MAX_MIRRORS
The chunk-recover.c/BTRFS_NUM_MIRRORS in the userspace means
the same thing as ctree.h/BTRFS_MAX_MIRRORS in the kernelspace,
so to stay consistent with the kernelspace, just make this movement
in the userspace:
	chunk-recover.c/BTRFS_NUM_MIRRORS
		===>
	ctree.h/BTRFS_MAX_MIRRORS

This provides convenience for future use.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-08-22 14:43:11 +02:00