Skip to contentSkip to conversation

xfs: Add support IOC_MOV_DATA ioctl

PATCH In review Namjae Jeon 5 participants 6 messages Jul 8, 2014 – Jul 14, 2014

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

PATCH 2/3 xfs: Add support IOC_MOV_DATA ioctl

This patch implements fs ioctl's IOC_MOV_DATA for XFS.

The semantics of this ioctl are:
1) Like collapse range, offsets and length should be file system block size aligned.
2) In the receiver file, atleast length size hole should be present at receiver_offset
3) It does not change file size of any of donor or receiver file.
4) It leaves a hole at the place from where blocks are moved out in donor file.
5) Both (donor_offset + length) and (receiver_offset + length) should be within size of donor file and receiver file respectively.
Only unwritten extents resides beyond file size and it does not make sense to transfer unwritten extents, leave apart the security issues it may raise.
6) If the range to be transfered from donor file contain any holes, they are replicated as it is in receiver file. It mean holes are preserved and the length of hole will be added to moved_len signifying that the hole range is succesfully transfered.

---
 fs/xfs/libxfs/xfs_bmap.c | 148 ++++++++++++++++++++++++++++
 fs/xfs/libxfs/xfs_bmap.h |   4 +
 fs/xfs/xfs_bmap_util.c   | 251 +++++++++++++++++++++++++++++++++++++++++++++++
 fs/xfs/xfs_bmap_util.h   |   3 +
 fs/xfs/xfs_iops.c        |   1 +
 5 files changed, 407 insertions(+)
fs/xfs/libxfs/xfs_bmap.c +148 -0
index 3dee150..c2ae99e 100644
@@ -5982,3 +5982,151 @@ del_cursor:
 	xfs_trans_log_inode(tp, ip, logflags);
 	return error;
 }
+/*
+ * Move an extent record pointed by got and whose extent number is stored in
+ * donor_ext from donor inode dip to receiver inode rip.
+ * Extent will be moved at roffset_fsb inside receiver inode.
+ * Caller of this function must make sure there is atleast got->br_blockcount
+ * size hole at roffset_fsb.
+ */
+int xfs_move_extent(
+	struct xfs_trans	*tp,
+	xfs_inode_t		*dip,
+	xfs_inode_t		*rip,
+	xfs_extnum_t		donor_ext,
+	struct xfs_bmbt_irec	*got,
+	xfs_fileoff_t		roffset_fsb,
+	xfs_fsblock_t		*dfirstblock,
+	struct xfs_bmap_free	*dflist,
+	xfs_fsblock_t		*rfirstblock,
+	struct xfs_bmap_free	*rflist)
+
+{
+	struct xfs_mount		*mp = dip->i_mount;
+	struct xfs_bmbt_rec_host	*gotp;
+	struct xfs_btree_cur		*dcur;
+	struct xfs_btree_cur		*rcur;
+	xfs_ifork_t			*difp;
+	xfs_ifork_t			*rifp;
+	xfs_extnum_t			idx;
+	int                             i;
+	int                             dlogflags;
+	int                             rlogflags;
+	int                             tmp_logflags;
+	int				error;
+	int				whichfork = XFS_DATA_FORK;
+	struct xfs_bmalloca		bma = { NULL };
+
+	if (unlikely(XFS_TEST_ERROR(
+	    (XFS_IFORK_FORMAT(dip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
+	     XFS_IFORK_FORMAT(dip, whichfork) != XFS_DINODE_FMT_BTREE),
+	     mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
+		XFS_ERROR_REPORT("xfs_move_extent",
+				XFS_ERRLEVEL_LOW, mp);
+		return -EFSCORRUPTED;
+	}
+
+	if (unlikely(XFS_TEST_ERROR(
+	    (XFS_IFORK_FORMAT(rip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
+	     XFS_IFORK_FORMAT(rip, whichfork) != XFS_DINODE_FMT_BTREE),
+	     mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
+		XFS_ERROR_REPORT("xfs_move_extent",
+				XFS_ERRLEVEL_LOW, mp);
+		return -EFSCORRUPTED;
+	}
+
+	if (XFS_FORCED_SHUTDOWN(mp))
+		return -EIO;
+
+	dlogflags = XFS_ILOG_CORE;
+	difp = XFS_IFORK_PTR(dip, whichfork);
+
+	if (difp->if_flags & XFS_IFBROOT) {
+		dcur = xfs_bmbt_init_cursor(mp, tp, dip, whichfork);
+		dcur->bc_private.b.firstblock = *dfirstblock;
+		dcur->bc_private.b.flist = dflist;
+		dcur->bc_private.b.flags = 0;
+	} else {
+		dcur = NULL;
+		dlogflags |= XFS_ILOG_DEXT;
+	}
+
+	rlogflags = XFS_ILOG_CORE;
+	rifp = XFS_IFORK_PTR(rip, whichfork);
+
+	if (rifp->if_flags & XFS_IFBROOT) {
+		rcur = xfs_bmbt_init_cursor(mp, tp, rip, whichfork);
+		rcur->bc_private.b.firstblock = *rfirstblock;
+		rcur->bc_private.b.flist = rflist;
+		rcur->bc_private.b.flags = 0;
+	} else {
+		rcur = NULL;
+		rlogflags |= XFS_ILOG_DEXT;
+	}
+
+	if (dcur) {
+		error = xfs_bmbt_lookup_eq(dcur,
+					   got->br_startoff,
+					   got->br_startblock,
+					   got->br_blockcount,
+					   &i);
+		if (error)
+			goto del_cursor;
+		XFS_WANT_CORRUPTED_GOTO(i == 1, del_cursor);
+	}
+
+	gotp = xfs_iext_bno_to_ext(rifp, roffset_fsb, &idx);
+
+	/* Initialize bma */
+	bma.tp = tp;
+	bma.ip = rip;
+	bma.flist = rflist;
+	bma.firstblock = rfirstblock;
+	bma.cur = rcur;
+	bma.got.br_startoff = roffset_fsb;
+	bma.got.br_startblock = got->br_startblock;
+	bma.got.br_blockcount = got->br_blockcount;
+	bma.got.br_state = got->br_state;
+	bma.idx = idx;
+
+	error = xfs_bmap_add_extent_hole_real(&bma, whichfork);
+	if (error)
+		goto del_cursor;
+	rlogflags |= bma.logflags;
+	rip->i_d.di_nblocks += got->br_blockcount;
+
+	xfs_iext_remove(dip, donor_ext, 1, 0);
+	if (dcur) {
+		error = xfs_btree_delete(dcur, &i);
+		if (error)
+			goto del_cursor;
+		XFS_WANT_CORRUPTED_GOTO(i == 1, del_cursor);
+	}
+	XFS_IFORK_NEXT_SET(dip, whichfork,
+			   XFS_IFORK_NEXTENTS(dip, whichfork) - 1);
+	dip->i_d.di_nblocks -= got->br_blockcount;
+
+	if (xfs_bmap_wants_extents(dip, whichfork)) {
+		ASSERT(dcur != NULL);
+		error = xfs_bmap_btree_to_extents(tp, dip, dcur, &tmp_logflags,
+				whichfork);
+		dlogflags |= tmp_logflags;
+	}
+
+del_cursor:
+	if (dcur) {
+		dcur->bc_private.b.allocated = 0;
+		xfs_btree_del_cursor(dcur,
+				error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
+	}
+	xfs_trans_log_inode(tp, dip, dlogflags);
+
+	if (rcur) {
+		rcur->bc_private.b.allocated = 0;
+		xfs_btree_del_cursor(rcur,
+				error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
+	}
+	xfs_trans_log_inode(tp, rip, rlogflags);
+
+	return error;
+}
fs/xfs/libxfs/xfs_bmap.h +4 -0
index af05899..ebfe584 100644
@@ -191,5 +191,9 @@ int	xfs_bmap_shift_extents_right(struct xfs_trans *tp, struct xfs_inode *ip,
 		xfs_extnum_t *current_ext, xfs_extnum_t end_ext,
 		xfs_fsblock_t *firstblock, struct xfs_bmap_free *flist,
 		int num_exts);
+int	xfs_move_extent(struct xfs_trans *, xfs_inode_t *, xfs_inode_t *,
+		xfs_extnum_t, struct xfs_bmbt_irec *, xfs_fileoff_t,
+		xfs_fsblock_t *, struct xfs_bmap_free *, xfs_fsblock_t *,
+		struct xfs_bmap_free *);
 
 #endif	/* __XFS_BMAP_H__ */
fs/xfs/xfs_bmap_util.c +251 -0

254 lines not on this page · raw message

fs/xfs/xfs_bmap_util.h +3 -0
index 9a18a4b..b2ae123 100644
@@ -104,6 +104,9 @@ int	xfs_collapse_file_space(struct xfs_inode *, xfs_off_t offset,
 				xfs_off_t len);
 int	xfs_insert_file_space(struct xfs_inode *, xfs_off_t offset,
 				xfs_off_t len);
+int	xfs_vn_move_data(struct inode *donor, struct inode *receiver,
+			 loff_t doffset, loff_t roffset, loff_t len,
+			 loff_t *moved_len);
 
 /* EOF block manipulation functions */
 bool	xfs_can_free_eofblocks(struct xfs_inode *ip, bool force);
fs/xfs/xfs_iops.c +1 -0
index d75621a..63c1621 100644
@@ -1100,6 +1100,7 @@ static const struct inode_operations xfs_inode_operations = {
 	.listxattr		= xfs_vn_listxattr,
 	.fiemap			= xfs_vn_fiemap,
 	.update_time		= xfs_vn_update_time,
+	.mov_data		= xfs_vn_move_data,
 };
 
 static const struct inode_operations xfs_dir_inode_operations = {
Signature · 6 lines
1.7.11-rc0

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw
Quoted 1 line · Namjae Jeon

This patch implements fs ioctl's IOC_MOV_DATA for XFS.

Shouldn't this share code with the XFS_IOC_SWAPEXT implementation?

#2 lore raw
Quoted 5 lines · Namjae Jeon
Quoted 1 line

This patch implements fs ioctl's IOC_MOV_DATA for XFS.

Shouldn't this share code with the XFS_IOC_SWAPEXT implementation?

Lukas has raised the same question for ext4.
Both xfs(XFS_IOC_SWAPEXT) and ext4(EXT4_IOC_MOVE_EXT) has ioctls which uses almost similar kind of structure as struct mov_data.
As such, there seems to be possiblity for sharing basic code (I will try) used for sanity checking in IOC_MOV_DATA to be shared with these ioctls but the core functionality code will remain different.

Thanks!

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#3 lore raw
Quoted 5 lines · Namjae Jeon
Quoted 5 lines · Namjae Jeon
Quoted 1 line

This patch implements fs ioctl's IOC_MOV_DATA for XFS.

Shouldn't this share code with the XFS_IOC_SWAPEXT implementation?

Quoted 8 lines

Lukas has raised the same question for ext4.
Both xfs(XFS_IOC_SWAPEXT) and ext4(EXT4_IOC_MOVE_EXT) has ioctls which uses almost similar kind of structure as struct mov_data.
As such, there seems to be possiblity for sharing basic code (I will try) used for sanity checking in IOC_MOV_DATA to be shared with these ioctls but the core functionality code will remain different.

Thanks!

Actually they are differ. EXT4_IOC_MOVE_EXT copy data inside kernel,
but XFS_IOC_SWAPEXT live this job to userpsace see:
http://oss.sgi.com/cgi-bin/gitweb.cgi?p=xfs/cmds/xfsprogs.git;a=blob;f=fsr/xfs_fsr.c packfile And I'll vote to make EXT4_IOC_MOVE_EXT deprecated, and implement EXT4_IOC_SWAPEXT as XFS does that.
Ted, Lukas what do you think about that?

Quoted 6 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#4 lore raw
Quoted 6 lines · Dmitry Monakhov

Actually they are differ. EXT4_IOC_MOVE_EXT copy data inside kernel,
but XFS_IOC_SWAPEXT live this job to userpsace see:
http://oss.sgi.com/cgi-bin/gitweb.cgi?p=xfs/cmds/xfsprogs.git;a=blob;f=fsr/xfs_fsr.c packfile And I'll vote to make EXT4_IOC_MOVE_EXT deprecated, and implement EXT4_IOC_SWAPEXT as XFS does that.
Ted, Lukas what do you think about that?

The reason why EXT4_IOC_MOVE_EXT moves the data via the cache is to avoid being subject to races if the file happens to mmap'ed and being actively modified at the time of the defrag operation.

I'm not sure how XFS handles that case, but if it's not somehow locking the file against mmap's before it starts the userspace copy, it would seem to me to be fairly dangerous in terms of prevent potential data loss in this scenario. Unless they are doing some especially clever?

Regards,

- Ted

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#5 lore raw
Quoted 12 lines · Theodore Ts'o
Quoted 6 lines · Dmitry Monakhov

Actually they are differ. EXT4_IOC_MOVE_EXT copy data inside kernel,
but XFS_IOC_SWAPEXT live this job to userpsace see:
http://oss.sgi.com/cgi-bin/gitweb.cgi?p=xfs/cmds/xfsprogs.git;a=blob;f=fsr/xfs_fsr.c packfile And I'll vote to make EXT4_IOC_MOVE_EXT deprecated, and implement EXT4_IOC_SWAPEXT as XFS does that.
Ted, Lukas what do you think about that?

The reason why EXT4_IOC_MOVE_EXT moves the data via the cache is to avoid being subject to races if the file happens to mmap'ed and being actively modified at the time of the defrag operation.

Quoted 5 lines

I'm not sure how XFS handles that case, but if it's not somehow locking the file against mmap's before it starts the userspace copy, it would seem to me to be fairly dangerous in terms of prevent potential data loss in this scenario. Unless they are doing some especially clever?

Yes, we're being clever:

a) we can snapshot the inode directly with bulkstat and then feed that as a cookie back into the swap extent ioctl, hence detect any change made to the inode since the snapshot was taken;

b) we do invisible IO to copy the data (i.e. doesn't update timestamps on the files); and

c) the swap ext ioctl aborts if the file is mmapped() at the time we do the extent swap.

Basically, if there is any inconsistency or trouble, we abort the swap without doing anything and leave userspace to clean up.

As it is, we'll be looking to replace the swapext call with this new move ioctl because we can do a lot more with it and avoids implementation wrinkles like having to check and handle different sized data and inode forks, and having to change the owner field in every bmap btree block after the swap has occurred.

FWIW, what we ideally need for these sorts of defrag programs is per-file freezing. i.e. we freeze the file to be defragged, then do the copy in userspace, swap/move the copied range and then unfreeze it once complete. That guarantees that the file is not modified in any way while userspace is doing the defrag...

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]