Skip to contentSkip to conversation

ntfs: validate and reserve bad clusters at mount

PATCH In review Namjae Jeon 2 participants 2 messages Oct 6 – Oct 7 NTFS filesystem

lore
Share & tools
lore

Namjae Jeon

author maintainer
#0 lore raw

PATCH 1/1 ntfs: validate and reserve bad clusters at mount

The cluster allocator only consults $Bitmap and never loads $BadClus:$Bad. A free bitmap bit for a known bad cluster can therefore place that cluster in the free-space cache and make it available for new file allocations.

Load the named nonresident $Bad stream and map every attribute extent before validating full-volume coverage and identity mappings for bad runs. Reserve only free bits in $Bitmap on writable mounts and flush the corrections before the initial free-space scan. Keep bad-cluster counts diagnostic so reservations are accounted for exactly once.

Force read-only mounts with repair advice when $Bad cannot be loaded or validated, and prevent read-write remounts after such errors. Leave the bitmap unchanged on read-only mounts and fail mounts on memory allocation failure. Suppress errors=panic while loading $BadClus so damaged metadata can fall back to read-only.

---
 fs/ntfs/bitmap.c |  53 +++++++++++++++
 fs/ntfs/bitmap.h |   2 +
 fs/ntfs/super.c  | 163 +++++++++++++++++++++++++++++++++++++++++++----
 fs/ntfs/volume.h |   2 +
 4 files changed, 209 insertions(+), 11 deletions(-)
fs/ntfs/bitmap.c +53 -0
index 912fdcca8e01..af86fb1c199b 100644
@@ -12,6 +12,59 @@
 #include "bitmap.h"
 #include "ntfs.h"
 
+/*
+ * Check a $BadClus run against $Bitmap and optionally reserve its free bits.
+ * This is only used during mount, before the free-space scan starts. The
+ * scan will account for the repaired bits, so do not use the regular bitmap
+ * setters, which wait for that scan and assume every bit changes state.
+ * @free_bits counts only bits that were free, including on read-only mounts.
+ */
+int ntfs_bitmap_check_used(struct ntfs_volume *vol, s64 start, s64 count,
+		bool repair, s64 *free_bits)
+{
+	struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
+	const unsigned int bits_per_page = PAGE_SIZE * BITS_PER_BYTE;
+	struct folio *folio;
+	u8 *bitmap;
+	unsigned int bit, end, bits;
+	s64 nr_free;
+
+	if (start < 0 || count <= 0 || start >= vol->nr_clusters ||
+	    count > vol->nr_clusters - start || NVolFreeClusterKnown(vol))
+		return -EINVAL;
+	if (repair && sb_rdonly(vol->sb))
+		return -EROFS;
+
+	while (count) {
+		folio = read_mapping_folio(mapping,
+					   start >> (PAGE_SHIFT + 3), NULL);
+		if (IS_ERR(folio))
+			return PTR_ERR(folio);
+
+		folio_lock(folio);
+		bitmap = kmap_local_folio(folio, 0);
+		bit = start & (bits_per_page - 1);
+		bits = min_t(s64, count, bits_per_page - bit);
+		end = bit + bits;
+		nr_free = 0;
+		for (bit = find_next_zero_bit_le(bitmap, end, bit); bit < end;
+		     bit = find_next_zero_bit_le(bitmap, end, bit + 1)) {
+			nr_free++;
+			if (repair)
+				__set_bit_le(bit, bitmap);
+		}
+		kunmap_local(bitmap);
+		if (repair && nr_free)
+			folio_mark_dirty(folio);
+		folio_unlock(folio);
+		folio_put(folio);
+		*free_bits += nr_free;
+		start += bits;
+		count -= bits;
+	}
+	return 0;
+}
+
 int ntfs_trim_fs(struct ntfs_volume *vol, struct fstrim_range *range)
 {
 	size_t buf_clusters;
fs/ntfs/bitmap.h +2 -0
index 31177fe61da6..eccd0cf0f852 100644
@@ -13,6 +13,8 @@
 #include "volume.h"
 
 int ntfs_trim_fs(struct ntfs_volume *vol, struct fstrim_range *range);
+int ntfs_bitmap_check_used(struct ntfs_volume *vol, s64 start, s64 count,
+		bool repair, s64 *free_bits);
 int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
 		const s64 count, const u8 value, const bool is_rollback);
 
fs/ntfs/super.c +152 -11

231 lines not on this page · raw message

fs/ntfs/volume.h +2 -0
index 0473b602084c..6d9d6eb79a7c 100644
@@ -86,6 +86,7 @@
  * @nls_utf8: NLS table for UTF-8.
  * @free_waitq: Wait queue for threads waiting for free clusters or MFT records.
  * @free_clusters: Track the number of free clusters.
+ * @bad_clusters: Number of clusters recorded in $BadClus:$Bad.
  * @free_mft_records: Track the free mft records.
  * @dirty_clusters: Number of clusters that are dirty.
  * @sparse_compression_unit: Size of compression/sparse unit in clusters.
@@ -123,6 +124,7 @@ struct ntfs_volume {
 	u32 index_record_size_mask;
 	u8 index_record_size_bits;
 	s64 nr_clusters;
+	s64 bad_clusters;
 	s64 mft_lcn;
 	s64 mftmirr_lcn;
 	u64 serial_no;
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Namjae Jeon

The cluster allocator only consults $Bitmap and never loads $BadClus:$Bad. A free bitmap bit for a known bad cluster can therefore place that cluster in the free-space cache and make it available for new file allocations.

Load the named nonresident $Bad stream and map every attribute extent before validating full-volume coverage and identity mappings for bad runs. Reserve only free bits in $Bitmap on writable mounts and flush the corrections before the initial free-space scan. Keep bad-cluster counts diagnostic so reservations are accounted for exactly once.

Quoted 7 lines

Force read-only mounts with repair advice when $Bad cannot be loaded or validated, and prevent read-write remounts after such errors. Leave the bitmap unchanged on read-only mounts and fail mounts on memory allocation failure. Suppress errors=panic while loading $BadClus so damaged metadata can fall back to read-only.

Looks good to me.

Signature · 2 lines
Thanks,
Hyunchul