Skip to contentSkip to conversation

ext4: Add the EXT4_IOC_DEFRAG ioctl and defrag main function

RFC PATCH In review Akira Fujita 1 participant 1 message Oct 24, 2008

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

RFC PATCH 1/9 ext4: Add the EXT4_IOC_DEFRAG ioctl and defrag main function

ext4: online defrag: Add the EXT4_IOC_DEFRAG ioctl and defrag main function.

From: Akira Fujita <[email protected]>

The EXT4_IOC_DEFRAG ioctl is the main ioctl of ext4 online defrag. Create the temporary inode in memory and allocate contiguous blocks to it. After that exchange data blocks between original inode and temporary inode. Lastly, remove the temporary inode which has fragmented data blocks. This method would be done between user space and kernel space per 64MB data size.

This patch is equivalent to creating/removing a temporary inode and common defrag functions.

---
 fs/ext4/Makefile       |    2 +-
 fs/ext4/defrag.c       |  499 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/ext4.h         |   10 +
 fs/ext4/ext4_extents.h |    2 +
 fs/ext4/extents.c      |    2 +-
 fs/ext4/ioctl.c        |   22 ++
 6 files changed, 535 insertions(+), 2 deletions(-)
fs/ext4/Makefile +1 -1
index a8ff003..e7fd8ba 100644
@@ -6,7 +6,7 @@ obj-$(CONFIG_EXT4_FS) += ext4.o
 
 ext4-y	:= balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
 		   ioctl.o namei.o super.o symlink.o hash.o resize.o extents.o \
-		   ext4_jbd2.o migrate.o mballoc.o
+		   ext4_jbd2.o migrate.o mballoc.o defrag.o
 
 ext4-$(CONFIG_EXT4_FS_XATTR)		+= xattr.o xattr_user.o xattr_trusted.o
 ext4-$(CONFIG_EXT4_FS_POSIX_ACL)	+= acl.o
fs/ext4/defrag.c new file +499 -0

499 lines not on this page · raw message

fs/ext4/ext4.h +10 -0
index b1cdd8d..30e3195 100644
@@ -302,6 +302,7 @@ struct ext4_new_group_data {
 #define EXT4_IOC_GROUP_ADD		_IOW('f', 8, struct ext4_new_group_input)
 #define EXT4_IOC_MIGRATE		_IO('f', 9)
  /* note ioctl 11 reserved for filesystem-independent FIEMAP ioctl */
+#define EXT4_IOC_DEFRAG			_IOW('f', 15, struct ext4_ext_defrag_data)
 
 /*
  * ioctl commands in 32 bit emulation
@@ -319,6 +320,12 @@ struct ext4_new_group_data {
 #define EXT4_IOC32_GETVERSION_OLD	FS_IOC32_GETVERSION
 #define EXT4_IOC32_SETVERSION_OLD	FS_IOC32_SETVERSION
 
+struct ext4_ext_defrag_data {
+	ext4_lblk_t start_offset;	/* start offset to defrag in blocks */
+	ext4_lblk_t defrag_size;	/* size of defrag in blocks */
+	ext4_fsblk_t goal;		/* block offset for allocation */
+};
+
 
 /*
  *  Mount options
@@ -1144,6 +1151,9 @@ extern void ext4_inode_bitmap_set(struct super_block *sb,
 				  struct ext4_group_desc *bg, ext4_fsblk_t blk);
 extern void ext4_inode_table_set(struct super_block *sb,
 				 struct ext4_group_desc *bg, ext4_fsblk_t blk);
+/* defrag.c */
+extern int ext4_defrag(struct file *filp, ext4_lblk_t block_start,
+			ext4_lblk_t defrag_size);
 
 static inline ext4_fsblk_t ext4_blocks_count(struct ext4_super_block *es)
 {
fs/ext4/ext4_extents.h +2 -0
index bec7ce5..a28d8d2 100644
@@ -246,5 +246,7 @@ extern int ext4_ext_search_left(struct inode *, struct ext4_ext_path *,
 extern int ext4_ext_search_right(struct inode *, struct ext4_ext_path *,
 						ext4_lblk_t *, ext4_fsblk_t *);
 extern void ext4_ext_drop_refs(struct ext4_ext_path *);
+extern ext4_fsblk_t ext_pblock(struct ext4_extent *ex);
+
 #endif /* _EXT4_EXTENTS */
 
fs/ext4/extents.c +1 -1
index ea2ce3c..406cab9 100644
@@ -49,7 +49,7 @@
  * ext_pblock:
  * combine low and high parts of physical block number into ext4_fsblk_t
  */
-static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
+ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
 {
 	ext4_fsblk_t block;
 
fs/ext4/ioctl.c +22 -0
index dc99b47..78d9174 100644
@@ -214,6 +214,28 @@ setversion_out:
 
 		return err;
 	}
+
+	case EXT4_IOC_DEFRAG: {
+		struct ext4_ext_defrag_data defrag;
+		int err;
+
+		if (!capable(CAP_DAC_OVERRIDE)) {
+			if ((inode->i_mode & S_IRUSR) != S_IRUSR)
+				return -EACCES;
+			if (current->fsuid != inode->i_uid)
+				return -EACCES;
+		}
+
+		if (copy_from_user(&defrag,
+			(struct ext4_ext_defrag_data __user *)arg,
+							sizeof(defrag)))
+			return -EFAULT;
+
+		err = ext4_defrag(filp, defrag.start_offset,
+			defrag.defrag_size);
+		return err;
+	}
+
 	case EXT4_IOC_GROUP_ADD: {
 		struct ext4_new_group_data input;
 		struct super_block *sb = inode->i_sb;