btrfs-progs/common/utils.c

1654 lines
36 KiB
C
Raw Permalink Normal View History

/*
* Copyright (C) 2007 Oracle. All rights reserved.
* Copyright (C) 2008 Morey Roof. 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2008-03-24 19:04:49 +00:00
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
#include <uuid/uuid.h>
#include <fcntl.h>
#include <unistd.h>
#include <mntent.h>
#include <ctype.h>
#include <limits.h>
#include <blkid/blkid.h>
#include <sys/vfs.h>
#include <sys/statfs.h>
#include <linux/magic.h>
#include <getopt.h>
#include <btrfsutil.h>
#include "kerncompat.h"
#include "kernel-lib/radix-tree.h"
#include "kernel-shared/ctree.h"
#include "kernel-shared/disk-io.h"
#include "kernel-shared/transaction.h"
#include "crypto/crc32c.h"
#include "common/utils.h"
#include "common/path-utils.h"
#include "common/device-scan.h"
#include "common/parse-utils.h"
#include "kernel-shared/volumes.h"
#include "ioctl.h"
#include "cmds/commands.h"
#include "common/open-utils.h"
#include "mkfs/common.h"
2008-03-24 19:04:49 +00:00
static int rand_seed_initialized = 0;
static unsigned short rand_seed[3];
struct btrfs_config bconf;
struct pending_dir {
struct list_head list;
char name[PATH_MAX];
};
/*
* Checks to make sure that the label matches our requirements.
* Returns:
0 if everything is safe and usable
-1 if the label is too long
*/
static int check_label(const char *input)
{
int len = strlen(input);
if (len > BTRFS_LABEL_SIZE - 1) {
error("label %s is too long (max %d)", input,
BTRFS_LABEL_SIZE - 1);
return -1;
}
return 0;
}
static int set_label_unmounted(const char *dev, const char *label)
{
struct btrfs_trans_handle *trans;
struct btrfs_root *root;
int ret;
ret = check_mounted(dev);
if (ret < 0) {
error("checking mount status of %s failed: %d", dev, ret);
return -1;
}
if (ret > 0) {
error("device %s is mounted, use mount point", dev);
return -1;
}
/* Open the super_block at the default location
* and as read-write.
*/
root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
if (!root) /* errors are printed by open_ctree() */
return -1;
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
__strncpy_null(root->fs_info->super_copy->label, label, BTRFS_LABEL_SIZE - 1);
btrfs_commit_transaction(trans, root);
/* Now we close it since we are done. */
close_ctree(root);
return 0;
}
static int set_label_mounted(const char *mount_path, const char *labelp)
{
int fd;
char label[BTRFS_LABEL_SIZE];
fd = open(mount_path, O_RDONLY | O_NOATIME);
if (fd < 0) {
error("unable to access %s: %m", mount_path);
return -1;
}
memset(label, 0, sizeof(label));
__strncpy_null(label, labelp, BTRFS_LABEL_SIZE - 1);
if (ioctl(fd, BTRFS_IOC_SET_FSLABEL, label) < 0) {
error("unable to set label of %s: %m", mount_path);
close(fd);
return -1;
}
close(fd);
return 0;
}
int get_label_unmounted(const char *dev, char *label)
{
struct btrfs_root *root;
int ret;
ret = check_mounted(dev);
if (ret < 0) {
error("checking mount status of %s failed: %d", dev, ret);
return -1;
}
/* Open the super_block at the default location
* and as read-only.
*/
root = open_ctree(dev, 0, 0);
if(!root)
return -1;
__strncpy_null(label, root->fs_info->super_copy->label,
BTRFS_LABEL_SIZE - 1);
/* Now we close it since we are done. */
close_ctree(root);
return 0;
}
/*
* If a partition is mounted, try to get the filesystem label via its
* mounted path rather than device. Return the corresponding error
* the user specified the device path.
*/
int get_label_mounted(const char *mount_path, char *labelp)
{
char label[BTRFS_LABEL_SIZE];
int fd;
int ret;
fd = open(mount_path, O_RDONLY | O_NOATIME);
if (fd < 0) {
error("unable to access %s: %m", mount_path);
return -1;
}
memset(label, '\0', sizeof(label));
ret = ioctl(fd, BTRFS_IOC_GET_FSLABEL, label);
if (ret < 0) {
if (errno != ENOTTY)
error("unable to get label of %s: %m", mount_path);
ret = -errno;
close(fd);
return ret;
}
__strncpy_null(labelp, label, BTRFS_LABEL_SIZE - 1);
close(fd);
return 0;
}
int get_label(const char *btrfs_dev, char *label)
{
int ret;
ret = path_is_reg_or_block_device(btrfs_dev);
if (!ret)
ret = get_label_mounted(btrfs_dev, label);
else if (ret > 0)
ret = get_label_unmounted(btrfs_dev, label);
return ret;
}
int set_label(const char *btrfs_dev, const char *label)
{
int ret;
if (check_label(label))
return -1;
ret = path_is_reg_or_block_device(btrfs_dev);
if (!ret)
ret = set_label_mounted(btrfs_dev, label);
else if (ret > 0)
ret = set_label_unmounted(btrfs_dev, label);
return ret;
}
u64 parse_qgroupid_or_path(const char *p)
{
enum btrfs_util_error err;
u64 id;
u64 qgroupid;
int fd;
int ret = 0;
if (p[0] == '/')
goto path;
ret = parse_qgroupid(p, &qgroupid);
if (ret < 0)
goto err;
return qgroupid;
path:
/* Path format like subv at 'my_subvol' is the fallback case */
err = btrfs_util_is_subvolume(p);
if (err)
goto err;
fd = open(p, O_RDONLY);
if (fd < 0)
goto err;
ret = lookup_path_rootid(fd, &id);
if (ret) {
errno = -ret;
error("failed to lookup root id: %m");
}
close(fd);
if (ret < 0)
goto err;
return id;
err:
error("invalid qgroupid or subvolume path: %s", p);
exit(-1);
}
void btrfs_format_csum(u16 csum_type, const u8 *data, char *output)
{
int i;
int cur = 0;
const int csum_size = btrfs_csum_type_size(csum_type);
output[0] = '\0';
snprintf(output, BTRFS_CSUM_STRING_LEN, "0x");
cur += strlen("0x");
for (i = 0; i < csum_size; i++) {
snprintf(output + cur, BTRFS_CSUM_STRING_LEN - cur, "%02x",
data[i]);
cur += 2;
}
}
int get_device_info(int fd, u64 devid,
struct btrfs_ioctl_dev_info_args *di_args)
{
int ret;
di_args->devid = devid;
memset(&di_args->uuid, '\0', sizeof(di_args->uuid));
ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args);
return ret < 0 ? -errno : 0;
}
int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
{
u64 count = 0;
int ret;
struct btrfs_ioctl_space_args *sargs;
sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
if (!sargs)
return -ENOMEM;
sargs->space_slots = 0;
sargs->total_spaces = 0;
ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
if (ret < 0) {
error("cannot get space info: %m");
free(sargs);
return -errno;
}
/* This really should never happen */
if (!sargs->total_spaces) {
free(sargs);
return -ENOENT;
}
count = sargs->total_spaces;
free(sargs);
sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
(count * sizeof(struct btrfs_ioctl_space_info)));
if (!sargs)
return -ENOMEM;
sargs->space_slots = count;
sargs->total_spaces = 0;
ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
if (ret < 0) {
error("cannot get space info with %llu slots: %m",
count);
free(sargs);
return -errno;
}
*sargs_ret = sargs;
return 0;
}
static u64 find_max_device_id(struct btrfs_ioctl_search_args *search_args,
int nr_items)
{
struct btrfs_dev_item *dev_item;
char *buf = search_args->buf;
buf += (nr_items - 1) * (sizeof(struct btrfs_ioctl_search_header)
+ sizeof(struct btrfs_dev_item));
buf += sizeof(struct btrfs_ioctl_search_header);
dev_item = (struct btrfs_dev_item *)buf;
return btrfs_stack_device_id(dev_item);
}
static int search_chunk_tree_for_fs_info(int fd,
struct btrfs_ioctl_fs_info_args *fi_args)
{
int ret;
int max_items;
u64 start_devid = 1;
struct btrfs_ioctl_search_args search_args;
struct btrfs_ioctl_search_key *search_key = &search_args.key;
fi_args->num_devices = 0;
max_items = BTRFS_SEARCH_ARGS_BUFSIZE
/ (sizeof(struct btrfs_ioctl_search_header)
+ sizeof(struct btrfs_dev_item));
search_key->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
search_key->min_objectid = BTRFS_DEV_ITEMS_OBJECTID;
search_key->max_objectid = BTRFS_DEV_ITEMS_OBJECTID;
search_key->min_type = BTRFS_DEV_ITEM_KEY;
search_key->max_type = BTRFS_DEV_ITEM_KEY;
search_key->min_transid = 0;
search_key->max_transid = (u64)-1;
search_key->nr_items = max_items;
search_key->max_offset = (u64)-1;
again:
search_key->min_offset = start_devid;
ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &search_args);
if (ret < 0)
return -errno;
fi_args->num_devices += (u64)search_key->nr_items;
if (search_key->nr_items == max_items) {
start_devid = find_max_device_id(&search_args,
search_key->nr_items) + 1;
goto again;
}
/* Get the latest max_id to stay consistent with the num_devices */
if (search_key->nr_items == 0)
/*
* last tree_search returns an empty buf, use the devid of
* the last dev_item of the previous tree_search
*/
fi_args->max_id = start_devid - 1;
else
fi_args->max_id = find_max_device_id(&search_args,
search_key->nr_items);
return 0;
}
/*
* For a given path, fill in the ioctl fs_ and info_ args.
* If the path is a btrfs mountpoint, fill info for all devices.
* If the path is a btrfs device, fill in only that device.
*
* The path provided must be either on a mounted btrfs fs,
* or be a mounted btrfs device.
*
* Returns 0 on success, or a negative errno.
*/
int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
struct btrfs_ioctl_dev_info_args **di_ret)
{
int fd = -1;
int ret = 0;
int ndevs = 0;
u64 last_devid = 0;
int replacing = 0;
struct btrfs_fs_devices *fs_devices_mnt = NULL;
struct btrfs_ioctl_dev_info_args *di_args;
struct btrfs_ioctl_dev_info_args tmp;
char mp[PATH_MAX];
DIR *dirstream = NULL;
memset(fi_args, 0, sizeof(*fi_args));
if (path_is_block_device(path) == 1) {
struct btrfs_super_block disk_super;
/* Ensure it's mounted, then set path to the mountpoint */
fd = open(path, O_RDONLY);
if (fd < 0) {
ret = -errno;
error("cannot open %s: %m", path);
goto out;
}
ret = check_mounted_where(fd, path, mp, sizeof(mp),
&fs_devices_mnt, SBREAD_DEFAULT);
if (!ret) {
ret = -EINVAL;
goto out;
}
if (ret < 0)
goto out;
path = mp;
/* Only fill in this one device */
fi_args->num_devices = 1;
ret = btrfs_read_dev_super(fd, &disk_super,
BTRFS_SUPER_INFO_OFFSET, 0);
if (ret < 0) {
ret = -EIO;
goto out;
}
last_devid = btrfs_stack_device_id(&disk_super.dev_item);
fi_args->max_id = last_devid;
memcpy(fi_args->fsid, fs_devices_mnt->fsid, BTRFS_FSID_SIZE);
close(fd);
}
/* at this point path must not be for a block device */
fd = open_file_or_dir(path, &dirstream);
if (fd < 0) {
ret = -errno;
goto out;
}
/* fill in fi_args if not just a single device */
if (fi_args->num_devices != 1) {
ret = ioctl(fd, BTRFS_IOC_FS_INFO, fi_args);
if (ret < 0) {
ret = -errno;
goto out;
}
/*
* The fs_args->num_devices does not include seed devices
*/
ret = search_chunk_tree_for_fs_info(fd, fi_args);
if (ret)
goto out;
/*
* search_chunk_tree_for_fs_info() will lacks the devid 0
* so manual probe for it here.
*/
ret = get_device_info(fd, 0, &tmp);
if (!ret) {
fi_args->num_devices++;
ndevs++;
replacing = 1;
if (last_devid == 0)
last_devid++;
}
}
if (!fi_args->num_devices)
goto out;
di_args = *di_ret = malloc((fi_args->num_devices) * sizeof(*di_args));
if (!di_args) {
ret = -errno;
goto out;
}
if (replacing)
memcpy(di_args, &tmp, sizeof(tmp));
btrfs-progs: fix invalid memory write in get_fs_info() As the link reported, btrfs fi sh may crash while a device is removing. valgrind reported: ====================================================================== ... ==883== Invalid write of size 8 ==883== at 0x13C99A: get_device_info (in /usr/bin/btrfs) ==883== by 0x13D715: get_fs_info (in /usr/bin/btrfs) ==883== by 0x153B5F: ??? (in /usr/bin/btrfs) ==883== by 0x11B0C1: main (in /usr/bin/btrfs) ==883== Address 0x4d8c7a0 is 0 bytes after a block of size 12,288 alloc'd ==883== at 0x483877F: malloc (vg_replace_malloc.c:299) ==883== by 0x13D861: get_fs_info (in /usr/bin/btrfs) ==883== by 0x153B5F: ??? (in /usr/bin/btrfs) ==883== by 0x11B0C1: main (in /usr/bin/btrfs) ==883== ==883== Invalid write of size 8 ==883== at 0x13C99D: get_device_info (in /usr/bin/btrfs) ==883== by 0x13D715: get_fs_info (in /usr/bin/btrfs) ==883== by 0x153B5F: ??? (in /usr/bin/btrfs) ==883== by 0x11B0C1: main (in /usr/bin/btrfs) ==883== Address 0x4d8c7a8 is 8 bytes after a block of size 12,288 alloc'd ==883== at 0x483877F: malloc (vg_replace_malloc.c:299) ==883== by 0x13D861: get_fs_info (in /usr/bin/btrfs) ==883== by 0x153B5F: ??? (in /usr/bin/btrfs) ==883== by 0x11B0C1: main (in /usr/bin/btrfs) ==883== ==883== Syscall param ioctl(generic) points to unaddressable byte(s) ==883== at 0x4CA9CBB: ioctl (in /usr/lib/libc-2.29.so) ==883== by 0x13C9AB: get_device_info (in /usr/bin/btrfs) ==883== by 0x13D715: get_fs_info (in /usr/bin/btrfs) ==883== by 0x153B5F: ??? (in /usr/bin/btrfs) ==883== by 0x11B0C1: main (in /usr/bin/btrfs) ==883== Address 0x4d8c7a0 is 0 bytes after a block of size 12,288 alloc'd ==883== at 0x483877F: malloc (vg_replace_malloc.c:299) ==883== by 0x13D861: get_fs_info (in /usr/bin/btrfs) ==883== by 0x153B5F: ??? (in /usr/bin/btrfs) ==883== by 0x11B0C1: main (in /usr/bin/btrfs) ==883== --883-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting --883-- si_code=1; Faulting address: 0x284D8C7B8; sp: 0x1002eb5e50 valgrind: the 'impossible' happened: Killed by fatal signal host stacktrace: ==883== at 0x5805261C: get_bszB_as_is (m_mallocfree.c:303) ==883== by 0x5805261C: get_bszB (m_mallocfree.c:315) ==883== by 0x5805261C: vgPlain_arena_malloc (m_mallocfree.c:1799) ==883== by 0x58005AD2: vgMemCheck_new_block (mc_malloc_wrappers.c:372) ==883== by 0x58005AD2: vgMemCheck_malloc (mc_malloc_wrappers.c:407) ==883== by 0x580A7373: do_client_request (scheduler.c:1925) ==883== by 0x580A7373: vgPlain_scheduler (scheduler.c:1488) ==883== by 0x580F57A0: thread_wrapper (syswrap-linux.c:103) ==883== by 0x580F57A0: run_a_thread_NORETURN (syswrap-linux.c:156) sched status: running_tid=1 Thread 1: status = VgTs_Runnable (lwpid 883) ==883== at 0x483877F: malloc (vg_replace_malloc.c:299) ==883== by 0x1534AA: ??? (in /usr/bin/btrfs) ==883== by 0x153C49: ??? (in /usr/bin/btrfs) ==883== by 0x11B0C1: main (in /usr/bin/btrfs) client stack range: [0x1FFEFFA000 0x1FFF000FFF] client SP: 0x1FFEFFDCE0 valgrind stack range: [0x1002DB6000 0x1002EB5FFF] top usage: 7520 of 1048576 ====================================================================== The above log says that invalid write to allocated @di_args happened in get_device_info() called in get_fs_info(). The size of @di_args is allocated according by fi_args->num_devices. And fi_args->num_devices is *the number of dev_items in chunk_tree*. However, in the loop to get devices info, btrfs-progs calls ioctl BTRFS_IOC_DEV_INFO which just finds device in fs_info->fs_devices->devices. Let's look at kernel side. In btrfs_rm_device(), btrfs_rm_dev_item() causes removal of related dev_items in chunk_tree. *Do something*. Then delete the device from device->fs_devices. So the case is: Userspace kernel get_fs_info() btrfs_rm_device() ... btrfs_rm_dev_item() determine fi_args->num_devices and fi_args->max_id by seraching chunk_tree. malloc() ... Loop(Crashed): call get_device_info() by devid from 1 to fi_args->max_id. mutex_lock(&fs_devices->device_list_mutex); list_del_rcu(&device->dev_list); ... In the loop of get_device_info(), get_device_info() still can get info of the removing device since it's still in fs_info->fs_devices->devices. Then the iterator value @ndev increaments causes invalid access out of bounds. Solved it by adding the check of @ndev while looping. Reported-by: Peter Hjalmarsson <kanelxake@gmail.com> Link: https://bugzilla.redhat.com/show_bug.cgi?id=1711787 Signed-off-by: Su Yue <Damenly_Su@gmx.com> Signed-off-by: David Sterba <dsterba@suse.com>
2019-06-03 01:27:54 +00:00
for (; last_devid <= fi_args->max_id && ndevs < fi_args->num_devices;
last_devid++) {
ret = get_device_info(fd, last_devid, &di_args[ndevs]);
if (ret == -ENODEV)
continue;
if (ret)
goto out;
ndevs++;
}
/*
* only when the only dev we wanted to find is not there then
* let any error be returned
*/
if (fi_args->num_devices != 1) {
BUG_ON(ndevs == 0);
ret = 0;
}
out:
close_file_or_dir(fd, dirstream);
return ret;
}
int get_fsid_fd(int fd, u8 *fsid)
{
int ret;
struct btrfs_ioctl_fs_info_args args;
ret = ioctl(fd, BTRFS_IOC_FS_INFO, &args);
if (ret < 0)
return -errno;
memcpy(fsid, args.fsid, BTRFS_FSID_SIZE);
return 0;
}
int get_fsid(const char *path, u8 *fsid, int silent)
{
int ret;
int fd;
fd = open(path, O_RDONLY);
if (fd < 0) {
if (!silent)
error("failed to open %s: %m", path);
return -errno;
}
ret = get_fsid_fd(fd, fsid);
close(fd);
return ret;
}
int test_num_disk_vs_raid(u64 metadata_profile, u64 data_profile,
btrfs-progs: mkfs: allow --data DUP for single device Current code don't support DUP profile on single device, except it is in mixed mode, because of following reasons: 1: Some SSD do deduplication internally, so the duplication on the filesystem side has no effect. 2: On a physical device, if the entire disk broken, --data DUP does not help. 3: Half performance compared to single profile. 4: We have a workaround: create multi-partition on a single device, and btffs will treat them as multi device. Instead of refusing --data DUP, we give the user a choice and print a wrning. Test: 1: Tested by xfstests Run with modified xfstests, I add test items of -d dup in single device into btrfs/* and common/rc, run tests of btrfs/*, with all mount option, no regression diffed with v4.3. 2: Tested by btrfs-progs Checked following commands in "-m dup -d dup" fs with memleck checking, all passed: mkfs.btrfs -f --data dup --metadata dup /dev/sda6 btrfs filesystem show /dev/sda6 btrfs filesystem label /dev/sda6 btrfs_label_test btrfs filesystem label /dev/sda6 btrfs device scan --all-devices btrfs device scan /dev/sda6 btrfs device scan /dev/sda6 btrfs device ready /dev/sda6 btrfs check /dev/sda6 btrfs check -s 1 /dev/sda6 btrfs check --repair /dev/sda6 btrfs check --init-csum-tree /dev/sda6 btrfs check --init-extent-tree /dev/sda6 btrfs check --check-data-csum /dev/sda6 btrfs check --qgroup-report /dev/sda6 btrfs rescue super-recover -y /dev/sda6 btrfs rescue zero-log /dev/sda6 btrfs restore -l /dev/sda6 btrfs restore /dev/sda6 / btrfs restore -s /dev/sda6 / btrfs restore -x /dev/sda6 / btrfs restore -m /dev/sda6 / btrfs restore -S /dev/sda6 / btrfs restore -v /dev/sda6 / btrfs restore -i /dev/sda6 / btrfs restore -o /dev/sda6 / btrfs restore -u0 /dev/sda6 / btrfs restore -u1 /dev/sda6 / btrfs restore -D /dev/sda6 / btrfs property list /dev/sda6 btrfs property get /dev/sda6 label btrfs property set /dev/sda6 label test btrfs property set /dev/sda6 label btrfs_label_test btrfs help btrfs help --full btrfs version btrfsck /dev/sda6 btrfs-find-root /dev/sda6 btrfs-find-root -a /dev/sda6 btrfs-map-logical -l1 /dev/sda6 btrfs-map-logical -l1 -c1 /dev/sda6 btrfs-map-logical -l1 -o /tmp/btrfs-map-logic-out /dev/sda6 btrfs-map-logical -l1 -b1 /dev/sda6 btrfs-select-super -s 0 /dev/sda6 btrfs-select-super -s 1 /dev/sda6 btrfstune -S 1 /dev/sda6 btrfstune -f -S 0 /dev/sda6 btrfstune -r /dev/sda6 btrfstune -x /dev/sda6 btrfstune -n /dev/sda6 btrfstune -f -U 00000000-0000-0000-0000-000000000000 /dev/sda6 btrfstune -f -u /dev/sda6 btrfs-calc-size /dev/sda6 btrfs-calc-size -v /dev/sda6 btrfs-calc-size -b /dev/sda6 btrfs-debug-tree /dev/sda6 btrfs-debug-tree -e /dev/sda6 btrfs-debug-tree -d /dev/sda6 btrfs-debug-tree -r /dev/sda6 btrfs-debug-tree -R /dev/sda6 btrfs-debug-tree -u /dev/sda6 btrfs-debug-tree -b 0 /dev/sda6 btrfs-debug-tree -t 0 /dev/sda6 btrfs-debug-tree -t 2 /dev/sda6 btrfs-show-super /dev/sda6 btrfs-show-super -i 0 /dev/sda6 btrfs-show-super -i 1 /dev/sda6 btrfs-show-super -i 2 /dev/sda6 btrfs-show-super -a /dev/sda6 btrfs-show-super -f /dev/sda6 btrfs-show-super -F /dev/sda6 btrfs subvolume list /mnt/btrfs-progs-tests btrfs subvolume create /mnt/btrfs-progs-tests/mysubvol btrfs subvolume list /mnt/btrfs-progs-tests btrfs subvolume get-default /mnt/btrfs-progs-tests btrfs subvolume set-default 258 /mnt/btrfs-progs-tests btrfs subvolume get-default /mnt/btrfs-progs-tests btrfs subvolume set-default /mnt/btrfs-progs-tests btrfs subvolume snapshot /mnt/btrfs-progs-tests/mysubvol /mnt/btrfs-progs-tests/mysubvol_snap btrfs subvolume list /mnt/btrfs-progs-tests btrfs subvolume find-new /mnt/btrfs-progs-tests 0 btrfs subvolume find-new /mnt/btrfs-progs-tests 0 btrfs subvolume find-new /mnt/btrfs-progs-tests/mysubvol 0 btrfs subvolume find-new /mnt/btrfs-progs-tests/mysubvol 0 btrfs subvolume show /mnt/btrfs-progs-tests btrfs subvolume show /mnt/btrfs-progs-tests/mysubvol btrfs subvolume show /mnt/btrfs-progs-tests/mysubvol_snap btrfs subvolume sync /mnt/btrfs-progs-tests btrfs subvolume delete /mnt/btrfs-progs-tests/mysubvol_snap btrfs subvolume delete /mnt/btrfs-progs-tests/mysubvol btrfs subvolume sync /mnt/btrfs-progs-tests btrfs filesystem df /mnt/btrfs-progs-tests btrfs filesystem show /mnt/btrfs-progs-tests btrfs filesystem sync /mnt/btrfs-progs-tests btrfs filesystem label /mnt/btrfs-progs-tests btrfs_label_test btrfs filesystem label /mnt/btrfs-progs-tests btrfs filesystem usage /mnt/btrfs-progs-tests btrfs filesystem defragment -s 1024 -l 2048 /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_0 btrfs filesystem defragment /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_1 btrfs filesystem defragment -f /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_2 btrfs filesystem defragment -czlib /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_3 btrfs filesystem defragment -clzo /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_4 btrfs filesystem defragment /mnt/btrfs-progs-tests/filesystem_test_dir btrfs filesystem defragment -r /mnt/btrfs-progs-tests/filesystem_test_dir btrfs filesystem defragment /mnt/btrfs-progs-tests btrfs filesystem resize 1:-10M /mnt/btrfs-progs-tests btrfs filesystem resize 1:max /mnt/btrfs-progs-tests btrfs balance start /mnt/btrfs-progs-tests btrfs balance start -v /mnt/btrfs-progs-tests btrfs balance start -f /mnt/btrfs-progs-tests btrfs balance status -v /mnt/btrfs-progs-tests btrfs balance pause /mnt/btrfs-progs-tests btrfs balance status /mnt/btrfs-progs-tests btrfs balance resume /mnt/btrfs-progs-tests btrfs balance status -v /mnt/btrfs-progs-tests btrfs balance cancel /mnt/btrfs-progs-tests btrfs balance start -dprofiles=single /mnt/btrfs-progs-tests btrfs balance start -dconvert=single /mnt/btrfs-progs-tests btrfs balance start -ddevid=1 /mnt/btrfs-progs-tests btrfs balance start -f -mprofiles=single /mnt/btrfs-progs-tests btrfs balance start -f -mconvert=single /mnt/btrfs-progs-tests btrfs balance start -f -mdevid=1 /mnt/btrfs-progs-tests btrfs balance start -f -sprofiles=single /mnt/btrfs-progs-tests btrfs balance start -f -sconvert=single /mnt/btrfs-progs-tests btrfs balance start -f -sdevid=1 /mnt/btrfs-progs-tests btrfs device add -f /dev/sda10 /mnt/btrfs-progs-tests btrfs device del /dev/sda10 /mnt/btrfs-progs-tests btrfs device stats /dev/sda6 btrfs device stats -z /dev/sda6 btrfs device stats /mnt/btrfs-progs-tests btrfs device stats -z /mnt/btrfs-progs-tests btrfs device usage /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub start -B /mnt/btrfs-progs-tests btrfs scrub start -B -d /mnt/btrfs-progs-tests btrfs scrub start -B -r /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub start /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub status -d /mnt/btrfs-progs-tests btrfs scrub status -R /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub start /dev/sda6 btrfs scrub status /dev/sda6 btrfs scrub status /dev/sda6 btrfs scrub status -d /dev/sda6 btrfs scrub status -R /dev/sda6 btrfs scrub status /dev/sda6 btrfs subvolume snapshot -r /mnt/btrfs-progs-tests /mnt/btrfs-progs-tests/snap1 btrfs send -f /tmp/btrfs_snapshot_test /mnt/btrfs-progs-tests/snap1 btrfs send -e -f /tmp/btrfs_snapshot_test /mnt/btrfs-progs-tests/snap1 btrfs send --no-data -f /tmp/btrfs_snapshot_test /mnt/btrfs-progs-tests/snap1 btrfs quota enable /mnt/btrfs-progs-tests btrfs quota rescan /mnt/btrfs-progs-tests btrfs quota rescan -s /mnt/btrfs-progs-tests btrfs quota rescan -w /mnt/btrfs-progs-tests btrfs quota disable /mnt/btrfs-progs-tests btrfs quota enable /mnt/btrfs-progs-tests btrfs qgroup create 1/5 /mnt/btrfs-progs-tests btrfs qgroup create 2/5 /mnt/btrfs-progs-tests btrfs qgroup assign 1/5 2/5 /mnt/btrfs-progs-tests btrfs qgroup limit 1G 1/5 /mnt/btrfs-progs-tests btrfs qgroup show /mnt/btrfs-progs-tests btrfs qgroup show -p -c -r -e -F -f /mnt/btrfs-progs-tests btrfs qgroup remove 1/5 2/5 /mnt/btrfs-progs-tests btrfs qgroup destroy 2/5 /mnt/btrfs-progs-tests btrfs qgroup destroy 1/5 /mnt/btrfs-progs-tests btrfs quota disable /mnt/btrfs-progs-tests btrfs replace start -f -B /dev/sda6 /dev/sda10 /mnt/btrfs-progs-tests btrfs replace status /mnt/btrfs-progs-tests btrfs replace start -f -B /dev/sda10 /dev/sda6 /mnt/btrfs-progs-tests btrfs-convert /dev/sda6 btrfs-convert -r /dev/sda6 btrfs-convert -d /dev/sda6 btrfs-convert -i /dev/sda6 btrfs-convert -n /dev/sda6 btrfs-convert -N 4096 /dev/sda6 btrfs-convert -l test /dev/sda6 btrfs-convert -L /dev/sda6 btrfs-convert --no-progress /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -c 0 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -c 9 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -t 0 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -t 1 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -t 32 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -w /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -w /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 btrfs-image -r -t 0 /tmp/btrfs_image.img /dev/sda6 btrfs-image -r -t 1 /tmp/btrfs_image.img /dev/sda6 btrfs-image -r -t 32 /tmp/btrfs_image.img /dev/sda6 btrfs-image -r -o /tmp/btrfs_image.img /dev/sda6 3: Manual check relation source by: grep DUP *.c Confirmed that all source are modified. 4: Use this raid type manually, do some operations in fs, no error found in command and dmesg. 5: Combination of dup conversion with fsck Confirmed OK with relative kernel patch titled: [PATCH] btrfs: Support convert to -d dup for btrfs-convert export TEST_DEV='/dev/vdc' export TEST_DIR='/var/ltf/tester/mnt' do_dup_test() { local m_from="$1" local d_from="$2" local m_to="$3" local d_to="$4" echo "Convert from -m $m_from -d $d_from to -m $m_to -d $d_to" umount "$TEST_DIR" &>/dev/null ./mkfs.btrfs -f -m "$m_from" -d "$d_from" "$TEST_DEV" >/dev/null || return 1 mount "$TEST_DEV" "$TEST_DIR" || return 1 cp -a /sbin/* "$TEST_DIR" [[ "$m_from" != "$m_to" ]] && { ./btrfs balance start -f -mconvert="$m_to" "$TEST_DIR" || return 1 } [[ "$d_from" != "$d_to" ]] && { local opt=() [[ "$d_to" == single ]] && opt+=("-f") ./btrfs balance start "${opt[@]}" -dconvert="$d_to" "$TEST_DIR" || return 1 } umount "$TEST_DIR" || return 1 ./btrfsck "$TEST_DEV" || return 1 echo return 0 } test_all() { for m_from in single dup; do for d_from in single dup; do for m_to in single dup; do for d_to in single dup; do do_dup_test "$m_from" "$d_from" "$m_to" "$d_to" || return 1 done done done done } test_all Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com> Tested-by: Austin S. Hemmelgarn <ahferroin7@gmail.com> [ minor updates in the changelog ] Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-19 09:36:24 +00:00
u64 dev_cnt, int mixed, int ssd)
{
u64 allowed;
u64 profile = metadata_profile | data_profile;
allowed = btrfs_bg_flags_for_device_num(dev_cnt);
if (dev_cnt > 1 && profile & BTRFS_BLOCK_GROUP_DUP) {
warning("DUP is not recommended on filesystem with multiple devices");
}
if (metadata_profile & ~allowed) {
fprintf(stderr,
"ERROR: unable to create FS with metadata profile %s "
"(have %llu devices but %d devices are required)\n",
btrfs_group_profile_str(metadata_profile), dev_cnt,
btrfs_bg_type_to_devs_min(metadata_profile));
return 1;
}
if (data_profile & ~allowed) {
fprintf(stderr,
"ERROR: unable to create FS with data profile %s "
"(have %llu devices but %d devices are required)\n",
btrfs_group_profile_str(data_profile), dev_cnt,
btrfs_bg_type_to_devs_min(data_profile));
return 1;
}
if (dev_cnt == 3 && profile & BTRFS_BLOCK_GROUP_RAID6) {
warning("RAID6 is not recommended on filesystem with 3 devices only");
}
if (dev_cnt == 2 && profile & BTRFS_BLOCK_GROUP_RAID5) {
warning("RAID5 is not recommended on filesystem with 2 devices only");
}
btrfs-progs: mkfs: allow --data DUP for single device Current code don't support DUP profile on single device, except it is in mixed mode, because of following reasons: 1: Some SSD do deduplication internally, so the duplication on the filesystem side has no effect. 2: On a physical device, if the entire disk broken, --data DUP does not help. 3: Half performance compared to single profile. 4: We have a workaround: create multi-partition on a single device, and btffs will treat them as multi device. Instead of refusing --data DUP, we give the user a choice and print a wrning. Test: 1: Tested by xfstests Run with modified xfstests, I add test items of -d dup in single device into btrfs/* and common/rc, run tests of btrfs/*, with all mount option, no regression diffed with v4.3. 2: Tested by btrfs-progs Checked following commands in "-m dup -d dup" fs with memleck checking, all passed: mkfs.btrfs -f --data dup --metadata dup /dev/sda6 btrfs filesystem show /dev/sda6 btrfs filesystem label /dev/sda6 btrfs_label_test btrfs filesystem label /dev/sda6 btrfs device scan --all-devices btrfs device scan /dev/sda6 btrfs device scan /dev/sda6 btrfs device ready /dev/sda6 btrfs check /dev/sda6 btrfs check -s 1 /dev/sda6 btrfs check --repair /dev/sda6 btrfs check --init-csum-tree /dev/sda6 btrfs check --init-extent-tree /dev/sda6 btrfs check --check-data-csum /dev/sda6 btrfs check --qgroup-report /dev/sda6 btrfs rescue super-recover -y /dev/sda6 btrfs rescue zero-log /dev/sda6 btrfs restore -l /dev/sda6 btrfs restore /dev/sda6 / btrfs restore -s /dev/sda6 / btrfs restore -x /dev/sda6 / btrfs restore -m /dev/sda6 / btrfs restore -S /dev/sda6 / btrfs restore -v /dev/sda6 / btrfs restore -i /dev/sda6 / btrfs restore -o /dev/sda6 / btrfs restore -u0 /dev/sda6 / btrfs restore -u1 /dev/sda6 / btrfs restore -D /dev/sda6 / btrfs property list /dev/sda6 btrfs property get /dev/sda6 label btrfs property set /dev/sda6 label test btrfs property set /dev/sda6 label btrfs_label_test btrfs help btrfs help --full btrfs version btrfsck /dev/sda6 btrfs-find-root /dev/sda6 btrfs-find-root -a /dev/sda6 btrfs-map-logical -l1 /dev/sda6 btrfs-map-logical -l1 -c1 /dev/sda6 btrfs-map-logical -l1 -o /tmp/btrfs-map-logic-out /dev/sda6 btrfs-map-logical -l1 -b1 /dev/sda6 btrfs-select-super -s 0 /dev/sda6 btrfs-select-super -s 1 /dev/sda6 btrfstune -S 1 /dev/sda6 btrfstune -f -S 0 /dev/sda6 btrfstune -r /dev/sda6 btrfstune -x /dev/sda6 btrfstune -n /dev/sda6 btrfstune -f -U 00000000-0000-0000-0000-000000000000 /dev/sda6 btrfstune -f -u /dev/sda6 btrfs-calc-size /dev/sda6 btrfs-calc-size -v /dev/sda6 btrfs-calc-size -b /dev/sda6 btrfs-debug-tree /dev/sda6 btrfs-debug-tree -e /dev/sda6 btrfs-debug-tree -d /dev/sda6 btrfs-debug-tree -r /dev/sda6 btrfs-debug-tree -R /dev/sda6 btrfs-debug-tree -u /dev/sda6 btrfs-debug-tree -b 0 /dev/sda6 btrfs-debug-tree -t 0 /dev/sda6 btrfs-debug-tree -t 2 /dev/sda6 btrfs-show-super /dev/sda6 btrfs-show-super -i 0 /dev/sda6 btrfs-show-super -i 1 /dev/sda6 btrfs-show-super -i 2 /dev/sda6 btrfs-show-super -a /dev/sda6 btrfs-show-super -f /dev/sda6 btrfs-show-super -F /dev/sda6 btrfs subvolume list /mnt/btrfs-progs-tests btrfs subvolume create /mnt/btrfs-progs-tests/mysubvol btrfs subvolume list /mnt/btrfs-progs-tests btrfs subvolume get-default /mnt/btrfs-progs-tests btrfs subvolume set-default 258 /mnt/btrfs-progs-tests btrfs subvolume get-default /mnt/btrfs-progs-tests btrfs subvolume set-default /mnt/btrfs-progs-tests btrfs subvolume snapshot /mnt/btrfs-progs-tests/mysubvol /mnt/btrfs-progs-tests/mysubvol_snap btrfs subvolume list /mnt/btrfs-progs-tests btrfs subvolume find-new /mnt/btrfs-progs-tests 0 btrfs subvolume find-new /mnt/btrfs-progs-tests 0 btrfs subvolume find-new /mnt/btrfs-progs-tests/mysubvol 0 btrfs subvolume find-new /mnt/btrfs-progs-tests/mysubvol 0 btrfs subvolume show /mnt/btrfs-progs-tests btrfs subvolume show /mnt/btrfs-progs-tests/mysubvol btrfs subvolume show /mnt/btrfs-progs-tests/mysubvol_snap btrfs subvolume sync /mnt/btrfs-progs-tests btrfs subvolume delete /mnt/btrfs-progs-tests/mysubvol_snap btrfs subvolume delete /mnt/btrfs-progs-tests/mysubvol btrfs subvolume sync /mnt/btrfs-progs-tests btrfs filesystem df /mnt/btrfs-progs-tests btrfs filesystem show /mnt/btrfs-progs-tests btrfs filesystem sync /mnt/btrfs-progs-tests btrfs filesystem label /mnt/btrfs-progs-tests btrfs_label_test btrfs filesystem label /mnt/btrfs-progs-tests btrfs filesystem usage /mnt/btrfs-progs-tests btrfs filesystem defragment -s 1024 -l 2048 /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_0 btrfs filesystem defragment /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_1 btrfs filesystem defragment -f /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_2 btrfs filesystem defragment -czlib /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_3 btrfs filesystem defragment -clzo /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_4 btrfs filesystem defragment /mnt/btrfs-progs-tests/filesystem_test_dir btrfs filesystem defragment -r /mnt/btrfs-progs-tests/filesystem_test_dir btrfs filesystem defragment /mnt/btrfs-progs-tests btrfs filesystem resize 1:-10M /mnt/btrfs-progs-tests btrfs filesystem resize 1:max /mnt/btrfs-progs-tests btrfs balance start /mnt/btrfs-progs-tests btrfs balance start -v /mnt/btrfs-progs-tests btrfs balance start -f /mnt/btrfs-progs-tests btrfs balance status -v /mnt/btrfs-progs-tests btrfs balance pause /mnt/btrfs-progs-tests btrfs balance status /mnt/btrfs-progs-tests btrfs balance resume /mnt/btrfs-progs-tests btrfs balance status -v /mnt/btrfs-progs-tests btrfs balance cancel /mnt/btrfs-progs-tests btrfs balance start -dprofiles=single /mnt/btrfs-progs-tests btrfs balance start -dconvert=single /mnt/btrfs-progs-tests btrfs balance start -ddevid=1 /mnt/btrfs-progs-tests btrfs balance start -f -mprofiles=single /mnt/btrfs-progs-tests btrfs balance start -f -mconvert=single /mnt/btrfs-progs-tests btrfs balance start -f -mdevid=1 /mnt/btrfs-progs-tests btrfs balance start -f -sprofiles=single /mnt/btrfs-progs-tests btrfs balance start -f -sconvert=single /mnt/btrfs-progs-tests btrfs balance start -f -sdevid=1 /mnt/btrfs-progs-tests btrfs device add -f /dev/sda10 /mnt/btrfs-progs-tests btrfs device del /dev/sda10 /mnt/btrfs-progs-tests btrfs device stats /dev/sda6 btrfs device stats -z /dev/sda6 btrfs device stats /mnt/btrfs-progs-tests btrfs device stats -z /mnt/btrfs-progs-tests btrfs device usage /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub start -B /mnt/btrfs-progs-tests btrfs scrub start -B -d /mnt/btrfs-progs-tests btrfs scrub start -B -r /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub start /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub status -d /mnt/btrfs-progs-tests btrfs scrub status -R /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub start /dev/sda6 btrfs scrub status /dev/sda6 btrfs scrub status /dev/sda6 btrfs scrub status -d /dev/sda6 btrfs scrub status -R /dev/sda6 btrfs scrub status /dev/sda6 btrfs subvolume snapshot -r /mnt/btrfs-progs-tests /mnt/btrfs-progs-tests/snap1 btrfs send -f /tmp/btrfs_snapshot_test /mnt/btrfs-progs-tests/snap1 btrfs send -e -f /tmp/btrfs_snapshot_test /mnt/btrfs-progs-tests/snap1 btrfs send --no-data -f /tmp/btrfs_snapshot_test /mnt/btrfs-progs-tests/snap1 btrfs quota enable /mnt/btrfs-progs-tests btrfs quota rescan /mnt/btrfs-progs-tests btrfs quota rescan -s /mnt/btrfs-progs-tests btrfs quota rescan -w /mnt/btrfs-progs-tests btrfs quota disable /mnt/btrfs-progs-tests btrfs quota enable /mnt/btrfs-progs-tests btrfs qgroup create 1/5 /mnt/btrfs-progs-tests btrfs qgroup create 2/5 /mnt/btrfs-progs-tests btrfs qgroup assign 1/5 2/5 /mnt/btrfs-progs-tests btrfs qgroup limit 1G 1/5 /mnt/btrfs-progs-tests btrfs qgroup show /mnt/btrfs-progs-tests btrfs qgroup show -p -c -r -e -F -f /mnt/btrfs-progs-tests btrfs qgroup remove 1/5 2/5 /mnt/btrfs-progs-tests btrfs qgroup destroy 2/5 /mnt/btrfs-progs-tests btrfs qgroup destroy 1/5 /mnt/btrfs-progs-tests btrfs quota disable /mnt/btrfs-progs-tests btrfs replace start -f -B /dev/sda6 /dev/sda10 /mnt/btrfs-progs-tests btrfs replace status /mnt/btrfs-progs-tests btrfs replace start -f -B /dev/sda10 /dev/sda6 /mnt/btrfs-progs-tests btrfs-convert /dev/sda6 btrfs-convert -r /dev/sda6 btrfs-convert -d /dev/sda6 btrfs-convert -i /dev/sda6 btrfs-convert -n /dev/sda6 btrfs-convert -N 4096 /dev/sda6 btrfs-convert -l test /dev/sda6 btrfs-convert -L /dev/sda6 btrfs-convert --no-progress /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -c 0 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -c 9 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -t 0 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -t 1 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -t 32 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -w /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -w /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 btrfs-image -r -t 0 /tmp/btrfs_image.img /dev/sda6 btrfs-image -r -t 1 /tmp/btrfs_image.img /dev/sda6 btrfs-image -r -t 32 /tmp/btrfs_image.img /dev/sda6 btrfs-image -r -o /tmp/btrfs_image.img /dev/sda6 3: Manual check relation source by: grep DUP *.c Confirmed that all source are modified. 4: Use this raid type manually, do some operations in fs, no error found in command and dmesg. 5: Combination of dup conversion with fsck Confirmed OK with relative kernel patch titled: [PATCH] btrfs: Support convert to -d dup for btrfs-convert export TEST_DEV='/dev/vdc' export TEST_DIR='/var/ltf/tester/mnt' do_dup_test() { local m_from="$1" local d_from="$2" local m_to="$3" local d_to="$4" echo "Convert from -m $m_from -d $d_from to -m $m_to -d $d_to" umount "$TEST_DIR" &>/dev/null ./mkfs.btrfs -f -m "$m_from" -d "$d_from" "$TEST_DEV" >/dev/null || return 1 mount "$TEST_DEV" "$TEST_DIR" || return 1 cp -a /sbin/* "$TEST_DIR" [[ "$m_from" != "$m_to" ]] && { ./btrfs balance start -f -mconvert="$m_to" "$TEST_DIR" || return 1 } [[ "$d_from" != "$d_to" ]] && { local opt=() [[ "$d_to" == single ]] && opt+=("-f") ./btrfs balance start "${opt[@]}" -dconvert="$d_to" "$TEST_DIR" || return 1 } umount "$TEST_DIR" || return 1 ./btrfsck "$TEST_DEV" || return 1 echo return 0 } test_all() { for m_from in single dup; do for d_from in single dup; do for m_to in single dup; do for d_to in single dup; do do_dup_test "$m_from" "$d_from" "$m_to" "$d_to" || return 1 done done done done } test_all Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com> Tested-by: Austin S. Hemmelgarn <ahferroin7@gmail.com> [ minor updates in the changelog ] Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-19 09:36:24 +00:00
warning_on(!mixed && (data_profile & BTRFS_BLOCK_GROUP_DUP) && ssd,
"DUP may not actually lead to 2 copies on the device, see manual page");
btrfs-progs: mkfs: allow --data DUP for single device Current code don't support DUP profile on single device, except it is in mixed mode, because of following reasons: 1: Some SSD do deduplication internally, so the duplication on the filesystem side has no effect. 2: On a physical device, if the entire disk broken, --data DUP does not help. 3: Half performance compared to single profile. 4: We have a workaround: create multi-partition on a single device, and btffs will treat them as multi device. Instead of refusing --data DUP, we give the user a choice and print a wrning. Test: 1: Tested by xfstests Run with modified xfstests, I add test items of -d dup in single device into btrfs/* and common/rc, run tests of btrfs/*, with all mount option, no regression diffed with v4.3. 2: Tested by btrfs-progs Checked following commands in "-m dup -d dup" fs with memleck checking, all passed: mkfs.btrfs -f --data dup --metadata dup /dev/sda6 btrfs filesystem show /dev/sda6 btrfs filesystem label /dev/sda6 btrfs_label_test btrfs filesystem label /dev/sda6 btrfs device scan --all-devices btrfs device scan /dev/sda6 btrfs device scan /dev/sda6 btrfs device ready /dev/sda6 btrfs check /dev/sda6 btrfs check -s 1 /dev/sda6 btrfs check --repair /dev/sda6 btrfs check --init-csum-tree /dev/sda6 btrfs check --init-extent-tree /dev/sda6 btrfs check --check-data-csum /dev/sda6 btrfs check --qgroup-report /dev/sda6 btrfs rescue super-recover -y /dev/sda6 btrfs rescue zero-log /dev/sda6 btrfs restore -l /dev/sda6 btrfs restore /dev/sda6 / btrfs restore -s /dev/sda6 / btrfs restore -x /dev/sda6 / btrfs restore -m /dev/sda6 / btrfs restore -S /dev/sda6 / btrfs restore -v /dev/sda6 / btrfs restore -i /dev/sda6 / btrfs restore -o /dev/sda6 / btrfs restore -u0 /dev/sda6 / btrfs restore -u1 /dev/sda6 / btrfs restore -D /dev/sda6 / btrfs property list /dev/sda6 btrfs property get /dev/sda6 label btrfs property set /dev/sda6 label test btrfs property set /dev/sda6 label btrfs_label_test btrfs help btrfs help --full btrfs version btrfsck /dev/sda6 btrfs-find-root /dev/sda6 btrfs-find-root -a /dev/sda6 btrfs-map-logical -l1 /dev/sda6 btrfs-map-logical -l1 -c1 /dev/sda6 btrfs-map-logical -l1 -o /tmp/btrfs-map-logic-out /dev/sda6 btrfs-map-logical -l1 -b1 /dev/sda6 btrfs-select-super -s 0 /dev/sda6 btrfs-select-super -s 1 /dev/sda6 btrfstune -S 1 /dev/sda6 btrfstune -f -S 0 /dev/sda6 btrfstune -r /dev/sda6 btrfstune -x /dev/sda6 btrfstune -n /dev/sda6 btrfstune -f -U 00000000-0000-0000-0000-000000000000 /dev/sda6 btrfstune -f -u /dev/sda6 btrfs-calc-size /dev/sda6 btrfs-calc-size -v /dev/sda6 btrfs-calc-size -b /dev/sda6 btrfs-debug-tree /dev/sda6 btrfs-debug-tree -e /dev/sda6 btrfs-debug-tree -d /dev/sda6 btrfs-debug-tree -r /dev/sda6 btrfs-debug-tree -R /dev/sda6 btrfs-debug-tree -u /dev/sda6 btrfs-debug-tree -b 0 /dev/sda6 btrfs-debug-tree -t 0 /dev/sda6 btrfs-debug-tree -t 2 /dev/sda6 btrfs-show-super /dev/sda6 btrfs-show-super -i 0 /dev/sda6 btrfs-show-super -i 1 /dev/sda6 btrfs-show-super -i 2 /dev/sda6 btrfs-show-super -a /dev/sda6 btrfs-show-super -f /dev/sda6 btrfs-show-super -F /dev/sda6 btrfs subvolume list /mnt/btrfs-progs-tests btrfs subvolume create /mnt/btrfs-progs-tests/mysubvol btrfs subvolume list /mnt/btrfs-progs-tests btrfs subvolume get-default /mnt/btrfs-progs-tests btrfs subvolume set-default 258 /mnt/btrfs-progs-tests btrfs subvolume get-default /mnt/btrfs-progs-tests btrfs subvolume set-default /mnt/btrfs-progs-tests btrfs subvolume snapshot /mnt/btrfs-progs-tests/mysubvol /mnt/btrfs-progs-tests/mysubvol_snap btrfs subvolume list /mnt/btrfs-progs-tests btrfs subvolume find-new /mnt/btrfs-progs-tests 0 btrfs subvolume find-new /mnt/btrfs-progs-tests 0 btrfs subvolume find-new /mnt/btrfs-progs-tests/mysubvol 0 btrfs subvolume find-new /mnt/btrfs-progs-tests/mysubvol 0 btrfs subvolume show /mnt/btrfs-progs-tests btrfs subvolume show /mnt/btrfs-progs-tests/mysubvol btrfs subvolume show /mnt/btrfs-progs-tests/mysubvol_snap btrfs subvolume sync /mnt/btrfs-progs-tests btrfs subvolume delete /mnt/btrfs-progs-tests/mysubvol_snap btrfs subvolume delete /mnt/btrfs-progs-tests/mysubvol btrfs subvolume sync /mnt/btrfs-progs-tests btrfs filesystem df /mnt/btrfs-progs-tests btrfs filesystem show /mnt/btrfs-progs-tests btrfs filesystem sync /mnt/btrfs-progs-tests btrfs filesystem label /mnt/btrfs-progs-tests btrfs_label_test btrfs filesystem label /mnt/btrfs-progs-tests btrfs filesystem usage /mnt/btrfs-progs-tests btrfs filesystem defragment -s 1024 -l 2048 /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_0 btrfs filesystem defragment /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_1 btrfs filesystem defragment -f /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_2 btrfs filesystem defragment -czlib /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_3 btrfs filesystem defragment -clzo /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_4 btrfs filesystem defragment /mnt/btrfs-progs-tests/filesystem_test_dir btrfs filesystem defragment -r /mnt/btrfs-progs-tests/filesystem_test_dir btrfs filesystem defragment /mnt/btrfs-progs-tests btrfs filesystem resize 1:-10M /mnt/btrfs-progs-tests btrfs filesystem resize 1:max /mnt/btrfs-progs-tests btrfs balance start /mnt/btrfs-progs-tests btrfs balance start -v /mnt/btrfs-progs-tests btrfs balance start -f /mnt/btrfs-progs-tests btrfs balance status -v /mnt/btrfs-progs-tests btrfs balance pause /mnt/btrfs-progs-tests btrfs balance status /mnt/btrfs-progs-tests btrfs balance resume /mnt/btrfs-progs-tests btrfs balance status -v /mnt/btrfs-progs-tests btrfs balance cancel /mnt/btrfs-progs-tests btrfs balance start -dprofiles=single /mnt/btrfs-progs-tests btrfs balance start -dconvert=single /mnt/btrfs-progs-tests btrfs balance start -ddevid=1 /mnt/btrfs-progs-tests btrfs balance start -f -mprofiles=single /mnt/btrfs-progs-tests btrfs balance start -f -mconvert=single /mnt/btrfs-progs-tests btrfs balance start -f -mdevid=1 /mnt/btrfs-progs-tests btrfs balance start -f -sprofiles=single /mnt/btrfs-progs-tests btrfs balance start -f -sconvert=single /mnt/btrfs-progs-tests btrfs balance start -f -sdevid=1 /mnt/btrfs-progs-tests btrfs device add -f /dev/sda10 /mnt/btrfs-progs-tests btrfs device del /dev/sda10 /mnt/btrfs-progs-tests btrfs device stats /dev/sda6 btrfs device stats -z /dev/sda6 btrfs device stats /mnt/btrfs-progs-tests btrfs device stats -z /mnt/btrfs-progs-tests btrfs device usage /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub start -B /mnt/btrfs-progs-tests btrfs scrub start -B -d /mnt/btrfs-progs-tests btrfs scrub start -B -r /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub start /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub status -d /mnt/btrfs-progs-tests btrfs scrub status -R /mnt/btrfs-progs-tests btrfs scrub status /mnt/btrfs-progs-tests btrfs scrub start /dev/sda6 btrfs scrub status /dev/sda6 btrfs scrub status /dev/sda6 btrfs scrub status -d /dev/sda6 btrfs scrub status -R /dev/sda6 btrfs scrub status /dev/sda6 btrfs subvolume snapshot -r /mnt/btrfs-progs-tests /mnt/btrfs-progs-tests/snap1 btrfs send -f /tmp/btrfs_snapshot_test /mnt/btrfs-progs-tests/snap1 btrfs send -e -f /tmp/btrfs_snapshot_test /mnt/btrfs-progs-tests/snap1 btrfs send --no-data -f /tmp/btrfs_snapshot_test /mnt/btrfs-progs-tests/snap1 btrfs quota enable /mnt/btrfs-progs-tests btrfs quota rescan /mnt/btrfs-progs-tests btrfs quota rescan -s /mnt/btrfs-progs-tests btrfs quota rescan -w /mnt/btrfs-progs-tests btrfs quota disable /mnt/btrfs-progs-tests btrfs quota enable /mnt/btrfs-progs-tests btrfs qgroup create 1/5 /mnt/btrfs-progs-tests btrfs qgroup create 2/5 /mnt/btrfs-progs-tests btrfs qgroup assign 1/5 2/5 /mnt/btrfs-progs-tests btrfs qgroup limit 1G 1/5 /mnt/btrfs-progs-tests btrfs qgroup show /mnt/btrfs-progs-tests btrfs qgroup show -p -c -r -e -F -f /mnt/btrfs-progs-tests btrfs qgroup remove 1/5 2/5 /mnt/btrfs-progs-tests btrfs qgroup destroy 2/5 /mnt/btrfs-progs-tests btrfs qgroup destroy 1/5 /mnt/btrfs-progs-tests btrfs quota disable /mnt/btrfs-progs-tests btrfs replace start -f -B /dev/sda6 /dev/sda10 /mnt/btrfs-progs-tests btrfs replace status /mnt/btrfs-progs-tests btrfs replace start -f -B /dev/sda10 /dev/sda6 /mnt/btrfs-progs-tests btrfs-convert /dev/sda6 btrfs-convert -r /dev/sda6 btrfs-convert -d /dev/sda6 btrfs-convert -i /dev/sda6 btrfs-convert -n /dev/sda6 btrfs-convert -N 4096 /dev/sda6 btrfs-convert -l test /dev/sda6 btrfs-convert -L /dev/sda6 btrfs-convert --no-progress /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -c 0 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -c 9 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -t 0 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -t 1 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -t 32 /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -w /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 mkfs.btrfs -f /dev/sda6 btrfs-image -w /dev/sda6 /tmp/btrfs_image.img btrfs-image -r /tmp/btrfs_image.img /dev/sda6 btrfs-image -r -t 0 /tmp/btrfs_image.img /dev/sda6 btrfs-image -r -t 1 /tmp/btrfs_image.img /dev/sda6 btrfs-image -r -t 32 /tmp/btrfs_image.img /dev/sda6 btrfs-image -r -o /tmp/btrfs_image.img /dev/sda6 3: Manual check relation source by: grep DUP *.c Confirmed that all source are modified. 4: Use this raid type manually, do some operations in fs, no error found in command and dmesg. 5: Combination of dup conversion with fsck Confirmed OK with relative kernel patch titled: [PATCH] btrfs: Support convert to -d dup for btrfs-convert export TEST_DEV='/dev/vdc' export TEST_DIR='/var/ltf/tester/mnt' do_dup_test() { local m_from="$1" local d_from="$2" local m_to="$3" local d_to="$4" echo "Convert from -m $m_from -d $d_from to -m $m_to -d $d_to" umount "$TEST_DIR" &>/dev/null ./mkfs.btrfs -f -m "$m_from" -d "$d_from" "$TEST_DEV" >/dev/null || return 1 mount "$TEST_DEV" "$TEST_DIR" || return 1 cp -a /sbin/* "$TEST_DIR" [[ "$m_from" != "$m_to" ]] && { ./btrfs balance start -f -mconvert="$m_to" "$TEST_DIR" || return 1 } [[ "$d_from" != "$d_to" ]] && { local opt=() [[ "$d_to" == single ]] && opt+=("-f") ./btrfs balance start "${opt[@]}" -dconvert="$d_to" "$TEST_DIR" || return 1 } umount "$TEST_DIR" || return 1 ./btrfsck "$TEST_DEV" || return 1 echo return 0 } test_all() { for m_from in single dup; do for d_from in single dup; do for m_to in single dup; do for d_to in single dup; do do_dup_test "$m_from" "$d_from" "$m_to" "$d_to" || return 1 done done done done } test_all Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com> Tested-by: Austin S. Hemmelgarn <ahferroin7@gmail.com> [ minor updates in the changelog ] Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-19 09:36:24 +00:00
return 0;
}
/*
* This reads a line from the stdin and only returns non-zero if the
* first whitespace delimited token is a case insensitive match with yes
* or y.
*/
int ask_user(const char *question)
{
char buf[30] = {0,};
char *saveptr = NULL;
char *answer;
printf("%s [y/N]: ", question);
return fgets(buf, sizeof(buf) - 1, stdin) &&
(answer = strtok_r(buf, " \t\n\r", &saveptr)) &&
(!strcasecmp(answer, "yes") || !strcasecmp(answer, "y"));
}
/*
btrfs-progs: reimplement find_mount_fsroot without libmount In commit 57cfe29e6936 ("btrfs-progs: utils: introduce find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a convenience library libmount, because getmntent does not provide the information we need to distinguish bind mounts. Using libmount turned out to be problematic in several ways: - static build got broken due to clashing symbols, eg. for parsing size or path canonicalization (#333) - long-term distros do not have libmount new enough (2.24+) to provide some functions (mnt_table_is_empty, #334) - libmount internally uses getgrnam_r/mnt_get_uid/... that are not static-build friendly, a warning is printed during link time for each binary; we don't use any of the functions - libmount has further library dependencies that we don't need: $ ldd /usr/lib64/libmount.so.1 linux-vdso.so.1 (0x00007fff4f175000) libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000) libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000) libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000) /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000) libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000) namely selinux, pcre and dl. Summing it up, libmount causes more trouble than it's worth using a convenience library, we want to keep the dependencies minimal so the custom mountinfo parser was inevitable. Issue: #333 Issue: #334 Issue: #336 Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-10 15:36:19 +00:00
* Partial representation of a line in /proc/pid/mountinfo
*/
struct mnt_entry {
const char *root;
const char *path;
const char *options1;
const char *fstype;
const char *device;
const char *options2;
};
/*
* Find first occurrence of up an option string (as "option=") in @options,
btrfs-progs: reimplement find_mount_fsroot without libmount In commit 57cfe29e6936 ("btrfs-progs: utils: introduce find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a convenience library libmount, because getmntent does not provide the information we need to distinguish bind mounts. Using libmount turned out to be problematic in several ways: - static build got broken due to clashing symbols, eg. for parsing size or path canonicalization (#333) - long-term distros do not have libmount new enough (2.24+) to provide some functions (mnt_table_is_empty, #334) - libmount internally uses getgrnam_r/mnt_get_uid/... that are not static-build friendly, a warning is printed during link time for each binary; we don't use any of the functions - libmount has further library dependencies that we don't need: $ ldd /usr/lib64/libmount.so.1 linux-vdso.so.1 (0x00007fff4f175000) libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000) libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000) libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000) /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000) libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000) namely selinux, pcre and dl. Summing it up, libmount causes more trouble than it's worth using a convenience library, we want to keep the dependencies minimal so the custom mountinfo parser was inevitable. Issue: #333 Issue: #334 Issue: #336 Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-10 15:36:19 +00:00
* separated by comma. Return allocated string as "option=value"
*/
static char *find_option(const char *options, const char *option)
{
char *tmp, *ret;
tmp = strstr(options, option);
if (!tmp)
return NULL;
ret = strdup(tmp);
tmp = ret;
while (*tmp && *tmp != ',')
tmp++;
*tmp = 0;
return ret;
}
/* Match whitespace separator */
static bool is_sep(char c)
{
return c == ' ' || c == '\t';
}
/* Advance @line skipping over all non-separator chars */
static void skip_nonsep(char **line)
{
while (**line && !is_sep(**line))
(*line)++;
}
/* Advance @line skipping over all separator chars, setting them to nul char */
static void skip_sep(char **line)
{
while (**line && is_sep(**line)) {
**line = 0;
(*line)++;
}
}
static bool isoctal(char c)
{
return '0' <= c && c <= '7';
}
/*
* Validate complete escape sequence used for mangling special chars in paths,
* eg. \012 == 10 == 0xa == '\n'.
* Mandatory format: backslash and 3 octal digits.
*/
static bool valid_escape(const char *str)
{
if (*str == 0 || *str != '\\')
return false;
str++;
if (*str == 0 || is_sep(*str) || !isoctal(*str))
return false;
str++;
if (*str == 0 || is_sep(*str) || !isoctal(*str))
return false;
str++;
if (*str == 0 || is_sep(*str) || !isoctal(*str))
return false;
return true;
}
/*
* Read a path from @line, with potentially mangled special characters.
* - the input is changed in-place when unmangling is done
* - end of path is a space character (a valid space in the path is mangled)
* - line is advanced to the final separator or nul character
* - returned path is a valid string terminated by zero or whitespace separator
*/
char *read_path(char **line)
{
char *ret = *line;
char *out = *line;
while (**line) {
if (is_sep(**line))
break;
if (valid_escape(*line)) {
char c;
(*line)++;
c = ((*(*line)++) & 0b111) << 6;
c |= ((*(*line)++) & 0b111) << 3;
c |= ((*(*line)++) & 0b111);
*out++ = c;
} else {
*out++ = *(*line)++;
}
}
/*
* Unmangled characters make the final string shorter, add the null
* terminator. Otherwise keep the line at the space separator so
* followup parsing can continue.
*/
if (out < *line)
*out = 0;
return ret;
}
/*
* Parse a line from /proc/pid/mountinfo
* Example:
272 265 0:49 /subvol /mnt/path rw,noatime shared:145 - btrfs /dev/sda1 rw,subvolid=5598,subvol=/subvol
0 1 2 3 4 5 6 7 8 9 10
* Fields related to paths and options are parsed, @line is changed in place,
* separators are replaced by nul char, paths could be unmangled.
*/
static void parse_mntinfo_line(char *line, struct mnt_entry *ent)
{
/* Skip 0 */
skip_nonsep(&line);
skip_sep(&line);
/* Skip 1 */
skip_nonsep(&line);
skip_sep(&line);
/* Skip 2 */
skip_nonsep(&line);
skip_sep(&line);
/* Read 3 */
ent->root = read_path(&line);
skip_sep(&line);
/* Read 4 */
ent->path = read_path(&line);
skip_sep(&line);
/* Read 5 */
ent->options1 = line;
skip_nonsep(&line);
skip_sep(&line);
/* Skip 6 */
skip_nonsep(&line);
skip_sep(&line);
/* Skip 7 */
skip_nonsep(&line);
skip_sep(&line);
/* Read 8 */
ent->fstype = line;
skip_nonsep(&line);
skip_sep(&line);
/* Read 9 */
ent->device = read_path(&line);
skip_sep(&line);
/* Read 10 */
ent->options2 = line;
skip_nonsep(&line);
skip_sep(&line);
}
/*
* Compare the subvolume passed with the pathname of the directory mounted in
* btrfs. The pathname inside btrfs is different from getmnt and friends, since
* it can detect bind mounts to content from the inside of the original mount.
*
* Example:
* # mount -o subvol=/vol /dev/sda2 /mnt
* # mount --bind /mnt/dir2 /othermnt
*
* # mounts
* ...
* /dev/sda2 on /mnt type btrfs (ro,relatime,ssd,space_cache,subvolid=256,subvol=/vol)
* /dev/sda2 on /othermnt type btrfs (ro,relatime,ssd,space_cache,subvolid=256,subvol=/vol)
*
* # cat /proc/self/mountinfo
*
* 38 30 0:32 /vol /mnt ro,relatime - btrfs /dev/sda2 ro,ssd,space_cache,subvolid=256,subvol=/vol
* 37 29 0:32 /vol/dir2 /othermnt ro,relatime - btrfs /dev/sda2 ro,ssd,space_cache,subvolid=256,subvol=/vol
*
btrfs-progs: reimplement find_mount_fsroot without libmount In commit 57cfe29e6936 ("btrfs-progs: utils: introduce find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a convenience library libmount, because getmntent does not provide the information we need to distinguish bind mounts. Using libmount turned out to be problematic in several ways: - static build got broken due to clashing symbols, eg. for parsing size or path canonicalization (#333) - long-term distros do not have libmount new enough (2.24+) to provide some functions (mnt_table_is_empty, #334) - libmount internally uses getgrnam_r/mnt_get_uid/... that are not static-build friendly, a warning is printed during link time for each binary; we don't use any of the functions - libmount has further library dependencies that we don't need: $ ldd /usr/lib64/libmount.so.1 linux-vdso.so.1 (0x00007fff4f175000) libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000) libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000) libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000) /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000) libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000) namely selinux, pcre and dl. Summing it up, libmount causes more trouble than it's worth using a convenience library, we want to keep the dependencies minimal so the custom mountinfo parser was inevitable. Issue: #333 Issue: #334 Issue: #336 Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-10 15:36:19 +00:00
* If we try to find a mount point only using subvol and subvolid from mount
* options we would get mislead to believe that /othermnt has the same content
btrfs-progs: reimplement find_mount_fsroot without libmount In commit 57cfe29e6936 ("btrfs-progs: utils: introduce find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a convenience library libmount, because getmntent does not provide the information we need to distinguish bind mounts. Using libmount turned out to be problematic in several ways: - static build got broken due to clashing symbols, eg. for parsing size or path canonicalization (#333) - long-term distros do not have libmount new enough (2.24+) to provide some functions (mnt_table_is_empty, #334) - libmount internally uses getgrnam_r/mnt_get_uid/... that are not static-build friendly, a warning is printed during link time for each binary; we don't use any of the functions - libmount has further library dependencies that we don't need: $ ldd /usr/lib64/libmount.so.1 linux-vdso.so.1 (0x00007fff4f175000) libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000) libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000) libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000) /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000) libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000) namely selinux, pcre and dl. Summing it up, libmount causes more trouble than it's worth using a convenience library, we want to keep the dependencies minimal so the custom mountinfo parser was inevitable. Issue: #333 Issue: #334 Issue: #336 Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-10 15:36:19 +00:00
* as /mnt.
*
* But, using mountinfo, we have the pathaname _inside_ the filesystem, so we
btrfs-progs: reimplement find_mount_fsroot without libmount In commit 57cfe29e6936 ("btrfs-progs: utils: introduce find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a convenience library libmount, because getmntent does not provide the information we need to distinguish bind mounts. Using libmount turned out to be problematic in several ways: - static build got broken due to clashing symbols, eg. for parsing size or path canonicalization (#333) - long-term distros do not have libmount new enough (2.24+) to provide some functions (mnt_table_is_empty, #334) - libmount internally uses getgrnam_r/mnt_get_uid/... that are not static-build friendly, a warning is printed during link time for each binary; we don't use any of the functions - libmount has further library dependencies that we don't need: $ ldd /usr/lib64/libmount.so.1 linux-vdso.so.1 (0x00007fff4f175000) libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000) libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000) libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000) /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000) libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000) namely selinux, pcre and dl. Summing it up, libmount causes more trouble than it's worth using a convenience library, we want to keep the dependencies minimal so the custom mountinfo parser was inevitable. Issue: #333 Issue: #334 Issue: #336 Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-10 15:36:19 +00:00
* can filter out the mount points with bind mounts which have different content
* from the original mounts, in this case the mount point with id 37.
*/
int find_mount_fsroot(const char *subvol, const char *subvolid, char **mount)
{
btrfs-progs: reimplement find_mount_fsroot without libmount In commit 57cfe29e6936 ("btrfs-progs: utils: introduce find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a convenience library libmount, because getmntent does not provide the information we need to distinguish bind mounts. Using libmount turned out to be problematic in several ways: - static build got broken due to clashing symbols, eg. for parsing size or path canonicalization (#333) - long-term distros do not have libmount new enough (2.24+) to provide some functions (mnt_table_is_empty, #334) - libmount internally uses getgrnam_r/mnt_get_uid/... that are not static-build friendly, a warning is printed during link time for each binary; we don't use any of the functions - libmount has further library dependencies that we don't need: $ ldd /usr/lib64/libmount.so.1 linux-vdso.so.1 (0x00007fff4f175000) libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000) libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000) libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000) /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000) libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000) namely selinux, pcre and dl. Summing it up, libmount causes more trouble than it's worth using a convenience library, we want to keep the dependencies minimal so the custom mountinfo parser was inevitable. Issue: #333 Issue: #334 Issue: #336 Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-10 15:36:19 +00:00
FILE *mnt;
char *buf = NULL;
int bs = 4096;
int line = 0;
int ret = 0;
bool found = false;
btrfs-progs: reimplement find_mount_fsroot without libmount In commit 57cfe29e6936 ("btrfs-progs: utils: introduce find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a convenience library libmount, because getmntent does not provide the information we need to distinguish bind mounts. Using libmount turned out to be problematic in several ways: - static build got broken due to clashing symbols, eg. for parsing size or path canonicalization (#333) - long-term distros do not have libmount new enough (2.24+) to provide some functions (mnt_table_is_empty, #334) - libmount internally uses getgrnam_r/mnt_get_uid/... that are not static-build friendly, a warning is printed during link time for each binary; we don't use any of the functions - libmount has further library dependencies that we don't need: $ ldd /usr/lib64/libmount.so.1 linux-vdso.so.1 (0x00007fff4f175000) libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000) libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000) libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000) /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000) libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000) namely selinux, pcre and dl. Summing it up, libmount causes more trouble than it's worth using a convenience library, we want to keep the dependencies minimal so the custom mountinfo parser was inevitable. Issue: #333 Issue: #334 Issue: #336 Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-10 15:36:19 +00:00
mnt = fopen("/proc/self/mountinfo", "r");
if (!mnt)
return -1;
btrfs-progs: reimplement find_mount_fsroot without libmount In commit 57cfe29e6936 ("btrfs-progs: utils: introduce find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a convenience library libmount, because getmntent does not provide the information we need to distinguish bind mounts. Using libmount turned out to be problematic in several ways: - static build got broken due to clashing symbols, eg. for parsing size or path canonicalization (#333) - long-term distros do not have libmount new enough (2.24+) to provide some functions (mnt_table_is_empty, #334) - libmount internally uses getgrnam_r/mnt_get_uid/... that are not static-build friendly, a warning is printed during link time for each binary; we don't use any of the functions - libmount has further library dependencies that we don't need: $ ldd /usr/lib64/libmount.so.1 linux-vdso.so.1 (0x00007fff4f175000) libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000) libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000) libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000) /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000) libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000) namely selinux, pcre and dl. Summing it up, libmount causes more trouble than it's worth using a convenience library, we want to keep the dependencies minimal so the custom mountinfo parser was inevitable. Issue: #333 Issue: #334 Issue: #336 Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-10 15:36:19 +00:00
buf = malloc(bs);
if (!buf) {
ret = -ENOMEM;
goto out;
}
btrfs-progs: reimplement find_mount_fsroot without libmount In commit 57cfe29e6936 ("btrfs-progs: utils: introduce find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a convenience library libmount, because getmntent does not provide the information we need to distinguish bind mounts. Using libmount turned out to be problematic in several ways: - static build got broken due to clashing symbols, eg. for parsing size or path canonicalization (#333) - long-term distros do not have libmount new enough (2.24+) to provide some functions (mnt_table_is_empty, #334) - libmount internally uses getgrnam_r/mnt_get_uid/... that are not static-build friendly, a warning is printed during link time for each binary; we don't use any of the functions - libmount has further library dependencies that we don't need: $ ldd /usr/lib64/libmount.so.1 linux-vdso.so.1 (0x00007fff4f175000) libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000) libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000) libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000) /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000) libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000) namely selinux, pcre and dl. Summing it up, libmount causes more trouble than it's worth using a convenience library, we want to keep the dependencies minimal so the custom mountinfo parser was inevitable. Issue: #333 Issue: #334 Issue: #336 Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-10 15:36:19 +00:00
do {
int ch;
btrfs-progs: reimplement find_mount_fsroot without libmount In commit 57cfe29e6936 ("btrfs-progs: utils: introduce find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a convenience library libmount, because getmntent does not provide the information we need to distinguish bind mounts. Using libmount turned out to be problematic in several ways: - static build got broken due to clashing symbols, eg. for parsing size or path canonicalization (#333) - long-term distros do not have libmount new enough (2.24+) to provide some functions (mnt_table_is_empty, #334) - libmount internally uses getgrnam_r/mnt_get_uid/... that are not static-build friendly, a warning is printed during link time for each binary; we don't use any of the functions - libmount has further library dependencies that we don't need: $ ldd /usr/lib64/libmount.so.1 linux-vdso.so.1 (0x00007fff4f175000) libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000) libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000) libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000) /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000) libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000) namely selinux, pcre and dl. Summing it up, libmount causes more trouble than it's worth using a convenience library, we want to keep the dependencies minimal so the custom mountinfo parser was inevitable. Issue: #333 Issue: #334 Issue: #336 Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-10 15:36:19 +00:00
ch = fgetc(mnt);
if (ch == -1)
break;
btrfs-progs: reimplement find_mount_fsroot without libmount In commit 57cfe29e6936 ("btrfs-progs: utils: introduce find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a convenience library libmount, because getmntent does not provide the information we need to distinguish bind mounts. Using libmount turned out to be problematic in several ways: - static build got broken due to clashing symbols, eg. for parsing size or path canonicalization (#333) - long-term distros do not have libmount new enough (2.24+) to provide some functions (mnt_table_is_empty, #334) - libmount internally uses getgrnam_r/mnt_get_uid/... that are not static-build friendly, a warning is printed during link time for each binary; we don't use any of the functions - libmount has further library dependencies that we don't need: $ ldd /usr/lib64/libmount.so.1 linux-vdso.so.1 (0x00007fff4f175000) libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000) libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000) libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000) /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000) libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000) namely selinux, pcre and dl. Summing it up, libmount causes more trouble than it's worth using a convenience library, we want to keep the dependencies minimal so the custom mountinfo parser was inevitable. Issue: #333 Issue: #334 Issue: #336 Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-10 15:36:19 +00:00
if (ch == '\n') {
struct mnt_entry ent;
char *opt;
const char *value;
buf[line] = 0;
parse_mntinfo_line(buf, &ent);
/* Skip unrelated mounts */
if (strcmp(ent.fstype, "btrfs") != 0)
goto nextline;
if (strlen(ent.root) != strlen(subvol))
goto nextline;
if (strcmp(ent.root, subvol) != 0)
goto nextline;
/*
* Match subvolume by id found in mountinfo and
* requested by the caller
*/
opt = find_option(ent.options2, "subvolid=");
if (!opt)
goto nextline;
value = opt + strlen("subvolid=");
if (strcmp(value, subvolid) != 0) {
free(opt);
goto nextline;
}
free(opt);
/*
* First match is in most cases the original mount, not
* a bind mount. In case there are no further bind
* mounts, return what we found in @mount. Any
* following mount that matches by path and subvolume
* id is a bind mount and we return the original mount.
*/
if (found)
goto out;
found = true;
*mount = strdup(ent.path);
ret = 0;
goto nextline;
}
/*
btrfs-progs: reimplement find_mount_fsroot without libmount In commit 57cfe29e6936 ("btrfs-progs: utils: introduce find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a convenience library libmount, because getmntent does not provide the information we need to distinguish bind mounts. Using libmount turned out to be problematic in several ways: - static build got broken due to clashing symbols, eg. for parsing size or path canonicalization (#333) - long-term distros do not have libmount new enough (2.24+) to provide some functions (mnt_table_is_empty, #334) - libmount internally uses getgrnam_r/mnt_get_uid/... that are not static-build friendly, a warning is printed during link time for each binary; we don't use any of the functions - libmount has further library dependencies that we don't need: $ ldd /usr/lib64/libmount.so.1 linux-vdso.so.1 (0x00007fff4f175000) libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000) libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000) libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000) /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000) libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000) namely selinux, pcre and dl. Summing it up, libmount causes more trouble than it's worth using a convenience library, we want to keep the dependencies minimal so the custom mountinfo parser was inevitable. Issue: #333 Issue: #334 Issue: #336 Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-10 15:36:19 +00:00
* Grow buffer if needed, there are 3 paths up to PATH_MAX and
* mount options are limited by page size. Often the overall
* line length does not exceed 256.
*/
btrfs-progs: reimplement find_mount_fsroot without libmount In commit 57cfe29e6936 ("btrfs-progs: utils: introduce find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a convenience library libmount, because getmntent does not provide the information we need to distinguish bind mounts. Using libmount turned out to be problematic in several ways: - static build got broken due to clashing symbols, eg. for parsing size or path canonicalization (#333) - long-term distros do not have libmount new enough (2.24+) to provide some functions (mnt_table_is_empty, #334) - libmount internally uses getgrnam_r/mnt_get_uid/... that are not static-build friendly, a warning is printed during link time for each binary; we don't use any of the functions - libmount has further library dependencies that we don't need: $ ldd /usr/lib64/libmount.so.1 linux-vdso.so.1 (0x00007fff4f175000) libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000) libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000) libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000) /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000) libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000) namely selinux, pcre and dl. Summing it up, libmount causes more trouble than it's worth using a convenience library, we want to keep the dependencies minimal so the custom mountinfo parser was inevitable. Issue: #333 Issue: #334 Issue: #336 Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-10 15:36:19 +00:00
if (line >= bs) {
char *tmp;
bs += 4096;
tmp = realloc(buf, bs);
if (!tmp) {
ret = -ENOMEM;
goto out;
}
buf = tmp;
}
buf[line++] = ch;
continue;
nextline:
line = 0;
} while (1);
out:
free(buf);
fclose(mnt);
return ret;
}
/*
* return 0 if a btrfs mount point is found
* return 1 if a mount point is found but not btrfs
* return <0 if something goes wrong
*/
int find_mount_root(const char *path, char **mount_root)
{
FILE *mnttab;
int fd;
struct mntent *ent;
int len;
int ret = 0;
int not_btrfs = 1;
int longest_matchlen = 0;
char *longest_match = NULL;
fd = open(path, O_RDONLY | O_NOATIME);
if (fd < 0)
return -errno;
close(fd);
mnttab = setmntent("/proc/self/mounts", "r");
if (!mnttab)
return -errno;
while ((ent = getmntent(mnttab))) {
if (path_is_in_dir(ent->mnt_dir, path)) {
len = strlen(ent->mnt_dir);
if (longest_matchlen <= len) {
free(longest_match);
longest_matchlen = len;
longest_match = strdup(ent->mnt_dir);
if (!longest_match) {
ret = -errno;
break;
}
not_btrfs = strcmp(ent->mnt_type, "btrfs");
}
}
}
endmntent(mnttab);
if (ret)
return ret;
if (!longest_match)
return -ENOENT;
if (not_btrfs) {
free(longest_match);
return 1;
}
ret = 0;
*mount_root = realpath(longest_match, NULL);
if (!*mount_root)
ret = -errno;
free(longest_match);
return ret;
}
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)
{
int level;
for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
if (!path->nodes[level])
break;
if (path->slots[level] + 1 >=
btrfs_header_nritems(path->nodes[level]))
continue;
if (level == 0)
btrfs_item_key_to_cpu(path->nodes[level], key,
path->slots[level] + 1);
else
btrfs_node_key_to_cpu(path->nodes[level], key,
path->slots[level] + 1);
return 0;
}
return 1;
}
const char* btrfs_group_type_str(u64 flag)
{
u64 mask = BTRFS_BLOCK_GROUP_TYPE_MASK |
BTRFS_SPACE_INFO_GLOBAL_RSV;
switch (flag & mask) {
case BTRFS_BLOCK_GROUP_DATA:
return "Data";
case BTRFS_BLOCK_GROUP_SYSTEM:
return "System";
case BTRFS_BLOCK_GROUP_METADATA:
return "Metadata";
case BTRFS_BLOCK_GROUP_DATA|BTRFS_BLOCK_GROUP_METADATA:
return "Data+Metadata";
case BTRFS_SPACE_INFO_GLOBAL_RSV:
return "GlobalReserve";
default:
return "unknown";
}
}
const char* btrfs_group_profile_str(u64 flag)
{
int index;
flag &= ~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_RESERVED);
if (flag & ~BTRFS_BLOCK_GROUP_PROFILE_MASK)
2021-11-04 01:58:07 +00:00
return "UNKNOWN";
index = btrfs_bg_flags_to_raid_index(flag);
2021-11-04 01:58:07 +00:00
return btrfs_raid_array[index].upper_name;
}
u64 div_factor(u64 num, int factor)
{
if (factor == 10)
return num;
num *= factor;
num /= 10;
return num;
}
/*
* Get the length of the string converted from a u64 number.
*
* Result is equal to log10(num) + 1, but without the use of math library.
*/
int count_digits(u64 num)
{
int ret = 0;
if (num == 0)
return 1;
while (num > 0) {
ret++;
num /= 10;
}
return ret;
}
int string_is_numerical(const char *str)
{
if (!str)
return 0;
if (!(*str >= '0' && *str <= '9'))
return 0;
while (*str >= '0' && *str <= '9')
str++;
if (*str != '\0')
return 0;
return 1;
}
int prefixcmp(const char *str, const char *prefix)
{
for (; ; str++, prefix++)
if (!*prefix)
return 0;
else if (*str != *prefix)
return (unsigned char)*prefix - (unsigned char)*str;
}
const char *subvol_strip_mountpoint(const char *mnt, const char *full_path)
{
int len = strlen(mnt);
if (!len)
return full_path;
if ((strncmp(mnt, full_path, len) != 0) || ((len > 1) && (full_path[len] != '/'))) {
error("not on mount point: %s", mnt);
exit(1);
}
if (mnt[len - 1] != '/')
len += 1;
return full_path + len;
}
/* Set the seed manually */
void init_rand_seed(u64 seed)
{
int i;
/* only use the last 48 bits */
for (i = 0; i < 3; i++) {
rand_seed[i] = (unsigned short)(seed ^ (unsigned short)(-1));
seed >>= 16;
}
rand_seed_initialized = 1;
}
static void __init_seed(void)
{
struct timeval tv;
int ret;
int fd;
if(rand_seed_initialized)
return;
/* Use urandom as primary seed source. */
fd = open("/dev/urandom", O_RDONLY);
if (fd >= 0) {
ret = read(fd, rand_seed, sizeof(rand_seed));
close(fd);
if (ret < sizeof(rand_seed))
goto fallback;
} else {
fallback:
/* Use time and pid as fallback seed */
warning("failed to read /dev/urandom, use time and pid as random seed");
gettimeofday(&tv, 0);
rand_seed[0] = getpid() ^ (tv.tv_sec & 0xFFFF);
rand_seed[1] = getppid() ^ (tv.tv_usec & 0xFFFF);
rand_seed[2] = (tv.tv_sec ^ tv.tv_usec) >> 16;
}
rand_seed_initialized = 1;
}
u32 rand_u32(void)
{
__init_seed();
/*
* Don't use nrand48, its range is [0,2^31) The highest bit will always
* be 0. Use jrand48 to include the highest bit.
*/
return (u32)jrand48(rand_seed);
}
/* Return random number in range [0, upper) */
unsigned int rand_range(unsigned int upper)
{
__init_seed();
/*
* Use the full 48bits to mod, which would be more uniformly
* distributed
*/
return (unsigned int)(jrand48(rand_seed) % upper);
}
int rand_int(void)
{
return (int)(rand_u32());
}
u64 rand_u64(void)
{
u64 ret = 0;
ret += rand_u32();
ret <<= 32;
ret += rand_u32();
return ret;
}
u16 rand_u16(void)
{
return (u16)(rand_u32());
}
u8 rand_u8(void)
{
return (u8)(rand_u32());
}
void btrfs_config_init(void)
{
bconf.output_format = CMD_FORMAT_TEXT;
bconf.verbose = BTRFS_BCONF_UNSET;
}
void bconf_be_verbose(void)
{
if (bconf.verbose == BTRFS_BCONF_UNSET)
bconf.verbose = 1;
else
bconf.verbose++;
}
void bconf_be_quiet(void)
{
bconf.verbose = BTRFS_BCONF_QUIET;
}
/* Returns total size of main memory in bytes, -1UL if error. */
unsigned long total_memory(void)
{
struct sysinfo si;
if (sysinfo(&si) < 0) {
error("can't determine memory size");
return -1UL;
}
return si.totalram * si.mem_unit; /* bytes */
}
void print_device_info(struct btrfs_device *device, char *prefix)
{
if (prefix)
printf("%s", prefix);
printf("Device: id = %llu, name = %s\n",
device->devid, device->name);
}
void print_all_devices(struct list_head *devices)
{
struct btrfs_device *dev;
printf("All Devices:\n");
list_for_each_entry(dev, devices, dev_list)
print_device_info(dev, "\t");
printf("\n");
}
static int bit_count(u64 x)
{
int ret = 0;
while (x) {
if (x & 1)
ret++;
x >>= 1;
}
return ret;
}
static char *sprint_profiles(u64 profiles)
{
int i;
int maxlen = 1;
char *ptr;
if (bit_count(profiles) <= 1)
return NULL;
for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2021-11-04 01:58:07 +00:00
maxlen += strlen(btrfs_raid_array[i].lower_name) + 2;
ptr = calloc(1, maxlen);
if (!ptr)
return NULL;
if (profiles & BTRFS_AVAIL_ALLOC_BIT_SINGLE)
2021-11-04 01:58:07 +00:00
strcat(ptr, btrfs_raid_array[BTRFS_RAID_SINGLE].lower_name);
for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
if (!(btrfs_raid_array[i].bg_flag & profiles))
continue;
if (ptr[0])
strcat(ptr, ", ");
2021-11-04 01:58:07 +00:00
strcat(ptr, btrfs_raid_array[i].lower_name);
}
return ptr;
}
static int btrfs_get_string_for_multiple_profiles(int fd, char **data_ret,
char **metadata_ret, char **mixed_ret, char **system_ret,
char **types_ret)
{
int ret;
int i;
struct btrfs_ioctl_space_args *sargs;
u64 data_profiles = 0;
u64 metadata_profiles = 0;
u64 system_profiles = 0;
u64 mixed_profiles = 0;
const u64 mixed_profile_fl = BTRFS_BLOCK_GROUP_METADATA |
BTRFS_BLOCK_GROUP_DATA;
ret = get_df(fd, &sargs);
if (ret < 0)
return -1;
for (i = 0; i < sargs->total_spaces; i++) {
u64 flags = sargs->spaces[i].flags;
if (!(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK))
flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE;
if ((flags & mixed_profile_fl) == mixed_profile_fl)
mixed_profiles |= flags;
else if (flags & BTRFS_BLOCK_GROUP_DATA)
data_profiles |= flags;
else if (flags & BTRFS_BLOCK_GROUP_METADATA)
metadata_profiles |= flags;
else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
system_profiles |= flags;
}
free(sargs);
data_profiles &= BTRFS_EXTENDED_PROFILE_MASK;
system_profiles &= BTRFS_EXTENDED_PROFILE_MASK;
mixed_profiles &= BTRFS_EXTENDED_PROFILE_MASK;
metadata_profiles &= BTRFS_EXTENDED_PROFILE_MASK;
*data_ret = sprint_profiles(data_profiles);
*metadata_ret = sprint_profiles(metadata_profiles);
*mixed_ret = sprint_profiles(mixed_profiles);
*system_ret = sprint_profiles(system_profiles);
if (types_ret) {
*types_ret = calloc(1, 64);
if (!*types_ret)
goto out;
if (*data_ret)
strcat(*types_ret, "data");
if (*metadata_ret) {
if ((*types_ret)[0])
strcat(*types_ret, ", ");
strcat(*types_ret, "metadata");
}
if (*mixed_ret) {
if ((*types_ret)[0])
strcat(*types_ret, ", ");
strcat(*types_ret, "data+metadata");
}
if (*system_ret) {
if ((*types_ret)[0])
strcat(*types_ret, ", ");
strcat(*types_ret, "system");
}
}
out:
return *data_ret || *metadata_ret || *mixed_ret || *system_ret;
}
/*
* Return string containing coma separated list of block group types that
* contain multiple profiles. The return value must be freed by the caller.
*/
char *btrfs_test_for_multiple_profiles(int fd)
{
char *data, *metadata, *system, *mixed, *types;
btrfs_get_string_for_multiple_profiles(fd, &data, &metadata, &mixed,
&system, &types);
free(data);
free(metadata);
free(mixed);
free(system);
return types;
}
int btrfs_warn_multiple_profiles(int fd)
{
int ret;
char *data_prof, *mixed_prof, *metadata_prof, *system_prof;
ret = btrfs_get_string_for_multiple_profiles(fd, &data_prof,
&metadata_prof, &mixed_prof, &system_prof, NULL);
if (ret != 1)
return ret;
fprintf(stderr,
"WARNING: Multiple block group profiles detected, see 'man btrfs(5)'.\n");
if (data_prof)
fprintf(stderr, "WARNING: Data: %s\n", data_prof);
if (metadata_prof)
fprintf(stderr, "WARNING: Metadata: %s\n", metadata_prof);
if (mixed_prof)
fprintf(stderr, "WARNING: Data+Metadata: %s\n", mixed_prof);
if (system_prof)
fprintf(stderr, "WARNING: System: %s\n", system_prof);
free(data_prof);
free(metadata_prof);
free(mixed_prof);
free(system_prof);
return 1;
}
/*
* Open a file in fsid directory in sysfs and return the file descriptor or
* error
*/
int sysfs_open_fsid_file(int fd, const char *filename)
{
u8 fsid[BTRFS_UUID_SIZE];
char fsid_str[BTRFS_UUID_UNPARSED_SIZE];
char sysfs_file[PATH_MAX];
int ret;
ret = get_fsid_fd(fd, fsid);
if (ret < 0)
return ret;
uuid_unparse(fsid, fsid_str);
ret = path_cat3_out(sysfs_file, "/sys/fs/btrfs", fsid_str, filename);
if (ret < 0)
return ret;
return open(sysfs_file, O_RDONLY);
}
/*
* Open a file in the toplevel sysfs directory and return the file descriptor
* or error.
*/
int sysfs_open_file(const char *name)
{
char path[PATH_MAX];
int ret;
ret = path_cat_out(path, "/sys/fs/btrfs", name);
if (ret < 0)
return ret;
return open(path, O_RDONLY);
}
/*
* Open a directory by name in fsid directory in sysfs and return the file
* descriptor or error, filedescriptor suitable for fdreaddir. The @dirname
* must be a directory name.
*/
int sysfs_open_fsid_dir(int fd, const char *dirname)
{
u8 fsid[BTRFS_UUID_SIZE];
char fsid_str[BTRFS_UUID_UNPARSED_SIZE];
char sysfs_file[PATH_MAX];
int ret;
ret = get_fsid_fd(fd, fsid);
if (ret < 0)
return ret;
uuid_unparse(fsid, fsid_str);
ret = path_cat3_out(sysfs_file, "/sys/fs/btrfs", fsid_str, dirname);
if (ret < 0)
return ret;
return open(sysfs_file, O_DIRECTORY | O_RDONLY);
}
/*
* Read up to @size bytes to @buf from @fd
*/
int sysfs_read_file(int fd, char *buf, size_t size)
{
lseek(fd, 0, SEEK_SET);
memset(buf, 0, size);
return read(fd, buf, size);
}
static const char exclop_def[][16] = {
[BTRFS_EXCLOP_NONE] = "none",
[BTRFS_EXCLOP_BALANCE] = "balance",
[BTRFS_EXCLOP_DEV_ADD] = "device add",
[BTRFS_EXCLOP_DEV_REMOVE] = "device remove",
[BTRFS_EXCLOP_DEV_REPLACE] = "device replace",
[BTRFS_EXCLOP_RESIZE] = "resize",
[BTRFS_EXCLOP_SWAP_ACTIVATE] = "swap activate",
};
/*
* Read currently running exclusive operation from sysfs. If this is not
* available, return BTRFS_EXCLOP_UNKNOWN
*/
int get_fs_exclop(int fd)
{
int sysfs_fd;
char buf[32];
int ret;
int i;
sysfs_fd = sysfs_open_fsid_file(fd, "exclusive_operation");
if (sysfs_fd < 0)
return BTRFS_EXCLOP_UNKNOWN;
memset(buf, 0, sizeof(buf));
ret = sysfs_read_file(sysfs_fd, buf, sizeof(buf));
close(sysfs_fd);
if (ret <= 0)
return BTRFS_EXCLOP_UNKNOWN;
i = strlen(buf) - 1;
while (i > 0 && isspace(buf[i])) i--;
if (i > 0)
buf[i + 1] = 0;
for (i = 0; i < ARRAY_SIZE(exclop_def); i++) {
if (strcmp(exclop_def[i], buf) == 0)
return i;
}
return BTRFS_EXCLOP_UNKNOWN;
}
const char *get_fs_exclop_name(int op)
{
if (0 <= op && op <= ARRAY_SIZE(exclop_def))
return exclop_def[op];
return "UNKNOWN";
}
/*
* Check if there's another exclusive operation running and either return error
* or wait until there's none in case @enqueue is true. The timeout between
* checks is 1 minute as we get notification on the sysfs file when the
* operation finishes.
*
* Return:
* 0 - caller can continue, nothing running or the status is not available
* 1 - another operation running
* <0 - there was another error
*/
int check_running_fs_exclop(int fd, enum exclusive_operation start, bool enqueue)
{
int sysfs_fd;
int exclop;
int ret;
sysfs_fd = sysfs_open_fsid_file(fd, "exclusive_operation");
if (sysfs_fd < 0) {
if (errno == ENOENT)
return 0;
return -errno;
}
exclop = get_fs_exclop(fd);
if (exclop <= 0) {
ret = 0;
goto out;
}
if (!enqueue) {
error(
"unable to start %s, another exclusive operation '%s' in progress",
get_fs_exclop_name(start),
get_fs_exclop_name(exclop));
ret = 1;
goto out;
}
while (exclop > 0) {
fd_set fds;
struct timeval tv = { .tv_sec = 60, .tv_usec = 0 };
FD_ZERO(&fds);
FD_SET(sysfs_fd, &fds);
ret = select(sysfs_fd + 1, NULL, NULL, &fds, &tv);
if (ret < 0) {
ret = -errno;
break;
}
if (ret > 0) {
/*
* Notified before the timeout, check again before
* returning. In case there are more operations
* waiting, we want to reduce the chances to race so
* reuse the remaining time to randomize the order.
*/
tv.tv_sec /= 2;
ret = select(sysfs_fd + 1, NULL, NULL, &fds, &tv);
exclop = get_fs_exclop(fd);
if (exclop <= 0)
ret = 0;
}
}
out:
close(sysfs_fd);
return ret;
}
/*
* This function should be only used when parsing command arg, it won't return
* error to its caller and rather exit directly just like usage().
*/
u64 arg_strtou64(const char *str)
{
u64 value;
char *ptr_parse_end = NULL;
value = strtoull(str, &ptr_parse_end, 0);
if (ptr_parse_end && *ptr_parse_end != '\0') {
fprintf(stderr, "ERROR: %s is not a valid numeric value.\n",
str);
exit(1);
}
/*
* if we pass a negative number to strtoull, it will return an
* unexpected number to us, so let's do the check ourselves.
*/
if (str[0] == '-') {
fprintf(stderr, "ERROR: %s: negative value is invalid.\n",
str);
exit(1);
}
if (value == ULLONG_MAX) {
fprintf(stderr, "ERROR: %s is too large.\n", str);
exit(1);
}
return value;
}
/*
* For a given:
* - file or directory return the containing tree root id
* - subvolume return its own tree id
* - BTRFS_EMPTY_SUBVOL_DIR_OBJECTID (directory with ino == 2) the result is
* undefined and function returns -1
*/
int lookup_path_rootid(int fd, u64 *rootid)
{
struct btrfs_ioctl_ino_lookup_args args;
int ret;
memset(&args, 0, sizeof(args));
args.treeid = 0;
args.objectid = BTRFS_FIRST_FREE_OBJECTID;
ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
if (ret < 0)
return -errno;
*rootid = args.treeid;
return 0;
}