diff mbox

[3/3] btrfs: Add btrfsck support for nocow compressed prealloc write.

Message ID 1411014138-20124-3-git-send-email-quwenruo@cn.fujitsu.com (mailing list archive)
State New, archived
Headers show

Commit Message

Qu Wenruo Sept. 18, 2014, 4:22 a.m. UTC
Add support for nocow compressed write into preallocated range.

The main change is the following:
1) use disk_bytenr + data_offset to search csum
2) use offset + num_bytes - data_offset to judge if this is a valid
file extent.
3) add file extent item size check,
Since now regular file extent has 2 sizes(one with appended members
and the old one),

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 cmds-check.c | 19 ++++++++++++++++---
 ctree.h      |  1 -
 2 files changed, 16 insertions(+), 4 deletions(-)
diff mbox

Patch

diff --git a/cmds-check.c b/cmds-check.c
index 268e588..7a18ad4 100644
--- a/cmds-check.c
+++ b/cmds-check.c
@@ -1175,12 +1175,17 @@  static int process_file_extent(struct btrfs_root *root,
 		num_bytes = (num_bytes + mask) & ~mask;
 	} else if (extent_type == BTRFS_FILE_EXTENT_REG ||
 		   extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
+		u32 item_size = btrfs_item_size_nr(eb, slot);
 		num_bytes = btrfs_file_extent_num_bytes(eb, fi);
-		disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
+		/* data_offset will fallback to 0 on normal extent */
+		disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi) +
+			      btrfs_ondemand_file_extent_data_offset(eb,
+							slot, fi);
 		extent_offset = btrfs_file_extent_offset(eb, fi);
 		if (num_bytes == 0 || (num_bytes & mask))
 			rec->errors |= I_ERR_BAD_FILE_EXTENT;
-		if (num_bytes + extent_offset >
+		if (num_bytes + extent_offset -
+		    btrfs_ondemand_file_extent_data_offset(eb, slot, fi) >
 		    btrfs_file_extent_ram_bytes(eb, fi))
 			rec->errors |= I_ERR_BAD_FILE_EXTENT;
 		if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
@@ -1190,6 +1195,13 @@  static int process_file_extent(struct btrfs_root *root,
 			rec->errors |= I_ERR_BAD_FILE_EXTENT;
 		if (disk_bytenr > 0)
 			rec->found_size += num_bytes;
+		if (item_size != BTRFS_FILE_EXTENT_SIZE_NORMAL &&
+		    item_size != BTRFS_FILE_EXTENT_SIZE_MAX)
+			rec->errors |= I_ERR_BAD_FILE_EXTENT;
+		if (!btrfs_fs_incompat(root->fs_info,
+				       BTRFS_FEATURE_INCOMPAT_COMPPREALLOC) &&
+		    (item_size == BTRFS_FILE_EXTENT_SIZE_MAX))
+			rec->errors |= I_ERR_BAD_FILE_EXTENT;
 	} else {
 		rec->errors |= I_ERR_BAD_FILE_EXTENT;
 	}
@@ -1198,7 +1210,8 @@  static int process_file_extent(struct btrfs_root *root,
 	if (disk_bytenr > 0) {
 		u64 found;
 		if (btrfs_file_extent_compression(eb, fi))
-			num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
+			num_bytes = btrfs_ondemand_file_extent_data_len(eb,
+					slot, fi);
 		else
 			disk_bytenr += extent_offset;
 
diff --git a/ctree.h b/ctree.h
index ab87133..e11e4d8 100644
--- a/ctree.h
+++ b/ctree.h
@@ -834,7 +834,6 @@  struct __compprealloc_data {
 	__le64 data_len;
 
 } __attribute__ ((__packed__));
-
 #define BTRFS_FILE_EXTENT_SIZE_NORMAL (sizeof(struct btrfs_file_extent_item))
 #define BTRFS_FILE_EXTENT_SIZE_MAX (sizeof(struct btrfs_file_extent_item) + \
 				    sizeof(struct __compprealloc_data))