Re: [PATCH 3/3] Btrfs-progs: list snapshots by generation
Liu Bo, I am trying to resolve a conflict with your patch vs Hugo' integration branch http://git.darksatanic.net/repo/btrfs-progs-unstable.git (integration-20120605) --- @@ -185,11 +199,15 @@ static int add_root(struct root_lookup *root_lookup, ri-dir_id = dir_id; ri-root_id = root_id; ri-ref_tree = ref_tree; - strncpy(ri-name, name, name_len); + if (name) + strncpy(ri-name, name, name_len); if (name_len 0) ri-name[name_len] = 0;here + if (gen) + ri-gen = *gen; --- Per the original patch http://permalink.gmane.org/gmane.comp.file-systems.btrfs/16914 it is ri-name[name_len-1] instead. and I don't see any further patches which made it to be ri-name[name_len] as in your patch. any idea? Thanks, Anand On 31/07/12 13:49, Liu Bo wrote: The idea is that we usually use snapshot to backup/restore our data, and the common way can be a cron script which makes lots of snapshots, so we can end up with spending some time to find the latest snapshot to restore. This adds a feature for 'btrfs subvolume list' to let it list snapshots by their _created_ generation. What we need to do is just to list them in descending order and get the latest snapshot. What's more, we can find the oldest snapshot as well by listing snapshots in ascending order. Signed-off-by: Liu Boliubo2...@cn.fujitsu.com --- btrfs-list.c | 176 -- cmds-subvolume.c | 19 +- 2 files changed, 185 insertions(+), 10 deletions(-) diff --git a/btrfs-list.c b/btrfs-list.c index 05360dc..0374b41 100644 --- a/btrfs-list.c +++ b/btrfs-list.c @@ -87,13 +87,23 @@ static int comp_entry(struct root_info *entry, u64 root_id, u64 ref_tree) return 0; } +static int comp_entry_with_gen(struct root_info *entry, u64 root_id, + u64 ref_tree, u64 gen) +{ + if (entry-gen gen) + return 1; + if (entry-gen gen) + return -1; + return comp_entry(entry, root_id, ref_tree); +} + /* * insert a new root into the tree. returns the existing root entry * if one is already there. Both root_id and ref_tree are used * as the key */ static struct rb_node *tree_insert(struct rb_root *root, u64 root_id, - u64 ref_tree, struct rb_node *node) + u64 ref_tree, u64 *gen, struct rb_node *node) { struct rb_node ** p =root-rb_node; struct rb_node * parent = NULL; @@ -104,7 +114,11 @@ static struct rb_node *tree_insert(struct rb_root *root, u64 root_id, parent = *p; entry = rb_entry(parent, struct root_info, rb_node); - comp = comp_entry(entry, root_id, ref_tree); + if (!gen) + comp = comp_entry(entry, root_id, ref_tree); + else + comp = comp_entry_with_gen(entry, root_id, ref_tree, + *gen); if (comp 0) p =(*p)-rb_left; @@ -171,7 +185,7 @@ static struct root_info *tree_search(struct rb_root *root, u64 root_id) */ static int add_root(struct root_lookup *root_lookup, u64 root_id, u64 ref_tree, u64 dir_id, char *name, - int name_len) + int name_len, u64 *gen) { struct root_info *ri; struct rb_node *ret; @@ -185,11 +199,15 @@ static int add_root(struct root_lookup *root_lookup, ri-dir_id = dir_id; ri-root_id = root_id; ri-ref_tree = ref_tree; - strncpy(ri-name, name, name_len); + if (name) + strncpy(ri-name, name, name_len); if (name_len 0) ri-name[name_len] = 0; + if (gen) + ri-gen = *gen; - ret = tree_insert(root_lookup-root, root_id, ref_tree,ri-rb_node); + ret = tree_insert(root_lookup-root, root_id, ref_tree, gen, + ri-rb_node); if (ret) { printf(failed to insert tree %llu\n, (unsigned long long)root_id); exit(1); @@ -693,7 +711,7 @@ again: dir_id = btrfs_stack_root_ref_dirid(ref); add_root(root_lookup, sh-objectid, sh-offset, -dir_id, name, name_len); +dir_id, name, name_len, NULL); } else if (get_gen sh-type == BTRFS_ROOT_ITEM_KEY) { ri = (struct btrfs_root_item *)(args.buf + off); gen = btrfs_root_generation(ri); @@ -750,6 +768,79 @@ again: return 0; } +static int __list_snapshot_search(int fd, struct root_lookup *root_lookup) +{ + int ret; + struct btrfs_ioctl_search_args args; + struct
Re: [PATCH 3/3] Btrfs-progs: list snapshots by generation
On 08/01/2012 05:16 AM, Goffredo Baroncelli wrote: Hi Bo, On 07/31/2012 07:49 AM, Liu Bo wrote: The idea is that we usually use snapshot to backup/restore our data, and the common way can be a cron script which makes lots of snapshots, so we can end up with spending some time to find the latest snapshot to restore. This adds a feature for 'btrfs subvolume list' to let it list snapshots by their _created_ generation. What we need to do is just to list them in descending order and get the latest snapshot. What's more, we can find the oldest snapshot as well by listing snapshots in ascending order. Signed-off-by: Liu Bo liubo2...@cn.fujitsu.com --- btrfs-list.c | 176 -- cmds-subvolume.c | 19 +- 2 files changed, 185 insertions(+), 10 deletions(-) [] static const char * const cmd_subvol_list_usage[] = { -btrfs subvolume list [-p] path, +btrfs subvolume list [-ps] path, List subvolumes (and snapshots), , --p print parent ID, +-p print parent ID, +-s value list snapshots with generation in ascending/descending order, + (1: ascending, 0: descending), Please change the user interface. I suggest something like: -s|-S list snapshots with generation in ascending|descending order Or better -ssort by generation -Psort by path -rreverse the sort order I prefer to the first one, since I have no any idea how to sort by path by then. Anyway, whichever your choice will be, please remember to update the man page too. ah, I should have remembered to update it, thanks for reminding. :) Will do it soon, thanks for reviewing this! thanks, liubo NULL }; [...] -- To unsubscribe from this list: send the line unsubscribe linux-btrfs in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html -- To unsubscribe from this list: send the line unsubscribe linux-btrfs in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 3/3] Btrfs-progs: list snapshots by generation
Hi Bo, On 07/31/2012 07:49 AM, Liu Bo wrote: The idea is that we usually use snapshot to backup/restore our data, and the common way can be a cron script which makes lots of snapshots, so we can end up with spending some time to find the latest snapshot to restore. This adds a feature for 'btrfs subvolume list' to let it list snapshots by their _created_ generation. What we need to do is just to list them in descending order and get the latest snapshot. What's more, we can find the oldest snapshot as well by listing snapshots in ascending order. Signed-off-by: Liu Bo liubo2...@cn.fujitsu.com --- btrfs-list.c | 176 -- cmds-subvolume.c | 19 +- 2 files changed, 185 insertions(+), 10 deletions(-) [] static const char * const cmd_subvol_list_usage[] = { - btrfs subvolume list [-p] path, + btrfs subvolume list [-ps] path, List subvolumes (and snapshots), , - -p print parent ID, + -p print parent ID, + -s value list snapshots with generation in ascending/descending order, + (1: ascending, 0: descending), Please change the user interface. I suggest something like: -s|-S list snapshots with generation in ascending|descending order Or better -s sort by generation -P sort by path -r reverse the sort order Anyway, whichever your choice will be, please remember to update the man page too. NULL }; [...] -- To unsubscribe from this list: send the line unsubscribe linux-btrfs in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
[PATCH 3/3] Btrfs-progs: list snapshots by generation
The idea is that we usually use snapshot to backup/restore our data, and the common way can be a cron script which makes lots of snapshots, so we can end up with spending some time to find the latest snapshot to restore. This adds a feature for 'btrfs subvolume list' to let it list snapshots by their _created_ generation. What we need to do is just to list them in descending order and get the latest snapshot. What's more, we can find the oldest snapshot as well by listing snapshots in ascending order. Signed-off-by: Liu Bo liubo2...@cn.fujitsu.com --- btrfs-list.c | 176 -- cmds-subvolume.c | 19 +- 2 files changed, 185 insertions(+), 10 deletions(-) diff --git a/btrfs-list.c b/btrfs-list.c index 05360dc..0374b41 100644 --- a/btrfs-list.c +++ b/btrfs-list.c @@ -87,13 +87,23 @@ static int comp_entry(struct root_info *entry, u64 root_id, u64 ref_tree) return 0; } +static int comp_entry_with_gen(struct root_info *entry, u64 root_id, + u64 ref_tree, u64 gen) +{ + if (entry-gen gen) + return 1; + if (entry-gen gen) + return -1; + return comp_entry(entry, root_id, ref_tree); +} + /* * insert a new root into the tree. returns the existing root entry * if one is already there. Both root_id and ref_tree are used * as the key */ static struct rb_node *tree_insert(struct rb_root *root, u64 root_id, - u64 ref_tree, struct rb_node *node) + u64 ref_tree, u64 *gen, struct rb_node *node) { struct rb_node ** p = root-rb_node; struct rb_node * parent = NULL; @@ -104,7 +114,11 @@ static struct rb_node *tree_insert(struct rb_root *root, u64 root_id, parent = *p; entry = rb_entry(parent, struct root_info, rb_node); - comp = comp_entry(entry, root_id, ref_tree); + if (!gen) + comp = comp_entry(entry, root_id, ref_tree); + else + comp = comp_entry_with_gen(entry, root_id, ref_tree, + *gen); if (comp 0) p = (*p)-rb_left; @@ -171,7 +185,7 @@ static struct root_info *tree_search(struct rb_root *root, u64 root_id) */ static int add_root(struct root_lookup *root_lookup, u64 root_id, u64 ref_tree, u64 dir_id, char *name, - int name_len) + int name_len, u64 *gen) { struct root_info *ri; struct rb_node *ret; @@ -185,11 +199,15 @@ static int add_root(struct root_lookup *root_lookup, ri-dir_id = dir_id; ri-root_id = root_id; ri-ref_tree = ref_tree; - strncpy(ri-name, name, name_len); + if (name) + strncpy(ri-name, name, name_len); if (name_len 0) ri-name[name_len] = 0; + if (gen) + ri-gen = *gen; - ret = tree_insert(root_lookup-root, root_id, ref_tree, ri-rb_node); + ret = tree_insert(root_lookup-root, root_id, ref_tree, gen, + ri-rb_node); if (ret) { printf(failed to insert tree %llu\n, (unsigned long long)root_id); exit(1); @@ -693,7 +711,7 @@ again: dir_id = btrfs_stack_root_ref_dirid(ref); add_root(root_lookup, sh-objectid, sh-offset, -dir_id, name, name_len); +dir_id, name, name_len, NULL); } else if (get_gen sh-type == BTRFS_ROOT_ITEM_KEY) { ri = (struct btrfs_root_item *)(args.buf + off); gen = btrfs_root_generation(ri); @@ -750,6 +768,79 @@ again: return 0; } +static int __list_snapshot_search(int fd, struct root_lookup *root_lookup) +{ + int ret; + struct btrfs_ioctl_search_args args; + struct btrfs_ioctl_search_key *sk = args.key; + struct btrfs_ioctl_search_header *sh; + unsigned long off = 0; + u64 gen = 0; + int i; + + root_lookup_init(root_lookup); + memset(args, 0, sizeof(args)); + + sk-tree_id = 1; + sk-max_type = BTRFS_ROOT_ITEM_KEY; + sk-min_type = BTRFS_ROOT_ITEM_KEY; + sk-min_objectid = BTRFS_FIRST_FREE_OBJECTID; + sk-max_objectid = BTRFS_LAST_FREE_OBJECTID; + sk-max_offset = (u64)-1; + sk-max_transid = (u64)-1; + sk-nr_items = 4096; + + while (1) { + ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, args); + if (ret 0) + return ret; + /* the ioctl returns the number of item it found in nr_items */ + if (sk-nr_items == 0) + break; + + off = 0; + + /* +