Btrfs-progs: add show subcommand to subvol cli

This adds show sub-command to the btrfs subvol cli
to display detailed inforamtion of the given subvol
or snapshot.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
This commit is contained in:
Anand Jain 2013-02-01 15:56:28 +08:00 committed by David Sterba
parent 16ef0a0c3c
commit 437eea9664
4 changed files with 182 additions and 7 deletions

View File

@ -1349,6 +1349,22 @@ static void print_subvolume_column(struct root_info *subv,
}
}
static void print_single_volume_info_raw(struct root_info *subv, char *raw_prefix)
{
int i;
for (i = 0; i < BTRFS_LIST_ALL; i++) {
if (!btrfs_list_columns[i].need_print)
continue;
if (raw_prefix)
printf("%s",raw_prefix);
print_subvolume_column(subv, i);
}
printf("\n");
}
static void print_single_volume_info_table(struct root_info *subv)
{
int i;
@ -1415,7 +1431,7 @@ static void print_all_volume_info_tab_head()
}
static void print_all_volume_info(struct root_lookup *sorted_tree,
int layout)
int layout, char *raw_prefix)
{
struct rb_node *n;
struct root_info *entry;
@ -1433,6 +1449,9 @@ static void print_all_volume_info(struct root_lookup *sorted_tree,
case BTRFS_LIST_LAYOUT_TABLE:
print_single_volume_info_table(entry);
break;
case BTRFS_LIST_LAYOUT_RAW:
print_single_volume_info_raw(entry, raw_prefix);
break;
}
n = rb_next(n);
}
@ -1459,7 +1478,7 @@ int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
struct btrfs_list_comparer_set *comp_set,
int layout, int full_path)
int layout, int full_path, char *raw_prefix)
{
struct root_lookup root_lookup;
struct root_lookup root_sort;
@ -1472,7 +1491,7 @@ int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
__filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
comp_set, top_id);
print_all_volume_info(&root_sort, layout);
print_all_volume_info(&root_sort, layout, raw_prefix);
__free_all_subvolumn(&root_lookup);
return 0;

View File

@ -20,6 +20,7 @@
#define BTRFS_LIST_LAYOUT_DEFAULT 0
#define BTRFS_LIST_LAYOUT_TABLE 1
#define BTRFS_LIST_LAYOUT_RAW 2
/*
* one of these for each root we find.
@ -151,7 +152,7 @@ int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
struct btrfs_list_comparer_set *comp_set,
int is_tab_result, int full_path);
int is_tab_result, int full_path, char *raw_prefix);
int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen);
int btrfs_list_get_default_subvolume(int fd, u64 *default_id);
char *btrfs_list_path_for_root(int fd, u64 root);

View File

@ -24,6 +24,7 @@
#include <libgen.h>
#include <limits.h>
#include <getopt.h>
#include <uuid/uuid.h>
#include "kerncompat.h"
#include "ioctl.h"
@ -431,11 +432,11 @@ static int cmd_subvol_list(int argc, char **argv)
if (is_tab_result)
ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
BTRFS_LIST_LAYOUT_TABLE,
!is_list_all && !is_only_in_path);
!is_list_all && !is_only_in_path, NULL);
else
ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
BTRFS_LIST_LAYOUT_DEFAULT,
!is_list_all && !is_only_in_path);
!is_list_all && !is_only_in_path, NULL);
if (ret)
return 19;
return 0;
@ -648,7 +649,7 @@ static int cmd_subvol_get_default(int argc, char **argv)
btrfs_list_setup_print_column(BTRFS_LIST_PATH);
ret = btrfs_list_subvols_print(fd, filter_set, NULL,
BTRFS_LIST_LAYOUT_DEFAULT, 1);
BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
if (ret)
return 19;
return 0;
@ -735,6 +736,153 @@ static int cmd_find_new(int argc, char **argv)
return 0;
}
static const char * const cmd_subvol_show_usage[] = {
"btrfs subvolume show <subvol-path>",
"Show more information of the subvolume",
NULL
};
static int cmd_subvol_show(int argc, char **argv)
{
struct root_info get_ri;
struct btrfs_list_filter_set *filter_set;
char tstr[256];
char uuidparse[37];
char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
char raw_prefix[] = "\t\t\t\t";
u64 sv_id, mntid;
int fd = -1, mntfd = -1;
int ret = -1;
if (check_argc_exact(argc, 2))
usage(cmd_subvol_show_usage);
fullpath = realpath(argv[1], 0);
if (!fullpath) {
fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
argv[1], strerror(errno));
goto out;
}
ret = test_issubvolume(fullpath);
if (ret < 0) {
fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
goto out;
}
if (!ret) {
fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
ret = -1;
goto out;
}
ret = find_mount_root(fullpath, &mnt);
if (ret < 0) {
fprintf(stderr, "ERROR: find_mount_root failed on %s: "
"%s\n", fullpath, strerror(-ret));
goto out;
}
ret = -1;
svpath = get_subvol_name(mnt, fullpath);
fd = open_file_or_dir(fullpath);
if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
goto out;
}
sv_id = btrfs_list_get_path_rootid(fd);
if (sv_id < 0) {
fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
fullpath);
goto out;
}
mntfd = open_file_or_dir(mnt);
if (mntfd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
goto out;
}
mntid = btrfs_list_get_path_rootid(mntfd);
if (mntid < 0) {
fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
goto out;
}
if (sv_id == BTRFS_FS_TREE_OBJECTID) {
printf("%s is btrfs root\n", fullpath);
goto out;
}
memset(&get_ri, 0, sizeof(get_ri));
get_ri.root_id = sv_id;
if (btrfs_get_subvol(mntfd, &get_ri)) {
fprintf(stderr, "ERROR: can't find '%s'\n",
svpath);
goto out;
}
ret = 0;
/* print the info */
printf("%s\n", fullpath);
printf("\tName: \t\t\t%s\n", get_ri.name);
if (uuid_is_null(get_ri.uuid))
strcpy(uuidparse, "-");
else
uuid_unparse(get_ri.uuid, uuidparse);
printf("\tuuid: \t\t\t%s\n", uuidparse);
if (uuid_is_null(get_ri.puuid))
strcpy(uuidparse, "-");
else
uuid_unparse(get_ri.puuid, uuidparse);
printf("\tParent uuid: \t\t%s\n", uuidparse);
if (get_ri.otime)
strftime(tstr, 256, "%Y-%m-%d %X",
localtime(&get_ri.otime));
else
strcpy(tstr, "-");
printf("\tCreation time: \t\t%s\n", tstr);
printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
printf("\tGen at creation: \t%llu\n", get_ri.ogen);
printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
/* print the snapshots of the given subvol if any*/
printf("\tSnapshot(s):\n");
filter_set = btrfs_list_alloc_filter_set();
btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
(u64)get_ri.uuid);
btrfs_list_setup_print_column(BTRFS_LIST_PATH);
btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
1, raw_prefix);
/* clean up */
if (get_ri.path)
free(get_ri.path);
if (get_ri.name)
free(get_ri.name);
if (get_ri.full_path)
free(get_ri.full_path);
out:
if (mntfd >= 0)
close(mntfd);
if (fd >= 0)
close(fd);
if (mnt)
free(mnt);
if (fullpath)
free(fullpath);
return ret;
}
const struct cmd_group subvolume_cmd_group = {
subvolume_cmd_group_usage, NULL, {
{ "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
@ -746,6 +894,7 @@ const struct cmd_group subvolume_cmd_group = {
{ "set-default", cmd_subvol_set_default,
cmd_subvol_set_default_usage, NULL, 0 },
{ "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
{ "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
{ 0, 0, 0, 0, 0 }
}
};

View File

@ -19,6 +19,8 @@ btrfs \- control a btrfs filesystem
.PP
\fBbtrfs\fP \fBsubvolume find-new\fP\fI <subvolume> <last_gen>\fP
.PP
\fBbtrfs\fP \fBsubvolume show\fP\fI <path>\fP
.PP
\fBbtrfs\fP \fBfilesystem defragment\fP -c[zlib|lzo] [-l \fIlen\fR] \
[-s \fIstart\fR] [-t \fIsize\fR] -[vf] <\fIfile\fR>|<\fIdir\fR> \
[<\fIfile\fR>|<\fIdir\fR>...]
@ -173,6 +175,10 @@ is similar to \fBsubvolume list\fR command.
List the recently modified files in a subvolume, after \fI<last_gen>\fR ID.
.TP
\fBsubvolume show\fR\fI <path>\fR
Show information of a given subvolume in the \fI<path>\fR.
.TP
\fBfilesystem defragment\fP -c[zlib|lzo] [-l \fIlen\fR] [-s \fIstart\fR] \
[-t \fIsize\fR] -[vf] <\fIfile\fR>|<\fIdir\fR> [<\fIfile\fR>|<\fIdir\fR>...]