[PATCH 2/2] Use fiemap internal infra-structure to handle FIBMAP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch modifies ioctl_fibmap to use FIEMAP interface internally. The
FIBMAP API is not changed, but now ->bmap can go.

Signed-off-by: Carlos Maiolino <cmaiolino@xxxxxxxxxx>
---
 fs/inode.c                  | 38 +++++++++++++++++++++++++++++++++++++-
 fs/ioctl.c                  | 11 +++++++++--
 include/uapi/linux/fiemap.h |  1 +
 3 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/fs/inode.c b/fs/inode.c
index 41812a3e829e..07befc5f253b 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -1588,9 +1588,45 @@ EXPORT_SYMBOL(iput);
  */
 sector_t bmap(struct inode *inode, sector_t block)
 {
+
+	struct fiemap_extent_info fieinfo = { 0, };
+	struct fiemap_extent fextent;
+	u64 start = block << inode->i_blkbits;
+	int error;
 	sector_t res = 0;
-	if (inode->i_mapping->a_ops->bmap)
+
+	if (inode->i_op->fiemap) {
+		fextent.fe_logical = 0;
+		fextent.fe_physical = 0;
+		fieinfo.fi_flags = FIEMAP_KERNEL_FIBMAP;
+		fieinfo.fi_extents_max = 1;
+		fieinfo.fi_extents_start = (__force struct fiemap_extent __user *) &fextent;
+
+		error = inode->i_op->fiemap(inode, &fieinfo, start, 1);
+
+		/* FIBMAP expect a zero return for error */
+		if (error)
+			goto out;
+
+		/*
+		 * Fiemap implementation of some filesystems will return the
+		 * extent map beginning at the requested offset
+		 * (fextent.fi_logical == start), while other FSes will return
+		 * the beginning of the extent at fextent.fe_logical, even if
+		 * the requested offset is somehwere in the middle of the
+		 * extent. We must be sure to return the correct block block
+		 * here in both cases.
+		 */
+		if (fextent.fe_logical != start)
+			res = (fextent.fe_physical + start) >> inode->i_blkbits;
+		else
+			res = fextent.fe_physical >> inode->i_blkbits;
+
+	} else if (inode->i_mapping->a_ops->bmap) {
 		res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
+	}
+
+out:
 	return res;
 }
 EXPORT_SYMBOL(bmap);
diff --git a/fs/ioctl.c b/fs/ioctl.c
index 413585d58415..58de1dd507b1 100644
--- a/fs/ioctl.c
+++ b/fs/ioctl.c
@@ -117,8 +117,14 @@ int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
 	extent.fe_flags = flags;
 
 	dest += fieinfo->fi_extents_mapped;
-	if (copy_to_user(dest, &extent, sizeof(extent)))
-		return -EFAULT;
+
+	if (fieinfo->fi_flags & FIEMAP_KERNEL_FIBMAP) {
+		memcpy((__force struct fiemap_extent *)dest, &extent,
+		       sizeof(extent));
+	} else {
+		if (copy_to_user(dest, &extent, sizeof(extent)))
+			return -EFAULT;
+	}
 
 	fieinfo->fi_extents_mapped++;
 	if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max)
@@ -146,6 +152,7 @@ int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags)
 	u32 incompat_flags;
 
 	incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags);
+	incompat_flags &= ~(FIEMAP_KERNEL_FIBMAP);
 	if (incompat_flags) {
 		fieinfo->fi_flags = incompat_flags;
 		return -EBADR;
diff --git a/include/uapi/linux/fiemap.h b/include/uapi/linux/fiemap.h
index 8c0bc24d5d95..7064a3fa5421 100644
--- a/include/uapi/linux/fiemap.h
+++ b/include/uapi/linux/fiemap.h
@@ -42,6 +42,7 @@ struct fiemap {
 #define FIEMAP_FLAG_SYNC	0x00000001 /* sync file data before map */
 #define FIEMAP_FLAG_XATTR	0x00000002 /* map extended attribute tree */
 #define FIEMAP_FLAG_CACHE	0x00000004 /* request caching of the extents */
+#define FIEMAP_KERNEL_FIBMAP	0x00000008 /* Use FIEMAP for FIBMAP */
 
 #define FIEMAP_FLAGS_COMPAT	(FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR)
 
-- 
2.14.4





[Index of Archives]     [Linux Ext4 Filesystem]     [Union Filesystem]     [Filesystem Testing]     [Ceph Users]     [Ecryptfs]     [AutoFS]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux Cachefs]     [Reiser Filesystem]     [Linux RAID]     [Samba]     [Device Mapper]     [CEPH Development]

  Powered by Linux