2012-02-03 19:00:17 +00: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.
|
|
|
|
*/
|
|
|
|
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "kerncompat.h"
|
|
|
|
#include <sys/ioctl.h>
|
2022-09-15 11:59:39 +00:00
|
|
|
#include <sys/stat.h>
|
2022-09-14 15:06:52 +00:00
|
|
|
#include <linux/version.h>
|
2022-11-07 20:10:42 +00:00
|
|
|
#include <linux/fs.h>
|
2012-02-03 19:00:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2013-09-23 19:18:17 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ftw.h>
|
2013-10-08 03:41:38 +00:00
|
|
|
#include <mntent.h>
|
|
|
|
#include <getopt.h>
|
2021-02-20 12:41:17 +00:00
|
|
|
#include <limits.h>
|
2022-09-15 11:59:39 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdbool.h>
|
2022-09-14 15:06:52 +00:00
|
|
|
#include <uuid/uuid.h>
|
2022-09-14 19:41:58 +00:00
|
|
|
#include "libbtrfsutil/btrfsutil.h"
|
2022-09-15 11:59:39 +00:00
|
|
|
#include "kernel-lib/list.h"
|
|
|
|
#include "kernel-lib/sizes.h"
|
2023-04-19 21:17:12 +00:00
|
|
|
#include "kernel-lib/list_sort.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/ctree.h"
|
2022-11-23 22:37:29 +00:00
|
|
|
#include "kernel-shared/compression.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/volumes.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/disk-io.h"
|
2022-09-15 11:59:39 +00:00
|
|
|
#include "common/defs.h"
|
|
|
|
#include "common/internal.h"
|
|
|
|
#include "common/messages.h"
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "common/utils.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/help.h"
|
2021-04-29 21:43:24 +00:00
|
|
|
#include "common/units.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/fsfeatures.h"
|
2019-07-01 18:54:39 +00:00
|
|
|
#include "common/path-utils.h"
|
2019-07-01 22:42:23 +00:00
|
|
|
#include "common/device-scan.h"
|
2021-05-06 22:34:26 +00:00
|
|
|
#include "common/device-utils.h"
|
2021-04-29 17:52:15 +00:00
|
|
|
#include "common/open-utils.h"
|
2021-09-03 20:29:06 +00:00
|
|
|
#include "common/parse-utils.h"
|
2022-09-15 21:32:00 +00:00
|
|
|
#include "common/filesystem-utils.h"
|
btrfs-progs: fi df: implement json output
Print JSON output with example output:
$ btrfs --format json filesystem df /path
{
"__header": {
"version": "1"
},
"filesystem-df": [
{
"bg-type": "Data",
"bg-profile": "single",
"total": "841813590016",
"used": "494085484544"
},
{
"bg-type": "System",
"bg-profile": "single",
"total": "33554432",
"used": "114688"
},
{
"bg-type": "Metadata",
"bg-profile": "single",
"total": "24696061952",
"used": "5743165440"
},
{
"bg-type": "GlobalReserve",
"bg-profile": "single",
"total": "536870912",
"used": "0"
}
]
}
Optionally there's "zone_unusable" key. The rowspec cannot print the
same output as now so the original implementation is preserved.
Signed-off-by: David Sterba <dsterba@suse.com>
2022-12-06 16:12:10 +00:00
|
|
|
#include "common/format-output.h"
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "cmds/commands.h"
|
|
|
|
#include "cmds/filesystem-usage.h"
|
2013-11-18 19:18:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* for btrfs fi show, we maintain a hash of fsids we've already printed.
|
|
|
|
* This way we don't print dups if a given FS is mounted more than once.
|
|
|
|
*/
|
|
|
|
static struct seen_fsid *seen_fsid_hash[SEEN_FSID_HASH_SIZE] = {NULL,};
|
2019-02-25 18:16:44 +00:00
|
|
|
static mode_t defrag_open_mode = O_RDONLY;
|
2013-11-18 19:18:08 +00:00
|
|
|
|
2012-02-08 15:45:54 +00:00
|
|
|
static const char * const filesystem_cmd_group_usage[] = {
|
|
|
|
"btrfs filesystem [<group>] <command> [<args>]",
|
|
|
|
NULL
|
|
|
|
};
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2014-04-23 16:47:52 +00:00
|
|
|
static const char * const cmd_filesystem_df_usage[] = {
|
2015-09-01 07:12:06 +00:00
|
|
|
"btrfs filesystem df [options] <path>",
|
|
|
|
"Show space usage information for a mount point",
|
2019-06-21 13:36:04 +00:00
|
|
|
"",
|
2015-11-06 15:39:49 +00:00
|
|
|
HELPINFO_UNITS_SHORT_LONG,
|
btrfs-progs: fi df: implement json output
Print JSON output with example output:
$ btrfs --format json filesystem df /path
{
"__header": {
"version": "1"
},
"filesystem-df": [
{
"bg-type": "Data",
"bg-profile": "single",
"total": "841813590016",
"used": "494085484544"
},
{
"bg-type": "System",
"bg-profile": "single",
"total": "33554432",
"used": "114688"
},
{
"bg-type": "Metadata",
"bg-profile": "single",
"total": "24696061952",
"used": "5743165440"
},
{
"bg-type": "GlobalReserve",
"bg-profile": "single",
"total": "536870912",
"used": "0"
}
]
}
Optionally there's "zone_unusable" key. The rowspec cannot print the
same output as now so the original implementation is preserved.
Signed-off-by: David Sterba <dsterba@suse.com>
2022-12-06 16:12:10 +00:00
|
|
|
HELPINFO_INSERT_GLOBALS,
|
|
|
|
HELPINFO_INSERT_FORMAT,
|
2015-09-01 07:12:06 +00:00
|
|
|
NULL
|
2012-02-03 19:00:17 +00:00
|
|
|
};
|
|
|
|
|
btrfs-progs: fi df: implement json output
Print JSON output with example output:
$ btrfs --format json filesystem df /path
{
"__header": {
"version": "1"
},
"filesystem-df": [
{
"bg-type": "Data",
"bg-profile": "single",
"total": "841813590016",
"used": "494085484544"
},
{
"bg-type": "System",
"bg-profile": "single",
"total": "33554432",
"used": "114688"
},
{
"bg-type": "Metadata",
"bg-profile": "single",
"total": "24696061952",
"used": "5743165440"
},
{
"bg-type": "GlobalReserve",
"bg-profile": "single",
"total": "536870912",
"used": "0"
}
]
}
Optionally there's "zone_unusable" key. The rowspec cannot print the
same output as now so the original implementation is preserved.
Signed-off-by: David Sterba <dsterba@suse.com>
2022-12-06 16:12:10 +00:00
|
|
|
static void print_df_text(int fd, struct btrfs_ioctl_space_args *sargs, unsigned unit_mode)
|
2013-09-27 12:24:41 +00:00
|
|
|
{
|
|
|
|
u64 i;
|
|
|
|
struct btrfs_ioctl_space_info *sp = sargs->spaces;
|
btrfs-progs: fi df: report zone_unusable on zoned filesystem
In the zoned mode there are parts of chunks that become unusable once
they get COWed and the zone must be reclaimed and reset to make the
space usable again. Provide a way to show the total size per block group
type in fi df:
$ btrfs fi df .
Data, single: total=1.00GiB, used=257.51MiB, zone_unusable=238.43MiB
System, single: total=256.00MiB, used=16.00KiB, zone_unusable=224.00KiB
Metadata, single: total=256.00MiB, used=816.00KiB, zone_unusable=8.61MiB
GlobalReserve, single: total=3.25MiB, used=0.00B
This will not be shown on non-zoned filesystems.
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 14:45:41 +00:00
|
|
|
u64 unusable;
|
|
|
|
bool ok;
|
2013-09-27 12:24:41 +00:00
|
|
|
|
|
|
|
for (i = 0; i < sargs->total_spaces; i++, sp++) {
|
2021-05-06 22:34:26 +00:00
|
|
|
unusable = device_get_zone_unusable(fd, sp->flags);
|
|
|
|
ok = (unusable != DEVICE_ZONE_UNUSABLE_UNKNOWN);
|
btrfs-progs: fi df: report zone_unusable on zoned filesystem
In the zoned mode there are parts of chunks that become unusable once
they get COWed and the zone must be reclaimed and reset to make the
space usable again. Provide a way to show the total size per block group
type in fi df:
$ btrfs fi df .
Data, single: total=1.00GiB, used=257.51MiB, zone_unusable=238.43MiB
System, single: total=256.00MiB, used=16.00KiB, zone_unusable=224.00KiB
Metadata, single: total=256.00MiB, used=816.00KiB, zone_unusable=8.61MiB
GlobalReserve, single: total=3.25MiB, used=0.00B
This will not be shown on non-zoned filesystems.
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 14:45:41 +00:00
|
|
|
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "%s, %s: total=%s, used=%s%s%s\n",
|
2014-10-23 17:05:31 +00:00
|
|
|
btrfs_group_type_str(sp->flags),
|
|
|
|
btrfs_group_profile_str(sp->flags),
|
2014-08-30 18:27:00 +00:00
|
|
|
pretty_size_mode(sp->total_bytes, unit_mode),
|
btrfs-progs: fi df: report zone_unusable on zoned filesystem
In the zoned mode there are parts of chunks that become unusable once
they get COWed and the zone must be reclaimed and reset to make the
space usable again. Provide a way to show the total size per block group
type in fi df:
$ btrfs fi df .
Data, single: total=1.00GiB, used=257.51MiB, zone_unusable=238.43MiB
System, single: total=256.00MiB, used=16.00KiB, zone_unusable=224.00KiB
Metadata, single: total=256.00MiB, used=816.00KiB, zone_unusable=8.61MiB
GlobalReserve, single: total=3.25MiB, used=0.00B
This will not be shown on non-zoned filesystems.
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 14:45:41 +00:00
|
|
|
pretty_size_mode(sp->used_bytes, unit_mode),
|
|
|
|
(ok ? ", zone_unusable=" : ""),
|
|
|
|
(ok ? pretty_size_mode(unusable, unit_mode) : ""));
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
2013-09-27 12:24:41 +00:00
|
|
|
}
|
|
|
|
|
btrfs-progs: fi df: implement json output
Print JSON output with example output:
$ btrfs --format json filesystem df /path
{
"__header": {
"version": "1"
},
"filesystem-df": [
{
"bg-type": "Data",
"bg-profile": "single",
"total": "841813590016",
"used": "494085484544"
},
{
"bg-type": "System",
"bg-profile": "single",
"total": "33554432",
"used": "114688"
},
{
"bg-type": "Metadata",
"bg-profile": "single",
"total": "24696061952",
"used": "5743165440"
},
{
"bg-type": "GlobalReserve",
"bg-profile": "single",
"total": "536870912",
"used": "0"
}
]
}
Optionally there's "zone_unusable" key. The rowspec cannot print the
same output as now so the original implementation is preserved.
Signed-off-by: David Sterba <dsterba@suse.com>
2022-12-06 16:12:10 +00:00
|
|
|
static const struct rowspec filesystem_df_rowspec[] = {
|
|
|
|
{ .key = "bg-type", .fmt = "%s", .out_json = "bg-type" },
|
|
|
|
{ .key = "bg-profile", .fmt = "%s", .out_json = "bg-profile" },
|
|
|
|
{ .key = "total", .fmt = "%llu", .out_json = "total" },
|
|
|
|
{ .key = "used", .fmt = "%llu", .out_json = "used" },
|
|
|
|
{ .key = "zone_unusable", .fmt = "%llu", .out_json = "zone_unusable" },
|
|
|
|
ROWSPEC_END
|
|
|
|
};
|
|
|
|
|
|
|
|
static void print_df_json(int fd, struct btrfs_ioctl_space_args *sargs)
|
|
|
|
{
|
|
|
|
struct format_ctx fctx;
|
|
|
|
u64 i;
|
|
|
|
struct btrfs_ioctl_space_info *sp = sargs->spaces;
|
|
|
|
u64 unusable;
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
fmt_start(&fctx, filesystem_df_rowspec, 1, 0);
|
|
|
|
fmt_print_start_group(&fctx, "filesystem-df", JSON_TYPE_ARRAY);
|
|
|
|
|
|
|
|
for (i = 0; i < sargs->total_spaces; i++, sp++) {
|
|
|
|
unusable = device_get_zone_unusable(fd, sp->flags);
|
|
|
|
ok = (unusable != DEVICE_ZONE_UNUSABLE_UNKNOWN);
|
|
|
|
|
|
|
|
fmt_print_start_group(&fctx, NULL, JSON_TYPE_MAP);
|
|
|
|
fmt_print(&fctx, "bg-type", btrfs_group_type_str(sp->flags));
|
|
|
|
fmt_print(&fctx, "bg-profile", btrfs_group_profile_str(sp->flags));
|
|
|
|
fmt_print(&fctx, "total", sp->total_bytes);
|
|
|
|
fmt_print(&fctx, "used", sp->used_bytes);
|
|
|
|
if (ok)
|
|
|
|
fmt_print(&fctx, "zone_unusable", unusable);
|
|
|
|
fmt_print_end_group(&fctx, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt_print_end_group(&fctx, "filesystem-df");
|
|
|
|
fmt_end(&fctx);
|
|
|
|
}
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_filesystem_df(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2013-09-27 12:24:41 +00:00
|
|
|
{
|
|
|
|
struct btrfs_ioctl_space_args *sargs = NULL;
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
char *path;
|
2014-08-30 18:27:00 +00:00
|
|
|
DIR *dirstream = NULL;
|
2015-09-01 07:12:06 +00:00
|
|
|
unsigned unit_mode;
|
2015-04-08 15:33:55 +00:00
|
|
|
|
2015-09-01 07:12:06 +00:00
|
|
|
unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
|
2013-09-27 12:24:41 +00:00
|
|
|
|
2018-03-07 02:42:01 +00:00
|
|
|
clean_args_no_options(cmd, argc, argv);
|
2016-03-01 15:02:08 +00:00
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2013-09-27 12:24:41 +00:00
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
path = argv[optind];
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-10-12 13:22:55 +00:00
|
|
|
fd = btrfs_open_dir(path, &dirstream, 1);
|
|
|
|
if (fd < 0)
|
2013-09-27 12:24:41 +00:00
|
|
|
return 1;
|
2015-10-12 13:22:55 +00:00
|
|
|
|
2013-09-27 12:24:41 +00:00
|
|
|
ret = get_df(fd, &sargs);
|
|
|
|
|
2014-07-30 07:26:32 +00:00
|
|
|
if (ret == 0) {
|
btrfs-progs: fi df: implement json output
Print JSON output with example output:
$ btrfs --format json filesystem df /path
{
"__header": {
"version": "1"
},
"filesystem-df": [
{
"bg-type": "Data",
"bg-profile": "single",
"total": "841813590016",
"used": "494085484544"
},
{
"bg-type": "System",
"bg-profile": "single",
"total": "33554432",
"used": "114688"
},
{
"bg-type": "Metadata",
"bg-profile": "single",
"total": "24696061952",
"used": "5743165440"
},
{
"bg-type": "GlobalReserve",
"bg-profile": "single",
"total": "536870912",
"used": "0"
}
]
}
Optionally there's "zone_unusable" key. The rowspec cannot print the
same output as now so the original implementation is preserved.
Signed-off-by: David Sterba <dsterba@suse.com>
2022-12-06 16:12:10 +00:00
|
|
|
if (bconf.output_format == CMD_FORMAT_JSON)
|
|
|
|
print_df_json(fd, sargs);
|
|
|
|
else
|
|
|
|
print_df_text(fd, sargs, unit_mode);
|
2013-09-27 12:24:41 +00:00
|
|
|
free(sargs);
|
|
|
|
} else {
|
2018-10-25 12:10:54 +00:00
|
|
|
errno = -ret;
|
|
|
|
error("get_df failed: %m");
|
2013-09-27 12:24:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 12:34:59 +00:00
|
|
|
btrfs_warn_multiple_profiles(fd);
|
2013-09-27 12:24:41 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
2013-09-04 15:22:23 +00:00
|
|
|
return !!ret;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
btrfs-progs: fi df: implement json output
Print JSON output with example output:
$ btrfs --format json filesystem df /path
{
"__header": {
"version": "1"
},
"filesystem-df": [
{
"bg-type": "Data",
"bg-profile": "single",
"total": "841813590016",
"used": "494085484544"
},
{
"bg-type": "System",
"bg-profile": "single",
"total": "33554432",
"used": "114688"
},
{
"bg-type": "Metadata",
"bg-profile": "single",
"total": "24696061952",
"used": "5743165440"
},
{
"bg-type": "GlobalReserve",
"bg-profile": "single",
"total": "536870912",
"used": "0"
}
]
}
Optionally there's "zone_unusable" key. The rowspec cannot print the
same output as now so the original implementation is preserved.
Signed-off-by: David Sterba <dsterba@suse.com>
2022-12-06 16:12:10 +00:00
|
|
|
static DEFINE_COMMAND_WITH_FLAGS(filesystem_df, "df", CMD_FORMAT_JSON);
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2017-10-26 18:44:55 +00:00
|
|
|
static int match_search_item_kernel(u8 *fsid, char *mnt, char *label,
|
2013-10-24 14:42:56 +00:00
|
|
|
char *search)
|
|
|
|
{
|
2013-11-04 07:17:41 +00:00
|
|
|
char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
|
2013-10-24 14:42:56 +00:00
|
|
|
int search_len = strlen(search);
|
|
|
|
|
2013-11-04 07:17:41 +00:00
|
|
|
search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
|
2013-10-24 14:42:56 +00:00
|
|
|
uuid_unparse(fsid, uuidbuf);
|
|
|
|
if (!strncmp(uuidbuf, search, search_len))
|
|
|
|
return 1;
|
|
|
|
|
2015-11-06 15:53:55 +00:00
|
|
|
if (*label && strcmp(label, search) == 0)
|
2013-10-24 14:42:56 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (strcmp(mnt, search) == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:03:59 +00:00
|
|
|
/* Search for user visible uuid 'search' in registered filesystems */
|
2016-09-29 13:12:35 +00:00
|
|
|
static int uuid_search(struct btrfs_fs_devices *fs_devices, const char *search)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2013-11-04 07:17:41 +00:00
|
|
|
char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
|
2012-02-03 19:00:17 +00:00
|
|
|
struct btrfs_device *device;
|
2012-04-25 18:22:32 +00:00
|
|
|
int search_len = strlen(search);
|
|
|
|
|
2013-11-04 07:17:41 +00:00
|
|
|
search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
|
2012-04-25 18:22:32 +00:00
|
|
|
uuid_unparse(fs_devices->fsid, uuidbuf);
|
|
|
|
if (!strncmp(uuidbuf, search, search_len))
|
|
|
|
return 1;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2017-12-07 09:10:05 +00:00
|
|
|
list_for_each_entry(device, &fs_devices->devices, dev_list) {
|
2012-02-03 19:00:17 +00:00
|
|
|
if ((device->label && strcmp(device->label, search) == 0) ||
|
|
|
|
strcmp(device->name, search) == 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-17 15:24:53 +00:00
|
|
|
/*
|
|
|
|
* Sort devices by devid, ascending
|
|
|
|
*/
|
|
|
|
static int cmp_device_id(void *priv, struct list_head *a,
|
|
|
|
struct list_head *b)
|
|
|
|
{
|
|
|
|
const struct btrfs_device *da = list_entry(a, struct btrfs_device,
|
|
|
|
dev_list);
|
|
|
|
const struct btrfs_device *db = list_entry(b, struct btrfs_device,
|
|
|
|
dev_list);
|
|
|
|
|
|
|
|
return da->devid < db->devid ? -1 :
|
|
|
|
da->devid > db->devid ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2014-10-06 10:16:46 +00:00
|
|
|
static void splice_device_list(struct list_head *seed_devices,
|
|
|
|
struct list_head *all_devices)
|
|
|
|
{
|
|
|
|
struct btrfs_device *in_all, *next_all;
|
|
|
|
struct btrfs_device *in_seed, *next_seed;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(in_all, next_all, all_devices, dev_list) {
|
|
|
|
list_for_each_entry_safe(in_seed, next_seed, seed_devices,
|
|
|
|
dev_list) {
|
|
|
|
if (in_all->devid == in_seed->devid) {
|
|
|
|
/*
|
|
|
|
* When do dev replace in a sprout fs
|
|
|
|
* to a dev in its seed fs, the replacing
|
|
|
|
* dev will reside in the sprout fs and
|
|
|
|
* the replaced dev will still exist
|
|
|
|
* in the seed fs.
|
|
|
|
* So pick the latest one when showing
|
|
|
|
* the sprout fs.
|
|
|
|
*/
|
|
|
|
if (in_all->generation
|
|
|
|
< in_seed->generation) {
|
|
|
|
list_del(&in_all->dev_list);
|
|
|
|
free(in_all);
|
|
|
|
} else if (in_all->generation
|
|
|
|
> in_seed->generation) {
|
|
|
|
list_del(&in_seed->dev_list);
|
|
|
|
free(in_seed);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list_splice(seed_devices, all_devices);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_devices(struct btrfs_fs_devices *fs_devices,
|
2015-06-18 06:46:11 +00:00
|
|
|
u64 *devs_found, unsigned unit_mode)
|
2014-10-06 10:16:46 +00:00
|
|
|
{
|
|
|
|
struct btrfs_device *device;
|
|
|
|
struct btrfs_fs_devices *cur_fs;
|
|
|
|
struct list_head *all_devices;
|
|
|
|
|
|
|
|
all_devices = &fs_devices->devices;
|
|
|
|
cur_fs = fs_devices->seed;
|
|
|
|
/* add all devices of seed fs to the fs to be printed */
|
|
|
|
while (cur_fs) {
|
|
|
|
splice_device_list(&cur_fs->devices, all_devices);
|
|
|
|
cur_fs = cur_fs->seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_sort(NULL, all_devices, cmp_device_id);
|
|
|
|
list_for_each_entry(device, all_devices, dev_list) {
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\tdevid %4llu size %s used %s path %s\n",
|
2022-09-27 23:13:00 +00:00
|
|
|
device->devid,
|
2015-06-18 06:46:11 +00:00
|
|
|
pretty_size_mode(device->total_bytes, unit_mode),
|
|
|
|
pretty_size_mode(device->bytes_used, unit_mode),
|
|
|
|
device->name);
|
2014-10-06 10:16:46 +00:00
|
|
|
|
|
|
|
(*devs_found)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 06:46:11 +00:00
|
|
|
static void print_one_uuid(struct btrfs_fs_devices *fs_devices,
|
|
|
|
unsigned unit_mode)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2013-11-04 07:17:41 +00:00
|
|
|
char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
|
2012-02-03 19:00:17 +00:00
|
|
|
struct btrfs_device *device;
|
|
|
|
u64 devs_found = 0;
|
|
|
|
u64 total;
|
|
|
|
|
2017-09-27 02:02:47 +00:00
|
|
|
if (add_seen_fsid(fs_devices->fsid, seen_fsid_hash, -1, NULL))
|
2013-11-18 19:18:08 +00:00
|
|
|
return;
|
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
uuid_unparse(fs_devices->fsid, uuidbuf);
|
|
|
|
device = list_entry(fs_devices->devices.next, struct btrfs_device,
|
|
|
|
dev_list);
|
|
|
|
if (device->label && device->label[0])
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "Label: '%s' ", device->label);
|
2012-02-03 19:00:17 +00:00
|
|
|
else
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "Label: none ");
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
total = device->total_devs;
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, " uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
|
2022-09-27 23:13:00 +00:00
|
|
|
total, pretty_size_mode(device->super_bytes_used, unit_mode));
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-06-18 06:46:11 +00:00
|
|
|
print_devices(fs_devices, &devs_found, unit_mode);
|
2013-07-09 20:24:43 +00:00
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
if (devs_found < total) {
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\t*** Some devices missing\n");
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\n");
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 03:41:38 +00:00
|
|
|
/* adds up all the used spaces as reported by the space info ioctl
|
|
|
|
*/
|
|
|
|
static u64 calc_used_bytes(struct btrfs_ioctl_space_args *si)
|
|
|
|
{
|
|
|
|
u64 ret = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < si->total_spaces; i++)
|
|
|
|
ret += si->spaces[i].used_bytes;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
|
|
|
|
struct btrfs_ioctl_dev_info_args *dev_info,
|
|
|
|
struct btrfs_ioctl_space_args *space_info,
|
2015-11-06 16:05:36 +00:00
|
|
|
char *label, unsigned unit_mode)
|
2013-10-08 03:41:38 +00:00
|
|
|
{
|
|
|
|
int i;
|
2014-02-07 07:07:19 +00:00
|
|
|
int fd;
|
2013-11-04 07:17:41 +00:00
|
|
|
char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
|
2013-10-08 03:41:38 +00:00
|
|
|
struct btrfs_ioctl_dev_info_args *tmp_dev_info;
|
2013-11-18 19:18:08 +00:00
|
|
|
int ret;
|
|
|
|
|
2017-09-27 02:02:47 +00:00
|
|
|
ret = add_seen_fsid(fs_info->fsid, seen_fsid_hash, -1, NULL);
|
2013-11-18 19:18:08 +00:00
|
|
|
if (ret == -EEXIST)
|
|
|
|
return 0;
|
|
|
|
else if (ret)
|
|
|
|
return ret;
|
2013-10-08 03:41:38 +00:00
|
|
|
|
|
|
|
uuid_unparse(fs_info->fsid, uuidbuf);
|
2015-11-06 15:53:55 +00:00
|
|
|
if (label && *label)
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "Label: '%s' ", label);
|
2013-10-24 14:42:56 +00:00
|
|
|
else
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "Label: none ");
|
2013-10-08 03:41:38 +00:00
|
|
|
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, " uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
|
2013-10-24 14:42:56 +00:00
|
|
|
fs_info->num_devices,
|
2015-06-18 06:46:11 +00:00
|
|
|
pretty_size_mode(calc_used_bytes(space_info),
|
|
|
|
unit_mode));
|
2013-10-08 03:41:38 +00:00
|
|
|
|
|
|
|
for (i = 0; i < fs_info->num_devices; i++) {
|
2014-11-13 01:36:20 +00:00
|
|
|
char *canonical_path;
|
|
|
|
|
2013-10-08 03:41:38 +00:00
|
|
|
tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
|
2014-02-07 07:07:19 +00:00
|
|
|
|
|
|
|
/* Add check for missing devices even mounted */
|
|
|
|
fd = open((char *)tmp_dev_info->path, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\tdevid %4llu size 0 used 0 path %s MISSING\n",
|
2022-03-29 08:30:41 +00:00
|
|
|
tmp_dev_info->devid, tmp_dev_info->path);
|
2014-02-07 07:07:19 +00:00
|
|
|
continue;
|
2022-03-29 08:30:41 +00:00
|
|
|
|
2014-02-07 07:07:19 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
2021-01-21 15:25:51 +00:00
|
|
|
canonical_path = path_canonicalize((char *)tmp_dev_info->path);
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\tdevid %4llu size %s used %s path %s\n",
|
2013-10-08 03:41:38 +00:00
|
|
|
tmp_dev_info->devid,
|
2015-06-18 06:46:11 +00:00
|
|
|
pretty_size_mode(tmp_dev_info->total_bytes, unit_mode),
|
|
|
|
pretty_size_mode(tmp_dev_info->bytes_used, unit_mode),
|
2014-11-13 01:36:20 +00:00
|
|
|
canonical_path);
|
|
|
|
|
|
|
|
free(canonical_path);
|
2013-10-08 03:41:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\n");
|
2013-10-08 03:41:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-18 06:46:11 +00:00
|
|
|
static int btrfs_scan_kernel(void *search, unsigned unit_mode)
|
2013-10-08 03:41:38 +00:00
|
|
|
{
|
2013-10-24 14:42:56 +00:00
|
|
|
int ret = 0, fd;
|
2014-04-08 09:16:02 +00:00
|
|
|
int found = 0;
|
2013-10-08 03:41:38 +00:00
|
|
|
FILE *f;
|
|
|
|
struct mntent *mnt;
|
|
|
|
struct btrfs_ioctl_fs_info_args fs_info_arg;
|
|
|
|
struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL;
|
2014-03-21 13:36:08 +00:00
|
|
|
struct btrfs_ioctl_space_args *space_info_arg = NULL;
|
2013-10-08 03:41:38 +00:00
|
|
|
char label[BTRFS_LABEL_SIZE];
|
|
|
|
|
|
|
|
f = setmntent("/proc/self/mounts", "r");
|
|
|
|
if (f == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2013-10-24 14:42:56 +00:00
|
|
|
memset(label, 0, sizeof(label));
|
2013-10-08 03:41:38 +00:00
|
|
|
while ((mnt = getmntent(f)) != NULL) {
|
2016-03-15 09:25:00 +00:00
|
|
|
free(dev_info_arg);
|
|
|
|
dev_info_arg = NULL;
|
2013-10-08 03:41:38 +00:00
|
|
|
if (strcmp(mnt->mnt_type, "btrfs"))
|
|
|
|
continue;
|
|
|
|
ret = get_fs_info(mnt->mnt_dir, &fs_info_arg,
|
|
|
|
&dev_info_arg);
|
2016-03-15 09:25:00 +00:00
|
|
|
if (ret)
|
2013-11-27 17:14:58 +00:00
|
|
|
goto out;
|
2013-10-08 03:41:38 +00:00
|
|
|
|
2015-09-15 08:46:23 +00:00
|
|
|
/* skip all fs already shown as mounted fs */
|
2017-09-27 02:02:19 +00:00
|
|
|
if (is_seen_fsid(fs_info_arg.fsid, seen_fsid_hash))
|
2015-09-15 08:46:23 +00:00
|
|
|
continue;
|
|
|
|
|
2015-09-15 08:46:22 +00:00
|
|
|
ret = get_label_mounted(mnt->mnt_dir, label);
|
|
|
|
/* provide backward kernel compatibility */
|
|
|
|
if (ret == -ENOTTY)
|
|
|
|
ret = get_label_unmounted(
|
|
|
|
(const char *)dev_info_arg->path, label);
|
|
|
|
|
2016-03-15 09:25:00 +00:00
|
|
|
if (ret)
|
2013-11-27 17:14:58 +00:00
|
|
|
goto out;
|
2016-03-15 09:25:00 +00:00
|
|
|
|
2013-10-24 14:42:56 +00:00
|
|
|
if (search && !match_search_item_kernel(fs_info_arg.fsid,
|
|
|
|
mnt->mnt_dir, label, search)) {
|
|
|
|
continue;
|
2013-10-08 03:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(mnt->mnt_dir, O_RDONLY);
|
2013-11-06 23:15:48 +00:00
|
|
|
if ((fd != -1) && !get_df(fd, &space_info_arg)) {
|
2013-10-08 03:41:38 +00:00
|
|
|
print_one_fs(&fs_info_arg, dev_info_arg,
|
2015-11-06 16:05:36 +00:00
|
|
|
space_info_arg, label, unit_mode);
|
2016-09-07 14:22:33 +00:00
|
|
|
free(space_info_arg);
|
2013-10-24 14:42:56 +00:00
|
|
|
memset(label, 0, sizeof(label));
|
2014-04-08 09:16:02 +00:00
|
|
|
found = 1;
|
2013-10-08 03:41:38 +00:00
|
|
|
}
|
2013-11-06 23:15:48 +00:00
|
|
|
if (fd != -1)
|
2013-10-08 03:41:38 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
2013-11-27 17:14:58 +00:00
|
|
|
|
|
|
|
out:
|
2016-03-15 09:25:00 +00:00
|
|
|
free(dev_info_arg);
|
2013-11-27 17:14:58 +00:00
|
|
|
endmntent(f);
|
2014-04-08 09:16:02 +00:00
|
|
|
return !found;
|
2013-10-08 03:41:38 +00:00
|
|
|
}
|
|
|
|
|
2014-10-06 10:16:46 +00:00
|
|
|
static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
|
|
|
|
{
|
|
|
|
struct btrfs_fs_devices *cur_seed, *next_seed;
|
|
|
|
struct btrfs_device *device;
|
|
|
|
|
|
|
|
while (!list_empty(&fs_devices->devices)) {
|
|
|
|
device = list_entry(fs_devices->devices.next,
|
|
|
|
struct btrfs_device, dev_list);
|
|
|
|
list_del(&device->dev_list);
|
|
|
|
|
|
|
|
free(device->name);
|
|
|
|
free(device->label);
|
|
|
|
free(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free seed fs chain */
|
|
|
|
cur_seed = fs_devices->seed;
|
|
|
|
fs_devices->seed = NULL;
|
|
|
|
while (cur_seed) {
|
|
|
|
next_seed = cur_seed->seed;
|
|
|
|
free(cur_seed);
|
|
|
|
|
|
|
|
cur_seed = next_seed;
|
|
|
|
}
|
|
|
|
|
2023-08-14 15:28:07 +00:00
|
|
|
list_del(&fs_devices->fs_list);
|
2014-10-06 10:16:46 +00:00
|
|
|
free(fs_devices);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int copy_device(struct btrfs_device *dst,
|
|
|
|
struct btrfs_device *src)
|
|
|
|
{
|
|
|
|
dst->devid = src->devid;
|
|
|
|
memcpy(dst->uuid, src->uuid, BTRFS_UUID_SIZE);
|
|
|
|
if (src->name == NULL)
|
|
|
|
dst->name = NULL;
|
|
|
|
else {
|
|
|
|
dst->name = strdup(src->name);
|
|
|
|
if (!dst->name)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
if (src->label == NULL)
|
|
|
|
dst->label = NULL;
|
|
|
|
else {
|
|
|
|
dst->label = strdup(src->label);
|
|
|
|
if (!dst->label) {
|
|
|
|
free(dst->name);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst->total_devs = src->total_devs;
|
|
|
|
dst->super_bytes_used = src->super_bytes_used;
|
|
|
|
dst->total_bytes = src->total_bytes;
|
|
|
|
dst->bytes_used = src->bytes_used;
|
|
|
|
dst->generation = src->generation;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int copy_fs_devices(struct btrfs_fs_devices *dst,
|
|
|
|
struct btrfs_fs_devices *src)
|
|
|
|
{
|
|
|
|
struct btrfs_device *cur_dev, *dev_copy;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
memcpy(dst->fsid, src->fsid, BTRFS_FSID_SIZE);
|
2018-10-11 15:03:59 +00:00
|
|
|
memcpy(dst->metadata_uuid, src->metadata_uuid, BTRFS_FSID_SIZE);
|
2014-10-06 10:16:46 +00:00
|
|
|
INIT_LIST_HEAD(&dst->devices);
|
|
|
|
dst->seed = NULL;
|
|
|
|
|
|
|
|
list_for_each_entry(cur_dev, &src->devices, dev_list) {
|
|
|
|
dev_copy = malloc(sizeof(*dev_copy));
|
|
|
|
if (!dev_copy) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = copy_device(dev_copy, cur_dev);
|
|
|
|
if (ret) {
|
|
|
|
free(dev_copy);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_add(&dev_copy->dev_list, &dst->devices);
|
|
|
|
dev_copy->fs_devices = dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int find_and_copy_seed(struct btrfs_fs_devices *seed,
|
|
|
|
struct btrfs_fs_devices *copy,
|
|
|
|
struct list_head *fs_uuids) {
|
|
|
|
struct btrfs_fs_devices *cur_fs;
|
|
|
|
|
2023-08-14 15:28:07 +00:00
|
|
|
list_for_each_entry(cur_fs, fs_uuids, fs_list)
|
2014-10-06 10:16:46 +00:00
|
|
|
if (!memcmp(seed->fsid, cur_fs->fsid, BTRFS_FSID_SIZE))
|
|
|
|
return copy_fs_devices(copy, cur_fs);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-11-11 06:39:14 +00:00
|
|
|
static int has_seed_devices(struct btrfs_fs_devices *fs_devices)
|
|
|
|
{
|
|
|
|
struct btrfs_device *device;
|
|
|
|
int dev_cnt_total, dev_cnt = 0;
|
|
|
|
|
|
|
|
device = list_first_entry(&fs_devices->devices, struct btrfs_device,
|
|
|
|
dev_list);
|
|
|
|
|
|
|
|
dev_cnt_total = device->total_devs;
|
|
|
|
|
|
|
|
list_for_each_entry(device, &fs_devices->devices, dev_list)
|
|
|
|
dev_cnt++;
|
|
|
|
|
|
|
|
return dev_cnt_total != dev_cnt;
|
|
|
|
}
|
|
|
|
|
2014-10-30 02:44:49 +00:00
|
|
|
static int search_umounted_fs_uuids(struct list_head *all_uuids,
|
2015-01-14 06:50:40 +00:00
|
|
|
char *search, int *found)
|
2014-10-06 10:16:46 +00:00
|
|
|
{
|
2014-10-30 02:44:49 +00:00
|
|
|
struct btrfs_fs_devices *cur_fs, *fs_copy;
|
2014-10-06 10:16:46 +00:00
|
|
|
struct list_head *fs_uuids;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
fs_uuids = btrfs_scanned_uuids();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The fs_uuids list is global, and open_ctree_* will
|
|
|
|
* modify it, make a private copy here
|
|
|
|
*/
|
2023-08-14 15:28:07 +00:00
|
|
|
list_for_each_entry(cur_fs, fs_uuids, fs_list) {
|
2014-10-06 10:16:46 +00:00
|
|
|
/* don't bother handle all fs, if search target specified */
|
|
|
|
if (search) {
|
|
|
|
if (uuid_search(cur_fs, search) == 0)
|
|
|
|
continue;
|
2015-01-14 06:50:40 +00:00
|
|
|
if (found)
|
|
|
|
*found = 1;
|
2014-10-06 10:16:46 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 02:44:50 +00:00
|
|
|
/* skip all fs already shown as mounted fs */
|
2017-09-27 02:02:19 +00:00
|
|
|
if (is_seen_fsid(cur_fs->fsid, seen_fsid_hash))
|
2014-10-30 02:44:50 +00:00
|
|
|
continue;
|
|
|
|
|
2015-10-22 03:53:39 +00:00
|
|
|
fs_copy = calloc(1, sizeof(*fs_copy));
|
2014-10-06 10:16:46 +00:00
|
|
|
if (!fs_copy) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = copy_fs_devices(fs_copy, cur_fs);
|
|
|
|
if (ret) {
|
|
|
|
free(fs_copy);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-08-14 15:28:07 +00:00
|
|
|
list_add(&fs_copy->fs_list, all_uuids);
|
2014-10-06 10:16:46 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 02:44:49 +00:00
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int map_seed_devices(struct list_head *all_uuids)
|
|
|
|
{
|
|
|
|
struct btrfs_fs_devices *cur_fs, *cur_seed;
|
|
|
|
struct btrfs_fs_devices *seed_copy;
|
|
|
|
struct btrfs_fs_devices *opened_fs;
|
|
|
|
struct btrfs_device *device;
|
|
|
|
struct btrfs_fs_info *fs_info;
|
|
|
|
struct list_head *fs_uuids;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
fs_uuids = btrfs_scanned_uuids();
|
|
|
|
|
2023-08-14 15:28:07 +00:00
|
|
|
list_for_each_entry(cur_fs, all_uuids, fs_list) {
|
2023-06-13 10:26:53 +00:00
|
|
|
struct open_ctree_args oca = { 0 };
|
2021-02-18 00:27:21 +00:00
|
|
|
|
2014-10-06 10:16:46 +00:00
|
|
|
device = list_first_entry(&cur_fs->devices,
|
|
|
|
struct btrfs_device, dev_list);
|
|
|
|
if (!device)
|
|
|
|
continue;
|
2014-11-11 06:39:14 +00:00
|
|
|
|
|
|
|
/* skip fs without seeds */
|
|
|
|
if (!has_seed_devices(cur_fs))
|
|
|
|
continue;
|
|
|
|
|
2014-10-06 10:16:46 +00:00
|
|
|
/*
|
|
|
|
* open_ctree_* detects seed/sprout mapping
|
|
|
|
*/
|
2023-06-13 10:26:53 +00:00
|
|
|
oca.filename = device->name;
|
|
|
|
oca.flags = OPEN_CTREE_PARTIAL;
|
|
|
|
fs_info = open_ctree_fs_info(&oca);
|
2014-10-06 10:16:46 +00:00
|
|
|
if (!fs_info)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy the seed chain under the opened fs
|
|
|
|
*/
|
|
|
|
opened_fs = fs_info->fs_devices;
|
|
|
|
cur_seed = cur_fs;
|
|
|
|
while (opened_fs->seed) {
|
|
|
|
seed_copy = malloc(sizeof(*seed_copy));
|
|
|
|
if (!seed_copy) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail_out;
|
|
|
|
}
|
|
|
|
ret = find_and_copy_seed(opened_fs->seed, seed_copy,
|
|
|
|
fs_uuids);
|
|
|
|
if (ret) {
|
|
|
|
free(seed_copy);
|
|
|
|
goto fail_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_seed->seed = seed_copy;
|
|
|
|
|
|
|
|
opened_fs = opened_fs->seed;
|
|
|
|
cur_seed = cur_seed->seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
close_ctree(fs_info->chunk_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
fail_out:
|
|
|
|
close_ctree(fs_info->chunk_root);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_filesystem_show_usage[] = {
|
2013-10-24 14:42:56 +00:00
|
|
|
"btrfs filesystem show [options] [<path>|<uuid>|<device>|label]",
|
2012-02-03 19:00:17 +00:00
|
|
|
"Show the structure of a filesystem",
|
2019-06-21 13:36:04 +00:00
|
|
|
"",
|
2023-02-22 00:37:18 +00:00
|
|
|
OPTLINE("-d|--all-devices", "show only disks under /dev containing btrfs filesystem"),
|
|
|
|
OPTLINE("-m|--mounted", "show only mounted btrfs"),
|
2015-11-06 15:39:49 +00:00
|
|
|
HELPINFO_UNITS_LONG,
|
2020-06-29 14:57:54 +00:00
|
|
|
"",
|
2012-02-03 19:00:17 +00:00
|
|
|
"If no argument is given, structure of all present filesystems is shown.",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_filesystem_show(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2014-10-06 10:16:46 +00:00
|
|
|
LIST_HEAD(all_uuids);
|
2012-02-03 19:00:17 +00:00
|
|
|
struct btrfs_fs_devices *fs_devices;
|
2021-11-10 20:07:55 +00:00
|
|
|
struct btrfs_root *root = NULL;
|
2013-08-14 23:16:45 +00:00
|
|
|
char *search = NULL;
|
2012-02-03 19:00:17 +00:00
|
|
|
int ret;
|
2014-09-13 01:21:21 +00:00
|
|
|
/* default, search both kernel and udev */
|
|
|
|
int where = -1;
|
2013-10-08 03:41:38 +00:00
|
|
|
int type = 0;
|
2015-06-12 11:18:44 +00:00
|
|
|
char mp[PATH_MAX];
|
2013-10-24 14:42:56 +00:00
|
|
|
char path[PATH_MAX];
|
2017-10-26 18:44:55 +00:00
|
|
|
u8 fsid[BTRFS_FSID_SIZE];
|
2014-08-11 09:12:16 +00:00
|
|
|
char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
|
2015-09-01 07:12:06 +00:00
|
|
|
unsigned unit_mode;
|
2014-01-14 11:17:07 +00:00
|
|
|
int found = 0;
|
2013-10-08 03:41:38 +00:00
|
|
|
|
2015-09-01 07:12:06 +00:00
|
|
|
unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
|
|
|
|
|
2018-06-20 00:38:38 +00:00
|
|
|
optind = 0;
|
2013-10-08 03:41:38 +00:00
|
|
|
while (1) {
|
2015-04-08 15:33:55 +00:00
|
|
|
int c;
|
2015-01-19 12:44:49 +00:00
|
|
|
static const struct option long_options[] = {
|
2013-10-08 03:41:38 +00:00
|
|
|
{ "all-devices", no_argument, NULL, 'd'},
|
|
|
|
{ "mounted", no_argument, NULL, 'm'},
|
2015-01-21 17:53:02 +00:00
|
|
|
{ NULL, 0, NULL, 0 }
|
2013-10-08 03:41:38 +00:00
|
|
|
};
|
2015-04-08 15:33:55 +00:00
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "dm", long_options, NULL);
|
2013-10-08 03:41:38 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
2014-09-13 01:21:21 +00:00
|
|
|
where = BTRFS_SCAN_LBLKID;
|
2013-10-08 03:41:38 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
where = BTRFS_SCAN_MOUNTED;
|
|
|
|
break;
|
|
|
|
default:
|
2018-03-07 02:54:38 +00:00
|
|
|
usage_unknown_option(cmd, argv);
|
2013-10-08 03:41:38 +00:00
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2013-10-24 14:42:56 +00:00
|
|
|
if (check_argc_max(argc, optind + 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2013-10-24 14:42:56 +00:00
|
|
|
|
2013-10-08 03:41:38 +00:00
|
|
|
if (argc > optind) {
|
|
|
|
search = argv[optind];
|
2015-11-06 15:53:55 +00:00
|
|
|
if (*search == 0)
|
2023-02-13 05:26:32 +00:00
|
|
|
usage(cmd, 1);
|
2013-10-24 14:42:56 +00:00
|
|
|
type = check_arg_type(search);
|
2014-11-27 02:01:35 +00:00
|
|
|
|
2013-11-15 11:25:35 +00:00
|
|
|
/*
|
2014-11-27 02:01:35 +00:00
|
|
|
* For search is a device:
|
|
|
|
* realpath do /dev/mapper/XX => /dev/dm-X
|
|
|
|
* which is required by BTRFS_SCAN_DEV
|
|
|
|
* For search is a mountpoint:
|
|
|
|
* realpath do /mnt/btrfs/ => /mnt/btrfs
|
|
|
|
* which shall be recognized by btrfs_scan_kernel()
|
2013-11-15 11:25:35 +00:00
|
|
|
*/
|
2014-12-23 19:34:52 +00:00
|
|
|
if (realpath(search, path))
|
|
|
|
search = path;
|
2014-11-27 02:01:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Needs special handling if input arg is block dev And if
|
|
|
|
* input arg is mount-point just print it right away
|
|
|
|
*/
|
|
|
|
if (type == BTRFS_ARG_BLKDEV && where != BTRFS_SCAN_LBLKID) {
|
|
|
|
ret = get_btrfs_mount(search, mp, sizeof(mp));
|
|
|
|
if (!ret) {
|
|
|
|
/* given block dev is mounted */
|
|
|
|
search = mp;
|
|
|
|
type = BTRFS_ARG_MNTPOINT;
|
2013-10-24 14:42:56 +00:00
|
|
|
} else {
|
2014-11-27 02:01:35 +00:00
|
|
|
ret = dev_to_fsid(search, fsid);
|
|
|
|
if (ret) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("no btrfs on %s", search);
|
2014-11-27 02:01:35 +00:00
|
|
|
return 1;
|
2013-11-15 11:25:35 +00:00
|
|
|
}
|
2014-11-27 02:01:35 +00:00
|
|
|
uuid_unparse(fsid, uuid_buf);
|
|
|
|
search = uuid_buf;
|
|
|
|
type = BTRFS_ARG_UUID;
|
|
|
|
goto devs_only;
|
2013-10-24 14:42:56 +00:00
|
|
|
}
|
2013-10-22 05:53:22 +00:00
|
|
|
}
|
2013-10-08 03:41:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 01:21:21 +00:00
|
|
|
if (where == BTRFS_SCAN_LBLKID)
|
2013-10-08 03:41:38 +00:00
|
|
|
goto devs_only;
|
|
|
|
|
|
|
|
/* show mounted btrfs */
|
2015-06-18 06:46:11 +00:00
|
|
|
ret = btrfs_scan_kernel(search, unit_mode);
|
2014-01-14 11:17:07 +00:00
|
|
|
if (search && !ret) {
|
|
|
|
/* since search is found we are done */
|
|
|
|
goto out;
|
|
|
|
}
|
2013-10-08 03:41:38 +00:00
|
|
|
|
|
|
|
/* shows mounted only */
|
|
|
|
if (where == BTRFS_SCAN_MOUNTED)
|
|
|
|
goto out;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2013-10-08 03:41:38 +00:00
|
|
|
devs_only:
|
2019-05-16 13:12:49 +00:00
|
|
|
if (type == BTRFS_ARG_REG) {
|
2021-11-10 20:07:55 +00:00
|
|
|
root = open_ctree(search, btrfs_sb_offset(0), 0);
|
|
|
|
if (root)
|
2019-05-16 13:12:49 +00:00
|
|
|
ret = 0;
|
|
|
|
else
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
2019-11-25 10:39:15 +00:00
|
|
|
ret = btrfs_scan_devices(0);
|
2019-05-16 13:12:49 +00:00
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2013-10-08 03:41:38 +00:00
|
|
|
if (ret) {
|
2016-10-28 16:47:35 +00:00
|
|
|
error("blkid device scan returned %d", ret);
|
2021-11-10 20:07:55 +00:00
|
|
|
goto out;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
2013-10-08 03:41:38 +00:00
|
|
|
|
2021-09-30 12:18:55 +00:00
|
|
|
/*
|
|
|
|
* The seed/sprout mappings are not detected yet, do mapping build for
|
|
|
|
* all umounted filesystems. But first, copy all unmounted UUIDs only
|
|
|
|
* to all_uuids.
|
|
|
|
*/
|
2015-01-14 06:50:40 +00:00
|
|
|
ret = search_umounted_fs_uuids(&all_uuids, search, &found);
|
|
|
|
if (ret < 0) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("searching target device returned error %d", ret);
|
2021-11-10 20:07:55 +00:00
|
|
|
goto out;
|
2014-10-30 02:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = map_seed_devices(&all_uuids);
|
2014-10-06 10:16:46 +00:00
|
|
|
if (ret) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("mapping seed devices returned error %d", ret);
|
2021-11-10 20:07:55 +00:00
|
|
|
goto out;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
2014-10-06 10:16:46 +00:00
|
|
|
|
2023-08-14 15:28:07 +00:00
|
|
|
list_for_each_entry(fs_devices, &all_uuids, fs_list)
|
2015-06-18 06:46:11 +00:00
|
|
|
print_one_uuid(fs_devices, unit_mode);
|
2014-10-06 10:16:46 +00:00
|
|
|
|
2016-06-24 08:24:47 +00:00
|
|
|
if (search && !found) {
|
|
|
|
error("not a valid btrfs filesystem: %s", search);
|
2014-01-14 11:17:07 +00:00
|
|
|
ret = 1;
|
2016-06-24 08:24:47 +00:00
|
|
|
}
|
2014-10-06 10:16:46 +00:00
|
|
|
while (!list_empty(&all_uuids)) {
|
|
|
|
fs_devices = list_entry(all_uuids.next,
|
2023-08-14 15:28:07 +00:00
|
|
|
struct btrfs_fs_devices, fs_list);
|
2014-10-06 10:16:46 +00:00
|
|
|
free_fs_devices(fs_devices);
|
2014-03-14 03:28:11 +00:00
|
|
|
}
|
2013-10-08 03:41:38 +00:00
|
|
|
out:
|
2021-11-10 20:07:55 +00:00
|
|
|
if (root)
|
|
|
|
close_ctree(root);
|
2017-09-27 02:02:19 +00:00
|
|
|
free_seen_fsid(seen_fsid_hash);
|
2021-11-10 20:07:55 +00:00
|
|
|
return !!ret;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(filesystem_show, "show");
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_filesystem_sync_usage[] = {
|
2012-02-03 19:00:17 +00:00
|
|
|
"btrfs filesystem sync <path>",
|
|
|
|
"Force a sync on a filesystem",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_filesystem_sync(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2018-01-25 09:35:27 +00:00
|
|
|
enum btrfs_util_error err;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2018-03-07 02:42:01 +00:00
|
|
|
clean_args_no_options(cmd, argc, argv);
|
2016-03-01 15:02:08 +00:00
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2018-01-25 09:35:27 +00:00
|
|
|
err = btrfs_util_sync(argv[optind]);
|
|
|
|
if (err) {
|
|
|
|
error_btrfs_util(err);
|
2013-09-04 15:22:23 +00:00
|
|
|
return 1;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(filesystem_sync, "sync");
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2021-09-06 14:06:55 +00:00
|
|
|
static int parse_compress_type_arg(char *s)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2021-09-06 14:06:55 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = parse_compress_type(s);
|
2021-09-18 15:44:57 +00:00
|
|
|
if (ret < 0) {
|
2021-09-06 14:06:55 +00:00
|
|
|
error("unknown compression type: %s", s);
|
2012-02-03 19:00:17 +00:00
|
|
|
exit(1);
|
2021-09-06 14:06:55 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_filesystem_defrag_usage[] = {
|
2012-02-03 19:00:17 +00:00
|
|
|
"btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
|
|
|
|
"Defragment a file or a directory",
|
|
|
|
"",
|
2023-02-22 00:37:18 +00:00
|
|
|
OPTLINE("-r", "defragment files recursively"),
|
|
|
|
OPTLINE("-c[zlib,lzo,zstd]", "compress the file while defragmenting, optional parameter (no space in between)"),
|
|
|
|
OPTLINE("-f", "flush data to disk immediately after defragmenting"),
|
|
|
|
OPTLINE("-s start", "defragment only from byte onward"),
|
|
|
|
OPTLINE("-l len", "defragment only up to len bytes"),
|
|
|
|
OPTLINE("-t size", "target extent size hint (default: 32M)"),
|
|
|
|
OPTLINE("-v", "deprecated, alias for global -v option"),
|
2019-11-25 10:39:07 +00:00
|
|
|
HELPINFO_INSERT_GLOBALS,
|
|
|
|
HELPINFO_INSERT_VERBOSE,
|
2020-06-29 14:57:54 +00:00
|
|
|
"",
|
2017-10-24 22:47:45 +00:00
|
|
|
"Warning: most Linux kernels will break up the ref-links of COW data",
|
|
|
|
"(e.g., files copied with 'cp --reflink', snapshots) which may cause",
|
|
|
|
"considerable increase of space usage. See btrfs-filesystem(8) for",
|
|
|
|
"more information.",
|
2012-02-03 19:00:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2013-09-23 19:18:17 +00:00
|
|
|
static struct btrfs_ioctl_defrag_range_args defrag_global_range;
|
|
|
|
static int defrag_global_errors;
|
|
|
|
static int defrag_callback(const char *fpath, const struct stat *sb,
|
|
|
|
int typeflag, struct FTW *ftwbuf)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
int fd = 0;
|
|
|
|
|
2014-01-09 22:47:14 +00:00
|
|
|
if ((typeflag == FTW_F) && S_ISREG(sb->st_mode)) {
|
2022-11-23 18:31:41 +00:00
|
|
|
pr_verbose(LOG_INFO, "%s\n", fpath);
|
2019-02-25 18:16:44 +00:00
|
|
|
fd = open(fpath, defrag_open_mode);
|
2016-09-20 14:45:36 +00:00
|
|
|
if (fd < 0) {
|
2013-09-23 19:18:17 +00:00
|
|
|
goto error;
|
2016-09-20 14:45:36 +00:00
|
|
|
}
|
2017-01-12 14:12:09 +00:00
|
|
|
ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &defrag_global_range);
|
2013-09-23 19:18:17 +00:00
|
|
|
close(fd);
|
2017-01-12 14:12:09 +00:00
|
|
|
if (ret && errno == ENOTTY) {
|
|
|
|
error(
|
|
|
|
"defrag range ioctl not supported in this kernel version, 2.6.33 and newer is required");
|
2013-09-23 19:18:17 +00:00
|
|
|
defrag_global_errors++;
|
|
|
|
return ENOTTY;
|
|
|
|
}
|
2016-09-20 14:45:36 +00:00
|
|
|
if (ret) {
|
2013-09-23 19:18:17 +00:00
|
|
|
goto error;
|
2016-09-20 14:45:36 +00:00
|
|
|
}
|
2013-09-23 19:18:17 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
2018-01-07 21:54:21 +00:00
|
|
|
error("defrag failed on %s: %m", fpath);
|
2013-09-23 19:18:17 +00:00
|
|
|
defrag_global_errors++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_filesystem_defrag(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2022-10-05 18:02:13 +00:00
|
|
|
bool flush = false;
|
2012-02-03 19:00:17 +00:00
|
|
|
u64 start = 0;
|
|
|
|
u64 len = (u64)-1;
|
2016-07-28 11:14:08 +00:00
|
|
|
u64 thresh;
|
2012-02-03 19:00:17 +00:00
|
|
|
int i;
|
2022-10-05 18:02:13 +00:00
|
|
|
bool recursive = false;
|
2012-02-03 19:00:17 +00:00
|
|
|
int ret = 0;
|
|
|
|
int compress_type = BTRFS_COMPRESS_NONE;
|
2013-09-23 19:18:17 +00:00
|
|
|
DIR *dirstream;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2019-02-25 18:16:44 +00:00
|
|
|
/*
|
|
|
|
* Kernel 4.19+ supports defragmention of files open read-only,
|
|
|
|
* otherwise it's an ETXTBSY error
|
|
|
|
*/
|
|
|
|
if (get_running_kernel_version() < KERNEL_VERSION(4,19,0))
|
|
|
|
defrag_open_mode = O_RDWR;
|
|
|
|
|
2016-07-28 11:14:08 +00:00
|
|
|
/*
|
|
|
|
* Kernel has a different default (256K) that is supposed to be safe,
|
|
|
|
* but it does not defragment very well. The 32M will likely lead to
|
2017-01-12 14:00:47 +00:00
|
|
|
* better results and is independent of the kernel default. We have to
|
|
|
|
* use the v2 defrag ioctl.
|
2016-07-28 11:14:08 +00:00
|
|
|
*/
|
2017-01-24 03:03:05 +00:00
|
|
|
thresh = SZ_32M;
|
2016-07-28 11:14:08 +00:00
|
|
|
|
2022-11-23 18:31:41 +00:00
|
|
|
/*
|
|
|
|
* Workaround to emulate previous behaviour, the log level has to be
|
|
|
|
* adjusted:
|
|
|
|
*
|
|
|
|
* - btrfs fi defrag - no file names printed (LOG_DEFAULT)
|
|
|
|
* - btrfs fi defrag -v - filenames printed (LOG_INFO)
|
|
|
|
* - btrfs -v fi defrag - filenames printed (LOG_INFO)
|
|
|
|
* - btrfs -v fi defrag -v - filenames printed (LOG_VERBOSE)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (bconf.verbose != BTRFS_BCONF_UNSET)
|
|
|
|
bconf.verbose++;
|
|
|
|
|
2013-09-23 19:18:17 +00:00
|
|
|
defrag_global_errors = 0;
|
|
|
|
defrag_global_errors = 0;
|
2018-06-20 00:38:38 +00:00
|
|
|
optind = 0;
|
2012-02-03 19:00:17 +00:00
|
|
|
while(1) {
|
2013-09-23 19:18:17 +00:00
|
|
|
int c = getopt(argc, argv, "vrc::fs:l:t:");
|
2012-02-03 19:00:17 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
switch(c) {
|
|
|
|
case 'c':
|
|
|
|
compress_type = BTRFS_COMPRESS_ZLIB;
|
|
|
|
if (optarg)
|
2021-09-06 14:06:55 +00:00
|
|
|
compress_type = parse_compress_type_arg(optarg);
|
2012-02-03 19:00:17 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
2022-10-05 18:02:13 +00:00
|
|
|
flush = true;
|
2012-02-03 19:00:17 +00:00
|
|
|
break;
|
|
|
|
case 'v':
|
2022-11-23 18:31:41 +00:00
|
|
|
if (bconf.verbose == BTRFS_BCONF_UNSET)
|
|
|
|
bconf.verbose = LOG_INFO;
|
|
|
|
else
|
|
|
|
bconf_be_verbose();
|
2012-02-03 19:00:17 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2021-01-21 15:25:51 +00:00
|
|
|
start = parse_size_from_string(optarg);
|
2012-02-03 19:00:17 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
2021-01-21 15:25:51 +00:00
|
|
|
len = parse_size_from_string(optarg);
|
2012-02-03 19:00:17 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2021-01-21 15:25:51 +00:00
|
|
|
thresh = parse_size_from_string(optarg);
|
2015-06-25 17:36:06 +00:00
|
|
|
if (thresh > (u32)-1) {
|
2016-01-11 10:01:47 +00:00
|
|
|
warning(
|
|
|
|
"target extent size %llu too big, trimmed to %u",
|
2015-06-25 17:36:06 +00:00
|
|
|
thresh, (u32)-1);
|
2015-07-24 08:35:02 +00:00
|
|
|
thresh = (u32)-1;
|
2015-06-25 17:36:06 +00:00
|
|
|
}
|
2013-09-23 19:18:17 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
2022-10-05 18:02:13 +00:00
|
|
|
recursive = true;
|
2012-02-03 19:00:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-03-07 02:54:38 +00:00
|
|
|
usage_unknown_option(cmd, argv);
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
if (check_argc_min(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-07-24 10:21:02 +00:00
|
|
|
memset(&defrag_global_range, 0, sizeof(defrag_global_range));
|
2013-09-23 19:18:17 +00:00
|
|
|
defrag_global_range.start = start;
|
|
|
|
defrag_global_range.len = len;
|
2015-06-25 17:36:06 +00:00
|
|
|
defrag_global_range.extent_thresh = (u32)thresh;
|
2012-02-03 19:00:17 +00:00
|
|
|
if (compress_type) {
|
2013-09-23 19:18:17 +00:00
|
|
|
defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
|
|
|
|
defrag_global_range.compress_type = compress_type;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
if (flush)
|
2013-09-23 19:18:17 +00:00
|
|
|
defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2016-12-12 17:38:47 +00:00
|
|
|
/*
|
|
|
|
* Look for directory arguments and warn if the recursive mode is not
|
|
|
|
* requested, as this is not implemented as recursive defragmentation
|
|
|
|
* in kernel. The stat errors are silent here as we check them below.
|
|
|
|
*/
|
|
|
|
if (!recursive) {
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
for (i = optind; i < argc; i++) {
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(argv[i], &st))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
warning(
|
|
|
|
"directory specified but recursive mode not requested: %s",
|
|
|
|
argv[i]);
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
warning(
|
|
|
|
"a directory passed to the defrag ioctl will not process the files\n"
|
|
|
|
"recursively but will defragment the subvolume tree and the extent tree.\n"
|
|
|
|
"If this is not intended, please use option -r .");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
for (i = optind; i < argc; i++) {
|
2014-01-09 22:47:14 +00:00
|
|
|
struct stat st;
|
2016-12-12 17:22:10 +00:00
|
|
|
int defrag_err = 0;
|
2014-01-09 22:47:14 +00:00
|
|
|
|
2013-09-23 19:18:17 +00:00
|
|
|
dirstream = NULL;
|
2019-02-25 18:16:44 +00:00
|
|
|
fd = open_file_or_dir3(argv[i], &dirstream, defrag_open_mode);
|
2012-02-03 19:00:17 +00:00
|
|
|
if (fd < 0) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("cannot open %s: %m", argv[i]);
|
2017-11-28 09:14:48 +00:00
|
|
|
ret = -errno;
|
|
|
|
goto next;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
2017-11-28 09:14:48 +00:00
|
|
|
|
|
|
|
ret = fstat(fd, &st);
|
|
|
|
if (ret) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("failed to stat %s: %m", argv[i]);
|
2017-11-28 09:14:48 +00:00
|
|
|
ret = -errno;
|
|
|
|
goto next;
|
2014-01-09 22:47:14 +00:00
|
|
|
}
|
|
|
|
if (!(S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))) {
|
2016-10-28 16:47:35 +00:00
|
|
|
error("%s is not a directory or a regular file",
|
2016-01-11 10:01:47 +00:00
|
|
|
argv[i]);
|
2017-11-28 09:14:48 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto next;
|
2014-01-09 22:47:14 +00:00
|
|
|
}
|
2016-12-06 04:39:38 +00:00
|
|
|
if (recursive && S_ISDIR(st.st_mode)) {
|
|
|
|
ret = nftw(argv[i], defrag_callback, 10,
|
2013-09-23 19:18:17 +00:00
|
|
|
FTW_MOUNT | FTW_PHYS);
|
2016-12-06 04:39:38 +00:00
|
|
|
if (ret == ENOTTY)
|
|
|
|
exit(1);
|
|
|
|
/* errors are handled in the callback */
|
|
|
|
ret = 0;
|
2013-09-23 19:18:17 +00:00
|
|
|
} else {
|
2022-11-23 18:31:41 +00:00
|
|
|
pr_verbose(LOG_INFO, "%s\n", argv[i]);
|
2017-01-12 14:12:09 +00:00
|
|
|
ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE,
|
2013-09-23 19:18:17 +00:00
|
|
|
&defrag_global_range);
|
2016-12-12 17:22:10 +00:00
|
|
|
defrag_err = errno;
|
2017-11-28 09:14:48 +00:00
|
|
|
if (ret && defrag_err == ENOTTY) {
|
|
|
|
error(
|
2017-01-12 14:12:09 +00:00
|
|
|
"defrag range ioctl not supported in this kernel version, 2.6.33 and newer is required");
|
2017-11-28 09:14:48 +00:00
|
|
|
defrag_global_errors++;
|
|
|
|
close_file_or_dir(fd, dirstream);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret) {
|
2018-10-25 12:10:54 +00:00
|
|
|
errno = defrag_err;
|
|
|
|
error("defrag failed on %s: %m", argv[i]);
|
2017-11-28 09:14:48 +00:00
|
|
|
goto next;
|
|
|
|
}
|
2013-09-23 19:18:17 +00:00
|
|
|
}
|
2017-11-28 09:14:48 +00:00
|
|
|
next:
|
|
|
|
if (ret)
|
2013-09-23 19:18:17 +00:00
|
|
|
defrag_global_errors++;
|
2017-11-28 09:14:48 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
2017-11-28 09:14:48 +00:00
|
|
|
|
2013-09-23 19:18:17 +00:00
|
|
|
if (defrag_global_errors)
|
2022-10-05 12:27:57 +00:00
|
|
|
pr_stderr(LOG_DEFAULT, "total %d failures\n", defrag_global_errors);
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2013-09-23 19:18:17 +00:00
|
|
|
return !!defrag_global_errors;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(filesystem_defrag, "defragment");
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_filesystem_resize_usage[] = {
|
2020-11-05 02:38:00 +00:00
|
|
|
"btrfs filesystem resize [options] [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>",
|
2012-02-03 19:00:17 +00:00
|
|
|
"Resize a filesystem",
|
|
|
|
"If 'max' is passed, the filesystem will occupy all available space",
|
2012-05-26 17:57:55 +00:00
|
|
|
"on the device 'devid'.",
|
2014-04-01 10:01:10 +00:00
|
|
|
"[kK] means KiB, which denotes 1KiB = 1024B, 1MiB = 1024KiB, etc.",
|
2020-11-05 02:38:00 +00:00
|
|
|
"",
|
2023-02-22 00:37:18 +00:00
|
|
|
OPTLINE("--enqueue", "wait if there's another exclusive operation running, otherwise continue"),
|
2012-02-03 19:00:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2021-02-20 12:41:17 +00:00
|
|
|
static int check_resize_args(const char *amount, const char *path) {
|
|
|
|
struct btrfs_ioctl_fs_info_args fi_args;
|
|
|
|
struct btrfs_ioctl_dev_info_args *di_args = NULL;
|
|
|
|
int ret, i, dev_idx = -1;
|
|
|
|
u64 devid = 1;
|
|
|
|
const char *res_str = NULL;
|
|
|
|
char *devstr = NULL, *sizestr = NULL;
|
|
|
|
u64 new_size = 0, old_size = 0, diff = 0;
|
|
|
|
int mod = 0;
|
|
|
|
char amount_dup[BTRFS_VOL_NAME_MAX];
|
|
|
|
|
|
|
|
ret = get_fs_info(path, &fi_args, &di_args);
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
error("unable to retrieve fs info");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fi_args.num_devices) {
|
|
|
|
error("no devices found");
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = snprintf(amount_dup, BTRFS_VOL_NAME_MAX, "%s", amount);
|
|
|
|
if (strlen(amount) != ret) {
|
|
|
|
error("newsize argument is too long");
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
sizestr = amount_dup;
|
|
|
|
devstr = strchr(sizestr, ':');
|
|
|
|
if (devstr) {
|
|
|
|
sizestr = devstr + 1;
|
|
|
|
*devstr = 0;
|
|
|
|
devstr = amount_dup;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
devid = strtoull(devstr, NULL, 10);
|
|
|
|
|
|
|
|
if (errno) {
|
|
|
|
error("failed to parse devid %s: %m", devstr);
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_idx = -1;
|
|
|
|
for(i = 0; i < fi_args.num_devices; i++) {
|
|
|
|
if (di_args[i].devid == devid) {
|
|
|
|
dev_idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev_idx < 0) {
|
|
|
|
error("cannot find devid: %lld", devid);
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(sizestr, "max") == 0) {
|
|
|
|
res_str = "max";
|
2021-05-20 20:14:18 +00:00
|
|
|
} else if (strcmp(sizestr, "cancel") == 0) {
|
|
|
|
/* Different format, print and exit */
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "Request to cancel resize\n");
|
2021-05-20 20:14:18 +00:00
|
|
|
goto out;
|
2021-02-20 12:41:17 +00:00
|
|
|
} else {
|
|
|
|
if (sizestr[0] == '-') {
|
|
|
|
mod = -1;
|
|
|
|
sizestr++;
|
|
|
|
} else if (sizestr[0] == '+') {
|
|
|
|
mod = 1;
|
|
|
|
sizestr++;
|
|
|
|
}
|
|
|
|
diff = parse_size_from_string(sizestr);
|
|
|
|
if (!diff) {
|
|
|
|
error("failed to parse size %s", sizestr);
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
old_size = di_args[dev_idx].total_bytes;
|
|
|
|
|
2021-04-19 13:05:49 +00:00
|
|
|
/* For target sizes without +/- sign prefix (e.g. 1:150g) */
|
|
|
|
if (mod == 0) {
|
|
|
|
new_size = diff;
|
|
|
|
} else if (mod < 0) {
|
2021-02-20 12:41:17 +00:00
|
|
|
if (diff > old_size) {
|
|
|
|
error("current size is %s which is smaller than %s",
|
|
|
|
pretty_size_mode(old_size, UNITS_DEFAULT),
|
|
|
|
pretty_size_mode(diff, UNITS_DEFAULT));
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
new_size = old_size - diff;
|
|
|
|
} else if (mod > 0) {
|
|
|
|
if (diff > ULLONG_MAX - old_size) {
|
|
|
|
error("increasing %s is out of range",
|
|
|
|
pretty_size_mode(diff, UNITS_DEFAULT));
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
new_size = old_size + diff;
|
|
|
|
}
|
|
|
|
new_size = round_down(new_size, fi_args.sectorsize);
|
|
|
|
res_str = pretty_size_mode(new_size, UNITS_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "Resize device id %lld (%s) from %s to %s\n", devid,
|
2021-02-20 12:41:17 +00:00
|
|
|
di_args[dev_idx].path,
|
|
|
|
pretty_size_mode(di_args[dev_idx].total_bytes, UNITS_DEFAULT),
|
|
|
|
res_str);
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(di_args);
|
2022-11-09 17:59:12 +00:00
|
|
|
return 0;
|
2021-02-20 12:41:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_filesystem_resize(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2012-02-03 19:00:17 +00:00
|
|
|
struct btrfs_ioctl_vol_args args;
|
|
|
|
int fd, res, len, e;
|
2012-02-03 19:00:17 +00:00
|
|
|
char *amount, *path;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2020-11-05 02:38:00 +00:00
|
|
|
int ret;
|
|
|
|
bool enqueue = false;
|
2021-05-20 20:14:18 +00:00
|
|
|
bool cancel = false;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2020-11-05 02:38:00 +00:00
|
|
|
/*
|
|
|
|
* Simplified option parser, accept only long options, the resize value
|
|
|
|
* could be negative and is recognized as short options by getopt
|
|
|
|
*/
|
|
|
|
for (optind = 1; optind < argc; optind++) {
|
|
|
|
if (strcmp(argv[optind], "--enqueue") == 0) {
|
|
|
|
enqueue = true;
|
|
|
|
} else if (strcmp(argv[optind], "--") == 0) {
|
|
|
|
/* Separator: options -- non-options */
|
|
|
|
} else if (strncmp(argv[optind], "--", 2) == 0) {
|
2022-01-13 16:24:52 +00:00
|
|
|
/* Emulate what getopt does on unknown option */
|
2020-11-05 02:38:00 +00:00
|
|
|
optind++;
|
|
|
|
usage_unknown_option(cmd, argv);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 15:02:08 +00:00
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 2))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
amount = argv[optind];
|
|
|
|
path = argv[optind + 1];
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
len = strlen(amount);
|
|
|
|
if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("resize value too long (%s)", amount);
|
2013-09-04 15:22:23 +00:00
|
|
|
return 1;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:14:18 +00:00
|
|
|
cancel = (strcmp("cancel", amount) == 0);
|
|
|
|
|
2015-10-12 13:22:55 +00:00
|
|
|
fd = btrfs_open_dir(path, &dirstream, 1);
|
2021-01-25 10:43:58 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
/* The path is a directory */
|
|
|
|
if (fd == -3) {
|
|
|
|
error(
|
|
|
|
"resize works on mounted filesystems and accepts only\n"
|
|
|
|
"directories as argument. Passing file containing a btrfs image\n"
|
|
|
|
"would resize the underlying filesystem instead of the image.\n");
|
|
|
|
}
|
2013-09-04 15:22:23 +00:00
|
|
|
return 1;
|
2021-01-25 10:43:58 +00:00
|
|
|
}
|
2013-01-23 23:11:24 +00:00
|
|
|
|
2021-05-20 20:14:18 +00:00
|
|
|
/*
|
|
|
|
* Check if there's an exclusive operation running if possible, otherwise
|
|
|
|
* let kernel handle it. Cancel request is completely handled in kernel
|
|
|
|
* so make it pass.
|
|
|
|
*/
|
|
|
|
if (!cancel) {
|
|
|
|
ret = check_running_fs_exclop(fd, BTRFS_EXCLOP_RESIZE, enqueue);
|
|
|
|
if (ret != 0) {
|
|
|
|
if (ret < 0)
|
|
|
|
error(
|
|
|
|
"unable to check status of exclusive operation: %m");
|
|
|
|
close_file_or_dir(fd, dirstream);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-08-25 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 12:41:17 +00:00
|
|
|
ret = check_resize_args(amount, path);
|
|
|
|
if (ret != 0) {
|
|
|
|
close_file_or_dir(fd, dirstream);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-12 12:36:51 +00:00
|
|
|
memset(&args, 0, sizeof(args));
|
2013-01-25 19:27:47 +00:00
|
|
|
strncpy_null(args.name, amount);
|
2012-02-03 19:00:17 +00:00
|
|
|
res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
|
|
|
|
e = errno;
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
2012-02-03 19:00:17 +00:00
|
|
|
if( res < 0 ){
|
2015-07-22 07:58:06 +00:00
|
|
|
switch (e) {
|
|
|
|
case EFBIG:
|
2016-01-11 10:01:47 +00:00
|
|
|
error("unable to resize '%s': no enough free space",
|
2015-07-22 07:58:06 +00:00
|
|
|
path);
|
|
|
|
break;
|
|
|
|
default:
|
2018-01-07 21:54:21 +00:00
|
|
|
error("unable to resize '%s': %m", path);
|
2015-07-22 07:58:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-09-04 15:22:23 +00:00
|
|
|
return 1;
|
2015-04-22 03:38:47 +00:00
|
|
|
} else if (res > 0) {
|
|
|
|
const char *err_str = btrfs_err_str(res);
|
|
|
|
|
|
|
|
if (err_str) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("resizing of '%s' failed: %s", path, err_str);
|
2015-04-22 03:38:47 +00:00
|
|
|
} else {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("resizing of '%s' failed: unknown error %d",
|
2015-04-22 03:38:47 +00:00
|
|
|
path, res);
|
|
|
|
}
|
|
|
|
return 1;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(filesystem_resize, "resize");
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-07-10 22:05:05 +00:00
|
|
|
static const char * const cmd_filesystem_label_usage[] = {
|
2013-07-23 02:43:21 +00:00
|
|
|
"btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
|
2012-12-17 11:35:29 +00:00
|
|
|
"Get or change the label of a filesystem",
|
2012-02-03 19:00:17 +00:00
|
|
|
"With one argument, get the label of filesystem on <device>.",
|
|
|
|
"If <newlabel> is passed, set the filesystem label to <newlabel>.",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_filesystem_label(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2018-03-07 02:42:01 +00:00
|
|
|
clean_args_no_options(cmd, argc, argv);
|
2016-03-01 15:02:08 +00:00
|
|
|
|
2016-03-14 00:15:42 +00:00
|
|
|
if (check_argc_min(argc - optind, 1) ||
|
|
|
|
check_argc_max(argc - optind, 2))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2016-03-14 00:15:42 +00:00
|
|
|
if (argc - optind > 1) {
|
2016-03-01 15:02:08 +00:00
|
|
|
return set_label(argv[optind], argv[optind + 1]);
|
2013-11-12 13:41:42 +00:00
|
|
|
} else {
|
|
|
|
char label[BTRFS_LABEL_SIZE];
|
|
|
|
int ret;
|
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
ret = get_label(argv[optind], label);
|
2013-11-12 13:41:42 +00:00
|
|
|
if (!ret)
|
2022-10-05 12:27:57 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "%s\n", label);
|
2013-11-12 13:41:42 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(filesystem_label, "label");
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2018-03-07 04:19:23 +00:00
|
|
|
static const char * const cmd_filesystem_balance_usage[] = {
|
|
|
|
"btrfs filesystem balance [args...] (alias of \"btrfs balance\")",
|
|
|
|
"Please see \"btrfs balance --help\" for more information.",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_filesystem_balance(const struct cmd_struct *unused,
|
|
|
|
int argc, char **argv)
|
2018-03-07 04:19:23 +00:00
|
|
|
{
|
2018-03-07 02:07:03 +00:00
|
|
|
return cmd_execute(&cmd_struct_balance, argc, argv);
|
2018-03-07 04:19:23 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
/*
|
|
|
|
* Compatible old "btrfs filesystem balance" command
|
|
|
|
*
|
|
|
|
* We can't use cmd_struct_balance directly here since this alias is
|
|
|
|
* for historical compatibility and is hidden.
|
|
|
|
*/
|
|
|
|
static DEFINE_COMMAND(filesystem_balance, "balance", cmd_filesystem_balance,
|
|
|
|
cmd_filesystem_balance_usage, NULL, CMD_HIDDEN);
|
|
|
|
|
2022-11-07 20:10:42 +00:00
|
|
|
static const char * const cmd_filesystem_mkswapfile_usage[] = {
|
|
|
|
"btrfs filesystem mkswapfile <file>",
|
2022-12-06 19:59:46 +00:00
|
|
|
"Create a new file that's suitable and formatted as a swapfile.",
|
|
|
|
"Create a new file that's suitable and formatted as a swapfile. Default",
|
|
|
|
"size is 2GiB, minimum size is 40KiB.",
|
|
|
|
"",
|
2023-02-22 00:37:18 +00:00
|
|
|
OPTLINE("-s|--size SIZE", "create file of SIZE (accepting k/m/g/e/p suffix)"),
|
|
|
|
OPTLINE("-U|--uuid UUID", "specify UUID to use, or a special value: clear (all zeros), random, time (time-based random)"),
|
2022-11-07 20:10:42 +00:00
|
|
|
HELPINFO_INSERT_GLOBALS,
|
|
|
|
HELPINFO_INSERT_VERBOSE,
|
|
|
|
HELPINFO_INSERT_QUIET,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2023-01-09 17:22:56 +00:00
|
|
|
* Swap signature in the first 4KiB, v2, no label:
|
2022-11-07 20:10:42 +00:00
|
|
|
*
|
|
|
|
* 00000400 .. = 01 00 00 00 ff ff 03 00 00 00 00 00 cb 70 8e 60
|
2023-01-09 17:22:56 +00:00
|
|
|
* ^^^^^^^^^^^ ^^^^^^^^^^^
|
|
|
|
* page count 4B uuid 4B
|
2022-11-07 20:10:42 +00:00
|
|
|
* 00000420 .. = 1d fb 4e ca be d4 3f 1f 6a 6b 0c 03 00 00 00 00
|
|
|
|
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
* uuid 8B
|
|
|
|
* 00000ff0 .. = 00 00 00 00 00 00 53 57 41 50 53 50 41 43 45 32
|
|
|
|
* S W A P S P A C E 2
|
|
|
|
*/
|
2023-02-21 00:20:52 +00:00
|
|
|
static int write_swap_signature(int fd, u32 page_count, const uuid_t uuid)
|
2022-11-07 20:10:42 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2023-01-09 17:22:56 +00:00
|
|
|
static unsigned char swap[SZ_4K] = {
|
2022-11-07 20:10:42 +00:00
|
|
|
[0x400] = 0x01,
|
2023-01-09 17:22:56 +00:00
|
|
|
/* 0x404 .. 0x407 number of pages (little-endian) */
|
|
|
|
/* 0x408 .. 0x40b number of bad pages (unused) */
|
2022-11-07 20:10:42 +00:00
|
|
|
/* 0x40c .. 0x42b UUID */
|
2023-01-09 17:22:56 +00:00
|
|
|
/* Last bytes of the page */
|
2022-11-07 20:10:42 +00:00
|
|
|
[0xff6] = 'S',
|
|
|
|
[0xff7] = 'W',
|
|
|
|
[0xff8] = 'A',
|
|
|
|
[0xff9] = 'P',
|
|
|
|
[0xffa] = 'S',
|
|
|
|
[0xffb] = 'P',
|
|
|
|
[0xffc] = 'A',
|
|
|
|
[0xffd] = 'C',
|
|
|
|
[0xffe] = 'E',
|
|
|
|
[0xfff] = '2',
|
|
|
|
};
|
2023-01-09 17:22:56 +00:00
|
|
|
u32 *pages = (u32 *)&swap[0x404];
|
2022-11-07 20:10:42 +00:00
|
|
|
|
2023-01-09 17:22:56 +00:00
|
|
|
*pages = cpu_to_le32(page_count);
|
2023-02-21 00:20:52 +00:00
|
|
|
memcpy(&swap[0x40c], uuid, 16);
|
2023-01-09 17:22:56 +00:00
|
|
|
ret = pwrite(fd, swap, SZ_4K, 0);
|
2022-11-07 20:10:42 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cmd_filesystem_mkswapfile(const struct cmd_struct *cmd, int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
const char *fname;
|
|
|
|
unsigned long flags;
|
|
|
|
u64 size = SZ_2G;
|
2023-01-09 17:22:56 +00:00
|
|
|
u64 page_count;
|
2023-02-21 00:20:52 +00:00
|
|
|
uuid_t uuid;
|
2022-11-07 20:10:42 +00:00
|
|
|
|
2023-02-21 00:20:52 +00:00
|
|
|
uuid_generate(uuid);
|
2022-11-07 20:10:42 +00:00
|
|
|
optind = 0;
|
|
|
|
while (1) {
|
|
|
|
int c;
|
|
|
|
static const struct option long_options[] = {
|
|
|
|
{ "size", required_argument, NULL, 's' },
|
2023-02-21 00:20:52 +00:00
|
|
|
{ "uuid", required_argument, NULL, 'U' },
|
2022-11-07 20:10:42 +00:00
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2023-02-21 00:20:52 +00:00
|
|
|
c = getopt_long(argc, argv, "s:U:", long_options, NULL);
|
2022-11-07 20:10:42 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 's':
|
|
|
|
size = parse_size_from_string(optarg);
|
|
|
|
/* Minimum limit reported by mkswap */
|
|
|
|
if (size < 40 * SZ_1K) {
|
|
|
|
error("swapfile needs to be at least 40 KiB");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2023-02-21 00:20:52 +00:00
|
|
|
case 'U':
|
|
|
|
if (strcmp(optarg, "clear") == 0) {
|
|
|
|
uuid_clear(uuid);
|
|
|
|
} else if (strcmp(optarg, "random") == 0) {
|
|
|
|
uuid_generate(uuid);
|
|
|
|
} else if (strcmp(optarg, "time") == 0) {
|
|
|
|
uuid_generate_time(uuid);
|
|
|
|
} else {
|
|
|
|
ret = uuid_parse(optarg, uuid);
|
|
|
|
if (ret == -1) {
|
|
|
|
error("UUID not recognized: %s", optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-11-07 20:10:42 +00:00
|
|
|
default:
|
|
|
|
usage_unknown_option(cmd, argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 1))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
fname = argv[optind];
|
|
|
|
pr_verbose(LOG_INFO, "create file %s with mode 0600\n", fname);
|
|
|
|
fd = open(fname, O_RDWR | O_CREAT | O_EXCL, 0600);
|
|
|
|
if (fd < 0) {
|
|
|
|
error("cannot create new swapfile: %m");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ret = ftruncate(fd, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
error("cannot truncate file: %m");
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
pr_verbose(LOG_INFO, "set NOCOW attribute\n");
|
|
|
|
flags = FS_NOCOW_FL;
|
|
|
|
ret = ioctl(fd, FS_IOC_SETFLAGS, &flags);
|
|
|
|
if (ret < 0) {
|
|
|
|
error("cannot set NOCOW flag: %m");
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
2023-01-09 17:22:56 +00:00
|
|
|
page_count = size / SZ_4K;
|
|
|
|
if (page_count <= 10) {
|
|
|
|
error("file too short");
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* First file page with header */
|
|
|
|
page_count--;
|
|
|
|
if (page_count > (u32)-1) {
|
|
|
|
error("file too big");
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
size = round_down(size, SZ_4K);
|
|
|
|
pr_verbose(LOG_INFO, "fallocate to size %llu, page size %u, %llu pages\n",
|
|
|
|
size, SZ_4K, page_count);
|
2022-11-07 20:10:42 +00:00
|
|
|
ret = fallocate(fd, 0, 0, size);
|
|
|
|
if (ret < 0) {
|
|
|
|
error("cannot fallocate file: %m");
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
pr_verbose(LOG_INFO, "write swap signature\n");
|
2023-02-21 00:20:52 +00:00
|
|
|
ret = write_swap_signature(fd, page_count, uuid);
|
2022-11-07 20:10:42 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
error("cannot write swap signature: %m");
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
pr_verbose(LOG_DEFAULT, "create swapfile %s size %s (%llu)\n",
|
|
|
|
fname, pretty_size_mode(size, UNITS_HUMAN), size);
|
|
|
|
out:
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static DEFINE_SIMPLE_COMMAND(filesystem_mkswapfile, "mkswapfile");
|
|
|
|
|
2015-06-09 12:22:05 +00:00
|
|
|
static const char filesystem_cmd_group_info[] =
|
|
|
|
"overall filesystem tasks and information";
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
static const struct cmd_group filesystem_cmd_group = {
|
2015-06-09 12:22:05 +00:00
|
|
|
filesystem_cmd_group_usage, filesystem_cmd_group_info, {
|
2018-03-07 02:07:03 +00:00
|
|
|
&cmd_struct_filesystem_df,
|
|
|
|
&cmd_struct_filesystem_du,
|
|
|
|
&cmd_struct_filesystem_show,
|
|
|
|
&cmd_struct_filesystem_sync,
|
|
|
|
&cmd_struct_filesystem_defrag,
|
|
|
|
&cmd_struct_filesystem_balance,
|
|
|
|
&cmd_struct_filesystem_resize,
|
|
|
|
&cmd_struct_filesystem_label,
|
|
|
|
&cmd_struct_filesystem_usage,
|
2022-11-07 20:10:42 +00:00
|
|
|
&cmd_struct_filesystem_mkswapfile,
|
2018-03-07 02:07:03 +00:00
|
|
|
NULL
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
DEFINE_GROUP_COMMAND_TOKEN(filesystem);
|