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"
|
btrfs-progs: Introduce warning and error for common use
Current code use fprintf(stderr, "...") to output warnning and
error information.
The error message have different style, as:
# grep fprintf *.c
fprintf(stderr, "Open ctree failed\n");
fprintf(stderr, "%s: open ctree failed\n", __func__);
fprintf(stderr, "ERROR: cannot open ctree\n");
...
And sometimes, we forgot add tailed '\n', or use printf instead,
as in current code:
printf("warning, device %llu is missing\n",
This patch introduce warning() and error() as common function,
to make:
1: Each warning and error information have same format
2: Easy to search/change all error message
3: Easy to modify function's internal for debug or other requirement,
for example:
print function/linenumber in error()
dumpstack in error()
add some trace for some style of message
add support for -v, -vv, ...
support for locales
custom output functions
support some special device/tty
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
[print newline after the message]
Signed-off-by: David Sterba <dsterba@suse.com>
2015-09-28 13:58:13 +00:00
|
|
|
#include <stdarg.h>
|
2022-09-15 11:59:39 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "kernel-lib/sizes.h"
|
|
|
|
#include "kernel-shared/ctree.h"
|
2020-03-04 20:09:30 +00:00
|
|
|
#include "common/defs.h"
|
2019-06-19 22:44:36 +00:00
|
|
|
#include "common/internal.h"
|
2019-06-19 22:44:36 +00:00
|
|
|
#include "common/messages.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/fsfeatures.h"
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "ioctl.h"
|
2013-01-28 05:22:30 +00:00
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
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);
|
2014-02-13 19:19:44 +00:00
|
|
|
int get_device_info(int fd, u64 devid,
|
|
|
|
struct btrfs_ioctl_dev_info_args *di_args);
|
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;
|
2016-11-21 12:52:01 +00:00
|
|
|
};
|
|
|
|
extern struct btrfs_config bconf;
|
|
|
|
|
|
|
|
void btrfs_config_init(void);
|
2019-11-25 10:39:03 +00:00
|
|
|
void bconf_be_verbose(void);
|
|
|
|
void bconf_be_quiet(void);
|
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);
|
|
|
|
|
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);
|
2020-04-04 10:32:08 +00:00
|
|
|
|
2021-04-15 05:30:11 +00:00
|
|
|
int sysfs_open_file(const char *name);
|
2020-08-25 15:03:36 +00:00
|
|
|
int sysfs_open_fsid_file(int fd, const char *filename);
|
|
|
|
int sysfs_read_file(int fd, char *buf, size_t size);
|
2021-06-24 20:10:11 +00:00
|
|
|
int sysfs_open_fsid_dir(int fd, const char *dirname);
|
2020-08-25 15:03:36 +00:00
|
|
|
|
2008-01-04 15:38:22 +00:00
|
|
|
#endif
|