Balance management userspace
v6 cover Balance management userspace
Hi, Chris,
This series is the userspace patches corresponding to the
kernel-side balance management stuff. This iteration fixes a use-after-free bug in the filtering code, and tidies up some of the man pages/help.
The patches are against the master branch of the tools repo.
Hugo.
Hugo Mills (8):
Balance progress monitoring.
Add --monitor option to btrfs balance progress.
User-space tool for cancelling balance operations.
Run userspace tool in background for balances.
Initial implementation of userspace interface for filtered balancing. Balance filter by device ID
Balance filter for virtual address range
Interface for device range balance filter
btrfs.c | 19 ++- btrfs_cmds.c | 460 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- btrfs_cmds.h | 2 + ioctl.h | 46 ++++++ man/btrfs.8.in | 81 +++++++++- 5 files changed, 595 insertions(+), 13 deletions(-)
Signature · 1 line
1.7.2.5
PATCH v6 1/8 Balance progress monitoring.
This patch introduces a basic form of progress monitoring for balance operations, by counting the number of block groups remaining. The information is exposed to userspace by an ioctl.
We also add "btrfs balance start" as an alias for "btrfs filesystem balance", so that all balance-related functions are available under one prefix.
--- btrfs.c | 8 +++++++ btrfs_cmds.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ btrfs_cmds.h | 1 + ioctl.h | 7 ++++++ man/btrfs.8.in | 16 ++++++++++++-- 5 files changed, 86 insertions(+), 3 deletions(-)
btrfs.c +8 -0
@@ -95,6 +95,14 @@ static struct Command commands[] = {"filesystem balance" ,"<path>\n" "Balance the chunks across the device." }, + { do_balance, 1, +"balance start" ,"<path>\n" +"Synonym for \"btrfs filesystem balance\"." + }, + { do_balance_progress, 1, +"balance progress" ,"<path>\n" +"Show progress of the balance operation running on <path>." + }, { do_scan,999 ,"device scan" ,"[<device> [<device>..]\n" "Scan all device for or the passed device for a btrfs\n"
btrfs_cmds.c +57 -0
69 lines not on this page · raw message
btrfs_cmds.h +1 -0
@@ -23,6 +23,7 @@ int do_defrag(int argc, char **argv);int do_show_filesystem (int nargs,char **argv);int do_add_volume (int nargs,char **args);int do_balance (int nargs,char **argv); +int do_balance_progress (int nargs,char **argv);int do_remove_volume (int nargs,char **args);int do_scan (int nargs,char **argv);int do_resize (int nargs,char **argv);
ioctl.h +7 -0
@@ -132,6 +132,11 @@ struct btrfs_ioctl_space_args {struct btrfs_ioctl_space_info spaces[0 ]; }; +struct btrfs_ioctl_balance_progress { +__u32 expected; +__u32 completed; +}; +#define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \ struct btrfs_ioctl_vol_args)#define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
@@ -169,4 +174,6 @@ struct btrfs_ioctl_space_args {#define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, u64)#define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \ struct btrfs_ioctl_space_args) +#define BTRFS_IOC_BALANCE_PROGRESS _IOR(BTRFS_IOCTL_MAGIC, 25, \ + struct btrfs_ioctl_balance_progress)#endif
man/btrfs.8.in +13 -3
@@ -21,11 +21,15 @@ btrfs \- control a btrfs filesystem .PP \fBbtrfs\fP \fBfilesystem resize\fP\fI [+/\-]<size>[gkm]|max <filesystem>\fP .PP +\fBbtrfs\fP \fBfilesystem balance\fP\fI <path> \fP +.PP \fBbtrfs\fP \fBdevice scan\fP\fI [<device> [<device>..]]\fP .PP -\fBbtrfs\fP \fBdevice show\fP\fI <dev>|<label> [<dev>|<label>...]\fP +\fBbtrfs\fP \fBbalance start\fP\fI <path> \fP +.PP +\fBbtrfs\fP \fBbalance progress\fP\fI <path>\fP .PP -\fBbtrfs\fP \fBdevice balance\fP\fI <path> \fP +\fBbtrfs\fP \fBdevice show\fP\fI <dev>|<label> [<dev>|<label>...]\fP .PP \fBbtrfs\fP \fBdevice add\fP\fI <dev> [<dev>..] <path> \fP .PP
@@ -143,7 +147,9 @@ Show the btrfs filesystem with some additional info. If no UUID or label is passed, \fBbtrfs\fR show info of all the btrfs filesystem. .TP -\fBdevice balance\fR \fI<path>\fR +\fBbalance start\fR \fI<path>\fR +.TP +\fBfilesystem balance\fR \fI<path>\fR Balance the chunks of the filesystem identified by \fI<path>\fR across the devices. .TP
@@ -156,6 +162,10 @@ Add device(s) to the filesystem identified by \fI<path>\fR. Remove device(s) from a filesystem identified by \fI<path>\fR. .PP +\fBbalance progress\fP \fI<path>\fP +Report progress on the currently-running balance operation on the +filesystem mounted at \fI<path>\fP. + .SH EXIT STATUS \fBbtrfs\fR returns a zero exist status if it succeeds. Non zero is returned in case of failure.
Signature · 1 line
1.7.2.5
PATCH v6 2/8 Add --monitor option to btrfs balance progress.
In reply to Hugo MillsFor the impatient, this patch introduces the pot-watching --monitor option, which checks the balance progress at regular intervals, and updates a single status line with the current progress and an estimated completion time.
--- btrfs.c | 4 +- btrfs_cmds.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++----- ioctl.h | 2 +- man/btrfs.8.in | 7 ++-- 4 files changed, 106 insertions(+), 16 deletions(-)
btrfs.c +2 -2
@@ -99,8 +99,8 @@ static struct Command commands[] = {"balance start" ,"<path>\n" "Synonym for \"btrfs filesystem balance\"." }, - { do_balance_progress, 1, -"balance progress" ,"<path>\n" + { do_balance_progress, -1, +"balance progress" ,"[-m|--monitor] <path>\n" "Show progress of the balance operation running on <path>." }, { do_scan,
btrfs_cmds.c +99 -10
131 lines not on this page · raw message
ioctl.h +1 -1
@@ -174,6 +174,6 @@ struct btrfs_ioctl_balance_progress {#define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, u64)#define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \ struct btrfs_ioctl_space_args) -#define BTRFS_IOC_BALANCE_PROGRESS _IOR(BTRFS_IOCTL_MAGIC, 25, \ +#define BTRFS_IOC_BALANCE_PROGRESS _IOR(BTRFS_IOCTL_MAGIC, 27, \ struct btrfs_ioctl_balance_progress)#endif
man/btrfs.8.in +4 -3
@@ -27,7 +27,7 @@ btrfs \- control a btrfs filesystem .PP \fBbtrfs\fP \fBbalance start\fP\fI <path> \fP .PP -\fBbtrfs\fP \fBbalance progress\fP\fI <path>\fP +\fBbtrfs\fP \fBbalance progress\fP [\fB-m\fP|\fB--monitor\fP] \fI<path>\fP .PP \fBbtrfs\fP \fBdevice show\fP\fI <dev>|<label> [<dev>|<label>...]\fP .PP
@@ -162,9 +162,10 @@ Add device(s) to the filesystem identified by \fI<path>\fR. Remove device(s) from a filesystem identified by \fI<path>\fR. .PP -\fBbalance progress\fP \fI<path>\fP +\fBbalance progress\fP [\fB-m\fP|\fB--monitor\fP] \fI<path>\fP Report progress on the currently-running balance operation on the -filesystem mounted at \fI<path>\fP. +filesystem mounted at \fI<path>\fP. Use --monitor to report progress +continually, including an estimate of completion time. .SH EXIT STATUS \fBbtrfs\fR returns a zero exist status if it succeeds. Non zero is returned in
Signature · 1 line
1.7.2.5
PATCH v6 3/8 User-space tool for cancelling balance operations.
In reply to Hugo MillsAdd an option to the btrfs tool to use the ioctl for cancelling balance operations.
--- btrfs.c | 4 ++++ btrfs_cmds.c | 41 +++++++++++++++++++++++++++++++++++++++++ btrfs_cmds.h | 1 + ioctl.h | 1 + man/btrfs.8.in | 6 ++++++ 5 files changed, 53 insertions(+), 0 deletions(-)
btrfs.c +4 -0
@@ -103,6 +103,10 @@ static struct Command commands[] = {"balance progress" ,"[-m|--monitor] <path>\n" "Show progress of the balance operation running on <path>." }, + { do_balance_cancel, 1, +"balance cancel" ,"<path>\n" +"Cancel the balance operation running on <path>." + }, { do_scan,999 ,"device scan" ,"[<device> [<device>..]\n" "Scan all device for or the passed device for a btrfs\n"
btrfs_cmds.c +41 -0
47 lines not on this page · raw message
btrfs_cmds.h +1 -0
@@ -24,6 +24,7 @@ int do_show_filesystem(int nargs, char **argv);int do_add_volume (int nargs,char **args);int do_balance (int nargs,char **argv);int do_balance_progress (int nargs,char **argv); +int do_balance_cancel (int nargs,char **argv);int do_remove_volume (int nargs,char **args);int do_scan (int nargs,char **argv);int do_resize (int nargs,char **argv);
ioctl.h +1 -0
@@ -176,4 +176,5 @@ struct btrfs_ioctl_balance_progress { struct btrfs_ioctl_space_args)#define BTRFS_IOC_BALANCE_PROGRESS _IOR(BTRFS_IOCTL_MAGIC, 27, \ struct btrfs_ioctl_balance_progress) +#define BTRFS_IOC_BALANCE_CANCEL _IO(BTRFS_IOCTL_MAGIC, 28)#endif
man/btrfs.8.in +6 -0
@@ -29,6 +29,8 @@ btrfs \- control a btrfs filesystem .PP \fBbtrfs\fP \fBbalance progress\fP [\fB-m\fP|\fB--monitor\fP] \fI<path>\fP .PP +\fBbtrfs\fP \fBbalance cancel\fP \fI<path>\fP +.PP \fBbtrfs\fP \fBdevice show\fP\fI <dev>|<label> [<dev>|<label>...]\fP .PP \fBbtrfs\fP \fBdevice add\fP\fI <dev> [<dev>..] <path> \fP
@@ -167,6 +169,10 @@ Report progress on the currently-running balance operation on the filesystem mounted at \fI<path>\fP. Use --monitor to report progress continually, including an estimate of completion time. +\fbalance cancel\fP \fI<path>\fP +Cancel the balance currently running on the filesystem mounted at +\fI<path>\fP. + .SH EXIT STATUS \fBbtrfs\fR returns a zero exist status if it succeeds. Non zero is returned in case of failure.
Signature · 1 line
1.7.2.5
PATCH v6 4/8 Run userspace tool in background for balances.
In reply to Hugo MillsThis patch makes a balance operation fork and detach from the current terminal, to run the userspace side of the balance in the background.
Introduce a --wait switch so that a synchronous balance can be done if the user requires.
--- btrfs.c | 8 ++++---- btrfs_cmds.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- man/btrfs.8.in | 14 ++++++++------ 3 files changed, 62 insertions(+), 12 deletions(-)
btrfs.c +4 -4
@@ -91,12 +91,12 @@ static struct Command commands[] = {"filesystem df" ,"<path>\n" "Show space usage information for a mount point\n." }, - { do_balance, 1, -"filesystem balance" ,"<path>\n" + { do_balance, -1, +"filesystem balance" ,"[-w|--wait] <path>\n" "Balance the chunks across the device." }, - { do_balance, 1, -"balance start" ,"<path>\n" + { do_balance, -1, +"balance start" ,"[-w|--wait] <path>\n" "Synonym for \"btrfs filesystem balance\"." }, { do_balance_progress, -1,
btrfs_cmds.c +50 -2
68 lines not on this page · raw message
man/btrfs.8.in +8 -6
@@ -21,11 +21,11 @@ btrfs \- control a btrfs filesystem .PP \fBbtrfs\fP \fBfilesystem resize\fP\fI [+/\-]<size>[gkm]|max <filesystem>\fP .PP -\fBbtrfs\fP \fBfilesystem balance\fP\fI <path> \fP +\fBbtrfs\fP \fBfilesystem balance\fP [\fB-w\fP|\fB--wait\fP] \fI<path>\fP .PP \fBbtrfs\fP \fBdevice scan\fP\fI [<device> [<device>..]]\fP .PP -\fBbtrfs\fP \fBbalance start\fP\fI <path> \fP +\fBbtrfs\fP \fBbalance start\fP [\fB-w\fP|\fB--wait\fP] \fI<path>\fP .PP \fBbtrfs\fP \fBbalance progress\fP [\fB-m\fP|\fB--monitor\fP] \fI<path>\fP .PP
@@ -149,11 +149,13 @@ Show the btrfs filesystem with some additional info. If no UUID or label is passed, \fBbtrfs\fR show info of all the btrfs filesystem. .TP -\fBbalance start\fR \fI<path>\fR +\fBdevice balance\fR [\fB-w\fP|\fB--wait\fP] \fI<path>\fR .TP -\fBfilesystem balance\fR \fI<path>\fR -Balance the chunks of the filesystem identified by \fI<path>\fR -across the devices. +\fBbalance start\fR [\fB-w\fP|\fB--wait\fP] \fI<path>\fR + +Balance the chunks of the filesystem identified by \fI<path>\fR across +the devices. The process runs in the background. Use \fB--wait\fP to +wait in the foreground for completion of the balance. .TP \fBdevice add\fR\fI <dev> [<dev>..] <path>\fR
Signature · 1 line
1.7.2.5
PATCH v6 5/8 Initial implementation of userspace interface for filtered balancing.
In reply to Hugo MillsIt is useful to be able to balance a subset of the full filesystem. This patch implements the infrastructure for filtering block groups on different criteria when balancing the filesystem.
--- btrfs.c | 5 +- btrfs_cmds.c | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- ioctl.h | 24 ++++++++ man/btrfs.8.in | 42 ++++++++++++--- 4 files changed, 224 insertions(+), 13 deletions(-)
btrfs.c +3 -2
@@ -92,8 +92,9 @@ static struct Command commands[] = {"Show space usage information for a mount point\n." }, { do_balance, -1, -"filesystem balance" ,"[-w|--wait] <path>\n" -"Balance the chunks across the device." +"filesystem balance" ,"[-w|--wait] [-c|--count] [-v|--verbose] [-f|--filter=<filter>:...] <path>\n" +"Balance chunks across the devices. --filter=help for help on filters.\n" +"--count to count chunks only (no balance performed).\n" }, { do_balance, -1,"balance start" ,"[-w|--wait] <path>\n"
btrfs_cmds.c +162 -4
214 lines not on this page · raw message
ioctl.h +24 -0
@@ -137,6 +137,28 @@ struct btrfs_ioctl_balance_progress {__u32 completed; }; +/* Types of balance filter */ +#define BTRFS_BALANCE_FILTER_COUNT_ONLY 0x1 + +#define BTRFS_BALANCE_FILTER_CHUNK_TYPE 0x2 +#define BTRFS_BALANCE_FILTER_MASK 0x3 + +/* All the possible options for a filter */ +struct btrfs_ioctl_balance_start { +__u64 flags;/* Bit field indicating which fields of this struct are filled */ + +/* Output values: chunk counts */ +__u32 examined; +__u32 balanced; + +/* For FILTER_CHUNK_TYPE */ +__u64 chunk_type;/* Flag bits required */ +__u64 chunk_type_mask;/* Mask of bits to examine */ + +__u64 spare[507 ];/* Make up the size of the structure to 4088 +* bytes for future expansion */ +}; +#define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \ struct btrfs_ioctl_vol_args)#define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
@@ -177,4 +199,6 @@ struct btrfs_ioctl_balance_progress {#define BTRFS_IOC_BALANCE_PROGRESS _IOR(BTRFS_IOCTL_MAGIC, 27, \ struct btrfs_ioctl_balance_progress)#define BTRFS_IOC_BALANCE_CANCEL _IO(BTRFS_IOCTL_MAGIC, 28) +#define BTRFS_IOC_BALANCE_FILTERED _IOWR(BTRFS_IOCTL_MAGIC, 29, \ + struct btrfs_ioctl_balance_start)#endif
man/btrfs.8.in +35 -7
69 lines not on this page · raw message
Signature · 1 line
1.7.2.5
PATCH v6 6/8 Balance filter by device ID
In reply to Hugo MillsAdd the userspace implementation for filtering balances by device ID.
--- btrfs_cmds.c | 13 +++++++++++++ ioctl.h | 8 ++++++-- man/btrfs.8.in | 7 +++++++ 3 files changed, 26 insertions(+), 2 deletions(-)
btrfs_cmds.c +13 -0
@@ -775,6 +775,10 @@ const struct filter_class_desc filter_class[] = {"\t\tmeta, sys, data, raid0, raid1, raid10, dup\n" "\tPrefix a <flagname> with ~ to negate the match.\n" , BTRFS_BALANCE_FILTER_CHUNK_TYPE }, + { "devid", +"devid=<n>\n" +"\tBalance only chunks which have a stripe on device <n>.\n" , + BTRFS_BALANCE_FILTER_DEVID }, { NULL, NULL, 0 } };
@@ -870,6 +874,15 @@ int parse_filter(struct btrfs_ioctl_balance_start *args, char *filters_string) part =strtok_r (NULL ,"=," , &subsave); }break ; + +case BTRFS_BALANCE_FILTER_DEVID : + errno =0 ; + args->devid =strtoull (part,NULL ,10 ); +if (errno !=0 ) { +fprintf (stderr,"ERROR: '%s' is not a valid device ID\n" , part); +return 15 ; + } +break ; } this_filter_string =strtok_r (NULL ,":" , &saveptr);
ioctl.h +6 -2
@@ -141,7 +141,8 @@ struct btrfs_ioctl_balance_progress {#define BTRFS_BALANCE_FILTER_COUNT_ONLY 0x1#define BTRFS_BALANCE_FILTER_CHUNK_TYPE 0x2 -#define BTRFS_BALANCE_FILTER_MASK 0x3 +#define BTRFS_BALANCE_FILTER_DEVID 0x4 +#define BTRFS_BALANCE_FILTER_MASK 0x7/* All the possible options for a filter */ struct btrfs_ioctl_balance_start {
@@ -155,7 +156,10 @@ struct btrfs_ioctl_balance_start {__u64 chunk_type;/* Flag bits required */ __u64 chunk_type_mask;/* Mask of bits to examine */ -__u64 spare[507 ];/* Make up the size of the structure to 4088 +/* For FILTER_DEVID */ +__u64 devid; + +__u64 spare[506 ];/* Make up the size of the structure to 4088 * bytes for future expansion */ };
man/btrfs.8.in +7 -0
@@ -203,6 +203,13 @@ chunk types. \fBraid0\fR, \fBraid1\fR, \fBraid10\fR, \fBdup\fR for chunks of the given replication levels. +.TP +\fBdevid\fR=\fI<n>\fR + +Select chunks which have data on device ID \fI<n>\fR. This can be +used, for example, to reduplicate data in a mirrored configuration +where one drive has been lost due to hardware failure. + .SH EXIT STATUS \fBbtrfs\fR returns a zero exist status if it succeeds. Non zero is returned in case of failure.
Signature · 1 line
1.7.2.5
PATCH v6 7/8 Balance filter for virtual address range
In reply to Hugo MillsImplement the userspace side of the virtual address range filter.
--- btrfs_cmds.c | 22 ++++++++++++++++++++++ ioctl.h | 9 +++++++-- man/btrfs.8.in | 9 +++++++++ 3 files changed, 38 insertions(+), 2 deletions(-)
btrfs_cmds.c +22 -0
@@ -779,6 +779,12 @@ const struct filter_class_desc filter_class[] = {"devid=<n>\n" "\tBalance only chunks which have a stripe on device <n>.\n" , BTRFS_BALANCE_FILTER_DEVID }, + { "vrange", +"vrange=<start>,<end>\n" +"\tBalances only chunks which have any bytes within the given\n" +"\trange of the filesystem's virtual address space.\n" +"\t<start> is inclusive, <end> is exclusive.\n" , + BTRFS_BALANCE_FILTER_VIRTUAL_ADDRESS_RANGE }, { NULL, NULL, 0 } };
@@ -883,6 +889,22 @@ int parse_filter(struct btrfs_ioctl_balance_start *args, char *filters_string)return 15 ; }break ; + +case BTRFS_BALANCE_FILTER_VIRTUAL_ADDRESS_RANGE : + errno =0 ; + args->vrange_start =strtoull (part,NULL ,10 ); +if (errno !=0 ) { +fprintf (stderr,"ERROR: '%s' is not a valid start address\n" , part); +return 15 ; + } + part =strtok_r (NULL ,"=," , &subsave); + errno =0 ; + args->vrange_end =strtoull (part,NULL ,10 ); +if (errno !=0 ) { +fprintf (stderr,"ERROR: '%s' is not a valid end address\n" , part); +return 15 ; + } +break ; } this_filter_string =strtok_r (NULL ,":" , &saveptr);
ioctl.h +7 -2
@@ -142,7 +142,8 @@ struct btrfs_ioctl_balance_progress {#define BTRFS_BALANCE_FILTER_CHUNK_TYPE 0x2#define BTRFS_BALANCE_FILTER_DEVID 0x4 -#define BTRFS_BALANCE_FILTER_MASK 0x7 +#define BTRFS_BALANCE_FILTER_VIRTUAL_ADDRESS_RANGE 0x8 +#define BTRFS_BALANCE_FILTER_MASK 0xf/* All the possible options for a filter */ struct btrfs_ioctl_balance_start {
@@ -159,7 +160,11 @@ struct btrfs_ioctl_balance_start {/* For FILTER_DEVID */ __u64 devid; -__u64 spare[506 ];/* Make up the size of the structure to 4088 +/* For FILTER_VIRTUAL_ADDRESS_RANGE */ +__u64 vrange_start; +__u64 vrange_end; + +__u64 spare[504 ];/* Make up the size of the structure to 4088 * bytes for future expansion */ };
man/btrfs.8.in +9 -0
@@ -210,6 +210,15 @@ Select chunks which have data on device ID \fI<n>\fR. This can be used, for example, to reduplicate data in a mirrored configuration where one drive has been lost due to hardware failure. +.TP +\fBvrange\fR=\fI<start>\fB,\fI<end>\fR + +Select chunks which have btrfs-internal virtual addresses within the +range \fI<start>\fR (inclusive) to \fI<end>\fR (exclusive). Given the +address of the last chunk moved, this filter can be used to restart a +cancelled or interrupted balance operation, by supplying a range of +\fB0,\fI<chunkaddr+1>\fR. + .SH EXIT STATUS \fBbtrfs\fR returns a zero exist status if it succeeds. Non zero is returned in case of failure.
Signature · 1 line
1.7.2.5
PATCH v6 8/8 Interface for device range balance filter
In reply to Hugo MillsImplement the userspace side of the balance filter for a range of bytes on any device. Note that this will match the same range on any device, so the use of the devid filter is recommended where this filter is used.
--- btrfs_cmds.c | 22 ++++++++++++++++++++++ ioctl.h | 9 +++++++-- man/btrfs.8.in | 8 ++++++++ 3 files changed, 37 insertions(+), 2 deletions(-)
btrfs_cmds.c +22 -0
@@ -785,6 +785,12 @@ const struct filter_class_desc filter_class[] = {"\trange of the filesystem's virtual address space.\n" "\t<start> is inclusive, <end> is exclusive.\n" , BTRFS_BALANCE_FILTER_VIRTUAL_ADDRESS_RANGE }, + { "drange", +"drange=<start>,<end>\n" +"\tBalances chunks which have any bytes within the given\n" +"\tbyte range on any of the filesystem's underlying devices.\n" +"\t<start> is inclusive, <end> is exclusive.\n" , + BTRFS_BALANCE_FILTER_DEVICE_ADDRESS_RANGE }, { NULL, NULL, 0 } };
@@ -905,6 +911,22 @@ int parse_filter(struct btrfs_ioctl_balance_start *args, char *filters_string)return 15 ; }break ; + +case BTRFS_BALANCE_FILTER_DEVICE_ADDRESS_RANGE : + errno =0 ; + args->drange_start =strtoull (part,NULL ,10 ); +if (errno !=0 ) { +fprintf (stderr,"ERROR: '%s' is not a valid start address\n" , part); +return 15 ; + } + part =strtok_r (NULL ,"=," , &subsave); + errno =0 ; + args->drange_end =strtoull (part,NULL ,10 ); +if (errno !=0 ) { +fprintf (stderr,"ERROR: '%s' is not a valid end address\n" , part); +return 15 ; + } +break ; } this_filter_string =strtok_r (NULL ,":" , &saveptr);
ioctl.h +7 -2
@@ -143,7 +143,8 @@ struct btrfs_ioctl_balance_progress {#define BTRFS_BALANCE_FILTER_CHUNK_TYPE 0x2#define BTRFS_BALANCE_FILTER_DEVID 0x4#define BTRFS_BALANCE_FILTER_VIRTUAL_ADDRESS_RANGE 0x8 -#define BTRFS_BALANCE_FILTER_MASK 0xf +#define BTRFS_BALANCE_FILTER_DEVICE_ADDRESS_RANGE 0x10 +#define BTRFS_BALANCE_FILTER_MASK 0x1f/* All the possible options for a filter */ struct btrfs_ioctl_balance_start {
@@ -164,7 +165,11 @@ struct btrfs_ioctl_balance_start {__u64 vrange_start;__u64 vrange_end; -__u64 spare[504 ];/* Make up the size of the structure to 4088 +/* For FILTER_DEVICE_ADDRESS_RANGE */ +__u64 drange_start; +__u64 drange_end; + +__u64 spare[502 ];/* Make up the size of the structure to 4088 * bytes for future expansion */ };
man/btrfs.8.in +8 -0
@@ -219,6 +219,14 @@ address of the last chunk moved, this filter can be used to restart a cancelled or interrupted balance operation, by supplying a range of \fB0,\fI<chunkaddr+1>\fR. +.TP +\fBdrange\fR=\fI<start>\fB,\fI<end>\fR + +Select chunks which contain data in the address range \fI<start>\fR +(inclusive) to \fI<end>\fR (exclusive) on \fIany\fR block device in +the filesystem. Can be mixed with the \fBdevid\fR filter to select +chunks in a given address range on a specific device. + .SH EXIT STATUS \fBbtrfs\fR returns a zero exist status if it succeeds. Non zero is returned in case of failure.
Signature · 1 line
1.7.2.5
Re: [PATCH v6 2/8] Add --monitor option to btrfs balance progress.
In reply to Hugo Mills-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 68 lines · Hugo Mills
For the impatient, this patch introduces the pot-watching --monitor option, which checks the balance progress at regular intervals, and updates a single status line with the current progress and an estimated completion time.
Signed-off-by Hugo Mills <[email protected]> --- btrfs.c |4 +-
btrfs_cmds.c| 109 ++++++++++++++++++++++++++++++++++++++++++++++++++-----ioctl.h| 2 +- man/btrfs.8.in | 7 ++-- 4 files changed, 106 insertions(+), 16 deletions(-)btrfs.c index 3a24019..0b6186c 100644 --- renamed from btrfs.c +0
-0
a/btrfs.c+++b/btrfs.c@@ -99,8 +99,8 @@ static struct Command commands[] = { "balance start", "<path>\n" "Synonym for \"btrfs filesystem balance\"." }, - {do_balance_progress, 1, - "balance progress", "<path>\n" + {do_balance_progress, -1, + "balance progress", "[-m|--monitor] <path>\n" "Show progress of the balance operation running on <path>." }, {do_scan, diff --gita/btrfs_cmds.cb/btrfs_cmds.cindex 00dda0a..ad2300d 100644 ---a/btrfs_cmds.c+++b/btrfs_cmds.c@@ -797,22 +797,108 @@ intget_balance_progress(char *path, struct btrfs_ioctl_balance_progress *bal)return err; }+const struct option progress_options[] = { + { "monitor", 0, NULL, 'm' }, + { NULL, 0, NULL, 0 } +}; + int
do_balance_progress(int argc, char **argv){ char *path; int ret = 0; int err = 0; structbtrfs_ioctl_balance_progressbal; +__u64last_completed= -1; +__u64initial_completed= -1; + struct timeval now; + struct timeval started; + int monitor = 0; + + optind = 1; + while(1) { + int c =getopt_long(argc, argv, "m", progress_options, NULL); + if (c < 0) + break; + switch(c) { + case 'm': + monitor = 1; + break; + default: +fprintf(stderr, "Invalid arguments for balance progress\n"); + free(argv); + return 1; + } + } + + if(optind >= argc) { +fprintf(stderr, "No filesystem path given for progress\n"); + return 1; + } + + path = argv[optind]; + do {
+ int prs = 0;- path = argv[1]; + ret =
get_balance_progress(path, &bal); + if (ret) + break;- ret =
get_balance_progress(path, &bal); - if (!ret) - printf("\r%u/%u block groups moved, " - "%0.2f%% complete.\n", - bal.completed, - bal.expected, - (float)bal.completed/bal.expected*100.0); + if (last_completed!= bal.completed) { + printf("\r%u/%u block groups moved, " + "%0.2f%% complete.", + bal.completed, +
bal.expected, + (float)bal.completed/bal.expected*100.0);
+ } + + if (initial_completed!= -1 + &&initial_completed!= bal.completed) { + ret =gettimeofday(&now, NULL); + if (ret) { +fprintf(stderr, "Can't read current time\n"); + return 22; + } + /* Seconds per block */ + float rate = (float)(now.tv_sec - started.tv_sec) + / (bal.completed -initial_completed); + intsecs_remaining= rate + * (bal.expected - bal.completed); +printf(" Time remaining"); + if (secs_remaining>= 60*60*24) { +printf(" %dd", secs_remaining / (60*60*24)); +secs_remaining%= 60*60*24; + prs = 1; + } + if (prs ||secs_remaining>= 60*60) { +printf(" %dh", secs_remaining / (60*60)); +secs_remaining%= 60*60; + prs = 1; + } + if (prs ||secs_remaining> 60) { +printf(" %dm", secs_remaining / 60); +secs_remaining%= 60; + } +printf(" %ds\x1b[K", secs_remaining); + } + + if (last_completed!= -1 &&last_completed!= bal.completed) { +initial_completed= bal.completed; + ret =gettimeofday(&started, NULL); + if (ret) { +fprintf(stderr, "Can't read current time\n"); + return 22; + } + } + +last_completed= bal.completed; + + if (monitor) { + fflush(stdout); + sleep(1); + } else { +printf("\n"); + } + } while(monitor);
Quoted 7 lines
switch(ret) { case 0: @@ -821,10 +907,13 @@ int
do_balance_progress(int argc, char **argv)fprintf(stderr, "ERROR:
can't access '%s'\n", path); return 13; case EINVAL: - fprintf(stderr, + if (!monitor) { +fprintf(stderr, "No balance operation running on '%s'.\n", path); - return 20; + return 20; + } + break; default:fprintf(stderr, "ERROR: ioctl returned error %d.", err);
The output of fprintf should end with a newline.
Cheers,
Andreas
Quoted 25 lines
return 21; diff --git
a/ioctl.hb/ioctl.hindex f07d3a2..3eeaa33 100644 ---a/ioctl.h+++b/ioctl.h@@ -174,6 +174,6 @@ structbtrfs_ioctl_balance_progress{ #defineBTRFS_IOC_DEFAULT_SUBVOL_IOW(BTRFS_IOCTL_MAGIC, 19, u64)#defineBTRFS_IOC_SPACE_INFO_IOWR(BTRFS_IOCTL_MAGIC, 20, \ struct btrfs_ioctl_space_args)btrfs.c index 3a24019..0b6186c 100644 --- renamed from btrfs.c +1
-1-#define BTRFS_IOC_BALANCE_PROGRESS _IOR(BTRFS_IOCTL_MAGIC, 25, \ +#define BTRFS_IOC_BALANCE_PROGRESS _IOR(BTRFS_IOCTL_MAGIC, 27, \struct
btrfs_ioctl_balance_progress) #endif diff --git a/man/btrfs.8.in b/man/btrfs.8.in index 7b66fb7..5fcad89 100644 --- a/man/btrfs.8.in +++ b/man/btrfs.8.in @@ -27,7 +27,7 @@ btrfs \- control a btrfs filesystem .PP \fBbtrfs\fP \fBbalance start\fP\fI <path> \fP .PP -\fBbtrfs\fP \fBbalance progress\fP\fI <path>\fP +\fBbtrfs\fP \fBbalance progress\fP [\fB-m\fP|\fB--monitor\fP] \fI<path>\fP .PP \fBbtrfs\fP \fBdevice show\fP\fI <dev>|<label> [<dev>|<label>...]\fP .PP @@ -162,9 +162,10 @@ Add device(s) to the filesystem identified by \fI<path>\fR. Remove device(s) from a filesystem identified by \fI<path>\fR. .PP-\fBbalance progress\fP \fI<path>\fP +\fBbalance progress\fP [\fB-m\fP|\fB--monitor\fP] \fI<path>\fP Report progress on the currently-running balance operation on the -filesystem mounted at \fI<path>\fP. +filesystem mounted at \fI<path>\fP. Use --monitor to report progress +continually, including an estimate of completion time.
Quoted 2 lines
.SH EXIT STATUS \fBbtrfs\fR returns a zero exist status if it succeeds. Non zero is returned in
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQIcBAEBAgAGBQJODPY2AAoJEJIcBJ3+XkgibsMP/Rl3UAdWks4qsTG5zHmhK2hd QgvveYc7eA5vbIFIXXLpWMmdgALvkogdsVae51PBdHe9Pc5KbVvH4n9VggGyMul7 cYJRmYLxXAwXnho8bqlCW6MBxZnYnUuEfG8BVmr7NbChwckr0+ffa5c76ZFb7B4n CyQF9JN8iwGHb808IlsHj2GOEUn9QZC/NzaUE62HuVK4s02FtUdWtvpzIt991IgG 4yWkH6te13WUGnfKTfmXBNUM+9K7Zf29TRHRCOOD7q4mWdZcc81/Up7sB590kjzg byWxZggc0sT9sL2WXkyiP+Wxsh4zeNo1sNzIPABMncdvkOToz1mGr53Ya15cqfgO 3yjrrScc/9XUweJnrz5aymUnYSyH/pBA9uNPlffYZEgTVCkC041nU/y1ly9Qxbji aObpFu8QWfIYw2igUOlPPr+bMdp3hpWmDrEaYsu+q6QpVSRj8M2fUqSNBaGtPXja lqgfgUSRmEcxgVPJwYUzNZht28qVKb26p+vi6F0DNBfAHuMXXZOOjsx79dX1eHw/ GRIqtYskyD9cGEvYHCsEmWfOJuyFjR+RgQwf1Qg21S8ZeZR9fP6Bwdht9dW5ngax c1LU5q2gB8ojPLG9QzO5/XAjSJpjRuu7T+CHdm5rndEZd8+qgp0+WQWDS84m5JEs zp0Jc8DBkq+RxmwNls9w
=JR07
-----END PGP SIGNATURE-----
Re: [PATCH v6 2/8] Add --monitor option to btrfs balance progress.
In reply to Hugo MillsQuoted 154 lines · Hugo Mills
For the impatient, this patch introduces the pot-watching --monitor option, which checks the balance progress at regular intervals, and updates a single status line with the current progress and an estimated completion time.
Signed-off-by Hugo Mills <[email protected]>--- btrfs.c | 4 +- btrfs_cmds.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++----- ioctl.h | 2 +- man/btrfs.8.in | 7 ++-- 4 files changed, 106 insertions(+), 16 deletions(-)btrfs.c +2
-2@@ -99,8 +99,8 @@ static struct Command commands[] = {"balance start" ,"<path>\n" "Synonym for \"btrfs filesystem balance\"." }, - { do_balance_progress, 1, -"balance progress" ,"<path>\n" + { do_balance_progress, -1, +"balance progress" ,"[-m|--monitor] <path>\n" "Show progress of the balance operation running on <path>." }, { do_scan,btrfs_cmds.c +94
-9@@ -797,22 +797,108 @@ int get_balance_progress(char *path, struct btrfs_ioctl_balance_progress *bal)return err; } +const struct option progress_options[] = { + {"monitor" ,0 ,NULL ,'m' }, + {NULL ,0 ,NULL ,0 } +}; +int do_balance_progress (int argc,char **argv) {char *path;int ret =0 ;int err =0 ;struct btrfs_ioctl_balance_progress bal; +__u64 last_completed =-1 ; +__u64 initial_completed =-1 ; +struct timeval now; +struct timeval started; +int monitor =0 ; + + optind =1 ; +while (1 ) { +int c =getopt_long (argc, argv,"m" , progress_options,NULL ); +if (c <0 ) +break ; +switch (c) { +case 'm' : + monitor =1 ; +break ; +default : +fprintf (stderr,"Invalid arguments for balance progress\n" ); +free (argv); +return 1 ; + } + } + +if (optind >= argc) { +fprintf (stderr,"No filesystem path given for progress\n" ); +return 1 ; + } + + path = argv[optind]; +do { +int prs =0 ; - path = argv[1 ]; + ret =get_balance_progress (path, &bal); +if (ret) +break ; - ret =get_balance_progress (path, &bal); -if (!ret) -printf ("\r%u/%u block groups moved, " -"%0.2f%% complete.\n" , - bal.completed, - bal.expected, - (float )bal.completed/bal.expected*100.0 ); +if (last_completed != bal.completed) { +printf ("\r%u/%u block groups moved, " +"%0.2f%% complete." , + bal.completed, + bal.expected, + (float )bal.completed/bal.expected*100.0 ); + } + +if (initial_completed !=-1 + && initial_completed != bal.completed) { + ret =gettimeofday (&now,NULL ); +if (ret) { +fprintf (stderr,"Can't read current time\n" ); +return 22 ; + } +/* Seconds per block */ +float rate = (float )(now.tv_sec - started.tv_sec) + / (bal.completed - initial_completed); +int secs_remaining = rate + * (bal.expected - bal.completed); +printf (" Time remaining" ); +if (secs_remaining >=60 *60 *24 ) { +printf (" %dd" , secs_remaining / (60 *60 *24 )); + secs_remaining %=60 *60 *24 ; + prs =1 ; + } +if (prs || secs_remaining >=60 *60 ) { +printf (" %dh" , secs_remaining / (60 *60 )); + secs_remaining %=60 *60 ; + prs =1 ; + } +if (prs || secs_remaining >60 ) { +printf (" %dm" , secs_remaining /60 ); + secs_remaining %=60 ; + } +printf (" %ds\x1b[K" , secs_remaining); + } + +if (last_completed !=-1 && last_completed != bal.completed) { + initial_completed = bal.completed; + ret =gettimeofday (&started,NULL ); +if (ret) { +fprintf (stderr,"Can't read current time\n" ); +return 22 ; + } + } + + last_completed = bal.completed; + +if (monitor) { +fflush (stdout); +sleep (1 ); + }else { +printf ("\n" ); + } + }while (monitor);switch (ret) {case 0 :@@ -821,10 +907,13 @@ int do_balance_progress(int argc, char **argv)fprintf (stderr,"ERROR: can't access '%s'\n" , path);return 13 ;case EINVAL : - fprintf(stderr,
btrfs_cmds.c int do_balance_progress(int argc, char **argv) · line 918
Quoted 10 lines
btrfs_cmds.c +5
-1+if (!monitor) { +fprintf (stderr,"No balance operation running on '%s'.\n" , path); -return 20 ; +return 20 ; + } +break ;default :fprintf (stderr,"ERROR: ioctl returned error %d." , err);
The output of fprintf should be terminated by a newline. Thanks,
Andreas
Quoted 39 lines
btrfs_cmds.c +0
-0return 21 ;ioctl.h +1
-1@@ -174,6 +174,6 @@ struct btrfs_ioctl_balance_progress {#define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, u64)#define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \ struct btrfs_ioctl_space_args) -#define BTRFS_IOC_BALANCE_PROGRESS _IOR(BTRFS_IOCTL_MAGIC, 25, \ +#define BTRFS_IOC_BALANCE_PROGRESS _IOR(BTRFS_IOCTL_MAGIC, 27, \ struct btrfs_ioctl_balance_progress)#endif man/btrfs.8.in +4
-3@@ -27,7 +27,7 @@ btrfs \- control a btrfs filesystem .PP \fBbtrfs\fP \fBbalance start\fP\fI <path> \fP .PP -\fBbtrfs\fP \fBbalance progress\fP\fI <path>\fP +\fBbtrfs\fP \fBbalance progress\fP [\fB-m\fP|\fB--monitor\fP] \fI<path>\fP .PP \fBbtrfs\fP \fBdevice show\fP\fI <dev>|<label> [<dev>|<label>...]\fP .PP@@ -162,9 +162,10 @@ Add device(s) to the filesystem identified by \fI<path>\fR. Remove device(s) from a filesystem identified by \fI<path>\fR. .PP -\fBbalance progress\fP \fI<path>\fP +\fBbalance progress\fP [\fB-m\fP|\fB--monitor\fP] \fI<path>\fP Report progress on the currently-running balance operation on the -filesystem mounted at \fI<path>\fP. +filesystem mounted at \fI<path>\fP. Use --monitor to report progress +continually, including an estimate of completion time. .SH EXIT STATUS \fBbtrfs\fR returns a zero exist status if it succeeds. Non zero is returned in