Skip to contentSkip to conversation

ntfs: propagate compression context allocation errors

PATCH In review Namjae Jeon 2 participants 21 messages Jul 21 – Jul 23 NTFS filesystem

lore
Share & tools
lore

Namjae Jeon

author maintainer
#0 lore raw

PATCH 1/11 ntfs: propagate compression context allocation errors

ntfs_compress_block() returns -ENOMEM when its compression context cannot be allocated, but its unsigned return type turns the error into a large positive value. ntfs_write_cb() then hides the allocation failure.

Use a signed return type and propagate negative errors to the caller.

---
 fs/ntfs/compress.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/ntfs/compress.c +9 -4
index fe1877b86f49..6b78a8efe3ac 100644
@@ -1070,10 +1070,10 @@ static void ntfs_skip_position(struct compress_context *pctx, const int i)
  *
  * Returns the size of the compressed block, including the
  * header (minimal size is 2, maximum size is 4098)
- * 0 if an error has been met.
+ * A negative error code if an error has been met.
  */
-static unsigned int ntfs_compress_block(const char *inbuf, const int bufsize,
-		char *outbuf)
+static int ntfs_compress_block(const char *inbuf, const int bufsize,
+			       char *outbuf)
 {
 	struct compress_context *pctx;
 	int i; /* current position */
@@ -1264,7 +1264,8 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 	char *outbuf = NULL, *pbuf, *inbuf;
 	u32 compsz, p, insz = pages_per_cb << PAGE_SHIFT;
 	s32 rounded, bio_size;
-	unsigned int sz, bsz;
+	int sz;
+	unsigned int bsz;
 	bool fail = false, allzeroes;
 	/* a single compressed zero */
 	static char onezero[] = {0x01, 0xb0, 0x00, 0x00};
@@ -1319,6 +1320,10 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 			bsz = insz - p;
 		pbuf = &outbuf[compsz];
 		sz = ntfs_compress_block(&inbuf[p], bsz, pbuf);
+		if (sz < 0) {
+			err = sz;
+			goto out;
+		}
 		/* fail if all the clusters (or more) are needed */
 		if (!sz || ((compsz + sz + vol->cluster_size + 2) >
 			    ni->itype.compressed.block_size))
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 2/11 ntfs: support large pages in compressed writes

ntfs_compress_write() derives its page count by shifting the compression block size and assumes that every compression block begins at a page boundary. This produces a zero page count for small compression blocks on large-page systems and ignores an in-page compression block offset.

Map every page covering the compression block, pass the in-page offset to ntfs_write_cb(), and stage uncompressed output in page-aligned pages.

---
 fs/ntfs/compress.c | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)
fs/ntfs/compress.c +22 -11
index 6b78a8efe3ac..006b8831836c 100644
@@ -1258,11 +1258,11 @@ static int ntfs_compress_block(const char *inbuf, const int bufsize,
 }
 
 static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
-		int pages_per_cb)
+		int pages_per_cb, unsigned int page_offset)
 {
 	struct ntfs_volume *vol = ni->vol;
-	char *outbuf = NULL, *pbuf, *inbuf;
-	u32 compsz, p, insz = pages_per_cb << PAGE_SHIFT;
+	char *outbuf = NULL, *pbuf, *inbuf, *in_mapping;
+	u32 compsz, p, insz = ni->itype.compressed.block_size;
 	s32 rounded, bio_size;
 	int sz;
 	unsigned int bsz;
@@ -1284,14 +1284,15 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 	loff_t new_length;
 	s64 new_vcn;
 
-	inbuf = vmap(pages, pages_per_cb, VM_MAP, PAGE_KERNEL_RO);
-	if (!inbuf)
+	in_mapping = vmap(pages, pages_per_cb, VM_MAP, PAGE_KERNEL_RO);
+	if (!in_mapping)
 		return -ENOMEM;
+	inbuf = in_mapping + page_offset;
 
 	/* may need 2 extra bytes per block and 2 more bytes */
 	pages_disk = kcalloc(pages_count, sizeof(struct page *), GFP_NOFS);
 	if (!pages_disk) {
-		vunmap(inbuf);
+		vunmap(in_mapping);
 		return -ENOMEM;
 	}
 
@@ -1361,7 +1362,9 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 		err = 0;
 		goto out;
 	} else {
+		memcpy(outbuf, inbuf, insz);
 		bio_size = insz;
+		pages = pages_disk;
 	}
 
 	new_vcn = ntfs_bytes_to_cluster(vol,
@@ -1420,7 +1423,8 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 					GFP_NOIO);
 			bio->bi_iter.bi_sector =
 				ntfs_bytes_to_sector(vol,
-						ntfs_cluster_to_bytes(vol, bio_lcn + i));
+						ntfs_cluster_to_bytes(vol, bio_lcn) +
+						((s64)i << PAGE_SHIFT));
 		}
 
 		if (!bio_add_page(bio, pages[i], page_size, 0)) {
@@ -1437,7 +1441,8 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 	err = submit_bio_wait(bio);
 	bio_put(bio);
 out:
-	vunmap(outbuf);
+	if (outbuf)
+		vunmap(outbuf);
 	for (i = 0; i < pages_count; i++) {
 		pg = pages_disk[i];
 		if (pg) {
@@ -1446,7 +1451,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 		}
 	}
 	kfree(pages_disk);
-	vunmap(inbuf);
+	vunmap(in_mapping);
 	NInoSetFileNameDirty(ni);
 	mark_mft_record_dirty(ni);
 
@@ -1458,12 +1463,15 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 {
 	struct folio *folio;
 	struct page **pages = NULL, *page;
-	int pages_per_cb = ni->itype.compressed.block_size >> PAGE_SHIFT;
+	int pages_per_cb;
 	int cb_size = ni->itype.compressed.block_size, cb_off, err = 0;
 	int i, ip;
 	size_t written = 0;
 	struct address_space *mapping = VFS_I(ni)->i_mapping;
 
+	pages_per_cb = DIV_ROUND_UP(offset_in_page(pos & ~(cb_size - 1)) +
+			cb_size, PAGE_SIZE);
+
 	pages = kmalloc_array(pages_per_cb, sizeof(struct page *), GFP_NOFS);
 	if (!pages)
 		return -ENOMEM;
@@ -1471,6 +1479,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 	while (count) {
 		pgoff_t index;
 		size_t copied, bytes;
+		unsigned int page_offset;
 		int off;
 
 		off = pos & (cb_size - 1);
@@ -1479,6 +1488,8 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 			bytes = count;
 
 		cb_off = pos & ~(cb_size - 1);
+		page_offset = offset_in_page(cb_off);
+		pages_per_cb = DIV_ROUND_UP(page_offset + cb_size, PAGE_SIZE);
 		index = cb_off >> PAGE_SHIFT;
 
 		if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
@@ -1527,7 +1538,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 			}
 		}
 
-		err = ntfs_write_cb(ni, pos, pages, pages_per_cb);
+		err = ntfs_write_cb(ni, pos, pages, pages_per_cb, page_offset);
 
 		for (i = 0; i < pages_per_cb; i++) {
 			folio = page_folio(pages[i]);
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 2/11
#2 lore raw

Re: [PATCH 02/11] ntfs: support large pages in compressed writes

2026년 7월 21일 (화) 오후 6:54, Namjae Jeon <[email protected]>님이 작성:

Quoted 10 lines

ntfs_compress_write() derives its page count by shifting the compression block size and assumes that every compression block begins at a page boundary. This produces a zero page count for small compression blocks on large-page systems and ignores an in-page compression block offset.

Map every page covering the compression block, pass the in-page offset to ntfs_write_cb(), and stage uncompressed output in page-aligned pages.

Looks good to me.

Quoted 127 lines
---
 fs/ntfs/compress.c | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)
fs/ntfs/compress.c +22 -12
index 6b78a8efe3ac..006b8831836c 100644
@@ -1258,11 +1258,11 @@ static int ntfs_compress_block(const char *inbuf, const int bufsize,
 }
 
 static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
-               int pages_per_cb)
+               int pages_per_cb, unsigned int page_offset)
 {
        struct ntfs_volume *vol = ni->vol;
-       char *outbuf = NULL, *pbuf, *inbuf;
-       u32 compsz, p, insz = pages_per_cb << PAGE_SHIFT;
+       char *outbuf = NULL, *pbuf, *inbuf, *in_mapping;
+       u32 compsz, p, insz = ni->itype.compressed.block_size;
        s32 rounded, bio_size;
        int sz;
        unsigned int bsz;
@@ -1284,14 +1284,15 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
        loff_t new_length;
        s64 new_vcn;
 
-       inbuf = vmap(pages, pages_per_cb, VM_MAP, PAGE_KERNEL_RO);
-       if (!inbuf)
+       in_mapping = vmap(pages, pages_per_cb, VM_MAP, PAGE_KERNEL_RO);
+       if (!in_mapping)
                return -ENOMEM;
+       inbuf = in_mapping + page_offset;
 
        /* may need 2 extra bytes per block and 2 more bytes */
        pages_disk = kcalloc(pages_count, sizeof(struct page *), GFP_NOFS);
        if (!pages_disk) {
-               vunmap(inbuf);
+               vunmap(in_mapping);
                return -ENOMEM;
        }
 
@@ -1361,7 +1362,9 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                err = 0;
                goto out;
        } else {
+               memcpy(outbuf, inbuf, insz);
                bio_size = insz;
+               pages = pages_disk;
        }
 
        new_vcn = ntfs_bytes_to_cluster(vol,
@@ -1420,7 +1423,8 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                                        GFP_NOIO);
                        bio->bi_iter.bi_sector =
                                ntfs_bytes_to_sector(vol,
-                                               ntfs_cluster_to_bytes(vol, bio_lcn + i));
+                                               ntfs_cluster_to_bytes(vol, bio_lcn) +
+                                               ((s64)i << PAGE_SHIFT));
                }
 
                if (!bio_add_page(bio, pages[i], page_size, 0)) {
@@ -1437,7 +1441,8 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
        err = submit_bio_wait(bio);
        bio_put(bio);
 out:
-       vunmap(outbuf);
+       if (outbuf)
+               vunmap(outbuf);
        for (i = 0; i < pages_count; i++) {
                pg = pages_disk[i];
                if (pg) {
@@ -1446,7 +1451,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                }
        }
        kfree(pages_disk);
-       vunmap(inbuf);
+       vunmap(in_mapping);
        NInoSetFileNameDirty(ni);
        mark_mft_record_dirty(ni);
 
@@ -1458,12 +1463,15 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 {
        struct folio *folio;
        struct page **pages = NULL, *page;
-       int pages_per_cb = ni->itype.compressed.block_size >> PAGE_SHIFT;
+       int pages_per_cb;
        int cb_size = ni->itype.compressed.block_size, cb_off, err = 0;
        int i, ip;
        size_t written = 0;
        struct address_space *mapping = VFS_I(ni)->i_mapping;
 
+       pages_per_cb = DIV_ROUND_UP(offset_in_page(pos & ~(cb_size - 1)) +
+                       cb_size, PAGE_SIZE);
+
        pages = kmalloc_array(pages_per_cb, sizeof(struct page *), GFP_NOFS);
        if (!pages)
                return -ENOMEM;
@@ -1471,6 +1479,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
        while (count) {
                pgoff_t index;
                size_t copied, bytes;
+               unsigned int page_offset;
                int off;
 
                off = pos & (cb_size - 1);
@@ -1479,6 +1488,8 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
                        bytes = count;
 
                cb_off = pos & ~(cb_size - 1);
+               page_offset = offset_in_page(cb_off);
+               pages_per_cb = DIV_ROUND_UP(page_offset + cb_size, PAGE_SIZE);
                index = cb_off >> PAGE_SHIFT;
 
                if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
@@ -1527,7 +1538,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
                        }
                }
 
-               err = ntfs_write_cb(ni, pos, pages, pages_per_cb);
+               err = ntfs_write_cb(ni, pos, pages, pages_per_cb, page_offset);
 
                for (i = 0; i < pages_per_cb; i++) {
                        folio = page_folio(pages[i]);
--

2.34.1

Signature · 2 lines
Thanks,
Hyunchul
author maintainer
#3 lore raw

PATCH 3/11 ntfs: punch all-zero compressed blocks

In reply to Namjae Jeon

When a rewritten compression block consists entirely of zeroes, ntfs_write_cb() returns without replacing its existing runlist mapping. The old on-disk contents therefore remain visible after cache eviction.

Punch the compression unit so that reads resolve it as a sparse block and release any clusters that held the previous contents.

---
 fs/ntfs/compress.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/ntfs/compress.c +6 -4
index 006b8831836c..33ed0456bf7e 100644
@@ -1279,9 +1279,10 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 	int i, err;
 	int pages_count = (round_up(ni->itype.compressed.block_size + 2 *
 		(ni->itype.compressed.block_size / NTFS_SB_SIZE) + 2, PAGE_SIZE)) / PAGE_SIZE;
+	u32 cb_clusters = ni->itype.compressed.block_clusters;
 	size_t new_rl_count;
 	struct bio *bio = NULL;
-	loff_t new_length;
+	loff_t cb_pos, new_length;
 	s64 new_vcn;
 
 	in_mapping = vmap(pages, pages_per_cb, VM_MAP, PAGE_KERNEL_RO);
@@ -1351,6 +1352,9 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 		}
 	}
 
+	cb_pos = pos & ~((loff_t)ni->itype.compressed.block_size - 1);
+	new_vcn = ntfs_bytes_to_cluster(vol, cb_pos);
+
 	if (!fail && !allzeroes) {
 		outbuf[compsz++] = 0;
 		outbuf[compsz++] = 0;
@@ -1359,7 +1363,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 		bio_size = rounded;
 		pages = pages_disk;
 	} else if (allzeroes) {
-		err = 0;
+		err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, cb_clusters);
 		goto out;
 	} else {
 		memcpy(outbuf, inbuf, insz);
@@ -1367,8 +1371,6 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 		pages = pages_disk;
 	}
 
-	new_vcn = ntfs_bytes_to_cluster(vol,
-			pos & ~((loff_t)ni->itype.compressed.block_size - 1));
 	new_length = ntfs_bytes_to_cluster(vol, round_up(bio_size, vol->cluster_size));
 
 	err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, ni->itype.compressed.block_clusters);
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 3/11
#4 lore raw

Re: [PATCH 03/11] ntfs: punch all-zero compressed blocks

2026년 7월 21일 (화) 오후 6:54, Namjae Jeon <[email protected]>님이 작성:

Quoted 9 lines

When a rewritten compression block consists entirely of zeroes, ntfs_write_cb() returns without replacing its existing runlist mapping. The old on-disk contents therefore remain visible after cache eviction.

Punch the compression unit so that reads resolve it as a sparse block and release any clusters that held the previous contents.

Looks good to me.

Quoted 51 lines
---
 fs/ntfs/compress.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/ntfs/compress.c +6 -5
index 006b8831836c..33ed0456bf7e 100644
@@ -1279,9 +1279,10 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
        int i, err;
        int pages_count = (round_up(ni->itype.compressed.block_size + 2 *
                (ni->itype.compressed.block_size / NTFS_SB_SIZE) + 2, PAGE_SIZE)) / PAGE_SIZE;
+       u32 cb_clusters = ni->itype.compressed.block_clusters;
        size_t new_rl_count;
        struct bio *bio = NULL;
-       loff_t new_length;
+       loff_t cb_pos, new_length;
        s64 new_vcn;
 
        in_mapping = vmap(pages, pages_per_cb, VM_MAP, PAGE_KERNEL_RO);
@@ -1351,6 +1352,9 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                }
        }
 
+       cb_pos = pos & ~((loff_t)ni->itype.compressed.block_size - 1);
+       new_vcn = ntfs_bytes_to_cluster(vol, cb_pos);
+
        if (!fail && !allzeroes) {
                outbuf[compsz++] = 0;
                outbuf[compsz++] = 0;
@@ -1359,7 +1363,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                bio_size = rounded;
                pages = pages_disk;
        } else if (allzeroes) {
-               err = 0;
+               err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, cb_clusters);
                goto out;
        } else {
                memcpy(outbuf, inbuf, insz);
@@ -1367,8 +1371,6 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                pages = pages_disk;
        }
 
-       new_vcn = ntfs_bytes_to_cluster(vol,
-                       pos & ~((loff_t)ni->itype.compressed.block_size - 1));
        new_length = ntfs_bytes_to_cluster(vol, round_up(bio_size, vol->cluster_size));
 
        err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, ni->itype.compressed.block_clusters);
--

2.34.1

Signature · 2 lines
Thanks,
Hyunchul
author maintainer
#5 lore raw

PATCH 4/11 ntfs: write compressed data before replacing old clusters

In reply to Namjae Jeon

ntfs_write_cb() punches the old compression unit and publishes the new mapping before submitting the replacement data. An allocation or I/O failure after the punch loses the previous contents and can leave the mapping pointing at unwritten clusters.

Allocate and write the replacement clusters first. Replace the runlist only after the synchronous write succeeds, and free new clusters on failure.

---
 fs/ntfs/compress.c | 63 ++++++++++++++++++++++++++--------------------
 1 file changed, 36 insertions(+), 27 deletions(-)
fs/ntfs/compress.c +36 -27
index 33ed0456bf7e..5b5cd494e5d8 100644
@@ -1373,10 +1373,6 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 
 	new_length = ntfs_bytes_to_cluster(vol, round_up(bio_size, vol->cluster_size));
 
-	err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, ni->itype.compressed.block_clusters);
-	if (err < 0)
-		goto out;
-
 	rlc = ntfs_cluster_alloc(vol, new_vcn, new_length, -1, DATA_ZONE,
 			false, true, true);
 	if (IS_ERR(rlc)) {
@@ -1385,28 +1381,6 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 	}
 
 	bio_lcn = rlc->lcn;
-	down_write(&ni->runlist.lock);
-	rl = ntfs_runlists_merge(&ni->runlist, rlc, 0, &new_rl_count);
-	if (IS_ERR(rl)) {
-		up_write(&ni->runlist.lock);
-		ntfs_error(vol->sb, "Failed to merge runlists");
-		err = PTR_ERR(rl);
-		if (ntfs_cluster_free_from_rl(vol, rlc))
-			ntfs_error(vol->sb, "Failed to free hot clusters.");
-		kvfree(rlc);
-		goto out;
-	}
-
-	ni->runlist.count = new_rl_count;
-	ni->runlist.rl = rl;
-
-	err = ntfs_attr_update_mapping_pairs(ni, 0);
-	up_write(&ni->runlist.lock);
-	if (err) {
-		err = -EIO;
-		goto out;
-	}
-
 	i = 0;
 	while (bio_size > 0) {
 		int page_size;
@@ -1423,6 +1397,10 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 		if (!bio) {
 			bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE,
 					GFP_NOIO);
+			if (!bio) {
+				err = -ENOMEM;
+				goto free_rlc;
+			}
 			bio->bi_iter.bi_sector =
 				ntfs_bytes_to_sector(vol,
 						ntfs_cluster_to_bytes(vol, bio_lcn) +
@@ -1433,7 +1411,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 			err = submit_bio_wait(bio);
 			bio_put(bio);
 			if (err)
-				goto out;
+				goto free_rlc;
 			bio = NULL;
 			goto setup_bio;
 		}
@@ -1442,6 +1420,37 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 
 	err = submit_bio_wait(bio);
 	bio_put(bio);
+	if (err)
+		goto free_rlc;
+
+	/* Do not discard the old compression block until the new one is safe. */
+	err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, cb_clusters);
+	if (err)
+		goto free_rlc;
+
+	down_write(&ni->runlist.lock);
+	rl = ntfs_runlists_merge(&ni->runlist, rlc, 0, &new_rl_count);
+	if (IS_ERR(rl)) {
+		up_write(&ni->runlist.lock);
+		ntfs_error(vol->sb, "Failed to merge runlists");
+		err = PTR_ERR(rl);
+		goto free_rlc;
+	}
+
+	ni->runlist.count = new_rl_count;
+	ni->runlist.rl = rl;
+	rlc = NULL;
+
+	err = ntfs_attr_update_mapping_pairs(ni, 0);
+	up_write(&ni->runlist.lock);
+	if (err)
+		err = -EIO;
+	goto out;
+
+free_rlc:
+	if (ntfs_cluster_free_from_rl(vol, rlc))
+		ntfs_error(vol->sb, "Failed to free hot clusters.");
+	kvfree(rlc);
 out:
 	if (outbuf)
 		vunmap(outbuf);
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 4/11
#6 lore raw

Re: [PATCH 04/11] ntfs: write compressed data before replacing old clusters

2026년 7월 21일 (화) 오후 6:54, Namjae Jeon <[email protected]>님이 작성:

Quoted 10 lines

ntfs_write_cb() punches the old compression unit and publishes the new mapping before submitting the replacement data. An allocation or I/O failure after the punch loses the previous contents and can leave the mapping pointing at unwritten clusters.

Allocate and write the replacement clusters first. Replace the runlist only after the synchronous write succeeds, and free new clusters on failure.

Looks good to me.

Quoted 109 lines
---
 fs/ntfs/compress.c | 63 ++++++++++++++++++++++++++--------------------
 1 file changed, 36 insertions(+), 27 deletions(-)
fs/ntfs/compress.c +36 -28
index 33ed0456bf7e..5b5cd494e5d8 100644
@@ -1373,10 +1373,6 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 
        new_length = ntfs_bytes_to_cluster(vol, round_up(bio_size, vol->cluster_size));
 
-       err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, ni->itype.compressed.block_clusters);
-       if (err < 0)
-               goto out;
-
        rlc = ntfs_cluster_alloc(vol, new_vcn, new_length, -1, DATA_ZONE,
                        false, true, true);
        if (IS_ERR(rlc)) {
@@ -1385,28 +1381,6 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
        }
 
        bio_lcn = rlc->lcn;
-       down_write(&ni->runlist.lock);
-       rl = ntfs_runlists_merge(&ni->runlist, rlc, 0, &new_rl_count);
-       if (IS_ERR(rl)) {
-               up_write(&ni->runlist.lock);
-               ntfs_error(vol->sb, "Failed to merge runlists");
-               err = PTR_ERR(rl);
-               if (ntfs_cluster_free_from_rl(vol, rlc))
-                       ntfs_error(vol->sb, "Failed to free hot clusters.");
-               kvfree(rlc);
-               goto out;
-       }
-
-       ni->runlist.count = new_rl_count;
-       ni->runlist.rl = rl;
-
-       err = ntfs_attr_update_mapping_pairs(ni, 0);
-       up_write(&ni->runlist.lock);
-       if (err) {
-               err = -EIO;
-               goto out;
-       }
-
        i = 0;
        while (bio_size > 0) {
                int page_size;
@@ -1423,6 +1397,10 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                if (!bio) {
                        bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE,
                                        GFP_NOIO);
+                       if (!bio) {
+                               err = -ENOMEM;
+                               goto free_rlc;
+                       }
                        bio->bi_iter.bi_sector =
                                ntfs_bytes_to_sector(vol,
                                                ntfs_cluster_to_bytes(vol, bio_lcn) +
@@ -1433,7 +1411,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                        err = submit_bio_wait(bio);
                        bio_put(bio);
                        if (err)
-                               goto out;
+                               goto free_rlc;
                        bio = NULL;
                        goto setup_bio;
                }
@@ -1442,6 +1420,37 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 
        err = submit_bio_wait(bio);
        bio_put(bio);
+       if (err)
+               goto free_rlc;
+
+       /* Do not discard the old compression block until the new one is safe. */
+       err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, cb_clusters);
+       if (err)
+               goto free_rlc;
+
+       down_write(&ni->runlist.lock);
+       rl = ntfs_runlists_merge(&ni->runlist, rlc, 0, &new_rl_count);
+       if (IS_ERR(rl)) {
+               up_write(&ni->runlist.lock);
+               ntfs_error(vol->sb, "Failed to merge runlists");
+               err = PTR_ERR(rl);
+               goto free_rlc;
+       }
+
+       ni->runlist.count = new_rl_count;
+       ni->runlist.rl = rl;
+       rlc = NULL;
+
+       err = ntfs_attr_update_mapping_pairs(ni, 0);
+       up_write(&ni->runlist.lock);
+       if (err)
+               err = -EIO;
+       goto out;
+
+free_rlc:
+       if (ntfs_cluster_free_from_rl(vol, rlc))
+               ntfs_error(vol->sb, "Failed to free hot clusters.");
+       kvfree(rlc);
 out:
        if (outbuf)
                vunmap(outbuf);
--

2.34.1

Signature · 2 lines
Thanks,
Hyunchul
author maintainer
#7 lore raw

PATCH 5/11 ntfs: fix initialized size and page state after compressed writes

In reply to Namjae Jeon

The write iterator now expands attributes before calling ntfs_compress_write(), so compressed writes must not expand the attribute themselves. However, the compressed path still needs to reject zero-byte iterator copies, advance initialized_size after successful I/O, and invalidate modified folios after a failed compression-unit write. Reject no-progress copies, persist the new initialized size on success,

and clear folio uptodate state when the synchronous write fails.

---
 fs/ntfs/compress.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
fs/ntfs/compress.c +14 -1
index 5b5cd494e5d8..a993cba3b961 100644
@@ -1549,13 +1549,26 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 			}
 		}
 
+		if (!copied) {
+			err = -EFAULT;
+			goto release_pages;
+		}
+
 		err = ntfs_write_cb(ni, pos, pages, pages_per_cb, page_offset);
+		if (!err && pos + copied > ni->initialized_size) {
+			mutex_lock(&ni->mrec_lock);
+			err = ntfs_attr_set_initialized_size(ni, pos + copied);
+			mutex_unlock(&ni->mrec_lock);
+		}
 
+release_pages:
 		for (i = 0; i < pages_per_cb; i++) {
 			folio = page_folio(pages[i]);
-			if (i < ip) {
+			if (!err) {
 				folio_clear_dirty(folio);
 				folio_mark_uptodate(folio);
+			} else {
+				folio_clear_uptodate(folio);
 			}
 			folio_unlock(folio);
 			folio_put(folio);
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 5/11
#8 lore raw

Re: [PATCH 05/11] ntfs: fix initialized size and page state after compressed writes

2026년 7월 21일 (화) 오후 6:54, Namjae Jeon <[email protected]>님이 작성:

Quoted 11 lines

The write iterator now expands attributes before calling ntfs_compress_write(), so compressed writes must not expand the attribute themselves. However, the compressed path still needs to reject zero-byte iterator copies, advance initialized_size after successful I/O, and invalidate modified folios after a failed compression-unit write. Reject no-progress copies, persist the new initialized size on success,

and clear folio uptodate state when the synchronous write fails.

Looks good to me.

Quoted 39 lines
---
 fs/ntfs/compress.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
fs/ntfs/compress.c +14 -2
index 5b5cd494e5d8..a993cba3b961 100644
@@ -1549,13 +1549,26 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
                        }
                }
 
+               if (!copied) {
+                       err = -EFAULT;
+                       goto release_pages;
+               }
+
                err = ntfs_write_cb(ni, pos, pages, pages_per_cb, page_offset);
+               if (!err && pos + copied > ni->initialized_size) {
+                       mutex_lock(&ni->mrec_lock);
+                       err = ntfs_attr_set_initialized_size(ni, pos + copied);
+                       mutex_unlock(&ni->mrec_lock);
+               }
 
+release_pages:
                for (i = 0; i < pages_per_cb; i++) {
                        folio = page_folio(pages[i]);
-                       if (i < ip) {
+                       if (!err) {
                                folio_clear_dirty(folio);
                                folio_mark_uptodate(folio);
+                       } else {
+                               folio_clear_uptodate(folio);
                        }
                        folio_unlock(folio);
                        folio_put(folio);
--

2.34.1

Signature · 2 lines
Thanks,
Hyunchul
author maintainer
#9 lore raw

PATCH 6/11 ntfs: reuse the compression context during writes

In reply to Namjae Jeon

ntfs_compress_block() allocates and initializes a roughly 40 KiB match finder context for every 4 KiB sub-block. A 64 KiB compression unit thus performs sixteen large allocations even though the calls are serialized.

Allocate one context for the complete write request and reset its hash chains for each sub-block as before.

---
 fs/ntfs/compress.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
fs/ntfs/compress.c +13 -15
index a993cba3b961..f073a53f6136 100644
@@ -1072,10 +1072,9 @@ static void ntfs_skip_position(struct compress_context *pctx, const int i)
  * header (minimal size is 2, maximum size is 4098)
  * A negative error code if an error has been met.
  */
-static int ntfs_compress_block(const char *inbuf, const int bufsize,
-			       char *outbuf)
+static int ntfs_compress_block(struct compress_context *pctx,
+			       const char *inbuf, const int bufsize, char *outbuf)
 {
-	struct compress_context *pctx;
 	int i; /* current position */
 	int j; /* end of best match from current position */
 	int k; /* end of best match from next position */
@@ -1090,10 +1089,6 @@ static int ntfs_compress_block(const char *inbuf, const int bufsize,
 	int tag;    /* current value of tag */
 	int ntag;   /* count of bits still undefined in tag */
 
-	pctx = kvzalloc(sizeof(struct compress_context), GFP_NOFS);
-	if (!pctx)
-		return -ENOMEM;
-
 	/*
 	 * All hash chains start as empty.  The special value '-1' indicates the
 	 * end of each hash chain.
@@ -1249,16 +1244,12 @@ static int ntfs_compress_block(const char *inbuf, const int bufsize,
 		xout = NTFS_SB_SIZE + 2;
 	}
 
-	/*
-	 * Free the compression context and return the total number of bytes
-	 * written to 'outbuf'.
-	 */
-	kvfree(pctx);
 	return xout;
 }
 
 static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
-		int pages_per_cb, unsigned int page_offset)
+		int pages_per_cb, unsigned int page_offset,
+		struct compress_context *ctx)
 {
 	struct ntfs_volume *vol = ni->vol;
 	char *outbuf = NULL, *pbuf, *inbuf, *in_mapping;
@@ -1321,7 +1312,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 		else
 			bsz = insz - p;
 		pbuf = &outbuf[compsz];
-		sz = ntfs_compress_block(&inbuf[p], bsz, pbuf);
+		sz = ntfs_compress_block(ctx, &inbuf[p], bsz, pbuf);
 		if (sz < 0) {
 			err = sz;
 			goto out;
@@ -1472,6 +1463,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 		struct iov_iter *from)
 {
+	struct compress_context *ctx;
 	struct folio *folio;
 	struct page **pages = NULL, *page;
 	int pages_per_cb;
@@ -1486,6 +1478,11 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 	pages = kmalloc_array(pages_per_cb, sizeof(struct page *), GFP_NOFS);
 	if (!pages)
 		return -ENOMEM;
+	ctx = kvzalloc_obj(*ctx, GFP_NOFS);
+	if (!ctx) {
+		kfree(pages);
+		return -ENOMEM;
+	}
 
 	while (count) {
 		pgoff_t index;
@@ -1554,7 +1551,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 			goto release_pages;
 		}
 
-		err = ntfs_write_cb(ni, pos, pages, pages_per_cb, page_offset);
+		err = ntfs_write_cb(ni, pos, pages, pages_per_cb, page_offset, ctx);
 		if (!err && pos + copied > ni->initialized_size) {
 			mutex_lock(&ni->mrec_lock);
 			err = ntfs_attr_set_initialized_size(ni, pos + copied);
@@ -1584,6 +1581,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 	}
 
 out:
+	kvfree(ctx);
 	kfree(pages);
 	if (err < 0)
 		written = err;
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 6/11
#10 lore raw

Re: [PATCH 06/11] ntfs: reuse the compression context during writes

2026년 7월 21일 (화) 오후 6:54, Namjae Jeon <[email protected]>님이 작성:

Quoted 9 lines

ntfs_compress_block() allocates and initializes a roughly 40 KiB match finder context for every 4 KiB sub-block. A 64 KiB compression unit thus performs sixteen large allocations even though the calls are serialized.

Allocate one context for the complete write request and reset its hash chains for each sub-block as before.

Looks good to me.

Quoted 100 lines
---
 fs/ntfs/compress.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
fs/ntfs/compress.c +13 -16
index a993cba3b961..f073a53f6136 100644
@@ -1072,10 +1072,9 @@ static void ntfs_skip_position(struct compress_context *pctx, const int i)
  * header (minimal size is 2, maximum size is 4098)
  * A negative error code if an error has been met.
  */
-static int ntfs_compress_block(const char *inbuf, const int bufsize,
-                              char *outbuf)
+static int ntfs_compress_block(struct compress_context *pctx,
+                              const char *inbuf, const int bufsize, char *outbuf)
 {
-       struct compress_context *pctx;
        int i; /* current position */
        int j; /* end of best match from current position */
        int k; /* end of best match from next position */
@@ -1090,10 +1089,6 @@ static int ntfs_compress_block(const char *inbuf, const int bufsize,
        int tag;    /* current value of tag */
        int ntag;   /* count of bits still undefined in tag */
 
-       pctx = kvzalloc(sizeof(struct compress_context), GFP_NOFS);
-       if (!pctx)
-               return -ENOMEM;
-
        /*
         * All hash chains start as empty.  The special value '-1' indicates the
         * end of each hash chain.
@@ -1249,16 +1244,12 @@ static int ntfs_compress_block(const char *inbuf, const int bufsize,
                xout = NTFS_SB_SIZE + 2;
        }
 
-       /*
-        * Free the compression context and return the total number of bytes
-        * written to 'outbuf'.
-        */
-       kvfree(pctx);
        return xout;
 }
 
 static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
-               int pages_per_cb, unsigned int page_offset)
+               int pages_per_cb, unsigned int page_offset,
+               struct compress_context *ctx)
 {
        struct ntfs_volume *vol = ni->vol;
        char *outbuf = NULL, *pbuf, *inbuf, *in_mapping;
@@ -1321,7 +1312,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                else
                        bsz = insz - p;
                pbuf = &outbuf[compsz];
-               sz = ntfs_compress_block(&inbuf[p], bsz, pbuf);
+               sz = ntfs_compress_block(ctx, &inbuf[p], bsz, pbuf);
                if (sz < 0) {
                        err = sz;
                        goto out;
@@ -1472,6 +1463,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
                struct iov_iter *from)
 {
+       struct compress_context *ctx;
        struct folio *folio;
        struct page **pages = NULL, *page;
        int pages_per_cb;
@@ -1486,6 +1478,11 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
        pages = kmalloc_array(pages_per_cb, sizeof(struct page *), GFP_NOFS);
        if (!pages)
                return -ENOMEM;
+       ctx = kvzalloc_obj(*ctx, GFP_NOFS);
+       if (!ctx) {
+               kfree(pages);
+               return -ENOMEM;
+       }
 
        while (count) {
                pgoff_t index;
@@ -1554,7 +1551,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
                        goto release_pages;
                }
 
-               err = ntfs_write_cb(ni, pos, pages, pages_per_cb, page_offset);
+               err = ntfs_write_cb(ni, pos, pages, pages_per_cb, page_offset, ctx);
                if (!err && pos + copied > ni->initialized_size) {
                        mutex_lock(&ni->mrec_lock);
                        err = ntfs_attr_set_initialized_size(ni, pos + copied);
@@ -1584,6 +1581,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
        }
 
 out:
+       kvfree(ctx);
        kfree(pages);
        if (err < 0)
                written = err;
--

2.34.1

Signature · 2 lines
Thanks,
Hyunchul
author maintainer
#11 lore raw

PATCH 7/11 ntfs: reuse compression output workspace across write units

In reply to Namjae Jeon

ntfs_write_cb() allocates output pages and creates input and output vmaps for every compression unit. Sequential writes repeatedly pay those allocation and page-table costs even though each unit has the same maximum output size.
Allocate and map the output workspace once per write request. Access

input sub-blocks with kmap_local_page(), and reuse the output pages and mapping for every compression unit in the request.

---
 fs/ntfs/compress.c | 135 ++++++++++++++++++++++++++++-----------------
 1 file changed, 85 insertions(+), 50 deletions(-)
fs/ntfs/compress.c +85 -50
index f073a53f6136..f3c14518f78e 100644
@@ -894,6 +894,12 @@ struct compress_context {
 	s16 prev[NTFS_SB_SIZE];
 };
 
+struct ntfs_compress_workspace {
+	struct page **pages;
+	char *outbuf;
+	unsigned int nr_pages;
+};
+
 /*
  * Hash the next 3-byte sequence in the input buffer
  */
@@ -1247,12 +1253,69 @@ static int ntfs_compress_block(struct compress_context *pctx,
 	return xout;
 }
 
+static int ntfs_compress_workspace_init(struct ntfs_inode *ni,
+					struct ntfs_compress_workspace *ws)
+{
+	unsigned int size, i;
+
+	size = ni->itype.compressed.block_size + 2 *
+		(ni->itype.compressed.block_size / NTFS_SB_SIZE) + 2;
+	ws->nr_pages = DIV_ROUND_UP(size, PAGE_SIZE);
+	ws->pages = kcalloc(ws->nr_pages, sizeof(*ws->pages), GFP_NOFS);
+	if (!ws->pages)
+		return -ENOMEM;
+
+	for (i = 0; i < ws->nr_pages; i++) {
+		ws->pages[i] = alloc_page(GFP_NOFS);
+		if (!ws->pages[i])
+			goto free_pages;
+	}
+
+	ws->outbuf = vmap(ws->pages, ws->nr_pages, VM_MAP, PAGE_KERNEL);
+	if (!ws->outbuf)
+		goto free_pages;
+	return 0;
+
+free_pages:
+	while (i)
+		put_page(ws->pages[--i]);
+	kfree(ws->pages);
+	return -ENOMEM;
+}
+
+static void ntfs_compress_workspace_free(struct ntfs_compress_workspace *ws)
+{
+	unsigned int i;
+
+	vunmap(ws->outbuf);
+	for (i = 0; i < ws->nr_pages; i++)
+		put_page(ws->pages[i]);
+	kfree(ws->pages);
+}
+
+static void ntfs_copy_cb(struct page **pages, int pages_per_cb,
+			 unsigned int page_offset,
+			 struct ntfs_compress_workspace *ws, unsigned int bytes)
+{
+	unsigned int copied = 0, i;
+
+	for (i = 0; i < pages_per_cb && copied < bytes; i++) {
+		unsigned int offset = i ? 0 : page_offset;
+		unsigned int len = min(bytes - copied, PAGE_SIZE - offset);
+		void *addr = kmap_local_page(pages[i]);
+
+		memcpy(ws->outbuf + copied, addr + offset, len);
+		kunmap_local(addr);
+		copied += len;
+	}
+}
+
 static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 		int pages_per_cb, unsigned int page_offset,
-		struct compress_context *ctx)
+		struct compress_context *ctx, struct ntfs_compress_workspace *ws)
 {
 	struct ntfs_volume *vol = ni->vol;
-	char *outbuf = NULL, *pbuf, *inbuf, *in_mapping;
+	char *outbuf = ws->outbuf, *pbuf;
 	u32 compsz, p, insz = ni->itype.compressed.block_size;
 	s32 rounded, bio_size;
 	int sz;
@@ -1264,55 +1327,32 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 	static char twozeroes[] = {0x02, 0xb0, 0x00, 0x00, 0x00};
 	/* more compressed zeroes, to be followed by some count */
 	static char morezeroes[] = {0x03, 0xb0, 0x02, 0x00};
-	struct page **pages_disk = NULL, *pg;
 	s64 bio_lcn;
 	struct runlist_element *rlc, *rl;
 	int i, err;
-	int pages_count = (round_up(ni->itype.compressed.block_size + 2 *
-		(ni->itype.compressed.block_size / NTFS_SB_SIZE) + 2, PAGE_SIZE)) / PAGE_SIZE;
 	u32 cb_clusters = ni->itype.compressed.block_clusters;
 	size_t new_rl_count;
 	struct bio *bio = NULL;
 	loff_t cb_pos, new_length;
 	s64 new_vcn;
 
-	in_mapping = vmap(pages, pages_per_cb, VM_MAP, PAGE_KERNEL_RO);
-	if (!in_mapping)
-		return -ENOMEM;
-	inbuf = in_mapping + page_offset;
-
-	/* may need 2 extra bytes per block and 2 more bytes */
-	pages_disk = kcalloc(pages_count, sizeof(struct page *), GFP_NOFS);
-	if (!pages_disk) {
-		vunmap(in_mapping);
-		return -ENOMEM;
-	}
-
-	for (i = 0; i < pages_count; i++) {
-		pg = alloc_page(GFP_KERNEL);
-		if (!pg) {
-			err = -ENOMEM;
-			goto out;
-		}
-		pages_disk[i] = pg;
-		lock_page(pg);
-	}
-
-	outbuf = vmap(pages_disk, pages_count, VM_MAP, PAGE_KERNEL);
-	if (!outbuf) {
-		err = -ENOMEM;
-		goto out;
-	}
-
 	compsz = 0;
 	allzeroes = true;
 	for (p = 0; (p < insz) && !fail; p += NTFS_SB_SIZE) {
+		unsigned int input_offset = page_offset + p;
+		unsigned int page_idx = input_offset >> PAGE_SHIFT;
+		const char *input;
+		void *addr;
+
 		if ((p + NTFS_SB_SIZE) < insz)
 			bsz = NTFS_SB_SIZE;
 		else
 			bsz = insz - p;
 		pbuf = &outbuf[compsz];
-		sz = ntfs_compress_block(ctx, &inbuf[p], bsz, pbuf);
+		addr = kmap_local_page(pages[page_idx]);
+		input = addr + offset_in_page(input_offset);
+		sz = ntfs_compress_block(ctx, input, bsz, pbuf);
+		kunmap_local(addr);
 		if (sz < 0) {
 			err = sz;
 			goto out;
@@ -1352,14 +1392,12 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 		rounded = ((compsz - 1) | (vol->cluster_size - 1)) + 1;
 		memset(&outbuf[compsz], 0, rounded - compsz);
 		bio_size = rounded;
-		pages = pages_disk;
 	} else if (allzeroes) {
 		err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, cb_clusters);
 		goto out;
 	} else {
-		memcpy(outbuf, inbuf, insz);
+		ntfs_copy_cb(pages, pages_per_cb, page_offset, ws, insz);
 		bio_size = insz;
-		pages = pages_disk;
 	}
 
 	new_length = ntfs_bytes_to_cluster(vol, round_up(bio_size, vol->cluster_size));
@@ -1398,7 +1436,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 						((s64)i << PAGE_SHIFT));
 		}
 
-		if (!bio_add_page(bio, pages[i], page_size, 0)) {
+		if (!bio_add_page(bio, ws->pages[i], page_size, 0)) {
 			err = submit_bio_wait(bio);
 			bio_put(bio);
 			if (err)
@@ -1443,17 +1481,6 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 		ntfs_error(vol->sb, "Failed to free hot clusters.");
 	kvfree(rlc);
 out:
-	if (outbuf)
-		vunmap(outbuf);
-	for (i = 0; i < pages_count; i++) {
-		pg = pages_disk[i];
-		if (pg) {
-			unlock_page(pg);
-			put_page(pg);
-		}
-	}
-	kfree(pages_disk);
-	vunmap(in_mapping);
 	NInoSetFileNameDirty(ni);
 	mark_mft_record_dirty(ni);
 
@@ -1463,6 +1490,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 		struct iov_iter *from)
 {
+	struct ntfs_compress_workspace ws = {};
 	struct compress_context *ctx;
 	struct folio *folio;
 	struct page **pages = NULL, *page;
@@ -1483,6 +1511,12 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 		kfree(pages);
 		return -ENOMEM;
 	}
+	err = ntfs_compress_workspace_init(ni, &ws);
+	if (err) {
+		kvfree(ctx);
+		kfree(pages);
+		return err;
+	}
 
 	while (count) {
 		pgoff_t index;
@@ -1551,7 +1585,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 			goto release_pages;
 		}
 
-		err = ntfs_write_cb(ni, pos, pages, pages_per_cb, page_offset, ctx);
+		err = ntfs_write_cb(ni, pos, pages, pages_per_cb, page_offset, ctx, &ws);
 		if (!err && pos + copied > ni->initialized_size) {
 			mutex_lock(&ni->mrec_lock);
 			err = ntfs_attr_set_initialized_size(ni, pos + copied);
@@ -1581,6 +1615,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 	}
 
 out:
+	ntfs_compress_workspace_free(&ws);
 	kvfree(ctx);
 	kfree(pages);
 	if (err < 0)
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 7/11
#12 lore raw

Re: [PATCH 07/11] ntfs: reuse compression output workspace across write units

2026년 7월 21일 (화) 오후 6:55, Namjae Jeon <[email protected]>님이 작성:

Quoted 11 lines

ntfs_write_cb() allocates output pages and creates input and output vmaps for every compression unit. Sequential writes repeatedly pay those allocation and page-table costs even though each unit has the same maximum output size.
Allocate and map the output workspace once per write request. Access

input sub-blocks with kmap_local_page(), and reuse the output pages and mapping for every compression unit in the request.

Looks good to me.

Quoted 242 lines
---
 fs/ntfs/compress.c | 135 ++++++++++++++++++++++++++++-----------------
 1 file changed, 85 insertions(+), 50 deletions(-)
fs/ntfs/compress.c +85 -51
index f073a53f6136..f3c14518f78e 100644
@@ -894,6 +894,12 @@ struct compress_context {
        s16 prev[NTFS_SB_SIZE];
 };
 
+struct ntfs_compress_workspace {
+       struct page **pages;
+       char *outbuf;
+       unsigned int nr_pages;
+};
+
 /*
  * Hash the next 3-byte sequence in the input buffer
  */
@@ -1247,12 +1253,69 @@ static int ntfs_compress_block(struct compress_context *pctx,
        return xout;
 }
 
+static int ntfs_compress_workspace_init(struct ntfs_inode *ni,
+                                       struct ntfs_compress_workspace *ws)
+{
+       unsigned int size, i;
+
+       size = ni->itype.compressed.block_size + 2 *
+               (ni->itype.compressed.block_size / NTFS_SB_SIZE) + 2;
+       ws->nr_pages = DIV_ROUND_UP(size, PAGE_SIZE);
+       ws->pages = kcalloc(ws->nr_pages, sizeof(*ws->pages), GFP_NOFS);
+       if (!ws->pages)
+               return -ENOMEM;
+
+       for (i = 0; i < ws->nr_pages; i++) {
+               ws->pages[i] = alloc_page(GFP_NOFS);
+               if (!ws->pages[i])
+                       goto free_pages;
+       }
+
+       ws->outbuf = vmap(ws->pages, ws->nr_pages, VM_MAP, PAGE_KERNEL);
+       if (!ws->outbuf)
+               goto free_pages;
+       return 0;
+
+free_pages:
+       while (i)
+               put_page(ws->pages[--i]);
+       kfree(ws->pages);
+       return -ENOMEM;
+}
+
+static void ntfs_compress_workspace_free(struct ntfs_compress_workspace *ws)
+{
+       unsigned int i;
+
+       vunmap(ws->outbuf);
+       for (i = 0; i < ws->nr_pages; i++)
+               put_page(ws->pages[i]);
+       kfree(ws->pages);
+}
+
+static void ntfs_copy_cb(struct page **pages, int pages_per_cb,
+                        unsigned int page_offset,
+                        struct ntfs_compress_workspace *ws, unsigned int bytes)
+{
+       unsigned int copied = 0, i;
+
+       for (i = 0; i < pages_per_cb && copied < bytes; i++) {
+               unsigned int offset = i ? 0 : page_offset;
+               unsigned int len = min(bytes - copied, PAGE_SIZE - offset);
+               void *addr = kmap_local_page(pages[i]);
+
+               memcpy(ws->outbuf + copied, addr + offset, len);
+               kunmap_local(addr);
+               copied += len;
+       }
+}
+
 static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                int pages_per_cb, unsigned int page_offset,
-               struct compress_context *ctx)
+               struct compress_context *ctx, struct ntfs_compress_workspace *ws)
 {
        struct ntfs_volume *vol = ni->vol;
-       char *outbuf = NULL, *pbuf, *inbuf, *in_mapping;
+       char *outbuf = ws->outbuf, *pbuf;
        u32 compsz, p, insz = ni->itype.compressed.block_size;
        s32 rounded, bio_size;
        int sz;
@@ -1264,55 +1327,32 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
        static char twozeroes[] = {0x02, 0xb0, 0x00, 0x00, 0x00};
        /* more compressed zeroes, to be followed by some count */
        static char morezeroes[] = {0x03, 0xb0, 0x02, 0x00};
-       struct page **pages_disk = NULL, *pg;
        s64 bio_lcn;
        struct runlist_element *rlc, *rl;
        int i, err;
-       int pages_count = (round_up(ni->itype.compressed.block_size + 2 *
-               (ni->itype.compressed.block_size / NTFS_SB_SIZE) + 2, PAGE_SIZE)) / PAGE_SIZE;
        u32 cb_clusters = ni->itype.compressed.block_clusters;
        size_t new_rl_count;
        struct bio *bio = NULL;
        loff_t cb_pos, new_length;
        s64 new_vcn;
 
-       in_mapping = vmap(pages, pages_per_cb, VM_MAP, PAGE_KERNEL_RO);
-       if (!in_mapping)
-               return -ENOMEM;
-       inbuf = in_mapping + page_offset;
-
-       /* may need 2 extra bytes per block and 2 more bytes */
-       pages_disk = kcalloc(pages_count, sizeof(struct page *), GFP_NOFS);
-       if (!pages_disk) {
-               vunmap(in_mapping);
-               return -ENOMEM;
-       }
-
-       for (i = 0; i < pages_count; i++) {
-               pg = alloc_page(GFP_KERNEL);
-               if (!pg) {
-                       err = -ENOMEM;
-                       goto out;
-               }
-               pages_disk[i] = pg;
-               lock_page(pg);
-       }
-
-       outbuf = vmap(pages_disk, pages_count, VM_MAP, PAGE_KERNEL);
-       if (!outbuf) {
-               err = -ENOMEM;
-               goto out;
-       }
-
        compsz = 0;
        allzeroes = true;
        for (p = 0; (p < insz) && !fail; p += NTFS_SB_SIZE) {
+               unsigned int input_offset = page_offset + p;
+               unsigned int page_idx = input_offset >> PAGE_SHIFT;
+               const char *input;
+               void *addr;
+
                if ((p + NTFS_SB_SIZE) < insz)
                        bsz = NTFS_SB_SIZE;
                else
                        bsz = insz - p;
                pbuf = &outbuf[compsz];
-               sz = ntfs_compress_block(ctx, &inbuf[p], bsz, pbuf);
+               addr = kmap_local_page(pages[page_idx]);
+               input = addr + offset_in_page(input_offset);
+               sz = ntfs_compress_block(ctx, input, bsz, pbuf);
+               kunmap_local(addr);
                if (sz < 0) {
                        err = sz;
                        goto out;
@@ -1352,14 +1392,12 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                rounded = ((compsz - 1) | (vol->cluster_size - 1)) + 1;
                memset(&outbuf[compsz], 0, rounded - compsz);
                bio_size = rounded;
-               pages = pages_disk;
        } else if (allzeroes) {
                err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, cb_clusters);
                goto out;
        } else {
-               memcpy(outbuf, inbuf, insz);
+               ntfs_copy_cb(pages, pages_per_cb, page_offset, ws, insz);
                bio_size = insz;
-               pages = pages_disk;
        }
 
        new_length = ntfs_bytes_to_cluster(vol, round_up(bio_size, vol->cluster_size));
@@ -1398,7 +1436,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                                                ((s64)i << PAGE_SHIFT));
                }
 
-               if (!bio_add_page(bio, pages[i], page_size, 0)) {
+               if (!bio_add_page(bio, ws->pages[i], page_size, 0)) {
                        err = submit_bio_wait(bio);
                        bio_put(bio);
                        if (err)
@@ -1443,17 +1481,6 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                ntfs_error(vol->sb, "Failed to free hot clusters.");
        kvfree(rlc);
 out:
-       if (outbuf)
-               vunmap(outbuf);
-       for (i = 0; i < pages_count; i++) {
-               pg = pages_disk[i];
-               if (pg) {
-                       unlock_page(pg);
-                       put_page(pg);
-               }
-       }
-       kfree(pages_disk);
-       vunmap(in_mapping);
        NInoSetFileNameDirty(ni);
        mark_mft_record_dirty(ni);
 
@@ -1463,6 +1490,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
                struct iov_iter *from)
 {
+       struct ntfs_compress_workspace ws = {};
        struct compress_context *ctx;
        struct folio *folio;
        struct page **pages = NULL, *page;
@@ -1483,6 +1511,12 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
                kfree(pages);
                return -ENOMEM;
        }
+       err = ntfs_compress_workspace_init(ni, &ws);
+       if (err) {
+               kvfree(ctx);
+               kfree(pages);
+               return err;
+       }
 
        while (count) {
                pgoff_t index;
@@ -1551,7 +1585,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
                        goto release_pages;
                }
 
-               err = ntfs_write_cb(ni, pos, pages, pages_per_cb, page_offset, ctx);
+               err = ntfs_write_cb(ni, pos, pages, pages_per_cb, page_offset, ctx, &ws);
                if (!err && pos + copied > ni->initialized_size) {
                        mutex_lock(&ni->mrec_lock);
                        err = ntfs_attr_set_initialized_size(ni, pos + copied);
@@ -1581,6 +1615,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
        }
 
 out:
+       ntfs_compress_workspace_free(&ws);
        kvfree(ctx);
        kfree(pages);
        if (err < 0)
--

2.34.1

Signature · 2 lines
Thanks,
Hyunchul
author maintainer
#13 lore raw

PATCH 8/11 ntfs: submit one bio per compressed write unit

In reply to Namjae Jeon

ntfs_write_cb() allocates a single-vector bio and synchronously submits it whenever another output page cannot be added. A 64 KiB uncompressed unit therefore requires up to sixteen separate bio submissions.

Allocate enough vectors for the complete unit, add all output pages, and perform one synchronous submission.

---
 fs/ntfs/compress.c | 43 +++++++++++--------------------------------
 1 file changed, 11 insertions(+), 32 deletions(-)
fs/ntfs/compress.c +11 -32
index f3c14518f78e..a3af669b1008 100644
@@ -1327,7 +1327,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 	static char twozeroes[] = {0x02, 0xb0, 0x00, 0x00, 0x00};
 	/* more compressed zeroes, to be followed by some count */
 	static char morezeroes[] = {0x03, 0xb0, 0x02, 0x00};
-	s64 bio_lcn;
+	s64 bio_lcn, bio_pos;
 	struct runlist_element *rlc, *rl;
 	int i, err;
 	u32 cb_clusters = ni->itype.compressed.block_clusters;
@@ -1410,41 +1410,20 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 	}
 
 	bio_lcn = rlc->lcn;
-	i = 0;
-	while (bio_size > 0) {
-		int page_size;
-
-		if (bio_size >= PAGE_SIZE) {
-			page_size = PAGE_SIZE;
-			bio_size -= PAGE_SIZE;
-		} else {
-			page_size = bio_size;
-			bio_size = 0;
-		}
+	bio_pos = ntfs_cluster_to_bytes(vol, bio_lcn);
+	bio = bio_alloc(vol->sb->s_bdev, DIV_ROUND_UP(bio_size, PAGE_SIZE),
+			REQ_OP_WRITE, GFP_NOIO);
+	bio->bi_iter.bi_sector = ntfs_bytes_to_sector(vol, bio_pos);
 
-setup_bio:
-		if (!bio) {
-			bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE,
-					GFP_NOIO);
-			if (!bio) {
-				err = -ENOMEM;
-				goto free_rlc;
-			}
-			bio->bi_iter.bi_sector =
-				ntfs_bytes_to_sector(vol,
-						ntfs_cluster_to_bytes(vol, bio_lcn) +
-						((s64)i << PAGE_SHIFT));
-		}
+	for (i = 0; bio_size; i++) {
+		unsigned int len = min_t(unsigned int, bio_size, PAGE_SIZE);
 
-		if (!bio_add_page(bio, ws->pages[i], page_size, 0)) {
-			err = submit_bio_wait(bio);
+		if (bio_add_page(bio, ws->pages[i], len, 0) != len) {
+			err = -EIO;
 			bio_put(bio);
-			if (err)
-				goto free_rlc;
-			bio = NULL;
-			goto setup_bio;
+			goto free_rlc;
 		}
-		i++;
+		bio_size -= len;
 	}
 
 	err = submit_bio_wait(bio);
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 8/11
#14 lore raw

Re: [PATCH 08/11] ntfs: submit one bio per compressed write unit

2026년 7월 21일 (화) 오후 6:55, Namjae Jeon <[email protected]>님이 작성:

Quoted 9 lines

ntfs_write_cb() allocates a single-vector bio and synchronously submits it whenever another output page cannot be added. A 64 KiB uncompressed unit therefore requires up to sixteen separate bio submissions.

Allocate enough vectors for the complete unit, add all output pages, and perform one synchronous submission.

Looks good to me.

Quoted 72 lines
---
 fs/ntfs/compress.c | 43 +++++++++++--------------------------------
 1 file changed, 11 insertions(+), 32 deletions(-)
fs/ntfs/compress.c +11 -33
index f3c14518f78e..a3af669b1008 100644
@@ -1327,7 +1327,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
        static char twozeroes[] = {0x02, 0xb0, 0x00, 0x00, 0x00};
        /* more compressed zeroes, to be followed by some count */
        static char morezeroes[] = {0x03, 0xb0, 0x02, 0x00};
-       s64 bio_lcn;
+       s64 bio_lcn, bio_pos;
        struct runlist_element *rlc, *rl;
        int i, err;
        u32 cb_clusters = ni->itype.compressed.block_clusters;
@@ -1410,41 +1410,20 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
        }
 
        bio_lcn = rlc->lcn;
-       i = 0;
-       while (bio_size > 0) {
-               int page_size;
-
-               if (bio_size >= PAGE_SIZE) {
-                       page_size = PAGE_SIZE;
-                       bio_size -= PAGE_SIZE;
-               } else {
-                       page_size = bio_size;
-                       bio_size = 0;
-               }
+       bio_pos = ntfs_cluster_to_bytes(vol, bio_lcn);
+       bio = bio_alloc(vol->sb->s_bdev, DIV_ROUND_UP(bio_size, PAGE_SIZE),
+                       REQ_OP_WRITE, GFP_NOIO);
+       bio->bi_iter.bi_sector = ntfs_bytes_to_sector(vol, bio_pos);
 
-setup_bio:
-               if (!bio) {
-                       bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE,
-                                       GFP_NOIO);
-                       if (!bio) {
-                               err = -ENOMEM;
-                               goto free_rlc;
-                       }
-                       bio->bi_iter.bi_sector =
-                               ntfs_bytes_to_sector(vol,
-                                               ntfs_cluster_to_bytes(vol, bio_lcn) +
-                                               ((s64)i << PAGE_SHIFT));
-               }
+       for (i = 0; bio_size; i++) {
+               unsigned int len = min_t(unsigned int, bio_size, PAGE_SIZE);
 
-               if (!bio_add_page(bio, ws->pages[i], page_size, 0)) {
-                       err = submit_bio_wait(bio);
+               if (bio_add_page(bio, ws->pages[i], len, 0) != len) {
+                       err = -EIO;
                        bio_put(bio);
-                       if (err)
-                               goto free_rlc;
-                       bio = NULL;
-                       goto setup_bio;
+                       goto free_rlc;
                }
-               i++;
+               bio_size -= len;
        }
 
        err = submit_bio_wait(bio);
--

2.34.1

Signature · 2 lines
Thanks,
Hyunchul
author maintainer
#15 lore raw

PATCH 9/11 ntfs: skip reads for full compression unit overwrites

In reply to Namjae Jeon

ntfs_compress_write() reads every page in a compression unit before copying new data into it. The read is unnecessary when an aligned write replaces every byte covered by the page-cache folios.

Detect full page-aligned compression unit overwrites and grab locked cache folios without reading them. Keep the read-modify-write path for partial units and units that cover only part of a large page.

---
 fs/ntfs/compress.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
fs/ntfs/compress.c +9 -2
index a3af669b1008..ea29fade9b9b 100644
@@ -1501,6 +1501,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 		pgoff_t index;
 		size_t copied, bytes;
 		unsigned int page_offset;
+		bool full_cb;
 		int off;
 
 		off = pos & (cb_size - 1);
@@ -1512,6 +1513,8 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 		page_offset = offset_in_page(cb_off);
 		pages_per_cb = DIV_ROUND_UP(page_offset + cb_size, PAGE_SIZE);
 		index = cb_off >> PAGE_SHIFT;
+		full_cb = !off && bytes == cb_size && !page_offset &&
+				!(cb_size & (PAGE_SIZE - 1));
 
 		if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
 			err = -EFAULT;
@@ -1519,7 +1522,10 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 		}
 
 		for (i = 0; i < pages_per_cb; i++) {
-			folio = read_mapping_folio(mapping, index + i, NULL);
+			if (full_cb)
+				folio = filemap_grab_folio(mapping, index + i);
+			else
+				folio = read_mapping_folio(mapping, index + i, NULL);
 			if (IS_ERR(folio)) {
 				for (ip = 0; ip < i; ip++) {
 					folio_unlock(page_folio(pages[ip]));
@@ -1529,7 +1535,8 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
 				goto out;
 			}
 
-			folio_lock(folio);
+			if (!full_cb)
+				folio_lock(folio);
 			pages[i] = folio_page(folio, 0);
 		}
 
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 9/11
#16 lore raw

Re: [PATCH 09/11] ntfs: skip reads for full compression unit overwrites

2026년 7월 21일 (화) 오후 6:55, Namjae Jeon <[email protected]>님이 작성:

Quoted 10 lines

ntfs_compress_write() reads every page in a compression unit before copying new data into it. The read is unnecessary when an aligned write replaces every byte covered by the page-cache folios.

Detect full page-aligned compression unit overwrites and grab locked cache folios without reading them. Keep the read-modify-write path for partial units and units that cover only part of a large page.

Looks good to me.

Quoted 50 lines
---
 fs/ntfs/compress.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
fs/ntfs/compress.c +9 -3
index a3af669b1008..ea29fade9b9b 100644
@@ -1501,6 +1501,7 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
                pgoff_t index;
                size_t copied, bytes;
                unsigned int page_offset;
+               bool full_cb;
                int off;
 
                off = pos & (cb_size - 1);
@@ -1512,6 +1513,8 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
                page_offset = offset_in_page(cb_off);
                pages_per_cb = DIV_ROUND_UP(page_offset + cb_size, PAGE_SIZE);
                index = cb_off >> PAGE_SHIFT;
+               full_cb = !off && bytes == cb_size && !page_offset &&
+                               !(cb_size & (PAGE_SIZE - 1));
 
                if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
                        err = -EFAULT;
@@ -1519,7 +1522,10 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
                }
 
                for (i = 0; i < pages_per_cb; i++) {
-                       folio = read_mapping_folio(mapping, index + i, NULL);
+                       if (full_cb)
+                               folio = filemap_grab_folio(mapping, index + i);
+                       else
+                               folio = read_mapping_folio(mapping, index + i, NULL);
                        if (IS_ERR(folio)) {
                                for (ip = 0; ip < i; ip++) {
                                        folio_unlock(page_folio(pages[ip]));
@@ -1529,7 +1535,8 @@ int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
                                goto out;
                        }
 
-                       folio_lock(folio);
+                       if (!full_cb)
+                               folio_lock(folio);
                        pages[i] = folio_page(folio, 0);
                }
 
--

2.34.1

Signature · 2 lines
Thanks,
Hyunchul
author maintainer
#17 lore raw

PATCH 10/11 ntfs: fix resident conversion in ntfs_new_attr_flags

In reply to Namjae Jeon

From: Hyunchul Lee <[email protected]>

When setting sparse/compressed flags on a resident attribute, the function skipped the resident-to-non-resident conversion and terminated.

---
 fs/ntfs/ea.c | 94 ++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 72 insertions(+), 22 deletions(-)
fs/ntfs/ea.c +72 -22
index d0044c61152a..95587b9a7129 100644
@@ -630,6 +630,7 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
 	struct mft_record *m;
 	struct attr_record *a;
 	__le16 new_aflags;
+	u16 old_name_ofs, old_mp_ofs;
 	int mp_size, mp_ofs, name_ofs, arec_size, err;
 
 	m = map_mft_record(ni);
@@ -662,8 +663,10 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
 	else
 		new_aflags &= ~ATTR_IS_COMPRESSED;
 
-	if (new_aflags == a->flags)
-		return 0;
+	if (new_aflags == a->flags) {
+		err = 0;
+		goto err_out;
+	}
 
 	if ((new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) ==
 			  (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) {
@@ -672,15 +675,40 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
 		goto err_out;
 	}
 
-	if (!a->non_resident)
-		goto out;
+	if (!a->non_resident) {
+		if (!(new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)))
+			return 0;
 
-	if (a->data.non_resident.data_size) {
-		pr_err("Can't change sparsed/compressed for non-empty file\n");
-		err = -EOPNOTSUPP;
-		goto err_out;
+		if (le32_to_cpu(a->data.resident.value_length)) {
+			pr_err("Can't change sparse/compressed for non-empty file");
+			err = -EOPNOTSUPP;
+			goto err_out;
+		}
+
+		err = ntfs_attr_make_non_resident(ni, 0);
+		if (err)
+			goto err_out;
+
+		ntfs_attr_reinit_search_ctx(ctx);
+		err = ntfs_attr_lookup(ni->type, ni->name,
+				ni->name_len, CASE_SENSITIVE,
+				0, NULL, 0, ctx);
+		if (err) {
+			err = -EINVAL;
+			goto err_out;
+		}
+		a = ctx->attr;
+	} else {
+		if (a->data.non_resident.data_size) {
+			pr_err("Can't change sparsed/compressed for non-empty file");
+			err = -EOPNOTSUPP;
+			goto err_out;
+		}
 	}
 
+	old_name_ofs = le16_to_cpu(a->name_offset);
+	old_mp_ofs = le16_to_cpu(a->data.non_resident.mapping_pairs_offset);
+
 	if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED))
 		name_ofs = (offsetof(struct attr_record,
 				     data.non_resident.compressed_size) +
@@ -703,6 +731,25 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
 	if (unlikely(err))
 		goto err_out;
 
+	/*
+	 * When compressed/sparse state changes, the non-resident header grows or
+	 * shrinks by the compressed_size field. Update the in-record payload layout
+	 * to match the new offsets before exposing the new mapping_pairs_offset.
+	 */
+	if (name_ofs > old_name_ofs) {
+		if (mp_ofs != old_mp_ofs)
+			memmove((u8 *)a + mp_ofs, (u8 *)a + old_mp_ofs, mp_size);
+		if (a->name_length)
+			memmove((u8 *)a + name_ofs, (u8 *)a + old_name_ofs,
+				a->name_length * sizeof(__le16));
+	} else {
+		if (a->name_length && name_ofs != old_name_ofs)
+			memmove((u8 *)a + name_ofs, (u8 *)a + old_name_ofs,
+				a->name_length * sizeof(__le16));
+		if (mp_ofs != old_mp_ofs)
+			memmove((u8 *)a + mp_ofs, (u8 *)a + old_mp_ofs, mp_size);
+	}
+
 	if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) {
 		a->data.non_resident.compression_unit = 0;
 		if (new_aflags & ATTR_IS_COMPRESSED || ni->vol->major_ver < 3)
@@ -723,28 +770,31 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
 			ni->itype.compressed.block_size_bits = 0;
 			ni->itype.compressed.block_clusters = 0;
 		}
-
-		if (new_aflags & ATTR_IS_SPARSE) {
-			NInoSetSparse(ni);
-			ni->flags |= FILE_ATTR_SPARSE_FILE;
-		}
-
-		if (new_aflags & ATTR_IS_COMPRESSED) {
-			NInoSetCompressed(ni);
-			ni->flags |= FILE_ATTR_COMPRESSED;
-		}
 	} else {
-		ni->flags &= ~(FILE_ATTR_SPARSE_FILE | FILE_ATTR_COMPRESSED);
 		a->data.non_resident.compression_unit = 0;
-		NInoClearSparse(ni);
-		NInoClearCompressed(ni);
 	}
 
 	a->name_offset = cpu_to_le16(name_ofs);
 	a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
 
-out:
 	a->flags = new_aflags;
+
+	if (new_aflags & ATTR_IS_SPARSE) {
+		NInoSetSparse(ni);
+		ni->flags |= FILE_ATTR_SPARSE_FILE;
+	} else {
+		NInoClearSparse(ni);
+		ni->flags &= ~FILE_ATTR_SPARSE_FILE;
+	}
+
+	if (new_aflags & ATTR_IS_COMPRESSED) {
+		NInoSetCompressed(ni);
+		ni->flags |= FILE_ATTR_COMPRESSED;
+	} else {
+		NInoClearCompressed(ni);
+		ni->flags &= ~FILE_ATTR_COMPRESSED;
+	}
+
 	mark_mft_record_dirty(ctx->ntfs_ino);
 err_out:
 	if (ctx)
Signature · 1 line
2.34.1
author maintainer
#18 lore raw

PATCH 11/11 ntfs: move attribute payload before shrinking its record

In reply to Namjae Jeon

ntfs_new_attr_flags() resizes the non-resident attribute record before moving its name and mapping pairs to their shorter-header offsets when compression or sparse state is cleared.

Shrinking the record first moves the following attribute over the tail of the old record. The subsequent memmove() therefore copies bytes from that following attribute instead of the old mapping pairs. Re-enabling compression on an empty file persists those bytes as a malformed mapping pairs array, which ntfsck reports as a missing or invalid run length.

Move the payload before shrinking the record, while retaining the existing resize-before-move ordering when growing it.

---
 fs/ntfs/ea.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
fs/ntfs/ea.c +15 -8
index 95587b9a7129..4fb10d51211c 100644
@@ -631,7 +631,7 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
 	struct attr_record *a;
 	__le16 new_aflags;
 	u16 old_name_ofs, old_mp_ofs;
-	int mp_size, mp_ofs, name_ofs, arec_size, err;
+	int mp_size, mp_ofs, name_ofs, old_arec_size, arec_size, err;
 
 	m = map_mft_record(ni);
 	if (IS_ERR(m))
@@ -726,6 +726,19 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
 
 	mp_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7;
 	arec_size = (mp_ofs + mp_size + 7) & ~7;
+	old_arec_size = le32_to_cpu(a->length);
+
+	/*
+	 * Move payloads before shrinking the record.  Otherwise resizing moves
+	 * the following attribute over the old payload before it can be copied.
+	 */
+	if (arec_size < old_arec_size) {
+		if (a->name_length && name_ofs != old_name_ofs)
+			memmove((u8 *)a + name_ofs, (u8 *)a + old_name_ofs,
+				a->name_length * sizeof(__le16));
+		if (mp_ofs != old_mp_ofs)
+			memmove((u8 *)a + mp_ofs, (u8 *)a + old_mp_ofs, mp_size);
+	}
 
 	err = ntfs_attr_record_resize(m, a, arec_size);
 	if (unlikely(err))
@@ -736,18 +749,12 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
 	 * shrinks by the compressed_size field. Update the in-record payload layout
 	 * to match the new offsets before exposing the new mapping_pairs_offset.
 	 */
-	if (name_ofs > old_name_ofs) {
+	if (arec_size > old_arec_size) {
 		if (mp_ofs != old_mp_ofs)
 			memmove((u8 *)a + mp_ofs, (u8 *)a + old_mp_ofs, mp_size);
 		if (a->name_length)
 			memmove((u8 *)a + name_ofs, (u8 *)a + old_name_ofs,
 				a->name_length * sizeof(__le16));
-	} else {
-		if (a->name_length && name_ofs != old_name_ofs)
-			memmove((u8 *)a + name_ofs, (u8 *)a + old_name_ofs,
-				a->name_length * sizeof(__le16));
-		if (mp_ofs != old_mp_ofs)
-			memmove((u8 *)a + mp_ofs, (u8 *)a + old_mp_ofs, mp_size);
 	}
 
 	if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) {
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 11/11
#19 lore raw

Re: [PATCH 11/11] ntfs: move attribute payload before shrinking its record

2026년 7월 21일 (화) 오후 6:55, Namjae Jeon <[email protected]>님이 작성:

Quoted 11 lines

ntfs_new_attr_flags() resizes the non-resident attribute record before moving its name and mapping pairs to their shorter-header offsets when compression or sparse state is cleared.

Shrinking the record first moves the following attribute over the tail of the old record. The subsequent memmove() therefore copies bytes from that following attribute instead of the old mapping pairs. Re-enabling compression on an empty file persists those bytes as a malformed mapping pairs array, which ntfsck reports as a missing or invalid run length.

Quoted 4 lines

Move the payload before shrinking the record, while retaining the existing resize-before-move ordering when growing it.

Looks good to me.

Quoted 60 lines
---
 fs/ntfs/ea.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
fs/ntfs/ea.c +15 -9
index 95587b9a7129..4fb10d51211c 100644
@@ -631,7 +631,7 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
        struct attr_record *a;
        __le16 new_aflags;
        u16 old_name_ofs, old_mp_ofs;
-       int mp_size, mp_ofs, name_ofs, arec_size, err;
+       int mp_size, mp_ofs, name_ofs, old_arec_size, arec_size, err;
 
        m = map_mft_record(ni);
        if (IS_ERR(m))
@@ -726,6 +726,19 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
 
        mp_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7;
        arec_size = (mp_ofs + mp_size + 7) & ~7;
+       old_arec_size = le32_to_cpu(a->length);
+
+       /*
+        * Move payloads before shrinking the record.  Otherwise resizing moves
+        * the following attribute over the old payload before it can be copied.
+        */
+       if (arec_size < old_arec_size) {
+               if (a->name_length && name_ofs != old_name_ofs)
+                       memmove((u8 *)a + name_ofs, (u8 *)a + old_name_ofs,
+                               a->name_length * sizeof(__le16));
+               if (mp_ofs != old_mp_ofs)
+                       memmove((u8 *)a + mp_ofs, (u8 *)a + old_mp_ofs, mp_size);
+       }
 
        err = ntfs_attr_record_resize(m, a, arec_size);
        if (unlikely(err))
@@ -736,18 +749,12 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
         * shrinks by the compressed_size field. Update the in-record payload layout
         * to match the new offsets before exposing the new mapping_pairs_offset.
         */
-       if (name_ofs > old_name_ofs) {
+       if (arec_size > old_arec_size) {
                if (mp_ofs != old_mp_ofs)
                        memmove((u8 *)a + mp_ofs, (u8 *)a + old_mp_ofs, mp_size);
                if (a->name_length)
                        memmove((u8 *)a + name_ofs, (u8 *)a + old_name_ofs,
                                a->name_length * sizeof(__le16));
-       } else {
-               if (a->name_length && name_ofs != old_name_ofs)
-                       memmove((u8 *)a + name_ofs, (u8 *)a + old_name_ofs,
-                               a->name_length * sizeof(__le16));
-               if (mp_ofs != old_mp_ofs)
-                       memmove((u8 *)a + mp_ofs, (u8 *)a + old_mp_ofs, mp_size);
        }
 
        if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) {
--

2.34.1

Signature · 2 lines
Thanks,
Hyunchul
maintainer Reviewed-by on v1 1/11
#20 lore raw
In reply to Namjae Jeon

2026년 7월 21일 (화) 오후 6:54, Namjae Jeon <[email protected]>님이 작성:

Quoted 8 lines

ntfs_compress_block() returns -ENOMEM when its compression context cannot be allocated, but its unsigned return type turns the error into a large positive value. ntfs_write_cb() then hides the allocation failure.

Use a signed return type and propagate negative errors to the caller.

Looks good to me.

Quoted 46 lines
---
 fs/ntfs/compress.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/ntfs/compress.c +9 -5
index fe1877b86f49..6b78a8efe3ac 100644
@@ -1070,10 +1070,10 @@ static void ntfs_skip_position(struct compress_context *pctx, const int i)
  *
  * Returns the size of the compressed block, including the
  * header (minimal size is 2, maximum size is 4098)
- * 0 if an error has been met.
+ * A negative error code if an error has been met.
  */
-static unsigned int ntfs_compress_block(const char *inbuf, const int bufsize,
-               char *outbuf)
+static int ntfs_compress_block(const char *inbuf, const int bufsize,
+                              char *outbuf)
 {
        struct compress_context *pctx;
        int i; /* current position */
@@ -1264,7 +1264,8 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
        char *outbuf = NULL, *pbuf, *inbuf;
        u32 compsz, p, insz = pages_per_cb << PAGE_SHIFT;
        s32 rounded, bio_size;
-       unsigned int sz, bsz;
+       int sz;
+       unsigned int bsz;
        bool fail = false, allzeroes;
        /* a single compressed zero */
        static char onezero[] = {0x01, 0xb0, 0x00, 0x00};
@@ -1319,6 +1320,10 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
                        bsz = insz - p;
                pbuf = &outbuf[compsz];
                sz = ntfs_compress_block(&inbuf[p], bsz, pbuf);
+               if (sz < 0) {
+                       err = sz;
+                       goto out;
+               }
                /* fail if all the clusters (or more) are needed */
                if (!sz || ((compsz + sz + vol->cluster_size + 2) >
                            ni->itype.compressed.block_size))
--

2.34.1

Signature · 2 lines
Thanks,
Hyunchul