diff mbox series

[1/8] btrfs: Cleanup and simplify find_newest_super_backup

Message ID 20191015154224.21537-2-nborisov@suse.com (mailing list archive)
State New, archived
Headers show
Series tree reading cleanups in mount | expand

Commit Message

Nikolay Borisov Oct. 15, 2019, 3:42 p.m. UTC
Backup roots are always written in a circular manner. By definition we
can only ever have 1 backup root whose generation equals to that of the
superblock. Hence, the 'if' in the for loop will trigger at most once.
This is sufficient to return the newest backup root.

Furthermore thew newest_gen parameter is always set to the generation
of the superblock. This value can be obtained from the fs_info.

This patch removes the unnecessary code dealing with the wraparound
case and makes 'newest_gen' a local variable.
---
 fs/btrfs/disk-io.c | 29 ++++++++++-------------------
 1 file changed, 10 insertions(+), 19 deletions(-)

Comments

David Sterba Oct. 17, 2019, 1:56 p.m. UTC | #1
On Tue, Oct 15, 2019 at 06:42:17PM +0300, Nikolay Borisov wrote:
> Backup roots are always written in a circular manner. By definition we
> can only ever have 1 backup root whose generation equals to that of the
> superblock. Hence, the 'if' in the for loop will trigger at most once.
> This is sufficient to return the newest backup root.
> 
> Furthermore thew newest_gen parameter is always set to the generation
> of the superblock. This value can be obtained from the fs_info.
> 
> This patch removes the unnecessary code dealing with the wraparound
> case and makes 'newest_gen' a local variable.

"This patch cannot be applied without an SOB line." --n. borisov

And the rest does not have it either, I'll add it to avoid pointless
resends.

Tip of the day: use 'git commit -save' for every commit, it adds SOB,
opens editor for changelog and also shows the the diff
diff mbox series

Patch

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index de5696023391..8b1f6385023d 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1761,18 +1761,18 @@  static int transaction_kthread(void *arg)
 }
 
 /*
- * this will find the highest generation in the array of
+ * This will find the highest generation in the array of
  * root backups.  The index of the highest array is returned,
- * or -1 if we can't find anything.
+ * or -EINVAL if we can't find anything.
  *
  * We check to make sure the array is valid by comparing the
  * generation of the latest  root in the array with the generation
  * in the super block.  If they don't match we pitch it.
  */
-static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen)
+static int find_newest_super_backup(struct btrfs_fs_info *info)
 {
+	u64 newest_gen = btrfs_super_generation(info->super_copy);
 	u64 cur;
-	int newest_index = -1;
 	struct btrfs_root_backup *root_backup;
 	int i;
 
@@ -1780,17 +1780,10 @@  static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen)
 		root_backup = info->super_copy->super_roots + i;
 		cur = btrfs_backup_tree_root_gen(root_backup);
 		if (cur == newest_gen)
-			newest_index = i;
+			return i;
 	}
 
-	/* check to see if we actually wrapped around */
-	if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) {
-		root_backup = info->super_copy->super_roots;
-		cur = btrfs_backup_tree_root_gen(root_backup);
-		if (cur == newest_gen)
-			newest_index = 0;
-	}
-	return newest_index;
+	return -EINVAL;
 }
 
 
@@ -1802,11 +1795,11 @@  static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen)
 static void find_oldest_super_backup(struct btrfs_fs_info *info,
 				     u64 newest_gen)
 {
-	int newest_index = -1;
+	int newest_index;
 
-	newest_index = find_newest_super_backup(info, newest_gen);
+	newest_index = find_newest_super_backup(info);
 	/* if there was garbage in there, just move along */
-	if (newest_index == -1) {
+	if (newest_index == -EINVAL) {
 		info->backup_root_index = 0;
 	} else {
 		info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS;
@@ -1923,9 +1916,7 @@  static noinline int next_root_backup(struct btrfs_fs_info *info,
 	int newest = *backup_index;
 
 	if (*num_backups_tried == 0) {
-		u64 gen = btrfs_super_generation(super);
-
-		newest = find_newest_super_backup(info, gen);
+		newest = find_newest_super_backup(info);
 		if (newest == -1)
 			return -1;