2008-01-04 23:38:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Oracle. All rights reserved.
|
2013-01-30 16:32:47 +08:00
|
|
|
* Copyright (C) 2008 Morey Roof. All rights reserved.
|
2008-01-04 23:38:22 +08:00
|
|
|
*
|
|
|
|
* 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>
|
2013-01-21 05:04:07 +08:00
|
|
|
#include <string.h>
|
2008-03-25 03:04:49 +08:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mount.h>
|
2008-01-04 23:38:22 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2017-07-26 04:51:34 +08:00
|
|
|
#include <sys/sysinfo.h>
|
2008-01-04 23:38:22 +08:00
|
|
|
#include <uuid/uuid.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2008-04-01 22:36:46 +08:00
|
|
|
#include <mntent.h>
|
2012-10-30 01:53:18 +08:00
|
|
|
#include <ctype.h>
|
2010-09-09 10:57:02 +08:00
|
|
|
#include <linux/loop.h>
|
|
|
|
#include <linux/major.h>
|
|
|
|
#include <linux/kdev_t.h>
|
|
|
|
#include <limits.h>
|
2013-04-15 14:38:09 +08:00
|
|
|
#include <blkid/blkid.h>
|
2014-02-14 03:19:01 +08:00
|
|
|
#include <sys/vfs.h>
|
2015-08-26 17:04:22 +08:00
|
|
|
#include <sys/statfs.h>
|
|
|
|
#include <linux/magic.h>
|
2017-01-26 20:27:58 +08:00
|
|
|
#include <getopt.h>
|
2014-02-14 03:19:01 +08:00
|
|
|
|
2018-01-21 16:24:50 +08:00
|
|
|
#include <btrfsutil.h>
|
|
|
|
|
2008-01-04 23:38:22 +08:00
|
|
|
#include "kerncompat.h"
|
2019-06-20 21:30:57 +08:00
|
|
|
#include "kernel-lib/radix-tree.h"
|
2008-01-04 23:38:22 +08:00
|
|
|
#include "ctree.h"
|
|
|
|
#include "disk-io.h"
|
|
|
|
#include "transaction.h"
|
2019-09-25 21:37:27 +08:00
|
|
|
#include "crypto/crc32c.h"
|
2019-06-20 07:46:21 +08:00
|
|
|
#include "common/utils.h"
|
2019-07-02 02:54:39 +08:00
|
|
|
#include "common/path-utils.h"
|
2019-07-02 06:42:23 +08:00
|
|
|
#include "common/device-scan.h"
|
2008-03-25 03:04:49 +08:00
|
|
|
#include "volumes.h"
|
2008-03-25 03:05:44 +08:00
|
|
|
#include "ioctl.h"
|
2019-06-20 07:46:21 +08:00
|
|
|
#include "cmds/commands.h"
|
2017-02-01 06:13:54 +08:00
|
|
|
#include "mkfs/common.h"
|
2008-03-25 03:04:49 +08:00
|
|
|
|
2018-11-27 01:14:48 +08:00
|
|
|
static int rand_seed_initialized = 0;
|
2016-05-26 10:56:50 +08:00
|
|
|
static unsigned short rand_seed[3];
|
|
|
|
|
2016-11-21 20:52:01 +08:00
|
|
|
struct btrfs_config bconf;
|
|
|
|
|
2008-01-04 23:38:22 +08:00
|
|
|
int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
|
|
|
|
struct btrfs_root *root, u64 objectid)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct btrfs_inode_item inode_item;
|
2010-11-17 09:07:52 +08:00
|
|
|
time_t now = time(NULL);
|
2008-01-04 23:38:22 +08:00
|
|
|
|
|
|
|
memset(&inode_item, 0, sizeof(inode_item));
|
|
|
|
btrfs_set_stack_inode_generation(&inode_item, trans->transid);
|
|
|
|
btrfs_set_stack_inode_size(&inode_item, 0);
|
|
|
|
btrfs_set_stack_inode_nlink(&inode_item, 1);
|
2017-05-18 09:40:25 +08:00
|
|
|
btrfs_set_stack_inode_nbytes(&inode_item, root->fs_info->nodesize);
|
2012-07-17 18:18:10 +08:00
|
|
|
btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755);
|
2010-11-17 09:07:52 +08:00
|
|
|
btrfs_set_stack_timespec_sec(&inode_item.atime, now);
|
|
|
|
btrfs_set_stack_timespec_nsec(&inode_item.atime, 0);
|
|
|
|
btrfs_set_stack_timespec_sec(&inode_item.ctime, now);
|
|
|
|
btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0);
|
|
|
|
btrfs_set_stack_timespec_sec(&inode_item.mtime, now);
|
|
|
|
btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0);
|
2016-11-01 00:06:15 +08:00
|
|
|
btrfs_set_stack_timespec_sec(&inode_item.otime, now);
|
2010-11-17 09:07:52 +08:00
|
|
|
btrfs_set_stack_timespec_nsec(&inode_item.otime, 0);
|
2008-01-04 23:38:22 +08:00
|
|
|
|
|
|
|
if (root->fs_info->tree_root == root)
|
2013-03-07 00:32:51 +08:00
|
|
|
btrfs_set_super_root_dir(root->fs_info->super_copy, objectid);
|
2008-01-04 23:38:22 +08:00
|
|
|
|
|
|
|
ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
|
|
|
|
if (ret)
|
|
|
|
goto error;
|
|
|
|
|
2008-07-25 00:13:32 +08:00
|
|
|
ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid, 0);
|
2008-01-04 23:38:22 +08:00
|
|
|
if (ret)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
btrfs_set_root_dirid(&root->root_item, objectid);
|
|
|
|
ret = 0;
|
|
|
|
error:
|
|
|
|
return ret;
|
|
|
|
}
|
2008-03-25 03:05:44 +08:00
|
|
|
|
2013-03-12 07:12:59 +08:00
|
|
|
/*
|
|
|
|
* Find the mount point for a mounted device.
|
|
|
|
* On success, returns 0 with mountpoint in *mp.
|
|
|
|
* On failure, returns -errno (not mounted yields -EINVAL)
|
|
|
|
* Is noisy on failures, expects to be given a mounted device.
|
|
|
|
*/
|
2013-10-22 13:53:21 +08:00
|
|
|
int get_btrfs_mount(const char *dev, char *mp, size_t mp_size)
|
|
|
|
{
|
2013-03-12 07:12:59 +08:00
|
|
|
int ret;
|
|
|
|
int fd = -1;
|
|
|
|
|
2019-07-02 03:29:43 +08:00
|
|
|
ret = path_is_block_device(dev);
|
2013-03-12 07:12:59 +08:00
|
|
|
if (ret <= 0) {
|
|
|
|
if (!ret) {
|
2016-03-14 21:36:30 +08:00
|
|
|
error("not a block device: %s", dev);
|
2013-03-12 07:12:59 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
} else {
|
2018-10-25 20:10:54 +08:00
|
|
|
errno = -ret;
|
|
|
|
error("cannot check %s: %m", dev);
|
2013-03-12 07:12:59 +08:00
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(dev, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
ret = -errno;
|
2018-01-08 05:54:21 +08:00
|
|
|
error("cannot open %s: %m", dev);
|
2013-03-12 07:12:59 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-06-18 23:00:14 +08:00
|
|
|
ret = check_mounted_where(fd, dev, mp, mp_size, NULL, SBREAD_DEFAULT);
|
2013-03-12 07:12:59 +08:00
|
|
|
if (!ret) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
} else { /* mounted, all good */
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a pathname, return a filehandle to:
|
|
|
|
* the original pathname or,
|
|
|
|
* if the pathname is a mounted btrfs device, to its mountpoint.
|
|
|
|
*
|
|
|
|
* On error, return -1, errno should be set.
|
|
|
|
*/
|
2015-10-12 21:23:02 +08:00
|
|
|
int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose)
|
2013-03-12 07:12:59 +08:00
|
|
|
{
|
2015-06-12 19:18:44 +08:00
|
|
|
char mp[PATH_MAX];
|
2015-10-12 21:23:02 +08:00
|
|
|
int ret;
|
2013-03-12 07:12:59 +08:00
|
|
|
|
2019-07-02 03:29:43 +08:00
|
|
|
if (path_is_block_device(path)) {
|
2013-03-12 07:12:59 +08:00
|
|
|
ret = get_btrfs_mount(path, mp, sizeof(mp));
|
|
|
|
if (ret < 0) {
|
|
|
|
/* not a mounted btrfs dev */
|
2015-10-12 21:23:02 +08:00
|
|
|
error_on(verbose, "'%s' is not a mounted btrfs device",
|
|
|
|
path);
|
2013-03-12 07:12:59 +08:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2015-10-12 21:23:02 +08:00
|
|
|
ret = open_file_or_dir(mp, dirstream);
|
2018-01-08 05:54:21 +08:00
|
|
|
error_on(verbose && ret < 0, "can't access '%s': %m",
|
|
|
|
path);
|
2015-10-12 21:23:02 +08:00
|
|
|
} else {
|
|
|
|
ret = btrfs_open_dir(path, dirstream, 1);
|
2013-03-12 07:12:59 +08:00
|
|
|
}
|
|
|
|
|
2015-10-12 21:23:02 +08:00
|
|
|
return ret;
|
2013-03-12 07:12:59 +08:00
|
|
|
}
|
|
|
|
|
2015-08-26 17:04:22 +08:00
|
|
|
/*
|
|
|
|
* Do the following checks before calling open_file_or_dir():
|
|
|
|
* 1: path is in a btrfs filesystem
|
2017-09-04 13:05:34 +08:00
|
|
|
* 2: path is a directory if dir_only is 1
|
2015-08-26 17:04:22 +08:00
|
|
|
*/
|
2017-09-04 13:05:34 +08:00
|
|
|
int btrfs_open(const char *path, DIR **dirstream, int verbose, int dir_only)
|
2015-08-26 17:04:22 +08:00
|
|
|
{
|
|
|
|
struct statfs stfs;
|
|
|
|
struct stat st;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (statfs(path, &stfs) != 0) {
|
2018-01-08 05:54:21 +08:00
|
|
|
error_on(verbose, "cannot access '%s': %m", path);
|
2015-08-26 17:04:22 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stfs.f_type != BTRFS_SUPER_MAGIC) {
|
2016-03-14 21:36:30 +08:00
|
|
|
error_on(verbose, "not a btrfs filesystem: %s", path);
|
2015-08-26 17:04:22 +08:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(path, &st) != 0) {
|
2018-01-08 05:54:21 +08:00
|
|
|
error_on(verbose, "cannot access '%s': %m", path);
|
2015-08-26 17:04:22 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-09-04 13:05:34 +08:00
|
|
|
if (dir_only && !S_ISDIR(st.st_mode)) {
|
2016-03-14 21:36:30 +08:00
|
|
|
error_on(verbose, "not a directory: %s", path);
|
2015-08-26 17:04:22 +08:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = open_file_or_dir(path, dirstream);
|
|
|
|
if (ret < 0) {
|
2018-01-08 05:54:21 +08:00
|
|
|
error_on(verbose, "cannot access '%s': %m", path);
|
2015-08-26 17:04:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-04 13:05:34 +08:00
|
|
|
int btrfs_open_dir(const char *path, DIR **dirstream, int verbose)
|
|
|
|
{
|
|
|
|
return btrfs_open(path, dirstream, verbose, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int btrfs_open_file_or_dir(const char *path, DIR **dirstream, int verbose)
|
|
|
|
{
|
|
|
|
return btrfs_open(path, dirstream, verbose, 0);
|
|
|
|
}
|
|
|
|
|
2010-09-09 10:57:02 +08:00
|
|
|
/* Checks if a file is used (directly or indirectly via a loop device)
|
|
|
|
* by a device in fs_devices
|
|
|
|
*/
|
2013-08-10 04:20:47 +08:00
|
|
|
static int blk_file_in_dev_list(struct btrfs_fs_devices* fs_devices,
|
|
|
|
const char* file)
|
2010-09-09 10:57:02 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct btrfs_device *device;
|
|
|
|
|
2017-12-07 17:10:05 +08:00
|
|
|
list_for_each_entry(device, &fs_devices->devices, dev_list) {
|
2010-09-09 10:57:02 +08:00
|
|
|
if((ret = is_same_loop_file(device->name, file)))
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-01 22:36:46 +08:00
|
|
|
/*
|
|
|
|
* returns 1 if the device was mounted, < 0 on error or 0 if everything
|
2010-09-09 10:57:02 +08:00
|
|
|
* is safe to continue.
|
2008-04-01 22:36:46 +08:00
|
|
|
*/
|
2010-09-09 10:57:02 +08:00
|
|
|
int check_mounted(const char* file)
|
2008-04-01 22:36:46 +08:00
|
|
|
{
|
2010-09-09 10:57:02 +08:00
|
|
|
int fd;
|
2011-07-18 20:37:23 +08:00
|
|
|
int ret;
|
2008-04-01 22:36:46 +08:00
|
|
|
|
2010-09-09 10:57:02 +08:00
|
|
|
fd = open(file, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2018-01-08 05:54:21 +08:00
|
|
|
error("mount check: cannot open %s: %m", file);
|
2008-04-01 22:36:46 +08:00
|
|
|
return -errno;
|
2010-09-09 10:57:02 +08:00
|
|
|
}
|
2008-04-01 22:36:46 +08:00
|
|
|
|
2018-06-18 23:00:14 +08:00
|
|
|
ret = check_mounted_where(fd, file, NULL, 0, NULL, SBREAD_DEFAULT);
|
2011-07-18 20:37:23 +08:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int check_mounted_where(int fd, const char *file, char *where, int size,
|
2018-06-18 23:00:14 +08:00
|
|
|
struct btrfs_fs_devices **fs_dev_ret, unsigned sbflags)
|
2011-07-18 20:37:23 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u64 total_devs = 1;
|
|
|
|
int is_btrfs;
|
|
|
|
struct btrfs_fs_devices *fs_devices_mnt = NULL;
|
|
|
|
FILE *f;
|
|
|
|
struct mntent *mnt;
|
|
|
|
|
2010-09-09 10:57:02 +08:00
|
|
|
/* scan the initial device */
|
|
|
|
ret = btrfs_scan_one_device(fd, file, &fs_devices_mnt,
|
2018-06-18 23:00:14 +08:00
|
|
|
&total_devs, BTRFS_SUPER_INFO_OFFSET, sbflags);
|
2010-09-09 10:57:02 +08:00
|
|
|
is_btrfs = (ret >= 0);
|
|
|
|
|
|
|
|
/* scan other devices */
|
|
|
|
if (is_btrfs && total_devs > 1) {
|
2014-11-11 19:35:58 +08:00
|
|
|
ret = btrfs_scan_devices();
|
2014-09-13 09:21:21 +08:00
|
|
|
if (ret)
|
2010-09-09 10:57:02 +08:00
|
|
|
return ret;
|
2008-04-01 22:36:46 +08:00
|
|
|
}
|
|
|
|
|
2016-05-12 07:50:36 +08:00
|
|
|
/* iterate over the list of currently mounted filesystems */
|
2013-11-07 10:53:43 +08:00
|
|
|
if ((f = setmntent ("/proc/self/mounts", "r")) == NULL)
|
2010-09-09 10:57:02 +08:00
|
|
|
return -errno;
|
|
|
|
|
2008-04-01 22:36:46 +08:00
|
|
|
while ((mnt = getmntent (f)) != NULL) {
|
2010-09-09 10:57:02 +08:00
|
|
|
if(is_btrfs) {
|
|
|
|
if(strcmp(mnt->mnt_type, "btrfs") != 0)
|
|
|
|
continue;
|
2008-04-01 22:36:46 +08:00
|
|
|
|
2010-09-09 10:57:02 +08:00
|
|
|
ret = blk_file_in_dev_list(fs_devices_mnt, mnt->mnt_fsname);
|
|
|
|
} else {
|
|
|
|
/* ignore entries in the mount table that are not
|
|
|
|
associated with a file*/
|
2019-07-02 06:09:21 +08:00
|
|
|
if((ret = path_is_reg_or_block_device(mnt->mnt_fsname)) < 0)
|
2010-09-09 10:57:02 +08:00
|
|
|
goto out_mntloop_err;
|
|
|
|
else if(!ret)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = is_same_loop_file(file, mnt->mnt_fsname);
|
2008-04-01 22:36:46 +08:00
|
|
|
}
|
|
|
|
|
2010-09-09 10:57:02 +08:00
|
|
|
if(ret < 0)
|
|
|
|
goto out_mntloop_err;
|
|
|
|
else if(ret)
|
|
|
|
break;
|
2008-04-01 22:36:46 +08:00
|
|
|
}
|
|
|
|
|
2010-09-09 10:57:02 +08:00
|
|
|
/* Did we find an entry in mnt table? */
|
2012-04-21 03:27:25 +08:00
|
|
|
if (mnt && size && where) {
|
2011-07-18 20:37:23 +08:00
|
|
|
strncpy(where, mnt->mnt_dir, size);
|
2012-04-21 03:27:25 +08:00
|
|
|
where[size-1] = 0;
|
|
|
|
}
|
2011-07-18 20:37:23 +08:00
|
|
|
if (fs_dev_ret)
|
|
|
|
*fs_dev_ret = fs_devices_mnt;
|
|
|
|
|
2010-09-09 10:57:02 +08:00
|
|
|
ret = (mnt != NULL);
|
|
|
|
|
|
|
|
out_mntloop_err:
|
2008-04-01 22:36:46 +08:00
|
|
|
endmntent (f);
|
2010-09-09 10:57:02 +08:00
|
|
|
|
2008-04-01 22:36:46 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-03-25 03:05:44 +08:00
|
|
|
struct pending_dir {
|
|
|
|
struct list_head list;
|
2013-01-24 03:31:24 +08:00
|
|
|
char name[PATH_MAX];
|
2008-03-25 03:05:44 +08:00
|
|
|
};
|
|
|
|
|
2015-02-28 02:37:24 +08:00
|
|
|
/*
|
|
|
|
* Note: this function uses a static per-thread buffer. Do not call this
|
|
|
|
* function more than 10 times within one argument list!
|
|
|
|
*/
|
2017-01-04 01:03:44 +08:00
|
|
|
const char *pretty_size_mode(u64 size, unsigned mode)
|
2015-02-28 02:37:24 +08:00
|
|
|
{
|
|
|
|
static __thread int ps_index = 0;
|
|
|
|
static __thread char ps_array[10][32];
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
ret = ps_array[ps_index];
|
|
|
|
ps_index++;
|
|
|
|
ps_index %= 10;
|
|
|
|
(void)pretty_size_snprintf(size, ret, 32, mode);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-14 05:34:35 +08:00
|
|
|
static const char* unit_suffix_binary[] =
|
2014-04-29 00:04:48 +08:00
|
|
|
{ "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
|
2014-06-14 05:34:35 +08:00
|
|
|
static const char* unit_suffix_decimal[] =
|
2014-11-05 01:22:51 +08:00
|
|
|
{ "B", "kB", "MB", "GB", "TB", "PB", "EB"};
|
2014-04-29 00:04:48 +08:00
|
|
|
|
2017-01-04 01:03:44 +08:00
|
|
|
int pretty_size_snprintf(u64 size, char *str, size_t str_size, unsigned unit_mode)
|
2008-04-23 02:06:31 +08:00
|
|
|
{
|
2014-04-29 00:04:48 +08:00
|
|
|
int num_divs;
|
2008-04-23 02:06:31 +08:00
|
|
|
float fraction;
|
2017-01-04 01:03:44 +08:00
|
|
|
u64 base = 0;
|
2014-08-31 02:27:00 +08:00
|
|
|
int mult = 0;
|
2014-06-14 05:34:35 +08:00
|
|
|
const char** suffix = NULL;
|
2017-01-04 01:03:44 +08:00
|
|
|
u64 last_size;
|
|
|
|
int negative;
|
2013-07-10 04:24:43 +08:00
|
|
|
|
2014-04-29 00:04:48 +08:00
|
|
|
if (str_size == 0)
|
2013-10-07 15:21:47 +08:00
|
|
|
return 0;
|
2008-04-23 02:06:31 +08:00
|
|
|
|
2017-01-04 01:03:44 +08:00
|
|
|
negative = !!(unit_mode & UNITS_NEGATIVE);
|
|
|
|
unit_mode &= ~UNITS_NEGATIVE;
|
|
|
|
|
2014-08-31 02:27:00 +08:00
|
|
|
if ((unit_mode & ~UNITS_MODE_MASK) == UNITS_RAW) {
|
2017-01-04 01:03:44 +08:00
|
|
|
if (negative)
|
|
|
|
snprintf(str, str_size, "%lld", size);
|
|
|
|
else
|
|
|
|
snprintf(str, str_size, "%llu", size);
|
2014-04-29 00:04:48 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2008-04-23 02:06:31 +08:00
|
|
|
|
2014-08-31 02:27:00 +08:00
|
|
|
if ((unit_mode & ~UNITS_MODE_MASK) == UNITS_BINARY) {
|
2014-04-29 00:04:48 +08:00
|
|
|
base = 1024;
|
2014-08-31 02:27:00 +08:00
|
|
|
mult = 1024;
|
2014-04-29 00:04:48 +08:00
|
|
|
suffix = unit_suffix_binary;
|
2014-08-31 02:27:00 +08:00
|
|
|
} else if ((unit_mode & ~UNITS_MODE_MASK) == UNITS_DECIMAL) {
|
2014-04-29 00:04:48 +08:00
|
|
|
base = 1000;
|
2014-08-31 02:27:00 +08:00
|
|
|
mult = 1000;
|
2014-04-29 00:04:48 +08:00
|
|
|
suffix = unit_suffix_decimal;
|
2012-10-02 04:11:27 +08:00
|
|
|
}
|
2014-04-29 00:04:48 +08:00
|
|
|
|
|
|
|
/* Unknown mode */
|
|
|
|
if (!base) {
|
2019-08-12 06:46:49 +08:00
|
|
|
fprintf(stderr, "INTERNAL ERROR: unknown unit base, mode %u\n",
|
2014-04-29 00:04:48 +08:00
|
|
|
unit_mode);
|
|
|
|
assert(0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_divs = 0;
|
|
|
|
last_size = size;
|
2014-08-31 02:27:00 +08:00
|
|
|
switch (unit_mode & UNITS_MODE_MASK) {
|
2018-12-05 14:40:10 +08:00
|
|
|
case UNITS_TBYTES:
|
|
|
|
base *= mult;
|
|
|
|
num_divs++;
|
2019-10-22 10:02:27 +08:00
|
|
|
/* fallthrough */
|
2018-12-05 14:40:10 +08:00
|
|
|
case UNITS_GBYTES:
|
|
|
|
base *= mult;
|
|
|
|
num_divs++;
|
2019-10-22 10:02:27 +08:00
|
|
|
/* fallthrough */
|
2018-12-05 14:40:10 +08:00
|
|
|
case UNITS_MBYTES:
|
|
|
|
base *= mult;
|
|
|
|
num_divs++;
|
2019-10-22 10:02:27 +08:00
|
|
|
/* fallthrough */
|
2018-12-05 14:40:10 +08:00
|
|
|
case UNITS_KBYTES:
|
|
|
|
num_divs++;
|
|
|
|
break;
|
2014-08-31 02:27:00 +08:00
|
|
|
case UNITS_BYTES:
|
2018-12-05 14:40:10 +08:00
|
|
|
base = 1;
|
|
|
|
num_divs = 0;
|
|
|
|
break;
|
2014-08-31 02:27:00 +08:00
|
|
|
default:
|
2017-01-04 01:03:44 +08:00
|
|
|
if (negative) {
|
|
|
|
s64 ssize = (s64)size;
|
|
|
|
s64 last_ssize = ssize;
|
|
|
|
|
|
|
|
while ((ssize < 0 ? -ssize : ssize) >= mult) {
|
|
|
|
last_ssize = ssize;
|
|
|
|
ssize /= mult;
|
|
|
|
num_divs++;
|
|
|
|
}
|
|
|
|
last_size = (u64)last_ssize;
|
|
|
|
} else {
|
|
|
|
while (size >= mult) {
|
|
|
|
last_size = size;
|
|
|
|
size /= mult;
|
|
|
|
num_divs++;
|
|
|
|
}
|
2014-08-31 02:27:00 +08:00
|
|
|
}
|
2016-03-09 10:10:55 +08:00
|
|
|
/*
|
|
|
|
* If the value is smaller than base, we didn't do any
|
|
|
|
* division, in that case, base should be 1, not original
|
|
|
|
* base, or the unit will be wrong
|
|
|
|
*/
|
|
|
|
if (num_divs == 0)
|
|
|
|
base = 1;
|
2014-04-29 00:04:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (num_divs >= ARRAY_SIZE(unit_suffix_binary)) {
|
|
|
|
str[0] = '\0';
|
|
|
|
printf("INTERNAL ERROR: unsupported unit suffix, index %d\n",
|
|
|
|
num_divs);
|
|
|
|
assert(0);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-01-04 01:03:44 +08:00
|
|
|
|
|
|
|
if (negative) {
|
|
|
|
fraction = (float)(s64)last_size / base;
|
|
|
|
} else {
|
|
|
|
fraction = (float)last_size / base;
|
|
|
|
}
|
2014-04-29 00:04:48 +08:00
|
|
|
|
|
|
|
return snprintf(str, str_size, "%.2f%s", fraction, suffix[num_divs]);
|
2008-04-23 02:06:31 +08:00
|
|
|
}
|
|
|
|
|
2010-12-06 01:46:44 +08:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2013-01-30 16:32:43 +08:00
|
|
|
static int check_label(const char *input)
|
2010-12-06 01:46:44 +08:00
|
|
|
{
|
|
|
|
int len = strlen(input);
|
|
|
|
|
2013-01-30 16:32:43 +08:00
|
|
|
if (len > BTRFS_LABEL_SIZE - 1) {
|
2016-05-20 19:39:02 +08:00
|
|
|
error("label %s is too long (max %d)", input,
|
|
|
|
BTRFS_LABEL_SIZE - 1);
|
2010-12-06 01:46:44 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-06-16 03:55:25 +08:00
|
|
|
|
2013-01-30 16:32:47 +08:00
|
|
|
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) {
|
2016-05-20 19:39:02 +08:00
|
|
|
error("checking mount status of %s failed: %d", dev, ret);
|
2013-01-30 16:32:47 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ret > 0) {
|
2016-05-20 19:39:02 +08:00
|
|
|
error("device %s is mounted, use mount point", dev);
|
2013-01-30 16:32:47 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the super_block at the default location
|
|
|
|
* and as read-write.
|
|
|
|
*/
|
2013-10-29 02:28:43 +08:00
|
|
|
root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
|
2013-01-30 16:32:47 +08:00
|
|
|
if (!root) /* errors are printed by open_ctree() */
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
trans = btrfs_start_transaction(root, 1);
|
2017-08-28 22:48:16 +08:00
|
|
|
BUG_ON(IS_ERR(trans));
|
2016-03-25 02:19:31 +08:00
|
|
|
__strncpy_null(root->fs_info->super_copy->label, label, BTRFS_LABEL_SIZE - 1);
|
|
|
|
|
2013-01-30 16:32:47 +08:00
|
|
|
btrfs_commit_transaction(trans, root);
|
|
|
|
|
|
|
|
/* Now we close it since we are done. */
|
|
|
|
close_ctree(root);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-22 18:40:29 +08:00
|
|
|
static int set_label_mounted(const char *mount_path, const char *labelp)
|
2013-01-30 16:32:47 +08:00
|
|
|
{
|
|
|
|
int fd;
|
2016-03-22 18:40:29 +08:00
|
|
|
char label[BTRFS_LABEL_SIZE];
|
2013-01-30 16:32:47 +08:00
|
|
|
|
|
|
|
fd = open(mount_path, O_RDONLY | O_NOATIME);
|
|
|
|
if (fd < 0) {
|
2018-01-08 05:54:21 +08:00
|
|
|
error("unable to access %s: %m", mount_path);
|
2013-01-30 16:32:47 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-03-22 18:40:29 +08:00
|
|
|
memset(label, 0, sizeof(label));
|
2016-03-25 02:19:31 +08:00
|
|
|
__strncpy_null(label, labelp, BTRFS_LABEL_SIZE - 1);
|
2013-01-30 16:32:47 +08:00
|
|
|
if (ioctl(fd, BTRFS_IOC_SET_FSLABEL, label) < 0) {
|
2018-01-08 05:54:21 +08:00
|
|
|
error("unable to set label of %s: %m", mount_path);
|
2013-01-30 16:32:47 +08:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-03-12 07:12:58 +08:00
|
|
|
close(fd);
|
2013-01-30 16:32:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-15 16:46:22 +08:00
|
|
|
int get_label_unmounted(const char *dev, char *label)
|
2013-01-30 16:32:47 +08:00
|
|
|
{
|
|
|
|
struct btrfs_root *root;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = check_mounted(dev);
|
|
|
|
if (ret < 0) {
|
2016-05-20 19:39:02 +08:00
|
|
|
error("checking mount status of %s failed: %d", dev, ret);
|
2013-01-30 16:32:47 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the super_block at the default location
|
|
|
|
* and as read-only.
|
|
|
|
*/
|
|
|
|
root = open_ctree(dev, 0, 0);
|
|
|
|
if(!root)
|
|
|
|
return -1;
|
|
|
|
|
2016-03-25 02:19:31 +08:00
|
|
|
__strncpy_null(label, root->fs_info->super_copy->label,
|
|
|
|
BTRFS_LABEL_SIZE - 1);
|
2013-01-30 16:32:47 +08:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
2013-07-15 13:30:54 +08:00
|
|
|
int get_label_mounted(const char *mount_path, char *labelp)
|
2013-01-30 16:32:47 +08:00
|
|
|
{
|
|
|
|
char label[BTRFS_LABEL_SIZE];
|
|
|
|
int fd;
|
2015-09-15 16:46:22 +08:00
|
|
|
int ret;
|
2013-01-30 16:32:47 +08:00
|
|
|
|
|
|
|
fd = open(mount_path, O_RDONLY | O_NOATIME);
|
|
|
|
if (fd < 0) {
|
2018-01-08 05:54:21 +08:00
|
|
|
error("unable to access %s: %m", mount_path);
|
2013-01-30 16:32:47 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(label, '\0', sizeof(label));
|
2015-09-15 16:46:22 +08:00
|
|
|
ret = ioctl(fd, BTRFS_IOC_GET_FSLABEL, label);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (errno != ENOTTY)
|
2018-01-08 05:54:21 +08:00
|
|
|
error("unable to get label of %s: %m", mount_path);
|
2015-09-15 16:46:22 +08:00
|
|
|
ret = -errno;
|
2013-01-30 16:32:47 +08:00
|
|
|
close(fd);
|
2015-09-15 16:46:22 +08:00
|
|
|
return ret;
|
2013-01-30 16:32:47 +08:00
|
|
|
}
|
|
|
|
|
2016-03-25 02:19:31 +08:00
|
|
|
__strncpy_null(labelp, label, BTRFS_LABEL_SIZE - 1);
|
2013-03-12 07:12:58 +08:00
|
|
|
close(fd);
|
2013-01-30 16:32:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-12 21:41:42 +08:00
|
|
|
int get_label(const char *btrfs_dev, char *label)
|
2013-01-30 16:32:47 +08:00
|
|
|
{
|
2013-07-15 13:30:54 +08:00
|
|
|
int ret;
|
|
|
|
|
2019-07-02 06:09:21 +08:00
|
|
|
ret = path_is_reg_or_block_device(btrfs_dev);
|
2014-07-31 11:23:44 +08:00
|
|
|
if (!ret)
|
2013-07-15 13:30:54 +08:00
|
|
|
ret = get_label_mounted(btrfs_dev, label);
|
2014-07-31 11:23:44 +08:00
|
|
|
else if (ret > 0)
|
|
|
|
ret = get_label_unmounted(btrfs_dev, label);
|
2013-11-12 21:41:42 +08:00
|
|
|
|
2013-07-15 13:30:54 +08:00
|
|
|
return ret;
|
2013-01-30 16:32:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int set_label(const char *btrfs_dev, const char *label)
|
|
|
|
{
|
2014-07-31 11:23:44 +08:00
|
|
|
int ret;
|
|
|
|
|
2013-01-30 16:32:47 +08:00
|
|
|
if (check_label(label))
|
|
|
|
return -1;
|
|
|
|
|
2019-07-02 06:09:21 +08:00
|
|
|
ret = path_is_reg_or_block_device(btrfs_dev);
|
2014-07-31 11:23:44 +08:00
|
|
|
if (!ret)
|
|
|
|
ret = set_label_mounted(btrfs_dev, label);
|
|
|
|
else if (ret > 0)
|
|
|
|
ret = set_label_unmounted(btrfs_dev, label);
|
|
|
|
|
|
|
|
return ret;
|
2013-01-30 16:32:47 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:55:57 +08:00
|
|
|
/*
|
|
|
|
* A not-so-good version fls64. No fascinating optimization since
|
|
|
|
* no one except parse_size use it
|
|
|
|
*/
|
|
|
|
static int fls64(u64 x)
|
2012-10-30 01:53:17 +08:00
|
|
|
{
|
2012-10-30 01:53:18 +08:00
|
|
|
int i;
|
2014-06-13 10:55:57 +08:00
|
|
|
|
|
|
|
for (i = 0; i <64; i++)
|
2014-07-27 00:49:55 +08:00
|
|
|
if (x << i & (1ULL << 63))
|
2014-06-13 10:55:57 +08:00
|
|
|
return 64 - i;
|
|
|
|
return 64 - i;
|
|
|
|
}
|
|
|
|
|
2019-08-27 23:12:13 +08:00
|
|
|
u64 parse_size(const char *s)
|
2014-06-13 10:55:57 +08:00
|
|
|
{
|
2012-10-30 01:53:17 +08:00
|
|
|
char c;
|
2014-06-13 10:55:57 +08:00
|
|
|
char *endptr;
|
2012-10-30 01:53:17 +08:00
|
|
|
u64 mult = 1;
|
2014-06-13 10:55:57 +08:00
|
|
|
u64 ret;
|
|
|
|
|
|
|
|
if (!s) {
|
2016-05-20 19:39:02 +08:00
|
|
|
error("size value is empty");
|
2014-06-13 10:55:57 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (s[0] == '-') {
|
2016-05-20 19:39:02 +08:00
|
|
|
error("size value '%s' is less equal than 0", s);
|
2014-06-13 10:55:57 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ret = strtoull(s, &endptr, 10);
|
|
|
|
if (endptr == s) {
|
2016-05-20 19:39:02 +08:00
|
|
|
error("size value '%s' is invalid", s);
|
2014-06-13 10:55:57 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (endptr[0] && endptr[1]) {
|
2016-05-20 19:39:02 +08:00
|
|
|
error("illegal suffix contains character '%c' in wrong position",
|
2014-06-13 10:55:57 +08:00
|
|
|
endptr[1]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* strtoll returns LLONG_MAX when overflow, if this happens,
|
|
|
|
* need to call strtoull to get the real size
|
|
|
|
*/
|
|
|
|
if (errno == ERANGE && ret == ULLONG_MAX) {
|
2016-05-20 19:39:02 +08:00
|
|
|
error("size value '%s' is too large for u64", s);
|
2014-06-13 10:55:57 +08:00
|
|
|
exit(1);
|
2012-10-30 01:53:18 +08:00
|
|
|
}
|
2014-06-13 10:55:57 +08:00
|
|
|
if (endptr[0]) {
|
|
|
|
c = tolower(endptr[0]);
|
2012-10-30 01:53:17 +08:00
|
|
|
switch (c) {
|
2012-10-30 01:53:19 +08:00
|
|
|
case 'e':
|
|
|
|
mult *= 1024;
|
2013-11-07 07:15:54 +08:00
|
|
|
/* fallthrough */
|
2012-10-30 01:53:19 +08:00
|
|
|
case 'p':
|
|
|
|
mult *= 1024;
|
2013-11-07 07:15:54 +08:00
|
|
|
/* fallthrough */
|
2012-10-30 01:53:19 +08:00
|
|
|
case 't':
|
|
|
|
mult *= 1024;
|
2013-11-07 07:15:54 +08:00
|
|
|
/* fallthrough */
|
2012-10-30 01:53:17 +08:00
|
|
|
case 'g':
|
|
|
|
mult *= 1024;
|
2013-11-07 07:15:54 +08:00
|
|
|
/* fallthrough */
|
2012-10-30 01:53:17 +08:00
|
|
|
case 'm':
|
|
|
|
mult *= 1024;
|
2013-11-07 07:15:54 +08:00
|
|
|
/* fallthrough */
|
2012-10-30 01:53:17 +08:00
|
|
|
case 'k':
|
|
|
|
mult *= 1024;
|
2013-11-07 07:15:54 +08:00
|
|
|
/* fallthrough */
|
2012-10-30 01:53:17 +08:00
|
|
|
case 'b':
|
|
|
|
break;
|
|
|
|
default:
|
2016-05-20 19:39:02 +08:00
|
|
|
error("unknown size descriptor '%c'", c);
|
2012-10-30 01:53:17 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2014-06-13 10:55:57 +08:00
|
|
|
/* Check whether ret * mult overflow */
|
|
|
|
if (fls64(ret) + fls64(mult) - 1 > 64) {
|
2016-05-20 19:39:02 +08:00
|
|
|
error("size value '%s' is too large for u64", s);
|
2014-06-13 10:55:57 +08:00
|
|
|
exit(1);
|
2012-10-30 01:53:18 +08:00
|
|
|
}
|
2014-06-13 10:55:57 +08:00
|
|
|
ret *= mult;
|
|
|
|
return ret;
|
2012-10-30 01:53:17 +08:00
|
|
|
}
|
|
|
|
|
2015-02-27 16:26:35 +08:00
|
|
|
u64 parse_qgroupid(const char *p)
|
|
|
|
{
|
|
|
|
char *s = strchr(p, '/');
|
|
|
|
const char *ptr_src_end = p + strlen(p);
|
|
|
|
char *ptr_parse_end = NULL;
|
2018-01-21 16:24:50 +08:00
|
|
|
enum btrfs_util_error err;
|
2015-02-27 16:26:35 +08:00
|
|
|
u64 level;
|
|
|
|
u64 id;
|
2015-02-27 16:26:36 +08:00
|
|
|
int fd;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (p[0] == '/')
|
|
|
|
goto path;
|
2015-02-27 16:26:35 +08:00
|
|
|
|
2015-02-27 16:26:36 +08:00
|
|
|
/* Numeric format like '0/257' is the primary case */
|
2015-02-27 16:26:35 +08:00
|
|
|
if (!s) {
|
|
|
|
id = strtoull(p, &ptr_parse_end, 10);
|
|
|
|
if (ptr_parse_end != ptr_src_end)
|
2015-02-27 16:26:36 +08:00
|
|
|
goto path;
|
2015-02-27 16:26:35 +08:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
level = strtoull(p, &ptr_parse_end, 10);
|
|
|
|
if (ptr_parse_end != s)
|
2015-02-27 16:26:36 +08:00
|
|
|
goto path;
|
2015-02-27 16:26:35 +08:00
|
|
|
|
|
|
|
id = strtoull(s + 1, &ptr_parse_end, 10);
|
|
|
|
if (ptr_parse_end != ptr_src_end)
|
2015-02-27 16:26:36 +08:00
|
|
|
goto path;
|
2015-02-27 16:26:35 +08:00
|
|
|
|
|
|
|
return (level << BTRFS_QGROUP_LEVEL_SHIFT) | id;
|
2015-02-27 16:26:36 +08:00
|
|
|
|
|
|
|
path:
|
|
|
|
/* Path format like subv at 'my_subvol' is the fallback case */
|
2018-01-21 16:24:50 +08:00
|
|
|
err = btrfs_util_is_subvolume(p);
|
|
|
|
if (err)
|
2015-02-27 16:26:36 +08:00
|
|
|
goto err;
|
|
|
|
fd = open(p, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
goto err;
|
2016-10-31 17:34:37 +08:00
|
|
|
ret = lookup_path_rootid(fd, &id);
|
2018-10-25 20:10:54 +08:00
|
|
|
if (ret) {
|
|
|
|
errno = -ret;
|
|
|
|
error("failed to lookup root id: %m");
|
|
|
|
}
|
2015-02-27 16:26:36 +08:00
|
|
|
close(fd);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
return id;
|
|
|
|
|
2015-02-27 16:26:35 +08:00
|
|
|
err:
|
2016-05-20 19:39:02 +08:00
|
|
|
error("invalid qgroupid or subvolume path: %s", p);
|
2015-02-27 16:26:35 +08:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2019-11-06 02:34:07 +08:00
|
|
|
enum btrfs_csum_type parse_csum_type(const char *s)
|
|
|
|
{
|
|
|
|
if (strcasecmp(s, "crc32c") == 0) {
|
|
|
|
return BTRFS_CSUM_TYPE_CRC32;
|
|
|
|
} else if (strcasecmp(s, "xxhash64") == 0 ||
|
|
|
|
strcasecmp(s, "xxhash") == 0) {
|
|
|
|
return BTRFS_CSUM_TYPE_XXHASH;
|
|
|
|
} else if (strcasecmp(s, "sha256") == 0) {
|
|
|
|
return BTRFS_CSUM_TYPE_SHA256;
|
|
|
|
} else if (strcasecmp(s, "blake2b") == 0 ||
|
|
|
|
strcasecmp(s, "blake2") == 0) {
|
|
|
|
return BTRFS_CSUM_TYPE_BLAKE2;
|
|
|
|
} else {
|
|
|
|
error("unknown csum type %s", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* not reached */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-16 03:47:27 +08:00
|
|
|
int open_file_or_dir3(const char *fname, DIR **dirstream, int open_flags)
|
2013-01-28 13:22:30 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct stat st;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
ret = stat(fname, &st);
|
|
|
|
if (ret < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2013-07-15 19:36:50 +08:00
|
|
|
*dirstream = opendir(fname);
|
|
|
|
if (!*dirstream)
|
2013-12-12 15:46:18 +08:00
|
|
|
return -1;
|
2013-07-15 19:36:50 +08:00
|
|
|
fd = dirfd(*dirstream);
|
2013-12-12 15:46:18 +08:00
|
|
|
} else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
|
2014-04-16 03:47:27 +08:00
|
|
|
fd = open(fname, open_flags);
|
2013-12-12 15:46:18 +08:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* we set this on purpose, in case the caller output
|
|
|
|
* strerror(errno) as success
|
|
|
|
*/
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
2013-01-28 13:22:30 +08:00
|
|
|
}
|
|
|
|
if (fd < 0) {
|
2013-12-12 15:46:18 +08:00
|
|
|
fd = -1;
|
2015-06-09 21:24:51 +08:00
|
|
|
if (*dirstream) {
|
2013-07-15 19:36:50 +08:00
|
|
|
closedir(*dirstream);
|
2015-06-09 21:24:51 +08:00
|
|
|
*dirstream = NULL;
|
|
|
|
}
|
2013-01-28 13:22:30 +08:00
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
2012-05-15 18:05:44 +08:00
|
|
|
|
2014-04-16 03:47:27 +08:00
|
|
|
int open_file_or_dir(const char *fname, DIR **dirstream)
|
|
|
|
{
|
|
|
|
return open_file_or_dir3(fname, dirstream, O_RDWR);
|
|
|
|
}
|
|
|
|
|
2013-07-15 19:36:50 +08:00
|
|
|
void close_file_or_dir(int fd, DIR *dirstream)
|
|
|
|
{
|
2018-01-25 20:37:13 +08:00
|
|
|
int old_errno;
|
|
|
|
|
|
|
|
old_errno = errno;
|
|
|
|
if (dirstream) {
|
2013-07-15 19:36:50 +08:00
|
|
|
closedir(dirstream);
|
2018-01-25 20:37:13 +08:00
|
|
|
} else if (fd >= 0) {
|
2013-07-15 19:36:50 +08:00
|
|
|
close(fd);
|
2018-01-25 20:37:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
errno = old_errno;
|
2013-07-15 19:36:50 +08:00
|
|
|
}
|
|
|
|
|
2014-02-14 03:19:44 +08:00
|
|
|
int get_device_info(int fd, u64 devid,
|
2013-08-10 04:20:47 +08:00
|
|
|
struct btrfs_ioctl_dev_info_args *di_args)
|
2012-05-15 18:05:44 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
di_args->devid = devid;
|
|
|
|
memset(&di_args->uuid, '\0', sizeof(di_args->uuid));
|
|
|
|
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args);
|
2016-01-12 20:35:50 +08:00
|
|
|
return ret < 0 ? -errno : 0;
|
2012-05-15 18:05:44 +08:00
|
|
|
}
|
|
|
|
|
2019-06-20 03:41:58 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-13 09:19:29 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-28 04:36:52 +08:00
|
|
|
/* Get the latest max_id to stay consistent with the num_devices */
|
2014-11-13 09:19:29 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-12 12:17:40 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-09-29 21:12:35 +08:00
|
|
|
int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
|
2012-05-15 18:05:44 +08:00
|
|
|
struct btrfs_ioctl_dev_info_args **di_ret)
|
|
|
|
{
|
2013-03-12 12:17:40 +08:00
|
|
|
int fd = -1;
|
2012-05-15 18:05:44 +08:00
|
|
|
int ret = 0;
|
|
|
|
int ndevs = 0;
|
2016-11-07 22:38:24 +08:00
|
|
|
u64 last_devid = 0;
|
2015-01-06 17:30:44 +08:00
|
|
|
int replacing = 0;
|
2012-05-15 18:05:44 +08:00
|
|
|
struct btrfs_fs_devices *fs_devices_mnt = NULL;
|
|
|
|
struct btrfs_ioctl_dev_info_args *di_args;
|
2015-01-06 17:30:44 +08:00
|
|
|
struct btrfs_ioctl_dev_info_args tmp;
|
2015-06-12 19:18:44 +08:00
|
|
|
char mp[PATH_MAX];
|
2013-07-15 19:36:50 +08:00
|
|
|
DIR *dirstream = NULL;
|
2012-05-15 18:05:44 +08:00
|
|
|
|
|
|
|
memset(fi_args, 0, sizeof(*fi_args));
|
|
|
|
|
2019-07-02 03:29:43 +08:00
|
|
|
if (path_is_block_device(path) == 1) {
|
2014-02-24 19:43:38 +08:00
|
|
|
struct btrfs_super_block *disk_super;
|
|
|
|
char buf[BTRFS_SUPER_INFO_SIZE];
|
|
|
|
|
2013-03-12 12:17:40 +08:00
|
|
|
/* Ensure it's mounted, then set path to the mountpoint */
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
ret = -errno;
|
2018-01-08 05:54:21 +08:00
|
|
|
error("cannot open %s: %m", path);
|
2013-03-12 12:17:40 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2012-05-15 18:05:44 +08:00
|
|
|
ret = check_mounted_where(fd, path, mp, sizeof(mp),
|
2018-06-18 23:00:14 +08:00
|
|
|
&fs_devices_mnt, SBREAD_DEFAULT);
|
2013-03-12 12:17:40 +08:00
|
|
|
if (!ret) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-05-15 18:05:44 +08:00
|
|
|
if (ret < 0)
|
2013-03-12 12:17:40 +08:00
|
|
|
goto out;
|
|
|
|
path = mp;
|
|
|
|
/* Only fill in this one device */
|
2012-05-15 18:05:44 +08:00
|
|
|
fi_args->num_devices = 1;
|
2014-02-24 19:43:38 +08:00
|
|
|
|
|
|
|
disk_super = (struct btrfs_super_block *)buf;
|
2014-07-03 17:36:36 +08:00
|
|
|
ret = btrfs_read_dev_super(fd, disk_super,
|
|
|
|
BTRFS_SUPER_INFO_OFFSET, 0);
|
2014-02-24 19:43:38 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
ret = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
2016-11-07 22:38:24 +08:00
|
|
|
last_devid = btrfs_stack_device_id(&disk_super->dev_item);
|
|
|
|
fi_args->max_id = last_devid;
|
2014-02-24 19:43:38 +08:00
|
|
|
|
2012-05-15 18:05:44 +08:00
|
|
|
memcpy(fi_args->fsid, fs_devices_mnt->fsid, BTRFS_FSID_SIZE);
|
|
|
|
close(fd);
|
2013-03-12 12:17:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* at this point path must not be for a block device */
|
2013-07-15 19:36:50 +08:00
|
|
|
fd = open_file_or_dir(path, &dirstream);
|
2013-03-12 12:17:40 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-11-13 09:19:29 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The fs_args->num_devices does not include seed devices
|
|
|
|
*/
|
|
|
|
ret = search_chunk_tree_for_fs_info(fd, fi_args);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
2015-01-06 17:30:44 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2016-11-07 22:38:24 +08:00
|
|
|
if (last_devid == 0)
|
|
|
|
last_devid++;
|
2015-01-06 17:30:44 +08:00
|
|
|
}
|
2012-05-15 18:05:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!fi_args->num_devices)
|
2013-03-12 12:17:40 +08:00
|
|
|
goto out;
|
2012-05-15 18:05:44 +08:00
|
|
|
|
2014-10-30 12:43:03 +08:00
|
|
|
di_args = *di_ret = malloc((fi_args->num_devices) * sizeof(*di_args));
|
2013-03-12 12:17:40 +08:00
|
|
|
if (!di_args) {
|
|
|
|
ret = -errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-05-15 18:05:44 +08:00
|
|
|
|
2015-01-06 17:30:44 +08:00
|
|
|
if (replacing)
|
|
|
|
memcpy(di_args, &tmp, sizeof(tmp));
|
2019-06-03 09:27:54 +08:00
|
|
|
for (; last_devid <= fi_args->max_id && ndevs < fi_args->num_devices;
|
|
|
|
last_devid++) {
|
2016-11-07 22:38:24 +08:00
|
|
|
ret = get_device_info(fd, last_devid, &di_args[ndevs]);
|
2012-05-15 18:05:44 +08:00
|
|
|
if (ret == -ENODEV)
|
|
|
|
continue;
|
|
|
|
if (ret)
|
2013-03-12 12:17:40 +08:00
|
|
|
goto out;
|
2012-05-15 18:05:44 +08:00
|
|
|
ndevs++;
|
|
|
|
}
|
|
|
|
|
2014-02-24 19:43:39 +08:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2013-03-12 12:17:40 +08:00
|
|
|
out:
|
2013-07-15 19:36:50 +08:00
|
|
|
close_file_or_dir(fd, dirstream);
|
2013-03-12 12:17:40 +08:00
|
|
|
return ret;
|
2012-05-15 18:05:44 +08:00
|
|
|
}
|
2013-02-14 15:53:04 +08:00
|
|
|
|
2017-09-27 10:01:43 +08:00
|
|
|
int get_fsid(const char *path, u8 *fsid, int silent)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
struct btrfs_ioctl_fs_info_args args;
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
ret = -errno;
|
|
|
|
if (!silent)
|
2018-10-25 20:10:54 +08:00
|
|
|
error("failed to open %s: %m", path);
|
2017-09-27 10:01:43 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_FS_INFO, &args);
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = -errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(fsid, args.fsid, BTRFS_FSID_SIZE);
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-16 16:20:56 +08:00
|
|
|
static int group_profile_devs_min(u64 flag)
|
|
|
|
{
|
|
|
|
switch (flag & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
|
|
|
|
case 0: /* single */
|
|
|
|
case BTRFS_BLOCK_GROUP_DUP:
|
|
|
|
return 1;
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID0:
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID1:
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID5:
|
|
|
|
return 2;
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID6:
|
2019-11-01 02:43:17 +08:00
|
|
|
case BTRFS_BLOCK_GROUP_RAID1C3:
|
2014-05-16 16:20:56 +08:00
|
|
|
return 3;
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID10:
|
2019-11-01 02:43:17 +08:00
|
|
|
case BTRFS_BLOCK_GROUP_RAID1C4:
|
2014-05-16 16:20:56 +08:00
|
|
|
return 4;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 20:11:25 +08:00
|
|
|
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 17:36:24 +08:00
|
|
|
u64 dev_cnt, int mixed, int ssd)
|
2013-08-07 20:11:25 +08:00
|
|
|
{
|
|
|
|
u64 allowed = 0;
|
2016-09-06 23:35:06 +08:00
|
|
|
u64 profile = metadata_profile | data_profile;
|
2013-08-07 20:11:25 +08:00
|
|
|
|
|
|
|
switch (dev_cnt) {
|
|
|
|
default:
|
|
|
|
case 4:
|
|
|
|
allowed |= BTRFS_BLOCK_GROUP_RAID10;
|
2019-11-01 02:43:17 +08:00
|
|
|
allowed |= BTRFS_BLOCK_GROUP_RAID10 | BTRFS_BLOCK_GROUP_RAID1C4;
|
2019-10-22 10:02:27 +08:00
|
|
|
/* fallthrough */
|
2013-08-07 20:11:25 +08:00
|
|
|
case 3:
|
2019-11-01 02:43:17 +08:00
|
|
|
allowed |= BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID1C3;
|
2019-10-22 10:02:27 +08:00
|
|
|
/* fallthrough */
|
2013-08-07 20:11:25 +08:00
|
|
|
case 2:
|
|
|
|
allowed |= BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
|
|
|
|
BTRFS_BLOCK_GROUP_RAID5;
|
2019-10-22 10:02:27 +08:00
|
|
|
/* fallthrough */
|
2013-08-07 20:11:25 +08:00
|
|
|
case 1:
|
|
|
|
allowed |= BTRFS_BLOCK_GROUP_DUP;
|
|
|
|
}
|
|
|
|
|
2016-09-06 23:35:06 +08:00
|
|
|
if (dev_cnt > 1 && profile & BTRFS_BLOCK_GROUP_DUP) {
|
2016-03-17 01:08:33 +08:00
|
|
|
warning("DUP is not recommended on filesystem with multiple devices");
|
2014-05-16 16:20:56 +08:00
|
|
|
}
|
2013-08-07 20:11:25 +08:00
|
|
|
if (metadata_profile & ~allowed) {
|
2015-06-11 06:51:15 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: unable to create FS with metadata profile %s "
|
2014-05-16 16:20:56 +08:00
|
|
|
"(have %llu devices but %d devices are required)\n",
|
|
|
|
btrfs_group_profile_str(metadata_profile), dev_cnt,
|
|
|
|
group_profile_devs_min(metadata_profile));
|
2013-08-07 20:11:25 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (data_profile & ~allowed) {
|
2015-06-11 06:51:15 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: unable to create FS with data profile %s "
|
2014-05-16 16:20:56 +08:00
|
|
|
"(have %llu devices but %d devices are required)\n",
|
|
|
|
btrfs_group_profile_str(data_profile), dev_cnt,
|
|
|
|
group_profile_devs_min(data_profile));
|
2013-08-07 20:11:25 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-06 23:35:06 +08:00
|
|
|
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 17:36:24 +08:00
|
|
|
warning_on(!mixed && (data_profile & BTRFS_BLOCK_GROUP_DUP) && ssd,
|
2015-11-27 00:56:45 +08:00
|
|
|
"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 17:36:24 +08:00
|
|
|
|
2013-08-07 20:11:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-30 22:54:48 +08:00
|
|
|
int group_profile_max_safe_loss(u64 flags)
|
|
|
|
{
|
|
|
|
switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
|
|
|
|
case 0: /* single */
|
|
|
|
case BTRFS_BLOCK_GROUP_DUP:
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID0:
|
|
|
|
return 0;
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID1:
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID5:
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID10:
|
|
|
|
return 1;
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID6:
|
2019-11-01 02:43:17 +08:00
|
|
|
case BTRFS_BLOCK_GROUP_RAID1C3:
|
2015-05-30 22:54:48 +08:00
|
|
|
return 2;
|
2019-11-01 02:43:17 +08:00
|
|
|
case BTRFS_BLOCK_GROUP_RAID1C4:
|
|
|
|
return 3;
|
2015-05-30 22:54:48 +08:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-18 16:27:35 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-03-22 23:56:33 +08:00
|
|
|
int ask_user(const char *question)
|
2013-09-18 16:27:35 +08:00
|
|
|
{
|
|
|
|
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"));
|
|
|
|
}
|
2013-09-19 00:19:30 +08:00
|
|
|
|
2014-07-23 13:47:35 +08:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2014-02-10 15:28:28 +08:00
|
|
|
int find_mount_root(const char *path, char **mount_root)
|
|
|
|
{
|
|
|
|
FILE *mnttab;
|
|
|
|
int fd;
|
|
|
|
struct mntent *ent;
|
|
|
|
int len;
|
2019-01-14 21:31:34 +08:00
|
|
|
int ret = 0;
|
2014-07-23 13:47:35 +08:00
|
|
|
int not_btrfs = 1;
|
2014-02-10 15:28:28 +08:00
|
|
|
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))) {
|
|
|
|
len = strlen(ent->mnt_dir);
|
2019-02-26 01:08:29 +08:00
|
|
|
if (strncmp(ent->mnt_dir, path, len) == 0) {
|
2014-09-04 10:27:48 +08:00
|
|
|
/* match found and use the latest match */
|
|
|
|
if (longest_matchlen <= len) {
|
2014-02-10 15:28:28 +08:00
|
|
|
free(longest_match);
|
|
|
|
longest_matchlen = len;
|
|
|
|
longest_match = strdup(ent->mnt_dir);
|
2019-01-14 21:31:34 +08:00
|
|
|
if (!longest_match) {
|
|
|
|
ret = -errno;
|
|
|
|
break;
|
|
|
|
}
|
2014-07-23 13:47:35 +08:00
|
|
|
not_btrfs = strcmp(ent->mnt_type, "btrfs");
|
2014-02-10 15:28:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endmntent(mnttab);
|
|
|
|
|
2019-01-14 21:31:34 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-07-23 13:47:34 +08:00
|
|
|
if (!longest_match)
|
2014-02-10 15:28:28 +08:00
|
|
|
return -ENOENT;
|
2014-07-23 13:47:35 +08:00
|
|
|
if (not_btrfs) {
|
|
|
|
free(longest_match);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-02-10 15:28:28 +08:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
*mount_root = realpath(longest_match, NULL);
|
|
|
|
if (!*mount_root)
|
|
|
|
ret = -errno;
|
|
|
|
|
|
|
|
free(longest_match);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-07-04 15:29:17 +08:00
|
|
|
|
2014-08-31 02:27:00 +08:00
|
|
|
void units_set_mode(unsigned *units, unsigned mode)
|
|
|
|
{
|
|
|
|
unsigned base = *units & UNITS_MODE_MASK;
|
|
|
|
|
|
|
|
*units = base | mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void units_set_base(unsigned *units, unsigned base)
|
|
|
|
{
|
|
|
|
unsigned mode = *units & ~UNITS_MODE_MASK;
|
|
|
|
|
|
|
|
*units = base | mode;
|
|
|
|
}
|
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-18 01:20:08 +08: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;
|
|
|
|
}
|
2014-10-24 01:05:31 +08:00
|
|
|
|
2016-03-23 00:01:20 +08:00
|
|
|
const char* btrfs_group_type_str(u64 flag)
|
2014-10-24 01:05:31 +08:00
|
|
|
{
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 00:01:20 +08:00
|
|
|
const char* btrfs_group_profile_str(u64 flag)
|
2014-10-24 01:05:31 +08:00
|
|
|
{
|
|
|
|
switch (flag & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
|
|
|
|
case 0:
|
|
|
|
return "single";
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID0:
|
|
|
|
return "RAID0";
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID1:
|
|
|
|
return "RAID1";
|
2019-11-01 02:43:17 +08:00
|
|
|
case BTRFS_BLOCK_GROUP_RAID1C3:
|
|
|
|
return "RAID1C3";
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID1C4:
|
|
|
|
return "RAID1C4";
|
2014-10-24 01:05:31 +08:00
|
|
|
case BTRFS_BLOCK_GROUP_RAID5:
|
|
|
|
return "RAID5";
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID6:
|
|
|
|
return "RAID6";
|
|
|
|
case BTRFS_BLOCK_GROUP_DUP:
|
|
|
|
return "DUP";
|
|
|
|
case BTRFS_BLOCK_GROUP_RAID10:
|
|
|
|
return "RAID10";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
2016-11-07 20:02:00 +08:00
|
|
|
/*
|
|
|
|
* Check if the BTRFS_IOC_TREE_SEARCH_V2 ioctl is supported on a given
|
|
|
|
* filesystem, opened at fd
|
|
|
|
*/
|
2014-11-26 01:46:16 +08:00
|
|
|
int btrfs_tree_search2_ioctl_supported(int fd)
|
|
|
|
{
|
|
|
|
struct btrfs_ioctl_search_args_v2 *args2;
|
|
|
|
struct btrfs_ioctl_search_key *sk;
|
|
|
|
int args2_size = 1024;
|
|
|
|
char args2_buf[args2_size];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
args2 = (struct btrfs_ioctl_search_args_v2 *)args2_buf;
|
|
|
|
sk = &(args2->key);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for the extent tree item in the root tree.
|
|
|
|
*/
|
|
|
|
sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
|
|
|
|
sk->min_objectid = BTRFS_EXTENT_TREE_OBJECTID;
|
|
|
|
sk->max_objectid = BTRFS_EXTENT_TREE_OBJECTID;
|
|
|
|
sk->min_type = BTRFS_ROOT_ITEM_KEY;
|
|
|
|
sk->max_type = BTRFS_ROOT_ITEM_KEY;
|
|
|
|
sk->min_offset = 0;
|
|
|
|
sk->max_offset = (u64)-1;
|
|
|
|
sk->min_transid = 0;
|
|
|
|
sk->max_transid = (u64)-1;
|
|
|
|
sk->nr_items = 1;
|
|
|
|
args2->buf_size = args2_size - sizeof(struct btrfs_ioctl_search_args_v2);
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH_V2, args2);
|
|
|
|
if (ret == -EOPNOTSUPP)
|
2016-11-07 20:02:00 +08:00
|
|
|
return 0;
|
2014-11-26 01:46:16 +08:00
|
|
|
else if (ret == 0)
|
2016-11-07 20:02:00 +08:00
|
|
|
return 1;
|
|
|
|
return ret;
|
2014-11-26 01:46:16 +08:00
|
|
|
}
|
2015-03-20 09:10:15 +08:00
|
|
|
|
2015-08-31 19:03:41 +08:00
|
|
|
unsigned int get_unit_mode_from_arg(int *argc, char *argv[], int df_mode)
|
|
|
|
{
|
|
|
|
unsigned int unit_mode = UNITS_DEFAULT;
|
|
|
|
int arg_i;
|
|
|
|
int arg_end;
|
|
|
|
|
|
|
|
for (arg_i = 0; arg_i < *argc; arg_i++) {
|
2016-03-14 08:02:34 +08:00
|
|
|
if (!strcmp(argv[arg_i], "--"))
|
|
|
|
break;
|
|
|
|
|
2015-08-31 19:03:41 +08:00
|
|
|
if (!strcmp(argv[arg_i], "--raw")) {
|
|
|
|
unit_mode = UNITS_RAW;
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[arg_i], "--human-readable")) {
|
|
|
|
unit_mode = UNITS_HUMAN_BINARY;
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(argv[arg_i], "--iec")) {
|
|
|
|
units_set_mode(&unit_mode, UNITS_BINARY);
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[arg_i], "--si")) {
|
|
|
|
units_set_mode(&unit_mode, UNITS_DECIMAL);
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(argv[arg_i], "--kbytes")) {
|
|
|
|
units_set_base(&unit_mode, UNITS_KBYTES);
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[arg_i], "--mbytes")) {
|
|
|
|
units_set_base(&unit_mode, UNITS_MBYTES);
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[arg_i], "--gbytes")) {
|
|
|
|
units_set_base(&unit_mode, UNITS_GBYTES);
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[arg_i], "--tbytes")) {
|
|
|
|
units_set_base(&unit_mode, UNITS_TBYTES);
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!df_mode)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!strcmp(argv[arg_i], "-b")) {
|
|
|
|
unit_mode = UNITS_RAW;
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[arg_i], "-h")) {
|
|
|
|
unit_mode = UNITS_HUMAN_BINARY;
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[arg_i], "-H")) {
|
|
|
|
unit_mode = UNITS_HUMAN_DECIMAL;
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[arg_i], "-k")) {
|
|
|
|
units_set_base(&unit_mode, UNITS_KBYTES);
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[arg_i], "-m")) {
|
|
|
|
units_set_base(&unit_mode, UNITS_MBYTES);
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[arg_i], "-g")) {
|
|
|
|
units_set_base(&unit_mode, UNITS_GBYTES);
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[arg_i], "-t")) {
|
|
|
|
units_set_base(&unit_mode, UNITS_TBYTES);
|
|
|
|
argv[arg_i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (arg_i = 0, arg_end = 0; arg_i < *argc; arg_i++) {
|
|
|
|
if (!argv[arg_i])
|
|
|
|
continue;
|
|
|
|
argv[arg_end] = argv[arg_i];
|
|
|
|
arg_end++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*argc = arg_end;
|
|
|
|
|
|
|
|
return unit_mode;
|
|
|
|
}
|
2015-10-10 22:30:56 +08:00
|
|
|
|
2017-02-01 23:09:47 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-10 22:30:56 +08:00
|
|
|
int string_is_numerical(const char *str)
|
|
|
|
{
|
2016-10-24 10:43:34 +08:00
|
|
|
if (!str)
|
|
|
|
return 0;
|
2015-10-10 22:30:56 +08:00
|
|
|
if (!(*str >= '0' && *str <= '9'))
|
|
|
|
return 0;
|
|
|
|
while (*str >= '0' && *str <= '9')
|
|
|
|
str++;
|
|
|
|
if (*str != '\0')
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2016-01-14 17:30:35 +08:00
|
|
|
|
2017-02-09 22:20:16 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:56:33 +08:00
|
|
|
const char *subvol_strip_mountpoint(const char *mnt, const char *full_path)
|
2016-03-21 15:21:03 +08:00
|
|
|
{
|
|
|
|
int len = strlen(mnt);
|
|
|
|
if (!len)
|
|
|
|
return full_path;
|
|
|
|
|
2018-05-15 23:22:14 +08:00
|
|
|
if ((strncmp(mnt, full_path, len) != 0) || ((len > 1) && (full_path[len] != '/'))) {
|
2018-02-17 23:05:34 +08:00
|
|
|
error("not on mount point: %s", mnt);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-03-21 15:21:03 +08:00
|
|
|
if (mnt[len - 1] != '/')
|
|
|
|
len += 1;
|
|
|
|
|
|
|
|
return full_path + len;
|
|
|
|
}
|
2016-03-21 15:21:04 +08:00
|
|
|
|
2017-02-01 23:09:47 +08:00
|
|
|
/* Set the seed manually */
|
2016-05-26 10:56:50 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-11-27 01:14:48 +08:00
|
|
|
rand_seed_initialized = 1;
|
2016-05-26 10:56:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __init_seed(void)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
|
2018-11-27 01:14:48 +08:00
|
|
|
if(rand_seed_initialized)
|
2016-05-26 10:56:50 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-11-27 01:14:48 +08:00
|
|
|
rand_seed_initialized = 1;
|
2016-05-26 10:56:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 rand_u32(void)
|
|
|
|
{
|
|
|
|
__init_seed();
|
|
|
|
/*
|
2018-11-27 01:01:42 +08:00
|
|
|
* Don't use nrand48, its range is [0,2^31) The highest bit will always
|
2016-05-26 10:56:50 +08:00
|
|
|
* be 0. Use jrand48 to include the highest bit.
|
|
|
|
*/
|
|
|
|
return (u32)jrand48(rand_seed);
|
|
|
|
}
|
|
|
|
|
2017-02-01 23:09:47 +08:00
|
|
|
/* Return random number in range [0, upper) */
|
2016-05-26 10:56:50 +08:00
|
|
|
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);
|
|
|
|
}
|
2016-11-21 20:52:01 +08:00
|
|
|
|
2017-02-01 23:09:47 +08:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-11-21 20:52:01 +08:00
|
|
|
void btrfs_config_init(void)
|
|
|
|
{
|
2018-03-06 10:29:30 +08:00
|
|
|
bconf.output_format = CMD_FORMAT_TEXT;
|
2016-11-21 20:52:01 +08:00
|
|
|
}
|
2017-07-26 04:51:34 +08:00
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
}
|
2017-12-05 16:39:44 +08:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2020-04-04 18:32:08 +08:00
|
|
|
|
|
|
|
static int bit_count(u64 x)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
while (x) {
|
|
|
|
if (x & 1)
|
|
|
|
ret++;
|
|
|
|
x >>= 1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-04 21:02:29 +08:00
|
|
|
static char *sprint_profiles(u64 profiles)
|
2020-04-04 18:32:08 +08:00
|
|
|
{
|
|
|
|
int i;
|
2020-05-04 21:02:29 +08:00
|
|
|
int maxlen = 1;
|
|
|
|
char *ptr;
|
2020-04-04 18:32:08 +08:00
|
|
|
|
2020-05-04 21:02:29 +08:00
|
|
|
if (bit_count(profiles) <= 1)
|
|
|
|
return NULL;
|
2020-04-04 18:32:08 +08:00
|
|
|
|
|
|
|
for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
|
2020-05-04 21:02:29 +08:00
|
|
|
maxlen += strlen(btrfs_raid_array[i].raid_name) + 2;
|
2020-04-04 18:32:08 +08:00
|
|
|
|
2020-05-04 21:02:29 +08:00
|
|
|
ptr = calloc(1, maxlen);
|
|
|
|
if (!ptr)
|
|
|
|
return NULL;
|
2020-04-04 18:32:08 +08:00
|
|
|
|
2020-05-04 21:52:25 +08:00
|
|
|
if (profiles & BTRFS_AVAIL_ALLOC_BIT_SINGLE)
|
|
|
|
strcat(ptr, btrfs_raid_array[BTRFS_RAID_SINGLE].raid_name);
|
|
|
|
|
2020-04-04 18:32:08 +08:00
|
|
|
for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
|
|
|
|
if (!(btrfs_raid_array[i].bg_flag & profiles))
|
|
|
|
continue;
|
|
|
|
|
2020-05-04 21:05:31 +08:00
|
|
|
if (ptr[0])
|
2020-05-04 21:02:29 +08:00
|
|
|
strcat(ptr, ", ");
|
|
|
|
strcat(ptr, btrfs_raid_array[i].raid_name);
|
2020-04-04 18:32:08 +08:00
|
|
|
}
|
2020-05-04 21:02:29 +08:00
|
|
|
|
|
|
|
return ptr;
|
2020-04-04 18:32:08 +08:00
|
|
|
}
|
|
|
|
|
2020-05-04 20:48:16 +08:00
|
|
|
static int btrfs_get_string_for_multiple_profiles(int fd, char **data_ret,
|
2020-05-05 04:03:09 +08:00
|
|
|
char **metadata_ret, char **mixed_ret, char **system_ret,
|
|
|
|
char **types_ret)
|
2020-04-04 18:32:08 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2020-05-04 21:02:29 +08:00
|
|
|
*data_ret = sprint_profiles(data_profiles);
|
|
|
|
*metadata_ret = sprint_profiles(metadata_profiles);
|
|
|
|
*mixed_ret = sprint_profiles(mixed_profiles);
|
|
|
|
*system_ret = sprint_profiles(system_profiles);
|
2020-04-04 18:32:08 +08:00
|
|
|
|
2020-05-05 04:03:09 +08:00
|
|
|
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:
|
2020-05-05 02:36:30 +08:00
|
|
|
return *data_ret || *metadata_ret || *mixed_ret || *system_ret;
|
2020-04-04 18:32:08 +08:00
|
|
|
}
|
|
|
|
|
2020-05-05 04:03:09 +08:00
|
|
|
/*
|
|
|
|
* 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)
|
2020-05-04 20:48:16 +08:00
|
|
|
{
|
2020-05-05 04:03:09 +08:00
|
|
|
char *data, *metadata, *system, *mixed, *types;
|
2020-05-04 21:02:29 +08:00
|
|
|
|
2020-05-05 04:03:09 +08:00
|
|
|
btrfs_get_string_for_multiple_profiles(fd, &data, &metadata, &mixed,
|
|
|
|
&system, &types);
|
2020-05-04 21:02:29 +08:00
|
|
|
free(data);
|
|
|
|
free(metadata);
|
|
|
|
free(mixed);
|
|
|
|
free(system);
|
|
|
|
|
2020-05-05 04:03:09 +08:00
|
|
|
return types;
|
2020-05-04 20:48:16 +08:00
|
|
|
}
|
|
|
|
|
2020-05-04 20:34:59 +08:00
|
|
|
int btrfs_warn_multiple_profiles(int fd)
|
2020-04-04 18:32:08 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *data_prof, *mixed_prof, *metadata_prof, *system_prof;
|
|
|
|
|
2020-05-04 20:34:59 +08:00
|
|
|
ret = btrfs_get_string_for_multiple_profiles(fd, &data_prof,
|
2020-05-05 04:03:09 +08:00
|
|
|
&metadata_prof, &mixed_prof, &system_prof, NULL);
|
2020-04-04 18:32:08 +08:00
|
|
|
|
|
|
|
if (ret != 1)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
fprintf(stderr,
|
2020-05-04 22:13:05 +08:00
|
|
|
"WARNING: Multiple block group profiles detected, see 'man btrfs(5)'.\n");
|
2020-04-04 18:32:08 +08:00
|
|
|
if (data_prof)
|
2020-05-04 22:13:05 +08:00
|
|
|
fprintf(stderr, "WARNING: Data: %s\n", data_prof);
|
|
|
|
|
2020-04-04 18:32:08 +08:00
|
|
|
if (metadata_prof)
|
2020-05-04 22:13:05 +08:00
|
|
|
fprintf(stderr, "WARNING: Metadata: %s\n", metadata_prof);
|
|
|
|
|
2020-04-04 18:32:08 +08:00
|
|
|
if (mixed_prof)
|
2020-05-04 22:13:05 +08:00
|
|
|
fprintf(stderr, "WARNING: Data+Metadata: %s\n", mixed_prof);
|
|
|
|
|
2020-04-04 18:32:08 +08:00
|
|
|
if (system_prof)
|
2020-05-04 22:13:05 +08:00
|
|
|
fprintf(stderr, "WARNING: System: %s\n", system_prof);
|
|
|
|
|
|
|
|
free(data_prof);
|
|
|
|
free(metadata_prof);
|
|
|
|
free(mixed_prof);
|
|
|
|
free(system_prof);
|
2020-04-04 18:32:08 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|