Skip to contentSkip to conversation

ext4: add EXT4_IOC_CONTROL_PA to create/discard inode PA

RFC PATCH In review Kazuya Mio 2 participants 3 messages Dec 1, 2010 – Dec 2, 2010

Subsystems (2)
lore
Share & tools
lore

Kazuya Mio

author
#0 lore raw

RFC PATCH v3 1/4 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/ext4_extents.h |    4 
 fs/ext4/extents.c      |    2 
 fs/ext4/inode.c        |    2 
 fs/ext4/ioctl.c        |   29 ++++
 fs/ext4/mballoc.c      |  345 ++++++++++++++++++++++++++++++++++++++++++++++---
 fs/ext4/mballoc.h      |    8 +
 7 files changed, 392 insertions(+), 19 deletions(-)
fs/ext4/ext4.h +21 -0
index 3069a1b..4094ae7 100644
@@ -108,6 +108,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 {
@@ -131,6 +133,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) */
+};
+
 /*
  * Logical to physical block mapping, used by ext4_map_blocks()
  *
@@ -541,6 +557,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)
 
 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
 /*
@@ -1691,6 +1708,8 @@ extern void ext4_free_blocks(handle_t *handle, struct inode *inode,
 extern int ext4_mb_add_groupinfo(struct super_block *sb,
 		ext4_group_t i, struct ext4_group_desc *desc);
 extern int ext4_trim_fs(struct super_block *, struct fstrim_range *);
+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 *,
@@ -1726,6 +1745,8 @@ extern int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf);
 extern qsize_t *ext4_get_reserved_space(struct inode *inode);
 extern void ext4_da_update_reserve_space(struct inode *inode,
 					int used, int quota_claim);
+extern int ext4_ind_map_blocks(handle_t *, struct inode *,
+					struct ext4_map_blocks *, 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/ext4_extents.h +4 -0
index 28ce70f..6d0ea53 100644
@@ -291,5 +291,9 @@ extern struct ext4_ext_path *ext4_ext_find_extent(struct inode *, ext4_lblk_t,
 							struct ext4_ext_path *);
 extern void ext4_ext_drop_refs(struct ext4_ext_path *);
 extern int ext4_ext_check_inode(struct inode *inode);
+extern unsigned int ext4_ext_check_overlap(struct inode *,
+						struct ext4_extent *,
+						struct ext4_ext_path *);
+
 #endif /* _EXT4_EXTENTS */
 
fs/ext4/extents.c +1 -1
index 0554c48..28f847e 100644
@@ -1584,7 +1584,7 @@ static int ext4_ext_try_to_merge(struct inode *inode,
  * such that there will be no overlap, and then returns 1.
  * If there is no overlap found, it returns 0.
  */
-static unsigned int ext4_ext_check_overlap(struct inode *inode,
+unsigned int ext4_ext_check_overlap(struct inode *inode,
 					   struct ext4_extent *newext,
 					   struct ext4_ext_path *path)
 {
fs/ext4/inode.c +1 -1
index b4236d4..f78e7cb 100644
@@ -940,7 +940,7 @@ err_out:
  * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
  * blocks.
  */
-static int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
+int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
 			       struct ext4_map_blocks *map,
 			       int flags)
 {
fs/ext4/ioctl.c +29 -0
index 1aae42d..a207691 100644
@@ -419,6 +419,34 @@ 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;
@@ -487,6 +515,7 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	}
 	case EXT4_IOC_MOVE_EXT:
 	case EXT4_IOC_DEBUG_DELALLOC:
+	case EXT4_IOC_CONTROL_PA:
 		break;
 	default:
 		return -ENOIOCTLCMD;
fs/ext4/mballoc.c +328 -17

418 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
#1 lore raw
Quoted 6 lines · Kazuya Mio

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
-----------

Quoted 7 lines

#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) */

Is the comment here wrong, or do I misunderstand that there is a 64-bit offset for the file, and only a 32-bit offset for the disk? It doesn't make sense to create a new API that only has a 32-bit offset for the disk, since ext4 is nearly supporting 64-bit filesystem blocks.

Looking at the patch, it seems that pi_pstart is indeed the physical offset, so it should be 64-bit, and it just appears that the comment is incorrect.

Cheers, Andreas

author
#2 lore raw
Quoted 1 line · Andreas Dilger

Is the comment here wrong, or do I misunderstand that there is a 64-bit offset for the file, and only a 32-bit offset for the disk? It doesn't make sense to create a new API that only has a 32-bit offset for the disk, since ext4 is nearly supporting 64-bit filesystem blocks.

Oops! Thank you for letting me know. I will fix the comments as follows:

struct ext4_prealloc_info {
__u64 pi_pstart; /* physical offset for the start of the PA from
* the beginning of the disk (in/out) */
__u32 pi_lstart; /* logical offset for the start of the PA from
* the beginning of the file (in/out) */

Regards,
Kazuya Mio