Skip to contentSkip to conversation

Btrfs-progs: add feature to get mininum size for resizing a fs/device

PATCH Superseded [email protected] 3 participants 8 messages Jun 17, 2015 – Jul 20, 2015

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs-progs: add feature to get mininum size for resizing a fs/device

From: Filipe Manana <[email protected]>

Currently there is not way for a user to know what is the minimum size a device of a btrfs filesystem can be resized to. Sometimes the value of total allocated space (sum of all allocated chunks/device extents), which can be parsed from 'btrfs filesystem show' and 'btrfs filesystem usage', works as the minimum size, but sometimes it does not, namely when device extents have to relocated to holes (unallocated space) within the new size of the device (the total allocated space sum).

This change adds the ability to reliably compute such minimum value and extents 'btrfs filesystem resize' with the following syntax to get such value:

btrfs filesystem resize [devid:]get_min_size

---
 Documentation/btrfs-filesystem.asciidoc |   4 +-
 Makefile.in                             |   8 +-
 cmds-filesystem.c                       | 219 +++++++++++++++++++++++++++++++-
 ctree.h                                 |   3 +
 tests/shrink-min-size-tests.sh          |  72 +++++++++++
 5 files changed, 302 insertions(+), 4 deletions(-)
 create mode 100755 tests/shrink-min-size-tests.sh
Documentation/btrfs-filesystem.asciidoc +3 -1
index f1c35b6..45f8cf7 100644
@@ -88,7 +88,7 @@ If a newlabel optional argument is passed, the label is changed.
 NOTE: the maximum allowable length shall be less than 256 chars
 
 // Some wording are extracted by the resize2fs man page
-*resize* [<devid>:][+/-]<size>[kKmMgGtTpPeE]|[<devid>:]max <path>::
+*resize* [<devid>:][+/-]<size>[kKmMgGtTpPeE]|[<devid>:]max|[<devid>:]get_min_size <path>::
 Resize a mounted filesystem identified by directory <path>. A particular device
 can be resized by specifying a <devid>.
 +
@@ -108,6 +108,8 @@ KiB, MiB, GiB, TiB, PiB, or EiB, respectively. Case does not matter.
 +
 If \'max' is passed, the filesystem will occupy all available space on the
 device devid.
+If \'get_min_size' is passed, return the minimum size the device can be
+shrunk to, without performing any resize operation.
 +
 The resize command does not manipulate the size of underlying
 partition.  If you wish to enlarge/reduce a filesystem, you must make sure you
Makefile.in +6 -2
index 860a390..202c51e 100644
@@ -46,7 +46,7 @@ libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
 	       crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
 	       extent_io.h ioctl.h ctree.h btrfsck.h version.h
-TESTS = fsck-tests.sh convert-tests.sh
+TESTS = fsck-tests.sh convert-tests.sh shrink-min-size-tests.sh
 
 prefix ?= @prefix@
 exec_prefix = @exec_prefix@
@@ -161,6 +161,10 @@ $(BUILDDIRS):
 	@echo "Making all in $(patsubst build-%,%,$@)"
 	$(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
 
+test-shrink-min-size: btrfs mkfs.btrfs
+	@echo "    [TEST]   shrink-min-size-tests.sh"
+	$(Q)bash tests/shrink-min-size-tests.sh
+
 test-convert: btrfs btrfs-convert
 	@echo "    [TEST]   convert-tests.sh"
 	$(Q)bash tests/convert-tests.sh
@@ -169,7 +173,7 @@ test-fsck: btrfs btrfs-image btrfs-corrupt-block btrfs-debug-tree mkfs.btrfs
 	@echo "    [TEST]   fsck-tests.sh"
 	$(Q)bash tests/fsck-tests.sh
 
-test: test-fsck test-convert
+test: test-fsck test-convert test-shrink-min-size
 
 #
 # NOTE: For static compiles, you need to have all the required libs
cmds-filesystem.c +218 -1

238 lines not on this page · raw message

ctree.h +3 -0
index f14a795..7d74d1a 100644
@@ -1491,6 +1491,9 @@ BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
 		   chunk_offset, 64);
 BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
 
+BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_length, struct btrfs_dev_extent,
+			 length, 64);
+
 static inline u8 *btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
 {
 	unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
tests/shrink-min-size-tests.sh new file +72 -0

72 lines not on this page · raw message

Signature · 1 line
2.1.3
#1 lore raw
Quoted 11 lines · On Wed, Jun 17, 2015 at 12:44:55PM +0100, [email protected] wrote:

Currently there is not way for a user to know what is the minimum size a device of a btrfs filesystem can be resized to. Sometimes the value of total allocated space (sum of all allocated chunks/device extents), which can be parsed from 'btrfs filesystem show' and 'btrfs filesystem usage', works as the minimum size, but sometimes it does not, namely when device extents have to relocated to holes (unallocated space) within the new size of the device (the total allocated space sum).

This change adds the ability to reliably compute such minimum value and extents 'btrfs filesystem resize' with the following syntax to get such value:

The test fails after I do this before unmount:

$SUDO_HELPER $TOP/btrfs balance start -mconvert=single -sconvert=single -f $TEST_MNT shrink_test

Output:

############### root_helper ../btrfs filesystem resize get_min_size ../tests/mnt 6480199680 bytes (6.04GiB)
min size = 6480199680
############### root_helper ../btrfs filesystem resize 6480199680 ../tests/mnt
ERROR: unable to resize '../tests/mnt' - No space left on device Resize '../tests/mnt' of '6480199680'

Last successful resize before this was:
Resize '../tests/mnt' of '7553941504'

Quoted 1 line

btrfs filesystem resize [devid:]get_min_size

I don't think this is the right interface, IMHO this fits into the inspect-internal group. The syntax for 'fi resize' is a bit cumbersome, I'd like to avoid complicating it further. But you can keep it as-is until the the bugs are fixed.

Quoted 12 lines
Makefile.in +1 -1
@@ -46,7 +46,7 @@ libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
 	       crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
 	       extent_io.h ioctl.h ctree.h btrfsck.h version.h
-TESTS = fsck-tests.sh convert-tests.sh
+TESTS = fsck-tests.sh convert-tests.sh shrink-min-size-tests.sh
 
 prefix ?= @prefix@
 exec_prefix = @exec_prefix@

Makefile.in $(BUILDDIRS): · line 166

Quoted 6 lines
Makefile.in +3 -0
@@ -161,6 +161,10 @@ $(BUILDDIRS):
 	@echo "Making all in $(patsubst build-%,%,$@)"
 	$(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
 
+test-shrink-min-size: btrfs mkfs.btrfs
+	@echo "    [TEST]   shrink-min-size-tests.sh"
+	$(Q)bash tests/shrink-min-size-tests.sh

Please move the test under the test-misc, for now it's a catch-all category.

General comments to the test script(s):
* the tests are supposed to be run unprivileged as much as possible, so the SUDO_HELPER needs to be used explicitly
* use the git binaries everywhere

(diff below)

Makefile.in $(BUILDDIRS): · line 169

Quoted 3 lines
Makefile.in +3 -0
+shrink_test()
+{
+	min_size=$(btrfs filesystem resize get_min_size $TEST_MNT)

sudo helper, use binary from git

Makefile.in $(BUILDDIRS):

Quoted 6 lines
Makefile.in +6 -0
+	if [ $? != 0 ]; then
+		_fail "Failed to get minimum size"
+	fi
+	min_size=$(echo $min_size | cut -d ' ' -f 1)
+	echo "min size = ${min_size}" >> $RESULTS
+	run_check btrfs filesystem resize $min_size $TEST_MNT

sudo helper, use binary from git

Makefile.in

Quoted 4 lines
Makefile.in +4 -0
+}
+
+run_check truncate -s 20G $IMAGE
+run_check $SUDO_HELPER $TOP/mkfs.btrfs -f $IMAGE

sudo helper not necessary, it's a file-backed image

Makefile.in

Quoted 5 lines
Makefile.in +5 -0
+run_check $SUDO_HELPER mount $IMAGE $TEST_MNT
+
+# Create 7 data block groups, each with a size of 1Gb.
+for ((i = 1; i <= 7; i++)); do
+	run_check fallocate -l 1G $TEST_MNT/foo$i

This failed as the mountpoint is created by root and I'm running the test under my user. A chown after mount fixed it.

Quoted 1 line

+done

[...]

@@ -23,19 +23,23 @@ setup_root_helper

shrink_test()
{
- min_size=$(btrfs filesystem resize get_min_size $TEST_MNT)
+ min_size=$(run_check_stdout $SUDO_HELPER $TOP/btrfs filesystem resize get_min_size $TEST_MNT)

        if [ $? != 0 ]; then
                _fail "Failed to get minimum size"
        fi
        min_size=$(echo $min_size | cut -d ' ' -f 1)
        echo "min size = ${min_size}" >> $RESULTS

- run_check btrfs filesystem resize $min_size $TEST_MNT
+ run_check $SUDO_HELPER $TOP/btrfs filesystem resize $min_size $TEST_MNT
}

run_check truncate -s 20G $IMAGE
-run_check $SUDO_HELPER $TOP/mkfs.btrfs -f $IMAGE
+run_check $TOP/mkfs.btrfs -f $IMAGE
run_check $SUDO_HELPER mount $IMAGE $TEST_MNT

+user=$(id -un)
+group=$(id -gn)
+run_check $SUDO_HELPER chown $user:$group $TEST_MNT +
# Create 7 data block groups, each with a size of 1Gb. for ((i = 1; i <= 7; i++)); do
run_check fallocate -l 1G $TEST_MNT/foo$i
@@ -43,7 +47,7 @@ done

# Make sure they are persisted (all the chunk, device and block group items

 # added to the chunk/dev/extent trees).
-run_check btrfs filesystem sync $TEST_MNT
+run_check $TOP/btrfs filesystem sync $TEST_MNT

# Now remove 3 of those 1G files. This will result in 3 block groups becoming # unused, which will be automatically deleted by the cleaner kthread, and this
@@ -58,9 +62,9 @@ run_check rm -f $TEST_MNT/foo6

 # groups - it could have been sleeping when they become unused. Then wait a bit
 # to allow the cleaner kthread to delete them and then finally ensure the
 # transaction started by the cleaner kthread is committed.
-run_check btrfs filesystem sync $TEST_MNT
+run_check $TOP/btrfs filesystem sync $TEST_MNT
 sleep 3
-run_check btrfs filesystem sync $TEST_MNT
+run_check $TOP/btrfs filesystem sync $TEST_MNT

# Now attempt to get the minimum size we can resize the filesystem to and verify

 # the resize operation succeeds. This size closely matches the sum of the size
---
#2 lore raw
Quoted 9 lines · David Sterba
Quoted 8 lines · On Wed, Jun 17, 2015 at 12:44:55PM +0100, [email protected] wrote:

Currently there is not way for a user to know what is the minimum size a device of a btrfs filesystem can be resized to. Sometimes the value of total allocated space (sum of all allocated chunks/device extents), which can be parsed from 'btrfs filesystem show' and 'btrfs filesystem usage', works as the minimum size, but sometimes it does not, namely when device extents have to relocated to holes (unallocated space) within the new size of the device (the total allocated space sum).

Quoted 8 lines
Quoted 3 lines

This change adds the ability to reliably compute such minimum value and extents 'btrfs filesystem resize' with the following syntax to get such value:

The test fails after I do this before unmount:

$SUDO_HELPER $TOP/btrfs balance start -mconvert=single -sconvert=single -f $TEST_MNT shrink_test

Where are you doing this exactly?

Just tried the following: https://friendpaste.com/2U7C4gBBLBjo4e2v1ZnJP2

Quoted 10 lines

Output:

############### root_helper ../btrfs filesystem resize get_min_size ../tests/mnt 6480199680 bytes (6.04GiB)
min size = 6480199680
############### root_helper ../btrfs filesystem resize 6480199680 ../tests/mnt
ERROR: unable to resize '../tests/mnt' - No space left on device Resize '../tests/mnt' of '6480199680'

Quoted 2 lines

Last successful resize before this was:
Resize '../tests/mnt' of '7553941504'

And it didn't fail for me on a 4.1 kernel at least. It produced those 2 sizes as well, but it didn't fail for any of them.

thanks

Quoted 119 lines
Quoted 2 lines

btrfs filesystem resize [devid:]get_min_size

I don't think this is the right interface, IMHO this fits into the inspect-internal group. The syntax for 'fi resize' is a bit cumbersome, I'd like to avoid complicating it further. But you can keep it as-is until the the bugs are fixed.

Quoted 12 lines
Makefile.in +1 -1
@@ -46,7 +46,7 @@ libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
 libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
             crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
             extent_io.h ioctl.h ctree.h btrfsck.h version.h
-TESTS = fsck-tests.sh convert-tests.sh
+TESTS = fsck-tests.sh convert-tests.sh shrink-min-size-tests.sh
 
 prefix ?= @prefix@
 exec_prefix = @exec_prefix@

Makefile.in $(BUILDDIRS): · line 166

Quoted 6 lines
Makefile.in +3 -0
@@ -161,6 +161,10 @@ $(BUILDDIRS):
      @echo "Making all in $(patsubst build-%,%,$@)"
      $(Q)$(MAKE) $(MAKEOPTS) -C $(patsubst build-%,%,$@)
 
+test-shrink-min-size: btrfs mkfs.btrfs
+     @echo "    [TEST]   shrink-min-size-tests.sh"
+     $(Q)bash tests/shrink-min-size-tests.sh

Please move the test under the test-misc, for now it's a catch-all category.

General comments to the test script(s):
* the tests are supposed to be run unprivileged as much as possible, so the SUDO_HELPER needs to be used explicitly
* use the git binaries everywhere

(diff below)

Makefile.in $(BUILDDIRS): · line 169

Quoted 3 lines
Makefile.in +3 -0
+shrink_test()
+{
+     min_size=$(btrfs filesystem resize get_min_size $TEST_MNT)

sudo helper, use binary from git

Makefile.in $(BUILDDIRS):

Quoted 6 lines
Makefile.in +6 -0
+     if [ $? != 0 ]; then
+             _fail "Failed to get minimum size"
+     fi
+     min_size=$(echo $min_size | cut -d ' ' -f 1)
+     echo "min size = ${min_size}" >> $RESULTS
+     run_check btrfs filesystem resize $min_size $TEST_MNT

sudo helper, use binary from git

Makefile.in

Quoted 4 lines
Makefile.in +4 -0
+}
+
+run_check truncate -s 20G $IMAGE
+run_check $SUDO_HELPER $TOP/mkfs.btrfs -f $IMAGE

sudo helper not necessary, it's a file-backed image

Makefile.in

Quoted 5 lines
Makefile.in +5 -0
+run_check $SUDO_HELPER mount $IMAGE $TEST_MNT
+
+# Create 7 data block groups, each with a size of 1Gb.
+for ((i = 1; i <= 7; i++)); do
+     run_check fallocate -l 1G $TEST_MNT/foo$i

This failed as the mountpoint is created by root and I'm running the test under my user. A chown after mount fixed it.

Quoted 1 line

+done

[...]

(quoted diff) +10 -7
@@ -23,19 +23,23 @@ setup_root_helper
 
 shrink_test()
 {
-       min_size=$(btrfs filesystem resize get_min_size $TEST_MNT)
+       min_size=$(run_check_stdout $SUDO_HELPER $TOP/btrfs filesystem resize get_min_size $TEST_MNT)
        if [ $? != 0 ]; then
                _fail "Failed to get minimum size"
        fi
        min_size=$(echo $min_size | cut -d ' ' -f 1)
        echo "min size = ${min_size}" >> $RESULTS
-       run_check btrfs filesystem resize $min_size $TEST_MNT
+       run_check $SUDO_HELPER $TOP/btrfs filesystem resize $min_size $TEST_MNT
 }
 
 run_check truncate -s 20G $IMAGE
-run_check $SUDO_HELPER $TOP/mkfs.btrfs -f $IMAGE
+run_check $TOP/mkfs.btrfs -f $IMAGE
 run_check $SUDO_HELPER mount $IMAGE $TEST_MNT
 
+user=$(id -un)
+group=$(id -gn)
+run_check $SUDO_HELPER chown $user:$group $TEST_MNT
+
 # Create 7 data block groups, each with a size of 1Gb.
 for ((i = 1; i <= 7; i++)); do
        run_check fallocate -l 1G $TEST_MNT/foo$i
@@ -43,7 +47,7 @@ done
 
 # Make sure they are persisted (all the chunk, device and block group items
 # added to the chunk/dev/extent trees).
-run_check btrfs filesystem sync $TEST_MNT
+run_check $TOP/btrfs filesystem sync $TEST_MNT
 
 # Now remove 3 of those 1G files. This will result in 3 block groups becoming
 # unused, which will be automatically deleted by the cleaner kthread, and this
@@ -58,9 +62,9 @@ run_check rm -f $TEST_MNT/foo6
 # groups - it could have been sleeping when they become unused. Then wait a bit
 # to allow the cleaner kthread to delete them and then finally ensure the
 # transaction started by the cleaner kthread is committed.
-run_check btrfs filesystem sync $TEST_MNT
+run_check $TOP/btrfs filesystem sync $TEST_MNT
 sleep 3
-run_check btrfs filesystem sync $TEST_MNT
+run_check $TOP/btrfs filesystem sync $TEST_MNT
 
 # Now attempt to get the minimum size we can resize the filesystem to and verify
 # the resize operation succeeds. This size closely matches the sum of the size
---
#3 lore raw
Quoted 10 lines · Filipe Manana
Quoted 5 lines · David Sterba

On Wed, Jun 17, 2015 at 12:44:55PM +0100, [email protected] wrote:
The test fails after I do this before unmount:

$SUDO_HELPER $TOP/btrfs balance start -mconvert=single -sconvert=single -f $TEST_MNT shrink_test

Where are you doing this exactly?

Just tried the following: https://friendpaste.com/2U7C4gBBLBjo4e2v1ZnJP2

Like in the the first part of the diff.

Quoted 9 lines
Quoted 9 lines

Output:

############### root_helper ../btrfs filesystem resize get_min_size ../tests/mnt 6480199680 bytes (6.04GiB)
min size = 6480199680
############### root_helper ../btrfs filesystem resize 6480199680 ../tests/mnt
ERROR: unable to resize '../tests/mnt' - No space left on device Resize '../tests/mnt' of '6480199680'

Quoted 5 lines
Quoted 2 lines

Last successful resize before this was:
Resize '../tests/mnt' of '7553941504'

And it didn't fail for me on a 4.1 kernel at least. It produced those 2 sizes as well, but it didn't fail for any of them.

The test was run on 4.0.5, I'm building a clean 4.1 + integration to verify it there.

#4 lore raw
Quoted 23 lines · David Sterba
Quoted 10 lines · Filipe Manana
Quoted 5 lines · David Sterba

On Wed, Jun 17, 2015 at 12:44:55PM +0100, [email protected] wrote:
The test fails after I do this before unmount:

$SUDO_HELPER $TOP/btrfs balance start -mconvert=single -sconvert=single -f $TEST_MNT shrink_test

Where are you doing this exactly?

Just tried the following: https://friendpaste.com/2U7C4gBBLBjo4e2v1ZnJP2

Like in the the first part of the diff.

Quoted 9 lines
Quoted 9 lines

Output:

############### root_helper ../btrfs filesystem resize get_min_size ../tests/mnt 6480199680 bytes (6.04GiB)
min size = 6480199680
############### root_helper ../btrfs filesystem resize 6480199680 ../tests/mnt
ERROR: unable to resize '../tests/mnt' - No space left on device Resize '../tests/mnt' of '6480199680'

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Last successful resize before this was:
Resize '../tests/mnt' of '7553941504'

And it didn't fail for me on a 4.1 kernel at least. It produced those 2 sizes as well, but it didn't fail for any of them.

The test was run on 4.0.5, I'm building a clean 4.1 + integration to verify it there.

Nevermind, I managed to reproduce it but by placing the balance/convert before the loop that tests the shrinking. btrfs_can_relocate() returns 0, meaning relocation should succeed if no other allocations happen in parallel, which is the case here, but btrfs_relocate_block_group() fails with ENOSPC - one of this functions isn't working as it should, I'll check what's going on soon.

thanks

#5 lore raw

PATCH v2 1/1 Btrfs-progs: add feature to get mininum size for resizing a fs/device

In reply to [email protected]

From: Filipe Manana <[email protected]>

Currently there is not way for a user to know what is the minimum size a device of a btrfs filesystem can be resized to. Sometimes the value of total allocated space (sum of all allocated chunks/device extents), which can be parsed from 'btrfs filesystem show' and 'btrfs filesystem usage', works as the minimum size, but sometimes it does not, namely when device extents have to relocated to holes (unallocated space) within the new size of the device (the total allocated space sum).

This change adds the ability to reliably compute such minimum value and extents 'btrfs filesystem resize' with the following syntax to get such value:

btrfs filesystem resize [devid:]get_min_size

---

V2: Check if device holes contain the location of superblock mirrors and
correct the minimum size accounting accordingly.
Added missing sudo calls to test, rebeased against development branch and moved it into the misc-tests category.

 Documentation/btrfs-filesystem.asciidoc |   4 +-
 cmds-filesystem.c                       | 219 +++++++++++++++++++++++++++++++-
 ctree.h                                 |   3 +
 tests/misc-tests.sh                     |   2 +
 tests/misc-tests/004-shrink-fs/test.sh  |  69 ++++++++++
 5 files changed, 295 insertions(+), 2 deletions(-)
 create mode 100755 tests/misc-tests/004-shrink-fs/test.sh
Documentation/btrfs-filesystem.asciidoc +3 -1
index 31cd51b..2b34242 100644
@@ -93,7 +93,7 @@ If a newlabel optional argument is passed, the label is changed.
 NOTE: the maximum allowable length shall be less than 256 chars
 
 // Some wording are extracted by the resize2fs man page
-*resize* [<devid>:][+/-]<size>[kKmMgGtTpPeE]|[<devid>:]max <path>::
+*resize* [<devid>:][+/-]<size>[kKmMgGtTpPeE]|[<devid>:]max|[<devid>:]get_min_size <path>::
 Resize a mounted filesystem identified by directory <path>. A particular device
 can be resized by specifying a <devid>.
 +
@@ -113,6 +113,8 @@ KiB, MiB, GiB, TiB, PiB, or EiB, respectively. Case does not matter.
 +
 If \'max' is passed, the filesystem will occupy all available space on the
 device devid.
+If \'get_min_size' is passed, return the minimum size the device can be
+shrunk to, without performing any resize operation.
 +
 The resize command does not manipulate the size of underlying
 partition.  If you wish to enlarge/reduce a filesystem, you must make sure you
cmds-filesystem.c +218 -1

238 lines not on this page · raw message

ctree.h +3 -0
index f14a795..7d74d1a 100644
@@ -1491,6 +1491,9 @@ BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
 		   chunk_offset, 64);
 BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
 
+BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_length, struct btrfs_dev_extent,
+			 length, 64);
+
 static inline u8 *btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
 {
 	unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
tests/misc-tests.sh +2 -0
index 5bbe914..2ae99db 100755
@@ -20,6 +20,8 @@ export RESULTS
 # For custom script needs to verfiy recovery
 export TEST_MNT
 export LANG
+# For tests that only use a loop device
+export IMAGE
 
 rm -f $RESULTS
 mkdir -p $TEST_MNT || _fail "unable to create mount point on $TEST_MNT"
tests/misc-tests/004-shrink-fs/test.sh new file +69 -0

69 lines not on this page · raw message

Signature · 1 line
2.1.3
#6 lore raw

PATCH v3 1/1 Btrfs-progs: add feature to get mininum size for resizing a fs/device

From: Filipe Manana <[email protected]>

Currently there is not way for a user to know what is the minimum size a device of a btrfs filesystem can be resized to. Sometimes the value of total allocated space (sum of all allocated chunks/device extents), which can be parsed from 'btrfs filesystem show' and 'btrfs filesystem usage', works as the minimum size, but sometimes it does not, namely when device extents have to relocated to holes (unallocated space) within the new size of the device (the total allocated space sum).

This change adds the ability to reliably compute such minimum value and extents 'btrfs filesystem resize' with the following syntax to get such value:

btrfs filesystem resize [devid:]get_min_size

---

V2: Check if device holes contain the location of superblock mirrors and
correct the minimum size accounting accordingly.
Added missing sudo calls to test, rebeased against development branch and moved it into the misc-tests category.

V3: Added missing changes to cmds-filesystems.c in v2. I forgot to add them
to V2 (left unstaged in local repository).

 Documentation/btrfs-filesystem.asciidoc |   4 +-
 cmds-filesystem.c                       | 255 +++++++++++++++++++++++++++++++-
 ctree.h                                 |   3 +
 tests/misc-tests.sh                     |   2 +
 tests/misc-tests/004-shrink-fs/test.sh  |  69 +++++++++
 5 files changed, 331 insertions(+), 2 deletions(-)
 create mode 100755 tests/misc-tests/004-shrink-fs/test.sh
Documentation/btrfs-filesystem.asciidoc +3 -1
index 31cd51b..2b34242 100644
@@ -93,7 +93,7 @@ If a newlabel optional argument is passed, the label is changed.
 NOTE: the maximum allowable length shall be less than 256 chars
 
 // Some wording are extracted by the resize2fs man page
-*resize* [<devid>:][+/-]<size>[kKmMgGtTpPeE]|[<devid>:]max <path>::
+*resize* [<devid>:][+/-]<size>[kKmMgGtTpPeE]|[<devid>:]max|[<devid>:]get_min_size <path>::
 Resize a mounted filesystem identified by directory <path>. A particular device
 can be resized by specifying a <devid>.
 +
@@ -113,6 +113,8 @@ KiB, MiB, GiB, TiB, PiB, or EiB, respectively. Case does not matter.
 +
 If \'max' is passed, the filesystem will occupy all available space on the
 device devid.
+If \'get_min_size' is passed, return the minimum size the device can be
+shrunk to, without performing any resize operation.
 +
 The resize command does not manipulate the size of underlying
 partition.  If you wish to enlarge/reduce a filesystem, you must make sure you
cmds-filesystem.c +254 -1

274 lines not on this page · raw message

ctree.h +3 -0
index f14a795..7d74d1a 100644
@@ -1491,6 +1491,9 @@ BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
 		   chunk_offset, 64);
 BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
 
+BTRFS_SETGET_STACK_FUNCS(stack_dev_extent_length, struct btrfs_dev_extent,
+			 length, 64);
+
 static inline u8 *btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
 {
 	unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
tests/misc-tests.sh +2 -0
index 5bbe914..2ae99db 100755
@@ -20,6 +20,8 @@ export RESULTS
 # For custom script needs to verfiy recovery
 export TEST_MNT
 export LANG
+# For tests that only use a loop device
+export IMAGE
 
 rm -f $RESULTS
 mkdir -p $TEST_MNT || _fail "unable to create mount point on $TEST_MNT"
tests/misc-tests/004-shrink-fs/test.sh new file +69 -0

69 lines not on this page · raw message

Signature · 1 line
2.1.3
maintainer
#7 lore raw
Quoted 10 lines · On Thu, Jul 16, 2015 at 04:47:13PM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

Currently there is not way for a user to know what is the minimum size a device of a btrfs filesystem can be resized to. Sometimes the value of total allocated space (sum of all allocated chunks/device extents), which can be parsed from 'btrfs filesystem show' and 'btrfs filesystem usage', works as the minimum size, but sometimes it does not, namely when device extents have to relocated to holes (unallocated space) within the new size of the device (the total allocated space sum).

Quoted 7 lines

This change adds the ability to reliably compute such minimum value and extents 'btrfs filesystem resize' with the following syntax to get such value:

btrfs filesystem resize [devid:]get_min_size

Applied, thanks. As mentioned before, the preferred interface is 'inspect-internal'. I've named the command 'min-dev-size' and added optional arugment --id as replacement for the original resize syntax. (It's a simple getopt stub so I'm not posting the patch.)