[RFC,v2,11/11] xfs: use common file type conversion
diff mbox

Message ID 1482315128-27549-1-git-send-email-amir73il@gmail.com
State New
Headers show

Commit Message

Amir Goldstein Dec. 21, 2016, 10:12 a.m. UTC
Deduplicate the common bits of the file type conversion implementation.

Use private conversion tables to convert from fs common FT_* file types
to on-disk file types and from on-disk file types to readdir(2) DT_* file
types.

NOTE that the common_to_dir3_ftype table is an indentity transformation -
the common FT_* values are an exact reflection of the v3 on-disk values.
The dir3_ftype_to_dtype table is identical to the common fs_dtype_by_ftype
table in linux/file_type.h with an additional single entry for the
never used value of XFS_DIR3_FT_WHT.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---
 fs/xfs/libxfs/xfs_dir2.c  | 48 +++++++++++++++++++++++++++++++++++------------
 fs/xfs/libxfs/xfs_dir2.h  |  4 ++--
 fs/xfs/xfs_dir2_readdir.c |  7 +------
 fs/xfs/xfs_iops.c         |  2 +-
 4 files changed, 40 insertions(+), 21 deletions(-)

Ted, Chris and Dave,

I would appreciate a NACK or ACK on this xfs example implementation.
If the major fs maintainers are on board, I will apply this approach
to all other fs.

This is a draft for the mid grounds approach suggested by Dave, which
keeps the control of on-disk bits in the hands of the the file system
code and still uses some of the common implementation.

As a code deduplication patch, one can argue that it does a pretty lousy
job, removing only a single line of common code (#define S_SHIFT 12), but
I do think that the result is cleaner without all the open coded >> S_SHIFT
conversions.

In any case, all for all the small file systems, this cleanup should be a
win (exofs ACKed already), so it would be nice to get feedback from more
maintainers.

Amir.

Extra food for thought:

Patch
diff mbox

=======================
Beyond the obvious open coded bits cleanup, going forward, this schema
will allow adding more common FT_ values and for file systems to catch up
with them at will. For example, if FT_WHT were to be added to the common
file types, xfs could join the party with existing v3 format by adding
a single line to private conversion table:
       [FT_WHT]    = XFS_DIR3_FT_WHT,
and other fs could join the FT_WHT party after sorting out whatever
feature flags that need to be sorted out.

I envision adding some opaque 'hint bits' to common ftype -
file systems that will support the opaque hint bits will store them as is
on-disk and repair tools will mask them out when comparing to inode mode.
Think of those bits as a way to white list or black list objects on a
file system for a very fast low-level directory tree iteration.

There is one such use case for overlayfs, but I am sure there are more.
The hint bits are not supposed to be set nor cleared during the life time
of the directory entry. Setting a file type hint bit should only be possible
at directory entry creation time (i.e. create,mknod,mkdir,symlink,rename).


v2:
- add private conversion from common to on-disk values

v1:
- use common conversion functions to get on-disk values

diff --git a/fs/xfs/libxfs/xfs_dir2.c b/fs/xfs/libxfs/xfs_dir2.c
index c58d72c..ae711ad 100644
--- a/fs/xfs/libxfs/xfs_dir2.c
+++ b/fs/xfs/libxfs/xfs_dir2.c
@@ -37,21 +37,45 @@  struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2, XFS_DIR3_FT_DIR };
 
 /*
  * @mode, if set, indicates that the type field needs to be set up.
- * This uses the transformation from file mode to DT_* as defined in linux/fs.h
- * for file type specification. This will be propagated into the directory
- * structure if appropriate for the given operation and filesystem config.
+ * readdir(2) DT_* file types and fs common FT_* file types are defined
+ * in linux/file_type.h.
+ *
+ * These tables define the transformation from common FT_* file types to on-disk
+ * file types and from on-disk file types to readdir(2) DT_* file types.
+ *
+ * The on-disk file types will be propagated into the directory structure if
+ * appropriate for the given operation and filesystem config.
+ *
+ * NOTE that the common_to_dir3_ftype table is an indentity transformation -
+ * the common values are an exact reflection on the xfs v3 on-disk values.
+ * The dir3_ftype_to_dtype table is identical to the common fs_dtype_by_ftype
+ * table in linux/file_type.h with an additional single entry for the
+ * never used value of XFS_DIR3_FT_WHT.
  */
-const unsigned char xfs_mode_to_ftype[S_IFMT >> S_SHIFT] = {
-	[0]			= XFS_DIR3_FT_UNKNOWN,
-	[S_IFREG >> S_SHIFT]    = XFS_DIR3_FT_REG_FILE,
-	[S_IFDIR >> S_SHIFT]    = XFS_DIR3_FT_DIR,
-	[S_IFCHR >> S_SHIFT]    = XFS_DIR3_FT_CHRDEV,
-	[S_IFBLK >> S_SHIFT]    = XFS_DIR3_FT_BLKDEV,
-	[S_IFIFO >> S_SHIFT]    = XFS_DIR3_FT_FIFO,
-	[S_IFSOCK >> S_SHIFT]   = XFS_DIR3_FT_SOCK,
-	[S_IFLNK >> S_SHIFT]    = XFS_DIR3_FT_SYMLINK,
+const unsigned char xfs_common_to_dir3_ftype[FT_MAX] = {
+	[FT_UNKNOWN]	= XFS_DIR3_FT_UNKNOWN,
+	[FT_REG_FILE]	= XFS_DIR3_FT_REG_FILE,
+	[FT_DIR]	= XFS_DIR3_FT_DIR,
+	[FT_CHRDEV]	= XFS_DIR3_FT_CHRDEV,
+	[FT_BLKDEV]	= XFS_DIR3_FT_BLKDEV,
+	[FT_FIFO]	= XFS_DIR3_FT_FIFO,
+	[FT_SOCK]	= XFS_DIR3_FT_SOCK,
+	[FT_SYMLINK]	= XFS_DIR3_FT_SYMLINK,
 };
 
+const unsigned char xfs_dir3_ftype_to_dtype[XFS_DIR3_FT_MAX] = {
+	[XFS_DIR3_FT_UNKNOWN]	= DT_UNKNOWN,
+	[XFS_DIR3_FT_REG_FILE]	= DT_REG,
+	[XFS_DIR3_FT_DIR]	= DT_DIR,
+	[XFS_DIR3_FT_CHRDEV]	= DT_CHR,
+	[XFS_DIR3_FT_BLKDEV]	= DT_BLK,
+	[XFS_DIR3_FT_FIFO]	= DT_FIFO,
+	[XFS_DIR3_FT_SOCK]	= DT_SOCK,
+	[XFS_DIR3_FT_SYMLINK]	= DT_LNK,
+	[XFS_DIR3_FT_WHT]	= DT_WHT,
+};
+
+
 /*
  * ASCII case-insensitive (ie. A-Z) support for directories that was
  * used in IRIX.
diff --git a/fs/xfs/libxfs/xfs_dir2.h b/fs/xfs/libxfs/xfs_dir2.h
index 0197590..5018850 100644
--- a/fs/xfs/libxfs/xfs_dir2.h
+++ b/fs/xfs/libxfs/xfs_dir2.h
@@ -34,8 +34,8 @@  extern struct xfs_name	xfs_name_dotdot;
 /*
  * directory filetype conversion tables.
  */
-#define S_SHIFT 12
-extern const unsigned char xfs_mode_to_ftype[];
+extern const unsigned char xfs_common_to_dir3_ftype[];
+extern const unsigned char xfs_dir3_ftype_to_dtype[];
 
 /*
  * directory operations vector for encode/decode routines
diff --git a/fs/xfs/xfs_dir2_readdir.c b/fs/xfs/xfs_dir2_readdir.c
index 003a99b..6d8a741 100644
--- a/fs/xfs/xfs_dir2_readdir.c
+++ b/fs/xfs/xfs_dir2_readdir.c
@@ -36,11 +36,6 @@ 
 /*
  * Directory file type support functions
  */
-static unsigned char xfs_dir3_filetype_table[] = {
-	DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK,
-	DT_FIFO, DT_SOCK, DT_LNK, DT_WHT,
-};
-
 static unsigned char
 xfs_dir3_get_dtype(
 	struct xfs_mount	*mp,
@@ -52,7 +47,7 @@  xfs_dir3_get_dtype(
 	if (filetype >= XFS_DIR3_FT_MAX)
 		return DT_UNKNOWN;
 
-	return xfs_dir3_filetype_table[filetype];
+	return xfs_dir3_ftype_to_dtype[filetype];
 }
 
 STATIC int
diff --git a/fs/xfs/xfs_iops.c b/fs/xfs/xfs_iops.c
index 308bebb..36742fb 100644
--- a/fs/xfs/xfs_iops.c
+++ b/fs/xfs/xfs_iops.c
@@ -103,7 +103,7 @@  xfs_dentry_to_name(
 {
 	namep->name = dentry->d_name.name;
 	namep->len = dentry->d_name.len;
-	namep->type = xfs_mode_to_ftype[(mode & S_IFMT) >> S_SHIFT];
+	namep->type = xfs_common_to_dir3_ftype[fs_umode_to_ftype(mode)];
 }
 
 STATIC void