Skip to contentSkip to conversation

libxfs: fix the size of xfs_mode_to_ftype table

PATCH New Amir Goldstein 1 participant 1 message Dec 25, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libxfs: fix the size of xfs_mode_to_ftype table

Fix the size of the xfs_mode_to_ftype conversion table, which was too small to handle an invalid value of mode=S_IFMT.

Use a convenience macro S_DT(mode) to convert from mode to dirent file type and change the name of the table to xfs_dtype_to_ftype to correctly describe its index values.

---
 libxfs/xfs_dir2.c    | 18 +++++++++---------
 libxfs/xfs_dir2.h    |  4 +++-
 repair/dino_chunks.c |  3 +--
 repair/phase6.c      |  2 +-
 4 files changed, 14 insertions(+), 13 deletions(-)

Eric,

This is a matching patch to the one I am sending Darrick for kernel, so that xfsprogs and kernel's libxfs will stay in sync.

Tested xfs_repair with the new xfs/348 test with -n ftype=0|1.

Amir.

v2:
- remove wrong argument about on-disk malformed mode from commit message
- address Brian's review comments
- test with new xfs/348

v1:
- fix patch for xfsprogs derived from kernel patch

libxfs/xfs_dir2.c +9 -9
index 4180a93..e21fd33 100644
@@ -39,15 +39,15 @@ struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2, XFS_DIR3_FT_DIR };
  * for file type specification. This will be propagated into the directory
  * structure if appropriate for the given operation and filesystem config.
  */
-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_dtype_to_ftype[S_DT_MAX+1] = {
+	[0]                = XFS_DIR3_FT_UNKNOWN,
+	[S_DT(S_IFREG)]    = XFS_DIR3_FT_REG_FILE,
+	[S_DT(S_IFDIR)]    = XFS_DIR3_FT_DIR,
+	[S_DT(S_IFCHR)]    = XFS_DIR3_FT_CHRDEV,
+	[S_DT(S_IFBLK)]    = XFS_DIR3_FT_BLKDEV,
+	[S_DT(S_IFIFO)]    = XFS_DIR3_FT_FIFO,
+	[S_DT(S_IFSOCK)]   = XFS_DIR3_FT_SOCK,
+	[S_DT(S_IFLNK)]    = XFS_DIR3_FT_SYMLINK,
 };
 
 /*
libxfs/xfs_dir2.h +3 -1
index 89b9e24..0291f0f 100644
@@ -35,7 +35,9 @@ extern struct xfs_name	xfs_name_dotdot;
  * directory filetype conversion tables.
  */
 #define S_SHIFT 12
-extern const unsigned char xfs_mode_to_ftype[];
+#define S_DT(mode)     (((mode) & S_IFMT) >> S_SHIFT)
+#define S_DT_MAX       S_DT(S_IFMT)
+extern const unsigned char xfs_dtype_to_ftype[];
 
 /*
  * directory operations vector for encode/decode routines
repair/dino_chunks.c +1 -2
index 4db9512..dd893af 100644
@@ -847,9 +847,8 @@ next_readbuf:
 			 * phase 6.
 			 */
 			di_mode = be16_to_cpu(dino->di_mode);
-			di_mode = (di_mode & S_IFMT) >> S_SHIFT;
 			set_inode_ftype(ino_rec, irec_offset,
-					xfs_mode_to_ftype[di_mode]);
+					xfs_dtype_to_ftype[S_DT(di_mode)]);
 
 			/*
 			 * store on-disk nlink count for comparing in phase 7
repair/phase6.c +1 -1
index 06eed16..941fffc 100644
@@ -1082,7 +1082,7 @@ mv_orphanage(
 	if ((err = -libxfs_iget(mp, NULL, ino, 0, &ino_p)))
 		do_error(_("%d - couldn't iget disconnected inode\n"), err);
 
-	xname.type = xfs_mode_to_ftype[(VFS_I(ino_p)->i_mode & S_IFMT)>>S_SHIFT];
+	xname.type = xfs_dtype_to_ftype[S_DT(VFS_I(ino_p)->i_mode)];
 
 	if (isa_dir)  {
 		irec = find_inode_rec(mp, XFS_INO_TO_AGNO(mp, orphanage_ino),
Signature · 1 line
2.7.4