2008-01-04 15:38:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Oracle. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2015-01-21 16:49:26 +00:00
|
|
|
#ifndef __BTRFS_UTILS_H__
|
|
|
|
#define __BTRFS_UTILS_H__
|
2008-03-24 19:03:18 +00:00
|
|
|
|
2022-09-15 11:59:39 +00:00
|
|
|
#include "kerncompat.h"
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
btrfs-progs: convert: insert a dummy inode item before inode ref for ext2/4
[BUG]
There is a report about failed btrfs-convert, which shows the following
error:
Create btrfs metadata
corrupt leaf: root=5 block=5001931145216 slot=1 ino=89911763, invalid previous key objectid, have 89911762 expect 89911763
leaf 5001931145216 items 336 free space 7 generation 90 owner FS_TREE
leaf 5001931145216 flags 0x1(WRITTEN) backref revision 1
fs uuid 8b69f018-37c3-4b30-b859-42ccfcbe2449
chunk uuid 448ce78c-ea41-49f6-99dc-46ad80b93da9
item 0 key (89911762 INODE_REF 3858733) itemoff 16222 itemsize 61
index 171 namelen 51 name: [FILENAME1]
item 1 key (89911763 INODE_REF 3858733) itemoff 16161 itemsize 61
index 103 namelen 51 name: [FILENAME2]
[CAUSE]
When iterating a directory, btrfs-convert would insert the DIR_ITEMs,
along with the INODE_REF of that inode.
This leads to above stray INODE_REFs, and trigger the tree-checker.
This can only happen for large fs, as for most cases we have all these
modified tree blocks cached, thus tree-checker won't be triggered.
But when the tree block cache is not hit, and we have to read from disk,
then such behavior can lead to above tree-checker error.
[FIX]
Insert a dummy INODE_ITEM for the INODE_REF first, the inode items would
be updated when iterating the child inode of the directory.
Issue: #731
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-01-13 08:37:06 +00:00
|
|
|
#include <sys/stat.h>
|
2023-08-10 13:01:21 +00:00
|
|
|
#include "kernel-lib/list.h"
|
2023-08-28 20:12:13 +00:00
|
|
|
#include "kernel-shared/volumes.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/fsfeatures.h"
|
2013-01-28 05:22:30 +00:00
|
|
|
|
2023-08-28 20:12:13 +00:00
|
|
|
struct list_head;
|
|
|
|
|
2020-11-05 00:06:29 +00:00
|
|
|
enum exclusive_operation {
|
|
|
|
BTRFS_EXCLOP_NONE,
|
|
|
|
BTRFS_EXCLOP_BALANCE,
|
2022-05-03 20:48:14 +00:00
|
|
|
BTRFS_EXCLOP_BALANCE_PAUSED,
|
2020-11-05 00:06:29 +00:00
|
|
|
BTRFS_EXCLOP_DEV_ADD,
|
|
|
|
BTRFS_EXCLOP_DEV_REMOVE,
|
|
|
|
BTRFS_EXCLOP_DEV_REPLACE,
|
|
|
|
BTRFS_EXCLOP_RESIZE,
|
|
|
|
BTRFS_EXCLOP_SWAP_ACTIVATE,
|
|
|
|
BTRFS_EXCLOP_UNKNOWN = -1,
|
|
|
|
};
|
|
|
|
|
btrfs-progs: convert: insert a dummy inode item before inode ref for ext2/4
[BUG]
There is a report about failed btrfs-convert, which shows the following
error:
Create btrfs metadata
corrupt leaf: root=5 block=5001931145216 slot=1 ino=89911763, invalid previous key objectid, have 89911762 expect 89911763
leaf 5001931145216 items 336 free space 7 generation 90 owner FS_TREE
leaf 5001931145216 flags 0x1(WRITTEN) backref revision 1
fs uuid 8b69f018-37c3-4b30-b859-42ccfcbe2449
chunk uuid 448ce78c-ea41-49f6-99dc-46ad80b93da9
item 0 key (89911762 INODE_REF 3858733) itemoff 16222 itemsize 61
index 171 namelen 51 name: [FILENAME1]
item 1 key (89911763 INODE_REF 3858733) itemoff 16161 itemsize 61
index 103 namelen 51 name: [FILENAME2]
[CAUSE]
When iterating a directory, btrfs-convert would insert the DIR_ITEMs,
along with the INODE_REF of that inode.
This leads to above stray INODE_REFs, and trigger the tree-checker.
This can only happen for large fs, as for most cases we have all these
modified tree blocks cached, thus tree-checker won't be triggered.
But when the tree block cache is not hit, and we have to read from disk,
then such behavior can lead to above tree-checker error.
[FIX]
Insert a dummy INODE_ITEM for the INODE_REF first, the inode items would
be updated when iterating the child inode of the directory.
Issue: #731
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-01-13 08:37:06 +00:00
|
|
|
static inline u32 btrfs_type_to_imode(u8 type)
|
|
|
|
{
|
|
|
|
static u32 imode_by_btrfs_type[] = {
|
|
|
|
[BTRFS_FT_REG_FILE] = S_IFREG,
|
|
|
|
[BTRFS_FT_DIR] = S_IFDIR,
|
|
|
|
[BTRFS_FT_CHRDEV] = S_IFCHR,
|
|
|
|
[BTRFS_FT_BLKDEV] = S_IFBLK,
|
|
|
|
[BTRFS_FT_FIFO] = S_IFIFO,
|
|
|
|
[BTRFS_FT_SOCK] = S_IFSOCK,
|
|
|
|
[BTRFS_FT_SYMLINK] = S_IFLNK,
|
|
|
|
};
|
|
|
|
|
|
|
|
return imode_by_btrfs_type[(type)];
|
|
|
|
}
|
|
|
|
|
2021-08-26 06:40:34 +00:00
|
|
|
/* 2 for "0x", 2 for each byte, plus nul */
|
|
|
|
#define BTRFS_CSUM_STRING_LEN (2 + 2 * BTRFS_CSUM_SIZE + 1)
|
2021-08-26 06:40:35 +00:00
|
|
|
void btrfs_format_csum(u16 csum_type, const u8 *data, char *output);
|
2016-09-29 13:12:35 +00:00
|
|
|
int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
|
2012-05-15 10:05:44 +00:00
|
|
|
struct btrfs_ioctl_dev_info_args **di_ret);
|
2017-09-27 02:01:43 +00:00
|
|
|
int get_fsid(const char *path, u8 *fsid, int silent);
|
2020-08-25 15:03:35 +00:00
|
|
|
int get_fsid_fd(int fd, u8 *fsid);
|
2020-11-05 00:06:29 +00:00
|
|
|
int get_fs_exclop(int fd);
|
2020-11-05 02:38:00 +00:00
|
|
|
int check_running_fs_exclop(int fd, enum exclusive_operation start, bool enqueue);
|
2020-11-05 00:06:29 +00:00
|
|
|
const char *get_fs_exclop_name(int op);
|
2017-09-27 02:02:19 +00:00
|
|
|
|
2014-12-25 01:16:34 +00:00
|
|
|
int check_arg_type(const char *input);
|
2016-03-22 15:56:33 +00:00
|
|
|
int ask_user(const char *question);
|
2016-10-31 09:34:37 +00:00
|
|
|
int lookup_path_rootid(int fd, u64 *rootid);
|
2021-01-13 14:28:43 +00:00
|
|
|
int find_mount_fsroot(const char *subvol, const char *subvolid, char **mount);
|
2014-02-10 07:28:28 +00:00
|
|
|
int find_mount_root(const char *path, char **mount_root);
|
2019-06-19 19:41:58 +00:00
|
|
|
int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret);
|
2013-09-18 16:19:30 +00:00
|
|
|
|
2016-03-22 15:56:33 +00:00
|
|
|
const char *subvol_strip_mountpoint(const char *mnt, const char *full_path);
|
Btrfs-progs: check, ability to detect and fix outdated snapshot root items
This change adds code to detect and fix the issue introduced in the kernel
release 3.17, where creation of read-only snapshots lead to a corrupted
filesystem if they were created at a moment when the source subvolume/snapshot
had orphan items. The issue was that the on-disk root items became incorrect,
referring to the pre orphan cleanup root node instead of the post orphan
cleanup root node.
A test filesystem can be generated with the test case recently submitted for
xfstests/fstests, which is essencially the following (bash script):
workout()
{
ops=$1
procs=$2
num_snapshots=$3
_scratch_mkfs >> $seqres.full 2>&1
_scratch_mount
snapshot_cmd="$BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT"
snapshot_cmd="$snapshot_cmd $SCRATCH_MNT/snap_\`date +'%H_%M_%S_%N'\`"
run_check $FSSTRESS_PROG -p $procs \
-x "$snapshot_cmd" -X $num_snapshots -d $SCRATCH_MNT -n $ops
}
ops=10000
procs=4
snapshots=500
workout $ops $procs $snapshots
Example of btrfsck's (btrfs check) behaviour against such filesystem:
$ btrfsck /dev/loop0
root item for root 311, current bytenr 44630016, current gen 60, current level 1, new bytenr 44957696, new gen 61, new level 1
root item for root 1480, current bytenr 1003569152, current gen 1271, current level 1, new bytenr 1004175360, new gen 1272, new level 1
root item for root 1509, current bytenr 1037434880, current gen 1300, current level 1, new bytenr 1038467072, new gen 1301, new level 1
root item for root 1562, current bytenr 33636352, current gen 1354, current level 1, new bytenr 34455552, new gen 1355, new level 1
root item for root 3094, current bytenr 1011712000, current gen 2935, current level 1, new bytenr 1008484352, new gen 2936, new level 1
root item for root 3716, current bytenr 80805888, current gen 3578, current level 1, new bytenr 73515008, new gen 3579, new level 1
root item for root 4085, current bytenr 714031104, current gen 3958, current level 1, new bytenr 716816384, new gen 3959, new level 1
Found 7 roots with an outdated root item.
Please run a filesystem check with the option --repair to fix them.
$ echo $?
1
$ btrfsck --repair /dev/loop0
enabling repair mode
fixing root item for root 311, current bytenr 44630016, current gen 60, current level 1, new bytenr 44957696, new gen 61, new level 1
fixing root item for root 1480, current bytenr 1003569152, current gen 1271, current level 1, new bytenr 1004175360, new gen 1272, new level 1
fixing root item for root 1509, current bytenr 1037434880, current gen 1300, current level 1, new bytenr 1038467072, new gen 1301, new level 1
fixing root item for root 1562, current bytenr 33636352, current gen 1354, current level 1, new bytenr 34455552, new gen 1355, new level 1
fixing root item for root 3094, current bytenr 1011712000, current gen 2935, current level 1, new bytenr 1008484352, new gen 2936, new level 1
fixing root item for root 3716, current bytenr 80805888, current gen 3578, current level 1, new bytenr 73515008, new gen 3579, new level 1
fixing root item for root 4085, current bytenr 714031104, current gen 3958, current level 1, new bytenr 716816384, new gen 3959, new level 1
Fixed 7 roots.
Checking filesystem on /dev/loop0
UUID: 2186e9b9-c977-4a35-9c7b-69c6609d4620
checking extents
checking free space cache
cache and super generation don't match, space cache will be invalidated
checking fs roots
checking csums
checking root refs
found 618537000 bytes used err is 0
total csum bytes: 130824
total tree bytes: 601620480
total fs tree bytes: 580288512
total extent tree bytes: 18464768
btree space waste bytes: 136939144
file data blocks allocated: 34150318080
referenced 27815415808
Btrfs v3.17-rc3-2-gbbe1dd8
$ echo $?
0
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-10-17 17:20:08 +00:00
|
|
|
int find_next_key(struct btrfs_path *path, struct btrfs_key *key);
|
2016-03-22 16:01:20 +00:00
|
|
|
const char* btrfs_group_type_str(u64 flag);
|
|
|
|
const char* btrfs_group_profile_str(u64 flag);
|
Btrfs-progs: check, ability to detect and fix outdated snapshot root items
This change adds code to detect and fix the issue introduced in the kernel
release 3.17, where creation of read-only snapshots lead to a corrupted
filesystem if they were created at a moment when the source subvolume/snapshot
had orphan items. The issue was that the on-disk root items became incorrect,
referring to the pre orphan cleanup root node instead of the post orphan
cleanup root node.
A test filesystem can be generated with the test case recently submitted for
xfstests/fstests, which is essencially the following (bash script):
workout()
{
ops=$1
procs=$2
num_snapshots=$3
_scratch_mkfs >> $seqres.full 2>&1
_scratch_mount
snapshot_cmd="$BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT"
snapshot_cmd="$snapshot_cmd $SCRATCH_MNT/snap_\`date +'%H_%M_%S_%N'\`"
run_check $FSSTRESS_PROG -p $procs \
-x "$snapshot_cmd" -X $num_snapshots -d $SCRATCH_MNT -n $ops
}
ops=10000
procs=4
snapshots=500
workout $ops $procs $snapshots
Example of btrfsck's (btrfs check) behaviour against such filesystem:
$ btrfsck /dev/loop0
root item for root 311, current bytenr 44630016, current gen 60, current level 1, new bytenr 44957696, new gen 61, new level 1
root item for root 1480, current bytenr 1003569152, current gen 1271, current level 1, new bytenr 1004175360, new gen 1272, new level 1
root item for root 1509, current bytenr 1037434880, current gen 1300, current level 1, new bytenr 1038467072, new gen 1301, new level 1
root item for root 1562, current bytenr 33636352, current gen 1354, current level 1, new bytenr 34455552, new gen 1355, new level 1
root item for root 3094, current bytenr 1011712000, current gen 2935, current level 1, new bytenr 1008484352, new gen 2936, new level 1
root item for root 3716, current bytenr 80805888, current gen 3578, current level 1, new bytenr 73515008, new gen 3579, new level 1
root item for root 4085, current bytenr 714031104, current gen 3958, current level 1, new bytenr 716816384, new gen 3959, new level 1
Found 7 roots with an outdated root item.
Please run a filesystem check with the option --repair to fix them.
$ echo $?
1
$ btrfsck --repair /dev/loop0
enabling repair mode
fixing root item for root 311, current bytenr 44630016, current gen 60, current level 1, new bytenr 44957696, new gen 61, new level 1
fixing root item for root 1480, current bytenr 1003569152, current gen 1271, current level 1, new bytenr 1004175360, new gen 1272, new level 1
fixing root item for root 1509, current bytenr 1037434880, current gen 1300, current level 1, new bytenr 1038467072, new gen 1301, new level 1
fixing root item for root 1562, current bytenr 33636352, current gen 1354, current level 1, new bytenr 34455552, new gen 1355, new level 1
fixing root item for root 3094, current bytenr 1011712000, current gen 2935, current level 1, new bytenr 1008484352, new gen 2936, new level 1
fixing root item for root 3716, current bytenr 80805888, current gen 3578, current level 1, new bytenr 73515008, new gen 3579, new level 1
fixing root item for root 4085, current bytenr 714031104, current gen 3958, current level 1, new bytenr 716816384, new gen 3959, new level 1
Fixed 7 roots.
Checking filesystem on /dev/loop0
UUID: 2186e9b9-c977-4a35-9c7b-69c6609d4620
checking extents
checking free space cache
cache and super generation don't match, space cache will be invalidated
checking fs roots
checking csums
checking root refs
found 618537000 bytes used err is 0
total csum bytes: 130824
total tree bytes: 601620480
total fs tree bytes: 580288512
total extent tree bytes: 18464768
btree space waste bytes: 136939144
file data blocks allocated: 34150318080
referenced 27815415808
Btrfs v3.17-rc3-2-gbbe1dd8
$ echo $?
0
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-10-17 17:20:08 +00:00
|
|
|
|
2017-02-01 15:09:47 +00:00
|
|
|
int count_digits(u64 num);
|
|
|
|
u64 div_factor(u64 num, int factor);
|
2014-12-19 14:31:00 +00:00
|
|
|
|
2017-07-25 20:51:34 +00:00
|
|
|
unsigned long total_memory(void);
|
|
|
|
|
2017-12-05 08:39:44 +00:00
|
|
|
void print_device_info(struct btrfs_device *device, char *prefix);
|
|
|
|
void print_all_devices(struct list_head *devices);
|
|
|
|
|
2019-11-25 10:39:03 +00:00
|
|
|
#define BTRFS_BCONF_UNSET -1
|
|
|
|
#define BTRFS_BCONF_QUIET 0
|
2016-11-21 12:52:01 +00:00
|
|
|
/*
|
|
|
|
* Global program state, configurable by command line and available to
|
|
|
|
* functions without extra context passing.
|
|
|
|
*/
|
|
|
|
struct btrfs_config {
|
2018-03-06 02:29:30 +00:00
|
|
|
unsigned int output_format;
|
2019-11-25 10:39:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Values:
|
|
|
|
* BTRFS_BCONF_QUIET
|
|
|
|
* BTRFS_BCONF_UNSET
|
|
|
|
* > 0: verbose level
|
|
|
|
*/
|
|
|
|
int verbose;
|
2023-10-24 12:35:06 +00:00
|
|
|
/* Command line request to skip any modification actions. */
|
|
|
|
int dry_run;
|
2023-08-10 13:01:21 +00:00
|
|
|
struct list_head params;
|
2016-11-21 12:52:01 +00:00
|
|
|
};
|
|
|
|
extern struct btrfs_config bconf;
|
|
|
|
|
2023-08-10 13:01:21 +00:00
|
|
|
struct config_param {
|
|
|
|
struct list_head list;
|
|
|
|
const char *key;
|
|
|
|
const char *value;
|
|
|
|
};
|
|
|
|
|
2016-11-21 12:52:01 +00:00
|
|
|
void btrfs_config_init(void);
|
2019-11-25 10:39:03 +00:00
|
|
|
void bconf_be_verbose(void);
|
|
|
|
void bconf_be_quiet(void);
|
2023-08-10 13:01:21 +00:00
|
|
|
void bconf_add_param(const char *key, const char *value);
|
|
|
|
void bconf_save_param(const char *str);
|
2023-10-24 12:35:06 +00:00
|
|
|
void bconf_set_dry_run(void);
|
|
|
|
bool bconf_is_dry_run(void);
|
2023-08-10 13:01:21 +00:00
|
|
|
const char *bconf_param_value(const char *key);
|
2016-11-21 12:52:01 +00:00
|
|
|
|
2016-05-26 02:56:50 +00:00
|
|
|
/* Pseudo random number generator wrappers */
|
2017-02-01 15:09:47 +00:00
|
|
|
int rand_int(void);
|
|
|
|
u8 rand_u8(void);
|
|
|
|
u16 rand_u16(void);
|
2016-05-26 02:56:50 +00:00
|
|
|
u32 rand_u32(void);
|
2017-02-01 15:09:47 +00:00
|
|
|
u64 rand_u64(void);
|
2016-05-26 02:56:50 +00:00
|
|
|
unsigned int rand_range(unsigned int upper);
|
|
|
|
void init_rand_seed(u64 seed);
|
|
|
|
|
2024-01-16 03:31:25 +00:00
|
|
|
bool get_env_bool(const char *env_name);
|
|
|
|
|
2020-05-04 20:03:09 +00:00
|
|
|
char *btrfs_test_for_multiple_profiles(int fd);
|
2020-05-04 12:34:59 +00:00
|
|
|
int btrfs_warn_multiple_profiles(int fd);
|
2022-10-20 14:29:13 +00:00
|
|
|
void btrfs_warn_experimental(const char *str);
|
2020-04-04 10:32:08 +00:00
|
|
|
|
2022-11-23 22:37:13 +00:00
|
|
|
/* An error code to error string mapping for the kernel error codes */
|
|
|
|
static inline char *btrfs_err_str(enum btrfs_err_code err_code)
|
|
|
|
{
|
|
|
|
switch (err_code) {
|
|
|
|
case BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET:
|
|
|
|
return "unable to go below two devices on raid1";
|
|
|
|
case BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET:
|
|
|
|
return "unable to go below three devices on raid1c3";
|
|
|
|
case BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET:
|
|
|
|
return "unable to go below four devices on raid1c4";
|
|
|
|
case BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET:
|
|
|
|
return "unable to go below four/two devices on raid10";
|
|
|
|
case BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET:
|
|
|
|
return "unable to go below two devices on raid5";
|
|
|
|
case BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET:
|
|
|
|
return "unable to go below three devices on raid6";
|
|
|
|
case BTRFS_ERROR_DEV_TGT_REPLACE:
|
|
|
|
return "unable to remove the dev_replace target dev";
|
|
|
|
case BTRFS_ERROR_DEV_MISSING_NOT_FOUND:
|
|
|
|
return "no missing devices found to remove";
|
|
|
|
case BTRFS_ERROR_DEV_ONLY_WRITABLE:
|
|
|
|
return "unable to remove the only writeable device";
|
|
|
|
case BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS:
|
|
|
|
return "add/delete/balance/replace/resize operation "
|
|
|
|
"in progress";
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-04 15:38:22 +00:00
|
|
|
#endif
|