Skip to contentSkip to conversation

ntfs: fix FITRIM bitmap indexing for large pages

PATCH Applied Hyunchul Lee 2 participants 4 messages Sep 21 NTFS filesystem

lore
Share & tools
lore

Hyunchul Lee

author maintainer
#0 lore raw

PATCH v2 1/1 ntfs: fix FITRIM bitmap indexing for large pages

The LCN bitmap has one bit per cluster, so a bitmap page represents PAGE_SIZE * 8 clusters. ntfs_trim_fs() still used a 15-bit shift when selecting the page. On arm64 with 16 KiB pages this selects the wrong bitmap page and can test or discard the wrong clusters.

Compute bitmap indices from buf_clusters, use the page cluster base as the bit-search origin, and keep the requested range as the clipping boundary.

Fixes 11ccc9107dc4 ("ntfs: update runlist handling and cluster allocator")
Cc Marek Wesołowski <[email protected]>

---
Changes in v2:
- Add Fixes tag.

 fs/ntfs/bitmap.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
fs/ntfs/bitmap.c +10 -9
index 1840b7d84c62..f0b40f374471 100644
@@ -20,7 +20,7 @@ int ntfs_trim_fs(struct ntfs_volume *vol, struct fstrim_range *range)
 	struct folio *folio;
 	unsigned long *bitmap;
 	char *kaddr;
-	u64 end, trimmed = 0, start_buf, end_buf, end_cluster;
+	u64 end, trimmed = 0, start_buf, end_buf, end_cluster, page_cluster;
 	u64 start_cluster = ntfs_bytes_to_cluster(vol, range->start);
 	u32 dq = bdev_discard_granularity(vol->sb->s_bdev);
 	int ret = 0;
@@ -45,8 +45,8 @@ int ntfs_trim_fs(struct ntfs_volume *vol, struct fstrim_range *range)
 		return -ENOMEM;
 
 	buf_clusters = PAGE_SIZE * 8;
-	start_index = start_cluster >> 15;
-	end_index = (end_cluster + buf_clusters - 1) >> 15;
+	start_index = start_cluster / buf_clusters;
+	end_index = DIV_ROUND_UP_ULL(end_cluster, buf_clusters);
 
 	for (index = start_index; index < end_index; index++) {
 		folio = ntfs_get_locked_folio(vol->lcnbmp_ino->i_mapping,
@@ -59,19 +59,20 @@ int ntfs_trim_fs(struct ntfs_volume *vol, struct fstrim_range *range)
 		kaddr = kmap_local_folio(folio, 0);
 		bitmap = (unsigned long *)kaddr;
 
-		start_buf = max_t(u64, index * buf_clusters, start_cluster);
-		end_buf = min_t(u64, (index + 1) * buf_clusters, end_cluster);
+		page_cluster = (u64)index * buf_clusters;
+		start_buf = max_t(u64, page_cluster, start_cluster);
+		end_buf = min_t(u64, page_cluster + buf_clusters, end_cluster);
 
 		end = start_buf;
 		while (end < end_buf) {
 			u64 aligned_start, aligned_end, aligned_count;
-			u64 start = find_next_zero_bit(bitmap, end_buf - start_buf,
-					end - start_buf) + start_buf;
+			u64 start = find_next_zero_bit(bitmap, end_buf - page_cluster,
+					end - page_cluster) + page_cluster;
 			if (start >= end_buf)
 				break;
 
-			end = find_next_bit(bitmap, end_buf - start_buf,
-					start - start_buf) + start_buf;
+			end = find_next_bit(bitmap, end_buf - page_cluster,
+					    start - page_cluster) + page_cluster;
 
 			aligned_start = ALIGN(ntfs_cluster_to_bytes(vol, start), dq);
 			aligned_end = ALIGN_DOWN(ntfs_cluster_to_bytes(vol, end), dq);
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v2 1/1 ntfs: require page-aligned full-page attribute writes

__ntfs_inode_non_resident_attr_pwrite() treats count == PAGE_SIZE as proof that the request covers the complete folio. it allocates the folio without reading it and copies at offset zero. That proof is valid only when pos is page aligned.

Fixes af0db57d4293 ("ntfs: update inode operations")
Cc Marek Wesołowski <[email protected]>

---
Changes in v2:
- Add Fixes tag.

 fs/ntfs/inode.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/ntfs/inode.c +2 -2
index d5e526d3612a..0391e4945e1c 100644
@@ -3720,7 +3720,7 @@ static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi,
 
 	index = pos >> PAGE_SHIFT;
 	while (count) {
-		if (count == PAGE_SIZE) {
+		if (count == PAGE_SIZE && !offset_in_page(pos)) {
 			folio = __filemap_get_folio(vi->i_mapping, index,
 					FGP_CREAT | FGP_LOCK,
 					mapping_gfp_mask(mapping));
@@ -3742,7 +3742,7 @@ static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi,
 
 		folio_wait_writeback(folio);
 
-		if (count == PAGE_SIZE) {
+		if (count == PAGE_SIZE && !offset_in_page(pos)) {
 			offset = 0;
 			attr_len = count;
 		} else {
Signature · 1 line
2.43.0
maintainer Applied
#2 lore raw

Re: [PATCH v2] ntfs: require page-aligned full-page attribute writes

Quoted 10 lines · Hyunchul Lee

__ntfs_inode_non_resident_attr_pwrite() treats count == PAGE_SIZE as proof that the request covers the complete folio. it allocates the folio without reading it and copies at offset zero. That proof is valid only when pos is page aligned.

Fixes af0db57d4293 ("ntfs: update inode operations")
Cc Marek Wesołowski <[email protected]>

Applied it to #ntfs-next.
Thanks.

maintainer Applied
#3 lore raw
In reply to Hyunchul Lee
Quoted 6 lines · Hyunchul Lee

The LCN bitmap has one bit per cluster, so a bitmap page represents PAGE_SIZE * 8 clusters. ntfs_trim_fs() still used a 15-bit shift when selecting the page. On arm64 with 16 KiB pages this selects the wrong bitmap page and can test or discard the wrong clusters.

Quoted 8 lines

Compute bitmap indices from buf_clusters, use the page cluster base as the bit-search origin, and keep the requested range as the clipping boundary.

Fixes 11ccc9107dc4 ("ntfs: update runlist handling and cluster allocator")
Cc Marek Wesołowski <[email protected]>

Applied it to #ntfs-next.
Thanks.