Skip to contentSkip to conversation

ntfs3: correctly create symlink for relative path

PATCH New Rong Zhang 2 participants 2 messages May 7, 2025 – May 8, 2025 NTFS3 filesystem

lore
Share & tools
lore

Rong Zhang

author
#0 lore raw

PATCH 2/2 ntfs3: correctly create symlink for relative path

After applying this patch, could correctly create symlink:

ln -s "relative/path/to/file" symlink

---
 fs/ntfs3/inode.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
fs/ntfs3/inode.c +18 -13
index 2641738d2934..7c79c2c7eff6 100644
@@ -1098,10 +1098,10 @@ int inode_read_data(struct inode *inode, void *data, size_t bytes)
  * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
  * for unicode string of @uni_len length.
  */
-static inline u32 ntfs_reparse_bytes(u32 uni_len)
+static inline u32 ntfs_reparse_bytes(u32 uni_len, bool is_absolute)
 {
 	/* Header + unicode string + decorated unicode string. */
-	return sizeof(short) * (2 * uni_len + 4) +
+	return sizeof(short) * (2 * uni_len + (is_absolute ? 4 : 0)) +
 	       offsetof(struct REPARSE_DATA_BUFFER,
 			SymbolicLinkReparseBuffer.PathBuffer);
 }
@@ -1114,8 +1114,11 @@ ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
 	struct REPARSE_DATA_BUFFER *rp;
 	__le16 *rp_name;
 	typeof(rp->SymbolicLinkReparseBuffer) *rs;
+	bool is_absolute;
 
-	rp = kzalloc(ntfs_reparse_bytes(2 * size + 2), GFP_NOFS);
+	is_absolute = (strlen(symname) > 1 && symname[1] == ':');
+
+	rp = kzalloc(ntfs_reparse_bytes(2 * size + 2, is_absolute), GFP_NOFS);
 	if (!rp)
 		return ERR_PTR(-ENOMEM);
 
@@ -1130,7 +1133,7 @@ ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
 		goto out;
 
 	/* err = the length of unicode name of symlink. */
-	*nsize = ntfs_reparse_bytes(err);
+	*nsize = ntfs_reparse_bytes(err, is_absolute);
 
 	if (*nsize > sbi->reparse.max_size) {
 		err = -EFBIG;
@@ -1150,7 +1153,7 @@ ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
 
 	/* PrintName + SubstituteName. */
 	rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
-	rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + 8);
+	rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + (is_absolute ? 8 : 0));
 	rs->PrintNameLength = rs->SubstituteNameOffset;
 
 	/*
@@ -1158,16 +1161,18 @@ ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
 	 * parse this path.
 	 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
 	 */
-	rs->Flags = 0;
+	rs->Flags = is_absolute ? 0 : SYMLINK_FLAG_RELATIVE;
 
-	memmove(rp_name + err + 4, rp_name, sizeof(short) * err);
+	memmove(rp_name + err + (is_absolute ? 4 : 0), rp_name, sizeof(short) * err);
 
-	/* Decorate SubstituteName. */
-	rp_name += err;
-	rp_name[0] = cpu_to_le16('\\');
-	rp_name[1] = cpu_to_le16('?');
-	rp_name[2] = cpu_to_le16('?');
-	rp_name[3] = cpu_to_le16('\\');
+	if (is_absolute) {
+		/* Decorate SubstituteName. */
+		rp_name += err;
+		rp_name[0] = cpu_to_le16('\\');
+		rp_name[1] = cpu_to_le16('?');
+		rp_name[2] = cpu_to_le16('?');
+		rp_name[3] = cpu_to_le16('\\');
+	}
 
 	return rp;
 out:
Signature · 1 line
2.49.0
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Rong,

kernel test robot noticed the following build warnings:

[auto build test WARNING on brauner-vfs/vfs.all]
[also build test WARNING on linus/master v6.15-rc5 next-20250508] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rong-Zhang/ntfs3-correctly-create-symlink-for-relative-path/20250507-153624
base:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all
patch link:    https://lore.kernel.org/r/20250507073534.11693-1-ulin0208%40gmail.com
patch subject: [PATCH 2/2] ntfs3: correctly create symlink for relative path
config: i386-randconfig-r133-20250508 (https://download.01.org/0day-ci/archive/20250509/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250509/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> fs/ntfs3/inode.c:1124:19: sparse: sparse: incorrect type in assignment (different base types) @@     expected restricted __le32 [usertype] Flags @@     got int @@

fs/ntfs3/inode.c:1124:19: sparse: expected restricted __le32 [usertype] Flags
fs/ntfs3/inode.c:1124:19: sparse: got int

vim +1124 fs/ntfs3/inode.c

1068
1069 static struct REPARSE_DATA_BUFFER *
1070 ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
1071 u32 size, u16 *nsize)
1072 {
1073 int i, err;
1074 struct REPARSE_DATA_BUFFER *rp;
1075 __le16 *rp_name;
1076 typeof(rp->SymbolicLinkReparseBuffer) *rs;
1077 bool is_absolute;
1078
1079 is_absolute = (strlen(symname) > 1 && symname[1] == ':');
1080
1081 rp = kzalloc(ntfs_reparse_bytes(2 * size + 2, is_absolute), GFP_NOFS);
1082 if (!rp)
1083 return ERR_PTR(-ENOMEM);
1084
1085 rs = &rp->SymbolicLinkReparseBuffer;
1086 rp_name = rs->PathBuffer;
1087
1088 /* Convert link name to UTF-16. */
1089 err = ntfs_nls_to_utf16(sbi, symname, size,
1090 (struct cpu_str *)(rp_name - 1), 2 * size,
1091 UTF16_LITTLE_ENDIAN);
1092 if (err < 0)
1093 goto out;
1094
1095 /* err = the length of unicode name of symlink. */
1096 *nsize = ntfs_reparse_bytes(err, is_absolute);
1097
1098 if (*nsize > sbi->reparse.max_size) {
1099 err = -EFBIG;
1100 goto out;
1101 }
1102
1103 /* Translate Linux '/' into Windows '\'. */
1104 for (i = 0; i < err; i++) {
1105 if (rp_name[i] == cpu_to_le16('/'))
1106 rp_name[i] = cpu_to_le16('\\');
1107 }
1108
1109 rp->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1110 rp->ReparseDataLength =
1111 cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER,
1112 SymbolicLinkReparseBuffer));
1113
1114 /* PrintName + SubstituteName. */
1115 rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
1116 rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + (is_absolute ? 8 : 0));
1117 rs->PrintNameLength = rs->SubstituteNameOffset;
1118
1119 /*
1120 * TODO: Use relative path if possible to allow Windows to
1121 * parse this path.
1122 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
1123 */

> 1124		rs->Flags = is_absolute ? 0 : SYMLINK_FLAG_RELATIVE;

1125
1126 memmove(rp_name + err + (is_absolute ? 4 : 0), rp_name, sizeof(short) * err);
1127
1128 if (is_absolute) {
1129 /* Decorate SubstituteName. */
1130 rp_name += err;
1131 rp_name[0] = cpu_to_le16('\\');
1132 rp_name[1] = cpu_to_le16('?');
1133 rp_name[2] = cpu_to_le16('?');
1134 rp_name[3] = cpu_to_le16('\\');
1135 }
1136
1137 return rp;
1138 out:
1139 kfree(rp);
1140 return ERR_PTR(err);
1141 }
1142

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki