* [PATCH] ntfs3: defer clean $LogFile reset until the first write
@ 2026-12-02 13:27 Artem S. Tashkinov
0 siblings, 0 replies; only message in thread
From: Artem S. Tashkinov @ 2026-12-02 13:27 UTC (permalink / raw)
To: Konstantin Komarov; +Cc: ntfs3, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 221 bytes --]
Hello everyone,
The patch is against the Linux 7.4-rc5 NTFS3 source. Applies to 7.3.7 as
well.
I've verified it works as expected. No writes occur until the first
write to an NTFS partition.
Regards,
Artem
[-- Attachment #2: ntfs3-defer-clean-logfile-reset-rfc.patch --]
[-- Type: text/x-patch, Size: 23070 bytes --]
ntfs3: defer clean $LogFile reset until the first write
Mounting an NTFS volume read-write can generate 67,117,056 bytes of
writes before any application modifies a file. The mount path provides
an exact match for that count. When log analysis reaches end_replay,
log_replay() can write two 4 KiB restart pages. Its caller then flushes
those writes and fills every allocated extent of an initialized
$LogFile with 0xff. For a 64 MiB journal, the total is 64 MiB + 8 KiB.
The noatime mount option has no bearing on either operation.
Inspecting the journal at mount is necessary. It establishes whether
the volume needs recovery before its metadata can be used. Clearing a
clean journal serves a different purpose: once Linux starts modifying
the volume, a later mount must not replay stale records against the
new metadata. A session that only reads the volume can leave that
journal intact.
Defer the reset when the primary boot sector is usable, the volume
was clean on entry and log analysis finds no work requiring immediate
recovery. Keep the existing mount-time handling for dirty volumes,
CHKDSK restart pages, incomplete log transfers and transaction replay.
In particular, last_log_lsn() can repair a log tail before the dirty
page and transaction tables are examined. An empty pair of tables
alone is therefore insufficient grounds for deferral.
For an eligible initialized journal, validate its extents and save a
private copy of the runlist in the superblock. If log_replay() would
write restart pages, retain the prepared page in memory. Publish the
pending reset only after its state is complete. Unmount can then free
that state without writing the journal if no operation required it.
Add ntfs_prepare_write() at the points where NTFS3 can begin modifying
file data or metadata. These include ordinary writes, shared writable
page faults, allocation and bitmap changes, namespace operations,
attribute updates and metadata writeback. Checking write_iter alone
would miss a rename or a store through a shared mapping. Conversely,
closing an unused writable file descriptor can enter the preallocation
trim path even when there is nothing to trim. Check the existing
attribute size and allocation before triggering the reset there.
Serialize the first writers with a per-superblock mutex. Write both
restart pages and flush them if a saved page is present. Then walk the
saved journal extents through the block-device buffer cache, skip
blocks already filled with 0xff and synchronously write each changed
block. Flush the device before clearing the pending flag. A reset I/O
error stays recorded for the lifetime of the mount so a later writer
cannot proceed after a partial reset. An IOCB_NOWAIT write encountering
a pending reset returns -EAGAIN.
This shifts the journal scan and any required clearing to the first
modification. A read-only workload on an eligible read-write mount
avoids both costs. A journal full of old records can still require a
full rewrite, and the first writer pays the latency of the synchronous
block writes.
Reported-by: Artem S. Tashkinov <aros@gmx.com>
Tested-by: Artem S. Tashkinov <aros@gmx.com>
Assisted-by: LLM
diff --git a/fs/ntfs3/attrib.c b/fs/ntfs3/attrib.c
index 805acc3..a4a5689 100644
--- a/fs/ntfs3/attrib.c
+++ b/fs/ntfs3/attrib.c
@@ -1214,6 +1214,10 @@ again:
if (vcn + to_alloc > asize)
to_alloc = asize - vcn;
+ err = ntfs_prepare_write(sbi);
+ if (err)
+ goto out;
+
if (da) {
CLST rlen1, rlen2;
if (!ntfs_check_free_space(sbi, to_alloc, 0, true)) {
diff --git a/fs/ntfs3/bitmap.c b/fs/ntfs3/bitmap.c
index 52feb35..58a4dd0 100644
--- a/fs/ntfs3/bitmap.c
+++ b/fs/ntfs3/bitmap.c
@@ -733,6 +733,10 @@ int wnd_set_free(struct wnd_bitmap *wnd, size_t bit, size_t bits)
struct buffer_head *bh;
u32 op;
+ err = ntfs_prepare_write(ntfs_sb(sb));
+ if (err)
+ return err;
+
for (; iw < wnd->nwnd && bits; iw++, bit += op, bits -= op, wbit = 0) {
if (iw + 1 == wnd->nwnd)
wbits = wnd->bits_last;
@@ -775,6 +779,10 @@ int wnd_set_used(struct wnd_bitmap *wnd, size_t bit, size_t bits)
struct buffer_head *bh;
u32 op;
+ err = ntfs_prepare_write(ntfs_sb(sb));
+ if (err)
+ return err;
+
for (; iw < wnd->nwnd && bits; iw++, bit += op, bits -= op, wbit = 0) {
if (unlikely(iw + 1 == wnd->nwnd))
wbits = wnd->bits_last;
@@ -1335,6 +1343,10 @@ int wnd_extend(struct wnd_bitmap *wnd, size_t new_bits)
if (new_bits <= old_bits)
return -EINVAL;
+ err = ntfs_prepare_write(sbi);
+ if (err)
+ return err;
+
/* Align to 8 byte boundary. */
new_wnd = bytes_to_block(sb, ntfs3_bitmap_size(new_bits));
new_last = new_bits & (wbits - 1);
diff --git a/fs/ntfs3/file.c b/fs/ntfs3/file.c
index 6c84a3e..ec50fa8 100644
--- a/fs/ntfs3/file.c
+++ b/fs/ntfs3/file.c
@@ -137,6 +137,7 @@ int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
struct ntfs_inode *ni = ntfs_i(inode);
u32 flags = fa->flags;
unsigned int new_fl = 0;
+ int err;
/* Avoid any operation if inode is bad. */
if (unlikely(is_bad_ni(ni)))
@@ -154,6 +155,10 @@ int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
if (flags & FS_APPEND_FL)
new_fl |= S_APPEND;
+ err = ntfs_prepare_write(ni->mi.sbi);
+ if (err)
+ return err;
+
inode_set_flags(inode, new_fl, S_IMMUTABLE | S_APPEND);
/* Save nodump flag to return in ntfs_getattr. */
@@ -371,12 +376,29 @@ static void ntfs_filemap_close(struct vm_area_struct *vma)
ntfs_zero_tail(inode->i_mapping, ni->i_valid);
}
+static vm_fault_t ntfs_page_mkwrite(struct vm_fault *vmf)
+{
+ struct inode *inode = file_inode(vmf->vma->vm_file);
+ int err = ntfs_prepare_write(ntfs_sb(inode->i_sb));
+
+ if (err)
+ return vmf_error(err);
+ return filemap_page_mkwrite(vmf);
+}
+
+/* An initially read-only shared mapping can become writable via mprotect. */
+static const struct vm_operations_struct ntfs_read_vm_ops = {
+ .fault = filemap_fault,
+ .map_pages = filemap_map_pages,
+ .page_mkwrite = ntfs_page_mkwrite,
+};
+
/* Copy of generic_file_vm_ops. */
static const struct vm_operations_struct ntfs_file_vm_ops = {
.close = ntfs_filemap_close,
.fault = filemap_fault,
.map_pages = filemap_map_pages,
- .page_mkwrite = filemap_page_mkwrite,
+ .page_mkwrite = ntfs_page_mkwrite,
};
/*
@@ -444,7 +466,9 @@ static int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
err = -EAGAIN;
goto out;
}
- err = ntfs_extend_initialized_size(file, ni, to);
+ err = ntfs_prepare_write(ni->mi.sbi);
+ if (!err)
+ err = ntfs_extend_initialized_size(file, ni, to);
inode_unlock(inode);
if (err)
goto out;
@@ -452,8 +476,8 @@ static int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
}
err = generic_file_mmap_prepare(desc);
- if (!err && rw)
- desc->vm_ops = &ntfs_file_vm_ops;
+ if (!err)
+ desc->vm_ops = rw ? &ntfs_file_vm_ops : &ntfs_read_vm_ops;
out:
return err;
}
@@ -505,6 +529,10 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
return -EOPNOTSUPP;
}
+ err = ntfs_prepare_write(sbi);
+ if (err)
+ return err;
+
ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
inode_lock(inode);
@@ -727,6 +755,10 @@ int ntfs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
if (err)
goto out;
+ err = ntfs_prepare_write(ni->mi.sbi);
+ if (err)
+ return err;
+
if (ia_valid & ATTR_SIZE) {
loff_t newsize, oldsize;
@@ -1272,6 +1304,15 @@ static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
if (ret <= 0)
goto out;
+ if ((iocb->ki_flags & IOCB_NOWAIT) &&
+ READ_ONCE(ni->mi.sbi->log_pending)) {
+ ret = -EAGAIN;
+ goto out;
+ }
+ ret = ntfs_prepare_write(ni->mi.sbi);
+ if (ret)
+ goto out;
+
err = file_modified(iocb->ki_filp);
if (err) {
ret = err;
@@ -1416,6 +1457,41 @@ int ntfs_file_open(struct inode *inode, struct file *file)
return generic_file_open(inode, file);
}
+/* The caller holds ni_lock and the runlist write lock. */
+static int ntfs_trim_prealloc(struct ntfs_inode *ni)
+{
+ struct ntfs_sb_info *sbi = ni->mi.sbi;
+ u64 size = i_size_read(&ni->vfs_inode);
+
+ if (unlikely(smp_load_acquire(&sbi->log_pending))) {
+ struct ATTRIB *a;
+ u64 unit = sbi->cluster_size;
+ int err;
+
+ a = ni_find_attr(ni, NULL, NULL, ATTR_DATA, ni->file.ads.name,
+ ni->file.ads.len, NULL, NULL);
+ if (!a)
+ return -ENOENT;
+ if (!a->non_res && le32_to_cpu(a->res.data_size) == size)
+ return 0;
+ if (a->non_res) {
+ if (is_attr_ext(a))
+ unit <<= a->nres.c_unit;
+ if (le64_to_cpu(a->nres.data_size) == size &&
+ le64_to_cpu(a->nres.valid_size) == ni->i_valid &&
+ le64_to_cpu(a->nres.alloc_size) == round_up(size, unit))
+ return 0;
+ }
+ err = ntfs_prepare_write(sbi);
+ if (err)
+ return err;
+ }
+
+ return attr_set_size_ex(ni, ATTR_DATA, ni->file.ads.name,
+ ni->file.ads.len, &ni->file.run, size,
+ &ni->i_valid, false, NULL, true);
+}
+
/*
* ntfs_file_release - file_operations::release
*
@@ -1447,10 +1523,7 @@ static int ntfs_file_release(struct inode *inode, struct file *file)
down_write(&ni->file.run_lock);
/* Deallocate preallocated. */
- err = attr_set_size_ex(ni, ATTR_DATA, ni->file.ads.name,
- ni->file.ads.len, &ni->file.run,
- inode->i_size, &ni->i_valid, false, NULL,
- true);
+ err = ntfs_trim_prealloc(ni);
up_write(&ni->file.run_lock);
ni_unlock(ni);
diff --git a/fs/ntfs3/frecord.c b/fs/ntfs3/frecord.c
index 16ac847..bcec70d 100644
--- a/fs/ntfs3/frecord.c
+++ b/fs/ntfs3/frecord.c
@@ -3246,6 +3246,13 @@ int ni_write_inode(struct inode *inode, int sync, const char *hint)
if (modified)
ni->mi.dirty = true;
+ if (unlikely(smp_load_acquire(&sbi->log_pending)) &&
+ (modified || ni_is_dirty(inode))) {
+ err = ntfs_prepare_write(sbi);
+ if (err)
+ goto out;
+ }
+
if (!ntfs_is_meta_file(sbi, inode->i_ino) &&
(modified || (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
/* Avoid __wait_on_freeing_inode(inode). */
diff --git a/fs/ntfs3/fslog.c b/fs/ntfs3/fslog.c
index bb8d40b..d1b1b03 100644
--- a/fs/ntfs3/fslog.c
+++ b/fs/ntfs3/fslog.c
@@ -1633,7 +1633,7 @@ static bool check_subseq_log_page(struct ntfs_log *log,
* Walks through the log pages for a file, searching for the
* last log page written to the file.
*/
-static int last_log_lsn(struct ntfs_log *log)
+static int last_log_lsn(struct ntfs_log *log, bool *defer)
{
int err;
bool usa_error = false;
@@ -2207,6 +2207,8 @@ file_is_valid:
if (page_off1 || tail_page) {
struct RECORD_PAGE_HDR *tmp_page;
+ *defer = false;
+
if (sb_rdonly(log->ni->mi.sbi->sb)) {
err = -EROFS;
goto out;
@@ -2266,6 +2268,7 @@ file_is_valid:
}
if (part_io_count) {
+ *defer = false;
if (sb_rdonly(log->ni->mi.sbi->sb)) {
err = -EROFS;
goto out;
@@ -3861,7 +3864,7 @@ dirty_vol:
* It replays log and empties it.
* Initialized is set false if logfile contains '-1'.
*/
-int log_replay(struct ntfs_inode *ni, bool *initialized)
+int log_replay(struct ntfs_inode *ni, bool *initialized, bool *defer)
{
int err;
struct ntfs_sb_info *sbi = ni->mi.sbi;
@@ -3997,6 +4000,9 @@ use_first_page:
kfree(log->rst_info2.r_page);
check_restart_area:
+ if (log->rst_info.chkdsk_was_run)
+ *defer = false;
+
/*
* If the restart area is at offset 0, we want
* to write the second restart area first.
@@ -4191,7 +4197,7 @@ find_oldest:
le32_add_cpu(&ra->open_log_count, 1);
/* Now we need to walk through looking for the last lsn. */
- err = last_log_lsn(log);
+ err = last_log_lsn(log, defer);
if (err)
goto out;
@@ -4890,6 +4896,7 @@ end_log_records_enumerate:
if ((!dptbl || !dptbl->total) && (!trtbl || !trtbl->total))
goto end_replay;
+ *defer = false;
sbi->flags |= NTFS_FLAGS_NEED_REPLAY;
if (is_ro)
goto out;
@@ -5327,7 +5334,7 @@ undo_action_done:
end_replay:
err = 0;
- if (is_ro)
+ if (is_ro && !*defer)
goto out;
rh = kzalloc(log->page_size, GFP_NOFS);
@@ -5360,6 +5367,16 @@ end_replay:
ntfs_fix_pre_write(&rh->rhdr, log->page_size);
+ if (*defer && *initialized && !log->set_dirty) {
+ sbi->restart_page = rh;
+ sbi->restart_size = log->page_size;
+ goto out;
+ }
+ if (is_ro) {
+ kfree(rh);
+ goto out;
+ }
+
err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rh, log->page_size, 0);
if (!err)
err = ntfs_sb_write_run(sbi, &log->ni->file.run, log->page_size,
@@ -5416,8 +5433,10 @@ out:
if (err == -EROFS)
err = 0;
- else if (log->set_dirty)
+ else if (log->set_dirty) {
+ *defer = false;
ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
+ }
trace_ntfs3_log_replay(&ni->vfs_inode, *initialized, err);
diff --git a/fs/ntfs3/fsntfs.c b/fs/ntfs3/fsntfs.c
index b55311c..815c785 100644
--- a/fs/ntfs3/fsntfs.c
+++ b/fs/ntfs3/fsntfs.c
@@ -10,6 +10,7 @@
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/nls.h>
+#include <linux/sched/mm.h>
#include "debug.h"
#include "ntfs.h"
@@ -279,7 +280,97 @@ out:
return err;
}
-int ntfs_loadlog_and_replay(struct ntfs_inode *ni, struct ntfs_sb_info *sbi)
+/* Use the buffer cache so previously read journal blocks stay coherent. */
+static int ntfs_clear_log_blocks(struct ntfs_sb_info *sbi,
+ const struct runs_tree *run)
+{
+ struct super_block *sb = sbi->sb;
+ size_t idx = 0;
+ CLST lcn, len;
+
+ while (run_get_entry(run, idx++, NULL, &lcn, &len)) {
+ u64 block = (u64)lcn * sbi->blocks_per_cluster;
+ u64 count = (u64)len * sbi->blocks_per_cluster;
+
+ while (count--) {
+ struct buffer_head *bh = ntfs_bread(sb, block++);
+ int err;
+
+ if (!bh)
+ return -EIO;
+ lock_buffer(bh);
+ if (!memchr_inv(bh->b_data, 0xff, bh->b_size)) {
+ unlock_buffer(bh);
+ put_bh(bh);
+ continue;
+ }
+ memset(bh->b_data, 0xff, bh->b_size);
+ mark_buffer_dirty(bh);
+ unlock_buffer(bh);
+ err = sync_dirty_buffer(bh);
+ put_bh(bh);
+ if (err)
+ return err;
+ }
+ }
+ return 0;
+}
+
+/*
+ * All first writers wait for the same reset. Only take block-device buffer
+ * locks here: callers may already hold NTFS inode or allocation locks.
+ */
+int ntfs_reset_pending_log(struct ntfs_sb_info *sbi)
+{
+ const struct runs_tree *run;
+ struct super_block *sb = sbi->sb;
+ unsigned int nofs;
+ int err;
+
+ mutex_lock(&sbi->log_mutex);
+ err = sbi->log_error;
+ if (!sbi->log_pending || err)
+ goto unlock;
+ if (sb_rdonly(sb)) {
+ err = -EROFS;
+ goto unlock;
+ }
+
+ nofs = memalloc_nofs_save();
+ run = &sbi->log_run;
+ if (sbi->restart_page) {
+ u32 len = sbi->restart_size;
+
+ err = ntfs_sb_write_run(sbi, run, 0, sbi->restart_page, len, 1);
+ if (!err)
+ err = ntfs_sb_write_run(sbi, run, len, sbi->restart_page,
+ len, 1);
+ if (!err)
+ err = blkdev_issue_flush(sb->s_bdev);
+ }
+
+ if (!err)
+ err = ntfs_clear_log_blocks(sbi, run);
+ if (!err)
+ err = blkdev_issue_flush(sb->s_bdev);
+ memalloc_nofs_restore(nofs);
+
+ if (err) {
+ WRITE_ONCE(sbi->log_error, err);
+ ntfs_err(sb, "failed to reset $LogFile (%d)", err);
+ } else {
+ run_close(&sbi->log_run);
+ kfree(sbi->restart_page);
+ sbi->restart_page = NULL;
+ smp_store_release(&sbi->log_pending, false);
+ }
+unlock:
+ mutex_unlock(&sbi->log_mutex);
+ return err;
+}
+
+int ntfs_loadlog_and_replay(struct ntfs_inode *ni, struct ntfs_sb_info *sbi,
+ bool defer)
{
int err = 0;
struct super_block *sb = sbi->sb;
@@ -327,7 +418,7 @@ int ntfs_loadlog_and_replay(struct ntfs_inode *ni, struct ntfs_sb_info *sbi)
/* LogFile should not contains attribute list. */
err = ni_load_all_mi(sbi->mft.ni);
if (!err)
- err = log_replay(ni, &initialized);
+ err = log_replay(ni, &initialized, &defer);
iput(inode);
sbi->mft.ni = NULL;
@@ -340,7 +431,35 @@ int ntfs_loadlog_and_replay(struct ntfs_inode *ni, struct ntfs_sb_info *sbi)
goto out;
}
- if (sb_rdonly(sb) || !initialized)
+ if (err || !initialized)
+ goto out;
+
+ if (defer) {
+ CLST vcn, lcn, len;
+ size_t idx = 0;
+ u64 next = 0;
+
+ while (run_get_entry(&ni->file.run, idx++, &vcn, &lcn, &len)) {
+ if (vcn != next || !len || lcn == SPARSE_LCN ||
+ lcn >= sbi->used.bitmap.nbits ||
+ len > sbi->used.bitmap.nbits - lcn) {
+ err = -EINVAL;
+ goto out;
+ }
+ next += len;
+ }
+ if (next < bytes_to_cluster(sbi, i_size_read(&ni->vfs_inode))) {
+ err = -EINVAL;
+ goto out;
+ }
+ err = run_clone(&ni->file.run, &sbi->log_run);
+ if (err)
+ goto out;
+ smp_store_release(&sbi->log_pending, true);
+ goto out;
+ }
+
+ if (sb_rdonly(sb))
goto out;
/* Fill LogFile by '-1' if it is initialized. */
@@ -969,6 +1088,10 @@ int ntfs_set_state(struct ntfs_sb_info *sbi, enum NTFS_DIRTY_FLAGS dirty)
(sbi->volume.flags & VOLUME_FLAG_DIRTY))
return 0;
+ err = ntfs_prepare_write(sbi);
+ if (err)
+ return err;
+
ni = sbi->volume.ni;
if (!ni)
return -EINVAL;
@@ -1454,6 +1577,10 @@ int ntfs_write_bh(struct ntfs_sb_info *sbi, struct NTFS_RECORD_HEADER *rhdr,
return -EINVAL;
}
+ err = ntfs_prepare_write(sbi);
+ if (err)
+ return err;
+
for (idx = 0; bytes && idx < nb->nbufs; idx += 1, off = 0) {
u32 op = block_size - off;
char *bh_data;
@@ -2688,6 +2815,10 @@ int ntfs_set_label(struct ntfs_sb_info *sbi, u8 *label, int len)
goto out;
}
+ err = ntfs_prepare_write(sbi);
+ if (err)
+ goto out;
+
ni_lock(ni);
/* Ignore any errors. */
diff --git a/fs/ntfs3/inode.c b/fs/ntfs3/inode.c
index 45f894d..c9179a7 100644
--- a/fs/ntfs3/inode.c
+++ b/fs/ntfs3/inode.c
@@ -1432,6 +1432,10 @@ int ntfs_create_inode(const struct mnt_idmap *idmap, struct inode *dir,
trace_ntfs3_create_inode(dir, dentry);
+ err = ntfs_prepare_write(sbi);
+ if (err)
+ return err;
+
if (!fnd)
ni_lock_dir(dir_ni);
@@ -1984,6 +1988,10 @@ int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
if (ntfs_is_meta_file(sbi, ni->mi.rno))
return -EINVAL;
+ err = ntfs_prepare_write(sbi);
+ if (err)
+ return err;
+
de = kzalloc(PATH_MAX, GFP_KERNEL);
if (!de)
return -ENOMEM;
diff --git a/fs/ntfs3/namei.c b/fs/ntfs3/namei.c
index f4286c6..5b0a2b8 100644
--- a/fs/ntfs3/namei.c
+++ b/fs/ntfs3/namei.c
@@ -146,6 +146,10 @@ static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
if (inode->i_nlink >= NTFS_LINK_MAX)
return -EMLINK;
+ err = ntfs_prepare_write(ntfs_sb(dir->i_sb));
+ if (err)
+ return err;
+
ni_lock_dir(ntfs_i(dir));
if (inode != dir)
ni_lock(ni);
@@ -196,6 +200,9 @@ static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
err = ntfs_unlink_inode(dir, dentry);
ni_unlock(dir_ni);
} else {
+ err = ntfs_prepare_write(ni->mi.sbi);
+ if (err)
+ return err;
ni_lock(ni);
/* Remove ADS. */
err = ni_remove_attr(ni, ATTR_DATA, ni->file.ads.name,
@@ -321,6 +328,10 @@ static int ntfs_rename(const struct mnt_idmap *idmap, struct inode *dir,
return -EINVAL;
}
+ err = ntfs_prepare_write(sbi);
+ if (err)
+ return err;
+
if (new_inode) {
/* Target name exists. Unlink it. */
dget(new_dentry);
diff --git a/fs/ntfs3/ntfs_fs.h b/fs/ntfs3/ntfs_fs.h
index 16e7eab..434f301 100644
--- a/fs/ntfs3/ntfs_fs.h
+++ b/fs/ntfs3/ntfs_fs.h
@@ -214,6 +214,14 @@ struct ntfs_index {
/* Ntfs file system in-core superblock data. */
struct ntfs_sb_info {
+ /* A private copy of the clean journal mapping for the first writer. */
+ struct mutex log_mutex;
+ struct runs_tree log_run;
+ void *restart_page;
+ u32 restart_size;
+ bool log_pending;
+ int log_error;
+
struct super_block *sb;
struct rcu_head rcu;
@@ -649,7 +657,7 @@ ssize_t ni_query_ads(struct ntfs_inode *ni, loff_t *pos, struct iov_iter *iter);
/* Globals from fslog.c */
bool check_index_header(const struct INDEX_HDR *hdr, size_t bytes);
-int log_replay(struct ntfs_inode *ni, bool *initialized);
+int log_replay(struct ntfs_inode *ni, bool *initialized, bool *defer);
/* Globals from fsntfs.c */
struct buffer_head *ntfs_bread(struct super_block *sb, sector_t block);
@@ -657,7 +665,17 @@ bool ntfs_fix_pre_write(struct NTFS_RECORD_HEADER *rhdr, size_t bytes);
int ntfs_fix_post_read(struct NTFS_RECORD_HEADER *rhdr, size_t bytes,
bool simple);
int ntfs_extend_init(struct ntfs_sb_info *sbi);
-int ntfs_loadlog_and_replay(struct ntfs_inode *ni, struct ntfs_sb_info *sbi);
+int ntfs_reset_pending_log(struct ntfs_sb_info *sbi);
+
+static inline int ntfs_prepare_write(struct ntfs_sb_info *sbi)
+{
+ if (unlikely(smp_load_acquire(&sbi->log_pending)))
+ return ntfs_reset_pending_log(sbi);
+ return 0;
+}
+
+int ntfs_loadlog_and_replay(struct ntfs_inode *ni, struct ntfs_sb_info *sbi,
+ bool defer);
int ntfs_look_for_free_space(struct ntfs_sb_info *sbi, CLST lcn, CLST len,
CLST *new_lcn, CLST *new_len,
enum ALLOCATE_OPT opt);
diff --git a/fs/ntfs3/super.c b/fs/ntfs3/super.c
index 29aabdc..63f1611 100644
--- a/fs/ntfs3/super.c
+++ b/fs/ntfs3/super.c
@@ -738,6 +738,10 @@ static noinline void ntfs3_put_sbi(struct ntfs_sb_info *sbi)
ntfs_update_mftmirr(sbi);
+ run_close(&sbi->log_run);
+ kfree(sbi->restart_page);
+ sbi->restart_page = NULL;
+
indx_clear(&sbi->security.index_sii);
indx_clear(&sbi->security.index_sdh);
indx_clear(&sbi->reparse.index_r);
@@ -875,6 +879,10 @@ static int ntfs_sync_fs(struct super_block *sb, int wait)
if (unlikely(ntfs3_forced_shutdown(sb)))
return -EIO;
+ err = READ_ONCE(sbi->log_error);
+ if (err)
+ return err;
+
ni = sbi->security.ni;
if (ni) {
inode = &ni->vfs_inode;
@@ -1467,7 +1475,7 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
ni = ntfs_i(inode);
- err = ntfs_loadlog_and_replay(ni, sbi);
+ err = ntfs_loadlog_and_replay(ni, sbi, !boot2 && !sbi->volume.real_dirty);
if (err)
goto put_inode_out;
@@ -1886,6 +1894,10 @@ int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
if (start >= end)
return 0;
+ err = ntfs_prepare_write(sbi);
+ if (err)
+ return err;
+
err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
GFP_NOFS);
@@ -1971,6 +1983,7 @@ static int ntfs_init_fs_context(struct fs_context *fc)
ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
DEFAULT_RATELIMIT_BURST);
+ mutex_init(&sbi->log_mutex);
mutex_init(&sbi->compress.mtx_lznt);
#ifdef CONFIG_NTFS3_LZX_XPRESS
mutex_init(&sbi->compress.mtx_xpress);
diff --git a/fs/ntfs3/xattr.c b/fs/ntfs3/xattr.c
index 7a138e7..50045f4 100644
--- a/fs/ntfs3/xattr.c
+++ b/fs/ntfs3/xattr.c
@@ -616,6 +616,10 @@ static noinline int ntfs_set_acl_ex(const struct mnt_idmap *idmap,
if (S_ISLNK(inode->i_mode))
return -EOPNOTSUPP;
+ err = ntfs_prepare_write(ntfs_sb(inode->i_sb));
+ if (err)
+ return err;
+
mode = inode->i_mode;
switch (type) {
case ACL_TYPE_ACCESS:
@@ -874,6 +878,10 @@ static noinline int ntfs_setxattr(const struct xattr_handler *handler,
struct ntfs_inode *ni = ntfs_i(inode);
enum FILE_ATTRIBUTE new_fa;
+ err = ntfs_prepare_write(ni->mi.sbi);
+ if (err)
+ return err;
+
/* Dispatch request. */
if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {
if (sizeof(u8) != size)
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-12-02 13:27 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02 13:27 [PATCH] ntfs3: defer clean $LogFile reset until the first write Artem S. Tashkinov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®