[PATCH v4 12/12] ntfs: support O_TMPFILE

From: Baolin Liu

Date: Sun Nov 15 2026 - 08:28:27 EST


From: Baolin Liu <liubaolin@xxxxxxxxxx>

Create regular files without FILE_NAME or directory index entries, with
zero on-disk links, and attach them using d_tmpfile(). Clear the in-
memory link count before EA setup so attribute inodes cannot pin
unpublished files in cache.

Allow the first link only with I_LINKABLE, preserving existing deletion
checks. Let the VFS enforce O_EXCL and clear I_LINKABLE after
publication. Reuse zero-link eviction for cleanup.

Document the interface and named-stream restrictions before publication.

Reviewed-by: Hyunchul Lee <hyc.lee@xxxxxxxxx>
Signed-off-by: Baolin Liu <liubaolin@xxxxxxxxxx>
---
No code changes since v3; rebased on the preceding fixes.

QEMU creation, publication, cleanup, O_EXCL, ACLs, permissions, idmapped
mounts, streams, EAs, direct I/O, mmap and concurrency checks passed.
Persistence, full-volume retries and injected rollback checks passed,
as did generic/004 and generic/389.

Documentation/filesystems/ntfs.rst | 15 +++++++++
fs/ntfs/namei.c | 54 ++++++++++++++++++++++++++----
2 files changed, 63 insertions(+), 6 deletions(-)

diff --git a/Documentation/filesystems/ntfs.rst b/Documentation/filesystems/ntfs.rst
index d5819da6ab53..635301117807 100644
--- a/Documentation/filesystems/ntfs.rst
+++ b/Documentation/filesystems/ntfs.rst
@@ -10,6 +10,7 @@ The Linux NTFS filesystem driver
- Overview
- Utilities support
- Linux file attributes
+ - Temporary files
- Supported mount options


@@ -63,6 +64,20 @@ These Linux flags are enforced by Linux. Storing them in an NTFS EA does
not make Windows enforce immutable, append-only, nodump or noatime semantics.


+Temporary files
+===============
+
+The driver supports ``O_TMPFILE`` for regular files. The file has no
+directory entry until it is linked with linkat(2); ``O_EXCL`` prevents
+this publication. Ownership and mode follow normal file creation rules.
+With ACLs enabled, the file's access ACL is derived from the creation
+directory's default ACL. Unpublished files are reclaimed when their last
+reference is released.
+
+Named stream ioctls cannot create or open streams on an unpublished
+temporary file. They can be used after the file has been linked.
+
+
Supported mount options
=======================

diff --git a/fs/ntfs/namei.c b/fs/ntfs/namei.c
index 1b4d1e02e183..67f1d7eecf2b 100644
--- a/fs/ntfs/namei.c
+++ b/fs/ntfs/namei.c
@@ -490,9 +490,11 @@ static struct ntfs_inode *__ntfs_create(const struct mnt_idmap *idmap, struct in
simple_inode_init_ts(vi);
ni->i_crtime = inode_get_ctime(vi);

- inode_set_mtime_to_ts(dir, ni->i_crtime);
- inode_set_ctime_to_ts(dir, ni->i_crtime);
- mark_inode_dirty(dir);
+ if (name) {
+ inode_set_mtime_to_ts(dir, ni->i_crtime);
+ inode_set_ctime_to_ts(dir, ni->i_crtime);
+ mark_inode_dirty(dir);
+ }

err = ntfs_mft_record_alloc(dir_ni->vol, mode, &ni, NULL,
&ni_mrec, -1);
@@ -514,6 +516,10 @@ static struct ntfs_inode *__ntfs_create(const struct mnt_idmap *idmap, struct in
inode_set_iversion(vi, 1);
insert_inode_hash(vi);

+ /* Attribute inodes must not pin an unpublished file in cache. */
+ if (!name)
+ clear_nlink(vi);
+
mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
if (NInoBeingDeleted(dir_ni)) {
@@ -659,6 +665,12 @@ static struct ntfs_inode *__ntfs_create(const struct mnt_idmap *idmap, struct in
if (err)
goto err_out;

+ if (!name) {
+ ni_mrec->link_count = 0;
+ mark_mft_record_dirty(ni);
+ goto done;
+ }
+
/* Create FILE_NAME attribute. */
fn_len = sizeof(struct file_name_attr) + name_len * sizeof(__le16);
fn = kzalloc(fn_len, GFP_NOFS);
@@ -711,12 +723,13 @@ static struct ntfs_inode *__ntfs_create(const struct mnt_idmap *idmap, struct in
goto err_out;
}

+ ni->flags = fn->file_attributes |
+ (ni->flags & FILE_ATTRIBUTE_RECALL_ON_OPEN);
+done:
unmap_mft_record(ni);
mutex_unlock(&dir_ni->mrec_lock);
mutex_unlock(&ni->mrec_lock);

- ni->flags = fn->file_attributes |
- (ni->flags & FILE_ATTRIBUTE_RECALL_ON_OPEN);
/* Set the sequence number. */
vi->i_generation = ni->seq_no;
set_nlink(vi, 1);
@@ -793,6 +806,8 @@ static struct ntfs_inode *__ntfs_create(const struct mnt_idmap *idmap, struct in
mutex_unlock(&dir_ni->mrec_lock);
mutex_unlock(&ni->mrec_lock);

+ /* The MFT record has already been freed above. */
+ set_nlink(vi, 1);
if (rollback_ea_vi)
iput(rollback_ea_vi);
if (rollback_eai_vi)
@@ -871,6 +886,31 @@ static int ntfs_create(const struct mnt_idmap *idmap, struct inode *dir,
return 0;
}

+static int ntfs_tmpfile(const struct mnt_idmap *idmap, struct inode *dir,
+ struct file *file, umode_t mode)
+{
+ struct ntfs_volume *vol = NTFS_SB(dir->i_sb);
+ struct ntfs_inode *ni;
+ struct inode *inode;
+ int err;
+
+ if (NVolShutdown(vol))
+ return -EIO;
+
+ err = ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
+ if (err)
+ return err;
+
+ ni = __ntfs_create(idmap, dir, NULL, 0, mode, 0, NULL, 0);
+ if (IS_ERR(ni))
+ return PTR_ERR(ni);
+
+ inode = VFS_I(ni);
+ d_tmpfile(file, inode);
+ unlock_new_inode(inode);
+ return finish_open_simple(file, 0);
+}
+
static int ntfs_check_unlinkable_dir(struct ntfs_attr_search_ctx *ctx, struct file_name_attr *fn)
{
int link_count;
@@ -1340,7 +1380,8 @@ static int __ntfs_link(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
goto err_out;
}

- if (le16_to_cpu(ni_mrec->link_count) == 0) {
+ if (!ni_mrec->link_count &&
+ !(inode_state_read_once(vi) & I_LINKABLE)) {
err = -ENOENT;
goto err_out;
}
@@ -1781,6 +1822,7 @@ static int ntfs_link(struct dentry *old_dentry, struct inode *dir,
const struct inode_operations ntfs_dir_inode_ops = {
.lookup = ntfs_lookup, /* VFS: Lookup directory. */
.create = ntfs_create,
+ .tmpfile = ntfs_tmpfile,
.unlink = ntfs_unlink,
.mkdir = ntfs_mkdir,
.rmdir = ntfs_rmdir,