lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20251118023602.15544-1-ssranevjti@gmail.com>
Date: Tue, 18 Nov 2025 08:06:02 +0530
From: ssrane_b23@...vjti.ac.in
To: linux-kernel@...r.kernel.org
Cc: daniel.pirkl@...il.cz,
	dushistov@...l.ru,
	Shaurya Rane <ssrane_b23@...vjti.ac.in>
Subject: [PATCH]  fs/ufs: optimize ufs_set_de_type() with lookup table

From: Shaurya Rane <ssrane_b23@...vjti.ac.in>

Replace the switch statement in ufs_set_de_type() with a static lookup
table as suggested by the existing TODO comment.

This optimization converts the case-by-case comparisons into a simple
array access. The implementation relies on the S_IFMT file type constants
having predictable bit patterns residing in the high bits (12-15).

By masking the mode and shifting right by 12 bits ((mode & S_IFMT) >> 12),
we obtain compact indices (0-15) that map directly to the DT_ constants:

  S_IFIFO  (0010000) -> index 1  -> DT_FIFO
  S_IFCHR  (0020000) -> index 2  -> DT_CHR
  S_IFDIR  (0040000) -> index 4  -> DT_DIR
  S_IFBLK  (0060000) -> index 6  -> DT_BLK
  S_IFREG  (0100000) -> index 8  -> DT_REG
  S_IFLNK  (0120000) -> index 10 -> DT_LNK
  S_IFSOCK (0140000) -> index 12 -> DT_SOCK

All other indices map to DT_UNKNOWN (0), maintaining identical logic to
the original default case.

Signed-off-by: Shaurya Rane <ssrane_b23@...vjti.ac.in>
---
 fs/ufs/util.h | 48 ++++++++++++++++++++----------------------------
 1 file changed, 20 insertions(+), 28 deletions(-)

diff --git a/fs/ufs/util.h b/fs/ufs/util.h
index 391bb4f11d74..eef8fed133db 100644
--- a/fs/ufs/util.h
+++ b/fs/ufs/util.h
@@ -149,37 +149,29 @@ ufs_set_de_namlen(struct super_block *sb, struct ufs_dir_entry *de, u16 value)
 static inline void
 ufs_set_de_type(struct super_block *sb, struct ufs_dir_entry *de, int mode)
 {
+	static const unsigned char ifmt_to_dt[16] = {
+		[0]	= DT_UNKNOWN,
+		[1]	= DT_FIFO,
+		[2]	= DT_CHR,
+		[3]	= DT_UNKNOWN,
+		[4]	= DT_DIR,
+		[5]	= DT_UNKNOWN,
+		[6]	= DT_BLK,
+		[7]	= DT_UNKNOWN,
+		[8]	= DT_REG,
+		[9]	= DT_UNKNOWN,
+		[10]	= DT_LNK,
+		[11]	= DT_UNKNOWN,
+		[12]	= DT_SOCK,
+		[13]	= DT_UNKNOWN,
+		[14]	= DT_UNKNOWN,
+		[15]	= DT_UNKNOWN,
+	};
+
 	if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) != UFS_DE_44BSD)
 		return;

-	/*
-	 * TODO turn this into a table lookup
-	 */
-	switch (mode & S_IFMT) {
-	case S_IFSOCK:
-		de->d_u.d_44.d_type = DT_SOCK;
-		break;
-	case S_IFLNK:
-		de->d_u.d_44.d_type = DT_LNK;
-		break;
-	case S_IFREG:
-		de->d_u.d_44.d_type = DT_REG;
-		break;
-	case S_IFBLK:
-		de->d_u.d_44.d_type = DT_BLK;
-		break;
-	case S_IFDIR:
-		de->d_u.d_44.d_type = DT_DIR;
-		break;
-	case S_IFCHR:
-		de->d_u.d_44.d_type = DT_CHR;
-		break;
-	case S_IFIFO:
-		de->d_u.d_44.d_type = DT_FIFO;
-		break;
-	default:
-		de->d_u.d_44.d_type = DT_UNKNOWN;
-	}
+	de->d_u.d_44.d_type = ifmt_to_dt[(mode & S_IFMT) >> 12];
 }

 static inline u32
--
2.34.1


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ