Skip to contentSkip to conversation

Batched discard support for Ext3/Ext4

PATCH In review Lukas Czerner 2 participants 7 messages Jul 27, 2010 – Jul 28, 2010

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

v3 cover Batched discard support for Ext3/Ext4

Hi all,

since my last post I have changed those patches slightly. Ext3 patch does not introduce DISCARD/NODISCARD mount option, so there is just FITRIM ioctl which can be invoked only by user with CAP_SYS_ADMIN.

Ext3 patch also involves journaling because as Jan pointed out not using journal can cause problems when system crashes. Jan please look at this I hope it is ok now. Also I am not sure if it would not be better to close the journal after allocating free space and then start new one after the TRIM (this was proposed by Josef Bacik).

Ext4 patch does not remove old "online" discard implementation and does not check for DISCARD mount option. To avoid journaling I have used alloc_sem semaphore to prevent allocations from the group while it is being trimmed. It seems sufficient to me.

The general idea of batched discards stays the same. So when FITRIM ioctl is invoked upon the mount point, it walks through all allocation groups searching for free extents bigger than minlen, then trim those extents. In ext3 consistency is assured by allocating these free extents in alloc. group (with journal involved). In ext4 consistency is assured by use of alloc_sem semaphore.

-Lukas

[PATCH 1/3] Add ioctl FITRIM.
[PATCH 2/3] Add batched discard support for ext3
[PATCH 3/3] Add batched discard support for ext4

#1 lore raw

PATCH v3 1/3 Add ioctl FITRIM.

Adds an filesystem independent ioctl to allow implementation of file system batched discard support.

---
 fs/ioctl.c         |   31 +++++++++++++++++++++++++++++++
 include/linux/fs.h |    2 ++
 2 files changed, 33 insertions(+), 0 deletions(-)
fs/ioctl.c +31 -0
index 7faefb4..09b33ae 100644
@@ -551,6 +551,33 @@ static int ioctl_fsthaw(struct file *filp)
 	return thaw_bdev(sb->s_bdev, sb);
 }
 
+static int ioctl_fstrim(struct file *filp, unsigned long arg)
+{
+	struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
+	unsigned int minlen;
+	int err;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	/* If filesystem doesn't support trim feature, return. */
+	if (sb->s_op->trim_fs == NULL)
+		return -EOPNOTSUPP;
+
+	/* If a blockdevice-backed filesystem isn't specified, return EINVAL. */
+	if (sb->s_bdev == NULL)
+		return -EINVAL;
+
+	err = get_user(minlen, (unsigned int __user *) arg);
+	if (err)
+		return err;
+
+	err = sb->s_op->trim_fs(minlen, sb);
+	if (err)
+		return err;
+	return 0;
+}
+
 /*
  * When you add any new common ioctls to the switches above and below
  * please update compat_sys_ioctl() too.
@@ -601,6 +628,10 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
 		error = ioctl_fsthaw(filp);
 		break;
 
+	case FITRIM:
+		error = ioctl_fstrim(filp, arg);
+		break;
+
 	case FS_IOC_FIEMAP:
 		return ioctl_fiemap(filp, arg);
 
include/linux/fs.h +2 -0
index 44f35ae..7a27fa4 100644
@@ -315,6 +315,7 @@ struct inodes_stat_t {
 #define FIGETBSZ   _IO(0x00,2)	/* get the block size used for bmap */
 #define FIFREEZE	_IOWR('X', 119, int)	/* Freeze */
 #define FITHAW		_IOWR('X', 120, int)	/* Thaw */
+#define FITRIM		_IOWR('X', 121, int)	/* Trim */
 
 #define	FS_IOC_GETFLAGS			_IOR('f', 1, long)
 #define	FS_IOC_SETFLAGS			_IOW('f', 2, long)
@@ -1580,6 +1581,7 @@ struct super_operations {
 	ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
 #endif
 	int (*bdev_try_to_free_page)(struct super_block*, struct page*, gfp_t);
+	int (*trim_fs) (unsigned int, struct super_block *);
 };
 
 /*
Signature · 1 line
1.7.1.1
#2 lore raw

PATCH v3 2/3 Add batched discard support for ext3

In reply to Lukas Czerner

Walk through each allocation group and trim all free extents. It can be invoked through TRIM ioctl on the file system. The main idea is to provide a way to trim the whole file system if needed, since some SSD's may suffer from performance loss after the whole device was filled (it does not mean that fs is full!).

It search for free extents in each allocation group. When the free extent is found, blocks are marked as used and then trimmed. Afterwards these blocks are marked as free in per-group bitmap.

---
 fs/ext3/balloc.c        |  229 +++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext3/super.c         |    1 +
 include/linux/ext3_fs.h |    1 +
 3 files changed, 231 insertions(+), 0 deletions(-)
fs/ext3/balloc.c +229 -0
index a177122..97c6867 100644
@@ -20,6 +20,7 @@
 #include <linux/ext3_jbd.h>
 #include <linux/quotaops.h>
 #include <linux/buffer_head.h>
+#include <linux/blkdev.h>
 
 /*
  * balloc.c contains the blocks allocation and deallocation routines
@@ -1876,3 +1877,231 @@ unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
 	return ext3_bg_num_gdb_meta(sb,group);
 
 }
+
+/**
+ * ext3_trim_all_free -- function to trim all free space in alloc. group
+ * @sb:			super block for file system
+ * @group:		allocation group to trim
+ * @gdp:		allocation group description structure
+ * @minblocks:		minimum extent block count
+ *
+ * ext3_trim_all_free walks through group's block bitmap searching for free
+ * blocks. When the free block is found, it tries to allocate this block and
+ * consequent free block to get the biggest free extent possible, until it
+ * reaches any used block. Then issue a TRIM command on this extent and free
+ * the extent in the block bitmap. This is done until whole group is scanned.
+ */
+ext3_grpblk_t ext3_trim_all_free(struct super_block *sb,
+			unsigned int group, ext3_grpblk_t minblocks)
+{
+	handle_t *handle;
+	ext3_grpblk_t max = EXT3_BLOCKS_PER_GROUP(sb);
+	ext3_grpblk_t next, count = 0, start, bit;
+	struct ext3_sb_info *sbi;
+	ext3_fsblk_t discard_block;
+	struct buffer_head *bitmap_bh = NULL;
+	struct buffer_head *gdp_bh;
+	ext3_grpblk_t free_blocks;
+	struct ext3_group_desc *gdp;
+	int err = 0, ret;
+	ext3_grpblk_t freed;
+
+	/**
+	 * We will update one block bitmap, and one group descriptor
+	 */
+	handle = ext3_journal_start_sb(sb, 2);
+	if (IS_ERR(handle)) {
+		err = PTR_ERR(handle);
+		ext3_warning(sb, __func__, "error %d on journal start", err);
+		return err;
+	}
+
+	bitmap_bh = read_block_bitmap(sb, group);
+	if (!bitmap_bh)
+		goto err_out;
+
+	BUFFER_TRACE(bitmap_bh, "getting undo access");
+	err = ext3_journal_get_undo_access(handle, bitmap_bh);
+	if (err)
+		goto err_out;
+
+	gdp = ext3_get_group_desc(sb, group, &gdp_bh);
+	if (!gdp)
+		goto err_out;
+
+	BUFFER_TRACE(gd_bh, "get_write_access");
+	err = ext3_journal_get_write_access(handle, gdp_bh);
+	if (err)
+		goto err_out;
+
+	free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
+	sbi = EXT3_SB(sb);
+
+	 /* Walk through the whole group */
+	start = 0;
+	while (start < max) {
+
+		start = bitmap_search_next_usable_block(start, bitmap_bh, max);
+		if (start < 0)
+			break;
+		next = start;
+
+		/**
+		 * Allocate contiguous free extents by setting bits in the
+		 * block bitmap
+		 */
+		while (next < max
+			&& claim_block(sb_bgl_lock(sbi, group),
+					next, bitmap_bh)) {
+			next++;
+		}
+
+		 /* We did not claimed any blocks */
+		if (next == start)
+			continue;
+
+		discard_block = (ext3_fsblk_t)start +
+				ext3_group_first_block_no(sb, group);
+
+		/* Update counters */
+		spin_lock(sb_bgl_lock(sbi, group));
+		le16_add_cpu(&gdp->bg_free_blocks_count, start - next);
+		spin_unlock(sb_bgl_lock(sbi, group));
+		percpu_counter_sub(&sbi->s_freeblocks_counter, next - start);
+
+		/* Do not issue a TRIM on extents smaller than minblocks */
+		if ((next - start) < minblocks)
+			goto free_extent;
+
+		 /* Send the TRIM command down to the device */
+		sb_issue_discard(sb, discard_block, next - start);
+		count += (next - start);
+		cond_resched();
+
+free_extent:
+
+		freed = 0;
+		jbd_lock_bh_state(bitmap_bh);
+
+		for (bit = start; bit < next; bit++) {
+
+			/**
+			 * @@@ This prevents newly-allocated data from being
+			 * freed and then reallocated within the same
+			 * transaction.
+			 */
+			BUFFER_TRACE(bitmap_bh, "set in b_committed_data");
+			J_ASSERT_BH(bitmap_bh,
+				bh2jh(bitmap_bh)->b_committed_data != NULL);
+			ext3_set_bit_atomic(sb_bgl_lock(sbi, group), bit,
+				bh2jh(bitmap_bh)->b_committed_data);
+
+			/**
+			 * We clear the bit in the bitmap after setting the
+			 * committed data bit, because this is the reverse
+			 * order to that which the allocator uses.
+			 */
+			BUFFER_TRACE(bitmap_bh, "clear bit");
+			if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, group),
+						bit, bitmap_bh->b_data)) {
+				jbd_unlock_bh_state(bitmap_bh);
+				ext3_error(sb, __func__,
+					"bit already cleared for block "E3FSBLK,
+					 (unsigned long)bit);
+				jbd_lock_bh_state(bitmap_bh);
+				BUFFER_TRACE(bitmap_bh, "bit already cleared");
+			} else {
+				freed++;
+			}
+		}
+
+		jbd_unlock_bh_state(bitmap_bh);
+
+		/* Update couters */
+		spin_lock(sb_bgl_lock(sbi, group));
+		le16_add_cpu(&gdp->bg_free_blocks_count, freed);
+		spin_unlock(sb_bgl_lock(sbi, group));
+		percpu_counter_add(&sbi->s_freeblocks_counter, next - start);
+
+		start = next;
+
+		if (signal_pending(current)) {
+			count = -ERESTARTSYS;
+			break;
+		}
+
+		/* No more suitable extents */
+		if ((free_blocks - count) < minblocks)
+			break;
+	}
+
+	/* We dirtied the bitmap block */
+	BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
+	err = ext3_journal_dirty_metadata(handle, bitmap_bh);
+
+	/* And the group descriptor block */
+	BUFFER_TRACE(gdp_bh, "dirtied group descriptor block");
+	ret = ext3_journal_dirty_metadata(handle, gdp_bh);
+	if (!err)
+		err = ret;
+
+	ext3_debug("trimmed %d blocks in the group %d\n",
+		count, group);
+
+err_out:
+	if (err) {
+		ext3_std_error(sb, err);
+		count = -err;
+	}
+
+	ext3_journal_stop(handle);
+	brelse(bitmap_bh);
+
+	return count;
+}
+
+/**
+ * ext3_trim_fs() -- trim ioctl handle function
+ * @minlen:		minimum extent length in Bytes
+ * @sb:			superblock for filesystem
+ *
+ * ext3_trim_fs goes through all allocation group searching for groups with more
+ * free space than minlen. For such a group ext3_trim_all_free function is
+ * invoked to trim all free space.
+ */
+int ext3_trim_fs(unsigned int minlen, struct super_block *sb)
+{
+	ext3_grpblk_t minblocks;
+	unsigned long ngroups;
+	unsigned int group;
+	struct ext3_group_desc *gdp;
+	ext3_grpblk_t free_blocks;
+	int ret = 0;
+
+	minblocks = DIV_ROUND_UP(minlen, sb->s_blocksize);
+	if (unlikely(minblocks > EXT3_BLOCKS_PER_GROUP(sb)))
+		return -EINVAL;
+
+	ngroups = EXT3_SB(sb)->s_groups_count;
+	smp_rmb();
+
+	for (group = 0; group < ngroups; group++) {
+
+		gdp = ext3_get_group_desc(sb, group, NULL);
+		if (!gdp)
+			break;
+
+		free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
+		if (free_blocks < minblocks)
+			continue;
+
+		ret = ext3_trim_all_free(sb, group, minblocks);
+		if (ret < 0)
+			break;
+	}
+
+	if (ret >= 0)
+		ret = 0;
+
+	return ret;
+}
fs/ext3/super.c +1 -0
index 1bee604..75688f3 100644
@@ -791,6 +791,7 @@ static const struct super_operations ext3_sops = {
 	.quota_write	= ext3_quota_write,
 #endif
 	.bdev_try_to_free_page = bdev_try_to_free_page,
+	.trim_fs	= ext3_trim_fs,
 };
 
 static const struct export_operations ext3_export_ops = {
include/linux/ext3_fs.h +1 -0
index 5f494b4..6e3fefc 100644
@@ -857,6 +857,7 @@ extern struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb,
 extern int ext3_should_retry_alloc(struct super_block *sb, int *retries);
 extern void ext3_init_block_alloc_info(struct inode *);
 extern void ext3_rsv_window_add(struct super_block *sb, struct ext3_reserve_window_node *rsv);
+extern int ext3_trim_fs(unsigned int minlen, struct super_block *sb);
 
 /* dir.c */
 extern int ext3_check_dir_entry(const char *, struct inode *,
Signature · 1 line
1.7.1.1
reviewer
#3 lore raw

Re: [PATCH 2/3] Add batched discard support for ext3

Quoted 9 lines · Lukas Czerner

Walk through each allocation group and trim all free extents. It can be invoked through TRIM ioctl on the file system. The main idea is to provide a way to trim the whole file system if needed, since some SSD's may suffer from performance loss after the whole device was filled (it does not mean that fs is full!).

It search for free extents in each allocation group. When the free extent is found, blocks are marked as used and then trimmed. Afterwards these blocks are marked as free in per-group bitmap.

Thanks for the patch. It looks much better than the previous one. Some
comments below.

Quoted 49 lines
---
 fs/ext3/balloc.c        |  229 +++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext3/super.c         |    1 +
 include/linux/ext3_fs.h |    1 +
 3 files changed, 231 insertions(+), 0 deletions(-)
fs/ext3/balloc.c +27 -0
index a177122..97c6867 100644
@@ -20,6 +20,7 @@
 #include <linux/ext3_jbd.h>
 #include <linux/quotaops.h>
 #include <linux/buffer_head.h>
+#include <linux/blkdev.h>
 
 /*
  * balloc.c contains the blocks allocation and deallocation routines
@@ -1876,3 +1877,231 @@ unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
 	return ext3_bg_num_gdb_meta(sb,group);
 
 }
+
+/**
+ * ext3_trim_all_free -- function to trim all free space in alloc. group
+ * @sb:			super block for file system
+ * @group:		allocation group to trim
+ * @gdp:		allocation group description structure
+ * @minblocks:		minimum extent block count
+ *
+ * ext3_trim_all_free walks through group's block bitmap searching for free
+ * blocks. When the free block is found, it tries to allocate this block and
+ * consequent free block to get the biggest free extent possible, until it
+ * reaches any used block. Then issue a TRIM command on this extent and free
+ * the extent in the block bitmap. This is done until whole group is scanned.
+ */
+ext3_grpblk_t ext3_trim_all_free(struct super_block *sb,
+			unsigned int group, ext3_grpblk_t minblocks)
+{
+	handle_t *handle;
+	ext3_grpblk_t max = EXT3_BLOCKS_PER_GROUP(sb);
+	ext3_grpblk_t next, count = 0, start, bit;
+	struct ext3_sb_info *sbi;
+	ext3_fsblk_t discard_block;
+	struct buffer_head *bitmap_bh = NULL;
+	struct buffer_head *gdp_bh;
+	ext3_grpblk_t free_blocks;
+	struct ext3_group_desc *gdp;

fs/ext3/balloc.c unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) · line 1915

Quoted 10 lines
fs/ext3/balloc.c +10 -0
+	int err = 0, ret;
+	ext3_grpblk_t freed;
+
+	/**
+	 * We will update one block bitmap, and one group descriptor
+	 */
+	handle = ext3_journal_start_sb(sb, 2);
+	if (IS_ERR(handle)) {
+		err = PTR_ERR(handle);
+		ext3_warning(sb, __func__, "error %d on journal start", err);

I think there's no need to issue another warning here... When
journal_start fails there should be notices about this in the log already.

Quoted 24 lines
fs/ext3/balloc.c +24 -0
+		return err;
+	}
+
+	bitmap_bh = read_block_bitmap(sb, group);
+	if (!bitmap_bh)
+		goto err_out;
+
+	BUFFER_TRACE(bitmap_bh, "getting undo access");
+	err = ext3_journal_get_undo_access(handle, bitmap_bh);
+	if (err)
+		goto err_out;
+
+	gdp = ext3_get_group_desc(sb, group, &gdp_bh);
+	if (!gdp)
+		goto err_out;
+
+	BUFFER_TRACE(gd_bh, "get_write_access");
+	err = ext3_journal_get_write_access(handle, gdp_bh);
+	if (err)
+		goto err_out;
+
+	free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
+	sbi = EXT3_SB(sb);
+

fs/ext3/balloc.c unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) · line 1949

Quoted 10 lines
fs/ext3/balloc.c +10 -0
+	 /* Walk through the whole group */
+	start = 0;
+	while (start < max) {
+
+		start = bitmap_search_next_usable_block(start, bitmap_bh, max);
+		if (start < 0)
+			break;
+		next = start;
+
+		/**

^^^ Use just /*. /** is reserved for comments processed by
kernel documentation generator and they have to have special format...

fs/ext3/balloc.c unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) · line 1959

Quoted 10 lines
fs/ext3/balloc.c +10 -0
+		 * Allocate contiguous free extents by setting bits in the
+		 * block bitmap
+		 */
+		while (next < max
+			&& claim_block(sb_bgl_lock(sbi, group),
+					next, bitmap_bh)) {
+			next++;
+		}
+
+		 /* We did not claimed any blocks */
                               ^^^^ claim
Quoted 10 lines
fs/ext3/balloc.c +10 -0
+		if (next == start)
+			continue;
+
+		discard_block = (ext3_fsblk_t)start +
+				ext3_group_first_block_no(sb, group);
+
+		/* Update counters */
+		spin_lock(sb_bgl_lock(sbi, group));
+		le16_add_cpu(&gdp->bg_free_blocks_count, start - next);
+		spin_unlock(sb_bgl_lock(sbi, group));

fs/ext3/balloc.c unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) · line 1979

Quoted 10 lines
fs/ext3/balloc.c +10 -0
+		percpu_counter_sub(&sbi->s_freeblocks_counter, next - start);
+
+		/* Do not issue a TRIM on extents smaller than minblocks */
+		if ((next - start) < minblocks)
+			goto free_extent;
+
+		 /* Send the TRIM command down to the device */
+		sb_issue_discard(sb, discard_block, next - start);
+		count += (next - start);
+		cond_resched();

Maybe you could cond_resched() after freeing the blocks instead of here?
So that we don't sleep with blocks unnecessarily allocated?

Quoted 8 lines
fs/ext3/balloc.c +8 -0
+
+free_extent:
+
+		freed = 0;
+		jbd_lock_bh_state(bitmap_bh);
+
+		for (bit = start; bit < next; bit++) {
+

fs/ext3/balloc.c unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) · line 1997

Quoted 10 lines
fs/ext3/balloc.c +10 -0
+			/**
+			 * @@@ This prevents newly-allocated data from being
+			 * freed and then reallocated within the same
+			 * transaction.
+			 */
+			BUFFER_TRACE(bitmap_bh, "set in b_committed_data");
+			J_ASSERT_BH(bitmap_bh,
+				bh2jh(bitmap_bh)->b_committed_data != NULL);
+			ext3_set_bit_atomic(sb_bgl_lock(sbi, group), bit,
+				bh2jh(bitmap_bh)->b_committed_data);

You don't have to do this. Since you didn't really allocate the blocks
(you are actually never going to commit bitmap changes to the journal) blocks can be happily reallocated just after you clear the bits in the bitmap. There's no problem with that. Also you don't have to hold bh_state lock at all.

Quoted 54 lines
fs/ext3/balloc.c +54 -0
+
+			/**
+			 * We clear the bit in the bitmap after setting the
+			 * committed data bit, because this is the reverse
+			 * order to that which the allocator uses.
+			 */
+			BUFFER_TRACE(bitmap_bh, "clear bit");
+			if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, group),
+						bit, bitmap_bh->b_data)) {
+				jbd_unlock_bh_state(bitmap_bh);
+				ext3_error(sb, __func__,
+					"bit already cleared for block "E3FSBLK,
+					 (unsigned long)bit);
+				jbd_lock_bh_state(bitmap_bh);
+				BUFFER_TRACE(bitmap_bh, "bit already cleared");
+			} else {
+				freed++;
+			}
+		}
+
+		jbd_unlock_bh_state(bitmap_bh);
+
+		/* Update couters */
+		spin_lock(sb_bgl_lock(sbi, group));
+		le16_add_cpu(&gdp->bg_free_blocks_count, freed);
+		spin_unlock(sb_bgl_lock(sbi, group));
+		percpu_counter_add(&sbi->s_freeblocks_counter, next - start);
+
+		start = next;
+
+		if (signal_pending(current)) {
+			count = -ERESTARTSYS;
+			break;
+		}
+
+		/* No more suitable extents */
+		if ((free_blocks - count) < minblocks)
+			break;
+	}
+
+	/* We dirtied the bitmap block */
+	BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
+	err = ext3_journal_dirty_metadata(handle, bitmap_bh);
+
+	/* And the group descriptor block */
+	BUFFER_TRACE(gdp_bh, "dirtied group descriptor block");
+	ret = ext3_journal_dirty_metadata(handle, gdp_bh);
+	if (!err)
+		err = ret;
+
+	ext3_debug("trimmed %d blocks in the group %d\n",
+		count, group);
+
+err_out:

fs/ext3/balloc.c unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) · line 2061

Quoted 10 lines
fs/ext3/balloc.c +10 -0
+	if (err) {
+		ext3_std_error(sb, err);
+		count = -err;
+	}
+
+	ext3_journal_stop(handle);
+	brelse(bitmap_bh);
+
+	return count;
+}

Otherwise the patch looks OK.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw

Re: [PATCH 2/3] Add batched discard support for ext3

Quoted 66 lines · Jan Kara
Quoted 9 lines · Lukas Czerner

Walk through each allocation group and trim all free extents. It can be invoked through TRIM ioctl on the file system. The main idea is to provide a way to trim the whole file system if needed, since some SSD's may suffer from performance loss after the whole device was filled (it does not mean that fs is full!).

It search for free extents in each allocation group. When the free extent is found, blocks are marked as used and then trimmed. Afterwards these blocks are marked as free in per-group bitmap.

Thanks for the patch. It looks much better than the previous one. Some
comments below.

Quoted 53 lines
---
 fs/ext3/balloc.c        |  229 +++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext3/super.c         |    1 +
 include/linux/ext3_fs.h |    1 +
 3 files changed, 231 insertions(+), 0 deletions(-)
fs/ext3/balloc.c +31 -0
index a177122..97c6867 100644
@@ -20,6 +20,7 @@
 #include <linux/ext3_jbd.h>
 #include <linux/quotaops.h>
 #include <linux/buffer_head.h>
+#include <linux/blkdev.h>
 
 /*
  * balloc.c contains the blocks allocation and deallocation routines
@@ -1876,3 +1877,231 @@ unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
 	return ext3_bg_num_gdb_meta(sb,group);
 
 }
+
+/**
+ * ext3_trim_all_free -- function to trim all free space in alloc. group
+ * @sb:			super block for file system
+ * @group:		allocation group to trim
+ * @gdp:		allocation group description structure
+ * @minblocks:		minimum extent block count
+ *
+ * ext3_trim_all_free walks through group's block bitmap searching for free
+ * blocks. When the free block is found, it tries to allocate this block and
+ * consequent free block to get the biggest free extent possible, until it
+ * reaches any used block. Then issue a TRIM command on this extent and free
+ * the extent in the block bitmap. This is done until whole group is scanned.
+ */
+ext3_grpblk_t ext3_trim_all_free(struct super_block *sb,
+			unsigned int group, ext3_grpblk_t minblocks)
+{
+	handle_t *handle;
+	ext3_grpblk_t max = EXT3_BLOCKS_PER_GROUP(sb);
+	ext3_grpblk_t next, count = 0, start, bit;
+	struct ext3_sb_info *sbi;
+	ext3_fsblk_t discard_block;
+	struct buffer_head *bitmap_bh = NULL;
+	struct buffer_head *gdp_bh;
+	ext3_grpblk_t free_blocks;
+	struct ext3_group_desc *gdp;
+	int err = 0, ret;
+	ext3_grpblk_t freed;
+
+	/**
Quoted 8 lines

fs/ext3/balloc.c unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) · line 1915

Quoted 6 lines
fs/ext3/balloc.c +6 -0
+	 * We will update one block bitmap, and one group descriptor
+	 */
+	handle = ext3_journal_start_sb(sb, 2);
+	if (IS_ERR(handle)) {
+		err = PTR_ERR(handle);
+		ext3_warning(sb, __func__, "error %d on journal start", err);

I think there's no need to issue another warning here... When
journal_start fails there should be notices about this in the log already.

That's right, thanks.

Quoted 29 lines
Quoted 29 lines
fs/ext3/balloc.c +28 -0
+		return err;
+	}
+
+	bitmap_bh = read_block_bitmap(sb, group);
+	if (!bitmap_bh)
+		goto err_out;
+
+	BUFFER_TRACE(bitmap_bh, "getting undo access");
+	err = ext3_journal_get_undo_access(handle, bitmap_bh);
+	if (err)
+		goto err_out;
+
+	gdp = ext3_get_group_desc(sb, group, &gdp_bh);
+	if (!gdp)
+		goto err_out;
+
+	BUFFER_TRACE(gd_bh, "get_write_access");
+	err = ext3_journal_get_write_access(handle, gdp_bh);
+	if (err)
+		goto err_out;
+
+	free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
+	sbi = EXT3_SB(sb);
+
+	 /* Walk through the whole group */
+	start = 0;
+	while (start < max) {
+
Quoted 8 lines

fs/ext3/balloc.c unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) · line 1949

Quoted 6 lines
fs/ext3/balloc.c +6 -0
+		start = bitmap_search_next_usable_block(start, bitmap_bh, max);
+		if (start < 0)
+			break;
+		next = start;
+
+		/**

^^^ Use just /*. /** is reserved for comments processed by
kernel documentation generator and they have to have special format...

Ok.

Quoted 12 lines

fs/ext3/balloc.c unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) · line 1959

Quoted 11 lines
fs/ext3/balloc.c +10 -0
+		 * Allocate contiguous free extents by setting bits in the
+		 * block bitmap
+		 */
+		while (next < max
+			&& claim_block(sb_bgl_lock(sbi, group),
+					next, bitmap_bh)) {
+			next++;
+		}
+
+		 /* We did not claimed any blocks */
                               ^^^^ claim

Oh, that is my astonishing English skills ;). Thanks.

Quoted 15 lines
Quoted 15 lines
fs/ext3/balloc.c +14 -0
+		if (next == start)
+			continue;
+
+		discard_block = (ext3_fsblk_t)start +
+				ext3_group_first_block_no(sb, group);
+
+		/* Update counters */
+		spin_lock(sb_bgl_lock(sbi, group));
+		le16_add_cpu(&gdp->bg_free_blocks_count, start - next);
+		spin_unlock(sb_bgl_lock(sbi, group));
+		percpu_counter_sub(&sbi->s_freeblocks_counter, next - start);
+
+		/* Do not issue a TRIM on extents smaller than minblocks */
+		if ((next - start) < minblocks)
Quoted 8 lines

fs/ext3/balloc.c unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) · line 1979

Quoted 6 lines
fs/ext3/balloc.c +6 -0
+			goto free_extent;
+
+		 /* Send the TRIM command down to the device */
+		sb_issue_discard(sb, discard_block, next - start);
+		count += (next - start);
+		cond_resched();

Maybe you could cond_resched() after freeing the blocks instead of here?
So that we don't sleep with blocks unnecessarily allocated?

Of course, that would be better.

Quoted 16 lines
Quoted 16 lines
fs/ext3/balloc.c +15 -0
+
+free_extent:
+
+		freed = 0;
+		jbd_lock_bh_state(bitmap_bh);
+
+		for (bit = start; bit < next; bit++) {
+
+			/**
+			 * @@@ This prevents newly-allocated data from being
+			 * freed and then reallocated within the same
+			 * transaction.
+			 */
+			BUFFER_TRACE(bitmap_bh, "set in b_committed_data");
+			J_ASSERT_BH(bitmap_bh,
Quoted 8 lines

fs/ext3/balloc.c unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) · line 1997

Quoted 3 lines
fs/ext3/balloc.c +3 -0
+				bh2jh(bitmap_bh)->b_committed_data != NULL);
+			ext3_set_bit_atomic(sb_bgl_lock(sbi, group), bit,
+				bh2jh(bitmap_bh)->b_committed_data);

You don't have to do this. Since you didn't really allocate the blocks
(you are actually never going to commit bitmap changes to the journal) blocks can be happily reallocated just after you clear the bits in the bitmap. There's no problem with that. Also you don't have to hold bh_state lock at all.

Thanks.

Quoted 58 lines
Quoted 58 lines
fs/ext3/balloc.c +57 -0
+
+			/**
+			 * We clear the bit in the bitmap after setting the
+			 * committed data bit, because this is the reverse
+			 * order to that which the allocator uses.
+			 */
+			BUFFER_TRACE(bitmap_bh, "clear bit");
+			if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, group),
+						bit, bitmap_bh->b_data)) {
+				jbd_unlock_bh_state(bitmap_bh);
+				ext3_error(sb, __func__,
+					"bit already cleared for block "E3FSBLK,
+					 (unsigned long)bit);
+				jbd_lock_bh_state(bitmap_bh);
+				BUFFER_TRACE(bitmap_bh, "bit already cleared");
+			} else {
+				freed++;
+			}
+		}
+
+		jbd_unlock_bh_state(bitmap_bh);
+
+		/* Update couters */
+		spin_lock(sb_bgl_lock(sbi, group));
+		le16_add_cpu(&gdp->bg_free_blocks_count, freed);
+		spin_unlock(sb_bgl_lock(sbi, group));
+		percpu_counter_add(&sbi->s_freeblocks_counter, next - start);
+
+		start = next;
+
+		if (signal_pending(current)) {
+			count = -ERESTARTSYS;
+			break;
+		}
+
+		/* No more suitable extents */
+		if ((free_blocks - count) < minblocks)
+			break;
+	}
+
+	/* We dirtied the bitmap block */
+	BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
+	err = ext3_journal_dirty_metadata(handle, bitmap_bh);
+
+	/* And the group descriptor block */
+	BUFFER_TRACE(gdp_bh, "dirtied group descriptor block");
+	ret = ext3_journal_dirty_metadata(handle, gdp_bh);
+	if (!err)
+		err = ret;
+
+	ext3_debug("trimmed %d blocks in the group %d\n",
+		count, group);
+
+err_out:
+	if (err) {
+		ext3_std_error(sb, err);
+		count = -err;
Quoted 8 lines

fs/ext3/balloc.c unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) · line 2061

Quoted 7 lines
fs/ext3/balloc.c +7 -0
+	}
+
+	ext3_journal_stop(handle);
+	brelse(bitmap_bh);
+
+	return count;
+}

Otherwise the patch looks OK.

Great, thanks for looking at this. I will resend the patch shortly.

Quoted 3 lines

Honza

-Lukas

#5 lore raw

PATCH v3 3/3 Add batched discard support for ext4

In reply to Lukas Czerner

Walk through each allocation group and trim all free extents. It can be invoked through TRIM ioctl on the file system. The main idea is to provide a way to trim the whole file system if needed, since some SSD's may suffer from performance loss after the whole device was filled (it does not mean that fs is full!).

It search fro free extents in each allocation group. When the free extent is found, blocks are marked as used and then trimmed. Afterwards these blocks are marked as free in per-group bitmap.

Since fstrim is a long operation it is good to have an ability to interrupt it by a signal. This was added by Dmitry Monakhov. Thanks Dimitry.

---
 fs/ext4/ext4.h    |    2 +
 fs/ext4/mballoc.c |  103 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/super.c   |    1 +
 3 files changed, 106 insertions(+), 0 deletions(-)
fs/ext4/ext4.h +2 -0
index bf938cf..ba0fff0 100644
@@ -1437,6 +1437,8 @@ extern int ext4_mb_add_groupinfo(struct super_block *sb,
 extern int ext4_mb_get_buddy_cache_lock(struct super_block *, ext4_group_t);
 extern void ext4_mb_put_buddy_cache_lock(struct super_block *,
 						ext4_group_t, int);
+extern int ext4_trim_fs(unsigned int, struct super_block *);
+
 /* inode.c */
 struct buffer_head *ext4_getblk(handle_t *, struct inode *,
 						ext4_lblk_t, int, int *);
fs/ext4/mballoc.c +103 -0

106 lines not on this page · raw message

fs/ext4/super.c +1 -0
index e14d22c..253eb98 100644
@@ -1109,6 +1109,7 @@ static const struct super_operations ext4_sops = {
 	.quota_write	= ext4_quota_write,
 #endif
 	.bdev_try_to_free_page = bdev_try_to_free_page,
+	.trim_fs	= ext4_trim_fs
 };
 
 static const struct super_operations ext4_nojournal_sops = {
Signature · 1 line
1.7.1.1
reviewer
#6 lore raw

Re: [PATCH 3/3] Add batched discard support for ext4

Quoted 10 lines · Lukas Czerner

Walk through each allocation group and trim all free extents. It can be invoked through TRIM ioctl on the file system. The main idea is to provide a way to trim the whole file system if needed, since some SSD's may suffer from performance loss after the whole device was filled (it does not mean that fs is full!).

It search fro free extents in each allocation group. When the free extent is found, blocks are marked as used and then trimmed. Afterwards these blocks are marked as free in per-group bitmap.

Quoted 5 lines

Since fstrim is a long operation it is good to have an ability to interrupt it by a signal. This was added by Dmitry Monakhov. Thanks Dimitry.

A couple of comments below...

Quoted 19 lines
---
 fs/ext4/ext4.h    |    2 +
 fs/ext4/mballoc.c |  103 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/super.c   |    1 +
 3 files changed, 106 insertions(+), 0 deletions(-)
fs/ext4/mballoc.c +5 -0
index b423a36..f00b7dd 100644
@@ -4640,3 +4640,106 @@ error_return:
 		kmem_cache_free(ext4_ac_cachep, ac);
 	return;
 }
+
+/**
+ * Trim "count" blocks starting at "start" in "group"
+ * This must be called under group lock
+ */

fs/ext4/mballoc.c error_return: · line 4657

Quoted 10 lines
fs/ext4/mballoc.c +10 -0
+static void ext4_trim_extent(struct super_block *sb, int start, int count,
+		ext4_group_t group)
+{
+	ext4_fsblk_t discard_block;
+	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
+
+	discard_block = (ext4_fsblk_t)group *
+			EXT4_BLOCKS_PER_GROUP(sb)
+			+ start
+			+ le32_to_cpu(es->s_first_data_block);

You can use ext4_group_first_block_no() I believe.

Quoted 24 lines
fs/ext4/mballoc.c +24 -0
+	trace_ext4_discard_blocks(sb,
+			(unsigned long long)discard_block,
+			count);
+	sb_issue_discard(sb, discard_block, count);
+	cond_resched();
+}
+
+/**
+ * Trim all free extents in group at least minblocks long
+ */
+ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
+		ext4_grpblk_t minblocks)
+{
+	struct buffer_head *bitmap_bh = NULL;
+	ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb);
+	ext4_grpblk_t start, next, count = 0;
+	struct ext4_group_info *grp;
+	int err = 0;
+
+	err = -EIO;
+	bitmap_bh = ext4_read_block_bitmap(sb, group);
+	if (!bitmap_bh)
+		return 0;
+

fs/ext4/mballoc.c error_return: · line 4691

Quoted 10 lines
fs/ext4/mballoc.c +10 -0
+	grp = ext4_get_group_info(sb, group);
+	start = grp->bb_first_free;
+
+	down_write(&grp->alloc_sem);
+	while (start < max) {
+
+		start = mb_find_next_zero_bit(bitmap_bh->b_data, max, start);
+		if (start >= max)
+			break;
+		next = mb_find_next_bit(bitmap_bh->b_data, max, start);

Hmm, I don't think this is right. If you want to avoid doing the same
thing as you do for ext3, you have to use the buddy bitmap and not the on-disk bitmap (we free blocks from a buddy bitmap only after a transaction freeing them is committed). That way you avoid trimming blocks that were freed in the transaction which is just committing (you mustn't do that). So you have to load the buddy bitmap for the group into memory (ext4_mb_load_buddy()), lock the group (ext4_group_lock()), and then you can investigate the buddy bitmap (EXT4_MB_BITMAP()). You could actually use the buddy information to make scanning bitmap faster (it carries information about larger chunks of free blocks) but that's a voluntary bonus I think ;).

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR