[RFC PATCH v4 09/10] ksmbd: add provenance time (ptime) and map it to CreationTime

From: koraynilay

Date: Sun Nov 15 2026 - 13:05:11 EST


Implement provenance time (ptime) in ksmbd, as introduced in a
previous commit ("vfs: add provenance time (ptime)").

Map it to SMB's CreationTime, making it settable by SMB clients. When
reporting CreationTime to clients, prioritize ptime over da.create_time
memorized in the user.DOSATTRIB xattr.

Update smb2_new_xattrs() and smb2_update_xattrs() to accept a stat
argument so they can check whether ptime is supported or already set and
behave accordingly. When ptime is natively supported and saved to the
filesystem, skip storing da.create_time in user.DOSATTRIB to avoid
redundant and desynchronized metadata, while falling back to the xattr
for filesystems without ptime.

Finally, add the ksmbd_vfs_set_ptime() helper to persist ptime to the
underlying filesystem, if supported.

Assisted-by: Gemini:3.8-flash-high antigravity-cli-1.5.7
Signed-off-by: koraynilay <koray.fra@xxxxxxxxx>
---
fs/smb/server/smb2pdu.c | 37 ++++++++++++++++++++++-------------
fs/smb/server/vfs.c | 43 +++++++++++++++++++++++++++++++++++++----
fs/smb/server/vfs.h | 1 +
3 files changed, 64 insertions(+), 17 deletions(-)

diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 63dfbffbb575..f389025dc518 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -3814,7 +3814,7 @@ static int smb2_create_truncate(const struct path *path)
}

static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, const struct path *path,
- struct ksmbd_file *fp)
+ struct ksmbd_file *fp, struct kstat *stat)
{
struct xattr_dos_attrib da = {0};
int rc;
@@ -3825,9 +3825,12 @@ static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, const struct path *

da.version = 4;
da.attr = le32_to_cpu(fp->f_ci->m_fattr);
- da.itime = da.create_time = fp->create_time;
- da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
- XATTR_DOSINFO_ITIME;
+ da.itime = fp->create_time;
+ da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_ITIME;
+ if (!(stat->result_mask & STATX_PTIME)) {
+ da.create_time = fp->create_time;
+ da.flags |= XATTR_DOSINFO_CREATE_TIME;
+ }

rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_idmap(path->mnt), path, &da, true);
if (rc)
@@ -3859,7 +3862,8 @@ static bool smb2_parent_compressed(struct ksmbd_tree_connect *tcon,
}

static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
- const struct path *path, struct ksmbd_file *fp)
+ const struct path *path, struct ksmbd_file *fp,
+ struct kstat *stat)
{
struct xattr_dos_attrib da = {};
bool store_dos_attrs = test_share_config_flag(tcon->share_conf,
@@ -3879,7 +3883,8 @@ static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
* or compression updates. Fall back to using a non-zero stored time
* for backwards compatibility.
*/
- if ((da.flags & XATTR_DOSINFO_CREATE_TIME) || da.create_time > 0)
+ if (!(stat->result_mask & STATX_PTIME) &&
+ ((da.flags & XATTR_DOSINFO_CREATE_TIME) || da.create_time > 0))
fp->create_time = da.create_time;
fp->itime = da.itime;
} else {
@@ -5505,7 +5510,9 @@ int smb2_open(struct ksmbd_work *work)
if (rc)
goto err_out1;

- if (stat.result_mask & STATX_BTIME)
+ if (stat.result_mask & STATX_PTIME)
+ fp->create_time = ksmbd_UnixTimeToNT(stat.ptime);
+ else if (stat.result_mask & STATX_BTIME)
fp->create_time = ksmbd_UnixTimeToNT(stat.btime);
else
fp->create_time = ksmbd_UnixTimeToNT(stat.ctime);
@@ -5517,12 +5524,12 @@ int smb2_open(struct ksmbd_work *work)
cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes)));

if (!created)
- smb2_update_xattrs(tcon, &path, fp);
+ smb2_update_xattrs(tcon, &path, fp, &stat);
if (need_truncate && req->FileAttributes) {
dos_attr = le32_to_cpu(req->FileAttributes);
fp->f_ci->m_fattr =
cpu_to_le32(smb2_get_dos_mode(&stat, dos_attr));
- smb2_new_xattrs(tcon, &path, fp);
+ smb2_new_xattrs(tcon, &path, fp, &stat);
}

ksmbd_vfs_update_compressed_fattr(path.dentry, &fp->f_ci->m_fattr);
@@ -5544,7 +5551,7 @@ int smb2_open(struct ksmbd_work *work)
}

if (created)
- smb2_new_xattrs(tcon, &path, fp);
+ smb2_new_xattrs(tcon, &path, fp, &stat);

fp->create_action = cpu_to_le32(file_info);

@@ -8995,6 +9002,7 @@ static int set_file_basic_info(struct ksmbd_file *fp,
const struct mnt_idmap *idmap;
__le32 attrs_mask = FILE_ATTRIBUTE_DIRECTORY_LE |
FILE_ATTRIBUTE_COMPRESSED_LE;
+ bool ptime_saved = false;
s64 crtime, time;
int rc = 0;

@@ -9016,8 +9024,11 @@ static int set_file_basic_info(struct ksmbd_file *fp,
return rc;

crtime = (s64)le64_to_cpu(file_info->CreationTime);
- if (crtime > 0)
+ if (crtime > 0) {
fp->create_time = crtime;
+ if (ksmbd_vfs_set_ptime(idmap, filp, crtime) == 0)
+ ptime_saved = true;
+ }

time = (s64)le64_to_cpu(file_info->LastAccessTime);
if (time > 0) {
@@ -9053,7 +9064,7 @@ static int set_file_basic_info(struct ksmbd_file *fp,
}

if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
- (crtime > 0 || file_info->Attributes)) {
+ ((!ptime_saved && crtime > 0) || file_info->Attributes)) {
struct xattr_dos_attrib da = {0};

rc = ksmbd_vfs_get_dos_attrib_xattr(idmap, fp->filp->f_path.dentry, &da);
@@ -9066,7 +9077,7 @@ static int set_file_basic_info(struct ksmbd_file *fp,
* itime gets preserved when reading the xattr if already present
* otherwise don't write it as it wasn't asked for by the client
*/
- if (crtime > 0) {
+ if (!ptime_saved && crtime > 0) {
da.create_time = fp->create_time;
da.flags |= XATTR_DOSINFO_CREATE_TIME;
} else if (da.create_time > 0) {
diff --git a/fs/smb/server/vfs.c b/fs/smb/server/vfs.c
index da13f13f8980..63453f5d02b9 100644
--- a/fs/smb/server/vfs.c
+++ b/fs/smb/server/vfs.c
@@ -602,7 +602,8 @@ int ksmbd_vfs_getattr(const struct path *path, struct kstat *stat)
{
int err;

- err = vfs_getattr(path, stat, STATX_BASIC_STATS | STATX_BTIME,
+ err = vfs_getattr(path, stat,
+ STATX_BASIC_STATS | STATX_BTIME | STATX_PTIME,
AT_STATX_SYNC_AS_STAT);
if (err)
pr_err("getattr failed, err %d\n", err);
@@ -1909,12 +1910,14 @@ int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work,
};

rc = vfs_getattr(&path, ksmbd_kstat->kstat,
- STATX_BASIC_STATS | STATX_BTIME,
+ STATX_BASIC_STATS | STATX_BTIME | STATX_PTIME,
AT_STATX_SYNC_AS_STAT);
if (rc)
return rc;

- if (ksmbd_kstat->kstat->result_mask & STATX_BTIME)
+ if (ksmbd_kstat->kstat->result_mask & STATX_PTIME)
+ time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ptime);
+ else if (ksmbd_kstat->kstat->result_mask & STATX_BTIME)
time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->btime);
else
time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
@@ -1941,7 +1944,8 @@ int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work,
* or compression updates. Fall back to using a non-zero stored time
* for backwards compatibility.
*/
- if ((da.flags & XATTR_DOSINFO_CREATE_TIME) || da.create_time > 0)
+ if (!(ksmbd_kstat->kstat->result_mask & STATX_PTIME) &&
+ ((da.flags & XATTR_DOSINFO_CREATE_TIME) || da.create_time > 0))
ksmbd_kstat->create_time = da.create_time;
} else {
ksmbd_debug(VFS, "fail to load dos attribute.\n");
@@ -2524,3 +2528,34 @@ int ksmbd_vfs_set_compression_create(struct ksmbd_work *work,
{
return __ksmbd_vfs_set_compression(work, fp, fmt, false);
}
+
+/**
+ * ksmbd_vfs_set_ptime() - set provenance time on filesystem
+ * @idmap: mount idmap
+ * @filp: open file pointer
+ * @crtime: creation time in NT time
+ *
+ * Return: 0 on success, otherwise error
+ */
+int ksmbd_vfs_set_ptime(const struct mnt_idmap *idmap, struct file *filp, u64 crtime)
+{
+ struct file_kattr fa = {
+ .fsx_valid = true,
+ .fsx_xflags = FS_XFLAG_PTIME,
+ .fsx_xflags_mask = FS_XFLAG_PTIME,
+ .fsx_ptime = ksmbd_NTtimeToUnix(cpu_to_le64(crtime)),
+ };
+ const struct cred *saved_cred;
+ int rc;
+
+ saved_cred = override_creds(filp->f_cred);
+ rc = mnt_want_write_file(filp);
+ if (rc)
+ goto out;
+
+ rc = vfs_fileattr_set(idmap, filp->f_path.dentry, &fa);
+ mnt_drop_write_file(filp);
+out:
+ revert_creds(saved_cred);
+ return rc;
+}
diff --git a/fs/smb/server/vfs.h b/fs/smb/server/vfs.h
index 21b0d29a3db0..5925d63841f2 100644
--- a/fs/smb/server/vfs.h
+++ b/fs/smb/server/vfs.h
@@ -187,4 +187,5 @@ int ksmbd_vfs_get_compression(struct ksmbd_file *fp, u16 *fmt);
int ksmbd_vfs_set_compression(struct ksmbd_work *work, struct ksmbd_file *fp, u16 fmt);
int ksmbd_vfs_set_compression_create(struct ksmbd_work *work,
struct ksmbd_file *fp, u16 fmt);
+int ksmbd_vfs_set_ptime(const struct mnt_idmap *idmap, struct file *filp, u64 crtime);
#endif /* __KSMBD_VFS_H__ */
--
2.56.0