Skip to contentSkip to conversation

ext4: add EXT4_IOC_CONTROL_PA to create/discard inode PA

RFC PATCH Superseded Kazuya Mio 1 participant 1 message Apr 14, 2010

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Kazuya Mio

author
#0 lore raw

RFC PATCH 1/3 ext4: add EXT4_IOC_CONTROL_PA to create/discard inode PA

EXT4_IOC_CONTROL_PA allows to create new inode PA based on the specified range, or to discard all inode PAs in the target inode.

INTERFACE
-----------

#define EXT4_IOC_CONTROL_PA _IOWR('f', 16, struct ext4_prealloc_info)

struct ext4_prealloc_info {
__u64 pi_pstart; /* physical offset for the start of the PA from
* the beginning of the file (in/out) */
__u32 pi_lstart; /* logical offset for the start of the PA from
* the beginning of the disk (in/out) */
__u32 pi_len; /* length for this PA (in/out) */
__u32 pi_free; /* the number of free blocks in this PA (out) */
__u16 pi_flags; /* flags for the inode PA setting ioctl (in) */
};

This ioctl has three modes by setting the following flags to pi_flags:

- EXT4_MB_MANDATORY
Create the specified inode PA to the target inode. If the block allocation cannot allocate the specified range, this ioctl returns -ENOSPC.

- EXT4_MB_ADVISORY
Create the specified inode PA to the target inode. If the block allocation cannot allocate the specified range, this ioctl creates an inode PA allocated in the different range.

- EXT4_MB_DISCARD_PA
Discard all inode PAs in the target inode.

This ioctl returns struct ext4_prealloc_info. pi_pstart shows where to create the inode PA, and pi_len shows how many blocks it allocates. If we call this ioctl with EXT4_MB_MANDATORY flag and the specified range cannot be allocated, this ioctl returns struct ext4_prealloc_info to give free space information near the specified range.

NOTE
------

This ioctl is limited to create up to 1024 inode PAs. Because I'm afraid to increase memory usage if there is no limit.

#define EXT4_MAX_PREALLOC 1024

---
 fs/ext4/ext4.h    |   21 ++++
 fs/ext4/inode.c   |    2 +-
 fs/ext4/ioctl.c   |   31 +++++
 fs/ext4/mballoc.c |  342 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 fs/ext4/mballoc.h |    8 ++
 5 files changed, 386 insertions(+), 18 deletions(-)
fs/ext4/ext4.h +21 -0
index edce3d9..b6be407 100644
@@ -102,6 +102,8 @@ typedef unsigned int ext4_group_t;
 #define EXT4_MB_DELALLOC_RESERVED	0x0400
 /* We are doing stream allocation */
 #define EXT4_MB_STREAM_ALLOC		0x0800
+/* create an inode PA that has only free blocks */
+#define EXT4_MB_HINT_PA_ONLY		0x1000
 
 
 struct ext4_allocation_request {
@@ -125,6 +127,20 @@ struct ext4_allocation_request {
 	unsigned int flags;
 };
 
+/* The maximum number of inode PAs that EXT4_IOC_CONTROL_PA can create */
+#define EXT4_MAX_PREALLOC	1024
+
+/* inode PA information */
+struct ext4_prealloc_info {
+	__u64 pi_pstart;	/* physical offset for the start of the PA from
+				 * the beginning of the file (in/out) */
+	__u32 pi_lstart;	/* logical offset for the start of the PA from
+				 * the beginning of the disk (in/out) */
+	__u32 pi_len;		/* length for this PA (in/out) */
+	__u32 pi_free;		/* the number of free blocks in this PA (out) */
+	__u16 pi_flags;		/* flags for the inode PA setting ioctl (in) */
+};
+
 /*
  * For delayed allocation tracking
  */
@@ -397,6 +413,7 @@ struct ext4_new_group_data {
  /* note ioctl 11 reserved for filesystem-independent FIEMAP ioctl */
 #define EXT4_IOC_ALLOC_DA_BLKS		_IO('f', 12)
 #define EXT4_IOC_MOVE_EXT		_IOWR('f', 15, struct move_extent)
+#define EXT4_IOC_CONTROL_PA		_IOWR('f', 16, struct ext4_prealloc_info)
 
 /*
  * ioctl commands in 32 bit emulation
@@ -1442,6 +1459,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_mb_control_pa(struct inode *inode,
+				struct ext4_prealloc_info *pi);
 /* inode.c */
 struct buffer_head *ext4_getblk(handle_t *, struct inode *,
 						ext4_lblk_t, int, int *);
@@ -1477,6 +1496,8 @@ extern qsize_t *ext4_get_reserved_space(struct inode *inode);
 extern int flush_completed_IO(struct inode *inode);
 extern void ext4_da_update_reserve_space(struct inode *inode,
 					int used, int quota_claim);
+extern int ext4_ind_get_blocks(handle_t *, struct inode *, ext4_lblk_t,
+				unsigned int, struct buffer_head *, int);
 /* ioctl.c */
 extern long ext4_ioctl(struct file *, unsigned int, unsigned long);
 extern long ext4_compat_ioctl(struct file *, unsigned int, unsigned long);
fs/ext4/inode.c +1 -1
index 567c9fc..8f34155 100644
@@ -916,7 +916,7 @@ err_out:
  * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
  * blocks.
  */
-static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
+int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
 			       ext4_lblk_t iblock, unsigned int maxblocks,
 			       struct buffer_head *bh_result,
 			       int flags)
fs/ext4/ioctl.c +31 -0
index 11c1484..ed68a83 100644
@@ -395,6 +395,35 @@ mext_out:
 		return 0;
 	}
 
+	case EXT4_IOC_CONTROL_PA:
+	{
+		struct ext4_prealloc_info pi;
+		int err;
+
+		if (!S_ISREG(inode->i_mode))
+			return -EINVAL;
+		if (!(filp->f_mode & FMODE_WRITE))
+			return -EBADF;
+
+		if (copy_from_user(&pi,
+			(struct ext4_prealloc_info __user *)arg, sizeof(pi)))
+			return -EFAULT;
+
+		err = ext4_mb_control_pa(inode, &pi);
+
+		/*
+		 * If ext4_mb_control_pa() returns ENOSPC, we need the
+		 * free space information of buddy bitmap to retry.
+		 */
+		if (err && err != -ENOSPC)
+			return err;
+
+		if (copy_to_user((struct ext4_prealloc_info __user *)arg,
+					&pi, sizeof(pi)))
+			return -EFAULT;
+		return err;
+	}
+
 
 	default:
 		return -ENOTTY;
@@ -443,6 +472,8 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 		break;
 	case EXT4_IOC_MOVE_EXT:
 		break;
+	case EXT4_IOC_CONTROL_PA:
+		break;
 	default:
 		return -ENOIOCTLCMD;
 	}
fs/ext4/mballoc.c +325 -17

416 lines not on this page · raw message

fs/ext4/mballoc.h +8 -0
index b619322..4d040a1 100644
@@ -225,4 +225,12 @@ static inline ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
 {
 	return ext4_group_first_block_no(sb, fex->fe_group) + fex->fe_start;
 }
+
+/*
+ * EXT4_IOC_CONTROL_PA flags to specify its behavior
+ */
+#define EXT4_MB_MANDATORY	0x0001
+#define EXT4_MB_ADVISORY	0x0002
+#define EXT4_MB_DISCARD_PA	0x0004
+
 #endif