]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
dm space maps: don't reset space map allocation cursor when committing
authorJoe Thornber <ejt@redhat.com>
Tue, 13 Apr 2021 08:03:49 +0000 (09:03 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 20 Jul 2021 14:22:37 +0000 (16:22 +0200)
[ Upstream commit 5faafc77f7de69147d1e818026b9a0cbf036a7b2 ]

Current commit code resets the place where the search for free blocks
will begin back to the start of the metadata device.  There are a couple
of repercussions to this:

- The first allocation after the commit is likely to take longer than
  normal as it searches for a free block in an area that is likely to
  have very few free blocks (if any).

- Any free blocks it finds will have been recently freed.  Reusing them
  means we have fewer old copies of the metadata to aid recovery from
  hardware error.

Fix these issues by leaving the cursor alone, only resetting when the
search hits the end of the metadata device.

Signed-off-by: Joe Thornber <ejt@redhat.com>
Signed-off-by: Mike Snitzer <snitzer@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/md/persistent-data/dm-space-map-disk.c
drivers/md/persistent-data/dm-space-map-metadata.c

index bf4c5e2ccb6ffc8d8e0fa5ba049429bdb6dc18d0..e0acae7a3815d4e026cf6b32dc0c4d868895678a 100644 (file)
@@ -171,6 +171,14 @@ static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
         * Any block we allocate has to be free in both the old and current ll.
         */
        r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, smd->begin, smd->ll.nr_blocks, b);
+       if (r == -ENOSPC) {
+               /*
+                * There's no free block between smd->begin and the end of the metadata device.
+                * We search before smd->begin in case something has been freed.
+                */
+               r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, 0, smd->begin, b);
+       }
+
        if (r)
                return r;
 
@@ -199,7 +207,6 @@ static int sm_disk_commit(struct dm_space_map *sm)
                return r;
 
        memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
-       smd->begin = 0;
        smd->nr_allocated_this_transaction = 0;
 
        r = sm_disk_get_nr_free(sm, &nr_free);
index 967d8f2a731fbda3a38b908e5511d91512ca3c76..62a4d7da9bd960fbf32cedb0107ae0f643019dd5 100644 (file)
@@ -451,6 +451,14 @@ static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
         * Any block we allocate has to be free in both the old and current ll.
         */
        r = sm_ll_find_common_free_block(&smm->old_ll, &smm->ll, smm->begin, smm->ll.nr_blocks, b);
+       if (r == -ENOSPC) {
+               /*
+                * There's no free block between smm->begin and the end of the metadata device.
+                * We search before smm->begin in case something has been freed.
+                */
+               r = sm_ll_find_common_free_block(&smm->old_ll, &smm->ll, 0, smm->begin, b);
+       }
+
        if (r)
                return r;
 
@@ -502,7 +510,6 @@ static int sm_metadata_commit(struct dm_space_map *sm)
                return r;
 
        memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
-       smm->begin = 0;
        smm->allocated_this_transaction = 0;
 
        return 0;