From fe140a69af90310bdad3dd455765fcd438c185f7 Mon Sep 17 00:00:00 2001
From: Vladimir Saveliev <c17830@cray.com>
Date: Thu, 21 Nov 2019 12:03:15 +0000
Subject: [PATCH] debugfs: add fake_fill_fs to fill fs for testing

The 'fake_fill_fs' command finds free blocks on a filesystem and
marks a specified fraction of those blocks in use.

Example: debugfs -w -R 'fake_fill_fs 50' <device>

This can be to used in order to quickly fragment a filesystem which
may useful for block allocation improvement estimation.

Use it carefully, there is no way to recover free space but using
e2fsck.

Change-Id: I5ef9eb250282f65b5d8b870b59a2c412bd97a1e5
Signed-off-by: Vladimir Saveliev <c17830@cray.com>
Signed-off-by: Artem Blagodarenko <c17828@cray.com>
Cray-bug-id: LUS-6746
---
 debugfs/debug_cmds.ct |  2 ++
 debugfs/debugfs.c     | 75 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)

diff --git a/debugfs/debug_cmds.ct b/debugfs/debug_cmds.ct
index 1ff6c9dc..12557888 100644
--- a/debugfs/debug_cmds.ct
+++ b/debugfs/debug_cmds.ct
@@ -229,5 +229,7 @@ request do_journal_write, "Write a transaction to the journal",
 request do_journal_run, "Recover the journal",
 	journal_run, jr;
 
+request do_fake_fill_fs, "Make the filesystem to look as if it was full",
+	fake_fill_fs;
 end;
 
diff --git a/debugfs/debugfs.c b/debugfs/debugfs.c
index faae12da..88473035 100644
--- a/debugfs/debugfs.c
+++ b/debugfs/debugfs.c
@@ -2164,6 +2164,81 @@ err:
 	ext2fs_free_mem(&buf);
 }
 
+void do_fake_fill_fs(int argc, char *argv[])
+{
+	__u64 block_count, i, g;
+	unsigned long v;
+	char *tmp;
+	int f, k, per_group;
+	unsigned long start = time(NULL);
+
+	if (common_args_process(argc, argv, 2, 2, argv[0],
+				"filling(%)", CHECK_FS_BITMAPS))
+		return;
+	if (check_fs_read_write(argv[0]))
+		return;
+	v = strtoul(argv[1], &tmp, 0);
+	if (v < 1 || v > 100) {
+		com_err("argv[0]", 0, "filling [1-100]");
+	}
+
+	per_group = current_fs->super->s_blocks_per_group;
+	block_count = ext2fs_blocks_count(current_fs->super);
+	for (g = 0; g < current_fs->group_desc_count; g++) {
+		__u64 upto;
+		__u64 use = 0;
+		int mark;
+
+		i = g * per_group;
+		upto = i + per_group;
+		if (upto > block_count)
+			upto = block_count;
+
+		if (ext2fs_bg_flags_test(current_fs, g, EXT2_BG_BLOCK_UNINIT)) {
+			errcode_t err;
+
+			/* skip first blocks consumed by metadata */
+			err = ext2fs_find_first_zero_block_bitmap2(current_fs->block_map, i, upto, &i);
+			assert (err == 0);
+			while (i < upto) {
+				mark = v;
+				if (i + mark > upto)
+					mark = upto - i;
+				assert(i < block_count);
+				assert(i + mark < block_count);
+				ext2fs_mark_block_bitmap_range2(current_fs->block_map, i, mark);
+				use += mark;
+				i += mark;
+				i += 100 - v;
+			}
+		} else {
+			int cnt = 0;
+			for (f = 0; i < upto; i++, cnt++) {
+				if (ext2fs_test_block_bitmap2(current_fs->block_map, i))
+					continue;
+
+				f %= 100;
+				if (f < v) {
+					ext2fs_mark_block_bitmap2(current_fs->block_map, i);
+					use++;
+				}
+				f ++;
+			}
+		}
+		if (g % (current_fs->group_desc_count / 100) == 0)
+			printf("now at %d (%d%%) after %d\n", g,
+				g * 100 / current_fs->group_desc_count,
+				time(NULL) - start);
+		ext2fs_bg_free_blocks_count_set(current_fs, g,
+				ext2fs_bg_free_blocks_count(current_fs, g) - use);
+		ext2fs_bg_flags_clear(current_fs, g, EXT2_BG_BLOCK_UNINIT);
+		ext2fs_group_desc_csum_set(current_fs, g);
+
+	}
+
+	ext2fs_mark_bb_dirty(current_fs);
+}
+
 #ifndef READ_ONLY
 void do_set_current_time(int argc, char *argv[])
 {
-- 
2.20.1