Skip to contentSkip to conversation

f2fs-tools: fix to skip block allocation for fsynced data

PATCH New Chao Yu 1 participant 1 message Aug 14, 2019

lore
Share & tools
lore

Chao Yu

author
#0 lore raw

PATCH 1/1 f2fs-tools: fix to skip block allocation for fsynced data

Previously, we don't allow block allocation on unclean umounted image, result in failing to repair quota system file.

In this patch, we port most recovery codes from kernel to userspace tools, so that on unclean image, during fsck initialization, we will record all data/node block address we may recover in kernel, and then during allocation of quota file repair, we can skip those blocks to avoid block use conflict.

Eventually, if free space is enough, we can repair the quota system file on an unclean umounted image.

---
 fsck/f2fs.h    |  78 ++++++++++++
 fsck/fsck.h    |   2 +
 fsck/mount.c   | 340 +++++++++++++++++++++++++++++++++++++++++++++++--
 fsck/node.c    |  22 +++-
 fsck/node.h    |  39 ++++++
 fsck/segment.c |  11 +-
 6 files changed, 466 insertions(+), 26 deletions(-)
fsck/f2fs.h +78 -0
index 52b81ff..399c74d 100644
@@ -33,10 +33,63 @@
 		typecheck(unsigned long long, b) &&                     \
 		((long long)((a) - (b)) > 0))
 
+#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+#define container_of(ptr, type, member) ({			\
+	const typeof(((type *)0)->member) * __mptr = (ptr);	\
+	(type *)((char *)__mptr - offsetof(type, member)); })
+
 struct list_head {
 	struct list_head *next, *prev;
 };
 
+static inline void __list_add(struct list_head *new,
+				struct list_head *prev,
+				struct list_head *next)
+{
+	next->prev = new;
+	new->next = next;
+	new->prev = prev;
+	prev->next = new;
+}
+
+static inline void __list_del(struct list_head * prev, struct list_head * next)
+{
+	next->prev = prev;
+	prev->next = next;
+}
+
+static inline void list_del(struct list_head *entry)
+{
+	__list_del(entry->prev, entry->next);
+}
+
+static inline void list_add_tail(struct list_head *new, struct list_head *head)
+{
+	__list_add(new, head->prev, head);
+}
+
+#define LIST_HEAD_INIT(name) { &(name), &(name) }
+
+#define list_entry(ptr, type, member)					\
+		container_of(ptr, type, member)
+
+#define list_first_entry(ptr, type, member)				\
+		list_entry((ptr)->next, type, member)
+
+#define list_next_entry(pos, member)					\
+		list_entry((pos)->member.next, typeof(*(pos)), member)
+
+#define list_for_each_entry(pos, head, member)				\
+	for (pos = list_first_entry(head, typeof(*pos), member);	\
+		&pos->member != (head);					\
+		pos = list_next_entry(pos, member))
+
+#define list_for_each_entry_safe(pos, n, head, member)			\
+	for (pos = list_first_entry(head, typeof(*pos), member),	\
+		n = list_next_entry(pos, member);			\
+		&pos->member != (head);					\
+		pos = n, n = list_next_entry(n, member))
+
 /*
  * indicate meta/data type
  */
@@ -80,9 +133,12 @@ struct f2fs_nm_info {
 
 struct seg_entry {
 	unsigned short valid_blocks;    /* # of valid blocks */
+	unsigned short ckpt_valid_blocks;	/* # of valid blocks last cp, for recovered data/node */
 	unsigned char *cur_valid_map;   /* validity bitmap of blocks */
+	unsigned char *ckpt_valid_map;	/* validity bitmap of blocks last cp, for recovered data/node */
 	unsigned char type;             /* segment type like CURSEG_XXX_TYPE */
 	unsigned char orig_type;        /* segment type like CURSEG_XXX_TYPE */
+	unsigned char ckpt_type;        /* segment type like CURSEG_XXX_TYPE , for recovered data/node */
 	unsigned long long mtime;       /* modification time of the segment */
 	int dirty;
 };
@@ -258,6 +314,17 @@ static inline unsigned int ofs_of_node(struct f2fs_node *node_blk)
 	return flag >> OFFSET_BIT_SHIFT;
 }
 
+static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
+{
+	return le64_to_cpu(cp->checkpoint_ver);
+}
+
+static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
+{
+	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
+	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
+}
+
 static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
 {
 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
@@ -373,6 +440,9 @@ static inline block_t __end_block_addr(struct f2fs_sb_info *sbi)
 #define START_BLOCK(sbi, segno)	(SM_I(sbi)->main_blkaddr +		\
 	((segno) << sbi->log_blocks_per_seg))
 
+#define NEXT_FREE_BLKADDR(sbi, curseg)					\
+	(START_BLOCK(sbi, (curseg)->segno) + (curseg)->next_blkoff)
+
 #define SIT_BLK_CNT(sbi)						\
 	((MAIN_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
 
@@ -392,6 +462,14 @@ static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
 		- (base + 1) + type;
 }
 
+/* for the list of fsync inodes, used only during recovery */
+struct fsync_inode_entry {
+	struct list_head list;	/* list head */
+	nid_t ino;		/* inode number */
+	block_t blkaddr;	/* block address locating the last fsync */
+	block_t last_dentry;	/* block address locating the last dentry */
+};
+
 #define nats_in_cursum(jnl)             (le16_to_cpu(jnl->n_nats))
 #define sits_in_cursum(jnl)             (le16_to_cpu(jnl->n_sits))
 
fsck/fsck.h +2 -0
index f7ddf50..ccf4a39 100644
@@ -230,6 +230,8 @@ extern void sit_dump(struct f2fs_sb_info *, unsigned int, unsigned int);
 extern void ssa_dump(struct f2fs_sb_info *, int, int);
 extern void dump_node(struct f2fs_sb_info *, nid_t, int);
 extern int dump_info_from_blkaddr(struct f2fs_sb_info *, u32);
+extern unsigned int start_bidx_of_node(unsigned int, struct f2fs_node *);
+
 
 /* defrag.c */
 int f2fs_defragment(struct f2fs_sb_info *, u64, u64, u64, int);
fsck/mount.c +327 -13

457 lines not on this page · raw message

fsck/node.c +15 -7
index 6bb5484..d76e3bc 100644
@@ -30,6 +30,16 @@ void f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
 	*nid = i;
 }
 
+void f2fs_release_nid(struct f2fs_sb_info *sbi, nid_t nid)
+{
+	struct f2fs_nm_info *nm_i = NM_I(sbi);
+
+	ASSERT(nid < nm_i->max_nid);
+	ASSERT(f2fs_test_bit(nid, nm_i->nid_bitmap));
+
+	f2fs_clear_bit(nid, nm_i->nid_bitmap);
+}
+
 void set_data_blkaddr(struct dnode_of_data *dn)
 {
 	__le32 *addr_array;
@@ -203,12 +213,6 @@ int get_dnode_of_data(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
 
 	for (i = 1; i <= level; i++) {
 		if (!nids[i] && mode == ALLOC_NODE) {
-			struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
-
-			if (!is_set_ckpt_flags(cp, CP_UMOUNT_FLAG)) {
-				c.alloc_failed = 1;
-				return -EINVAL;
-			}
 			f2fs_alloc_nid(sbi, &nids[i]);
 
 			dn->nid = nids[i];
@@ -216,7 +220,11 @@ int get_dnode_of_data(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
 			/* Function new_node_blk get a new f2fs_node blk and update*/
 			/* We should make sure that dn->node_blk == NULL*/
 			nblk[i] = new_node_block(sbi, dn, noffset[i]);
-			ASSERT(nblk[i]);
+			if (!nblk[i]) {
+				f2fs_release_nid(sbi, nids[i]);
+				c.alloc_failed = 1;
+				return -EINVAL;
+			}
 
 			set_nid(parent, offset[i - 1], nids[i], i == 1);
 		} else {
fsck/node.h +39 -0
index cbf7ed7..d927a09 100644
@@ -103,4 +103,43 @@ static inline int IS_DNODE(struct f2fs_node *node_page)
 	return 1;
 }
 
+static inline nid_t ino_of_node(struct f2fs_node *node_blk)
+{
+	return le32_to_cpu(node_blk->footer.ino);
+}
+
+static inline __u64 cpver_of_node(struct f2fs_node *node_blk)
+{
+	return le64_to_cpu(node_blk->footer.cp_ver);
+}
+
+static inline bool is_recoverable_dnode(struct f2fs_sb_info *sbi,
+						struct f2fs_node *node_blk)
+{
+	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
+	__u64 cp_ver = cur_cp_version(ckpt);
+
+	/* Don't care crc part, if fsck.f2fs sets it. */
+	if (is_set_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG))
+		return (cp_ver << 32) == (cpver_of_node(node_blk) << 32);
+
+	if (is_set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG))
+		cp_ver |= (cur_cp_crc(ckpt) << 32);
+
+	return cp_ver == cpver_of_node(node_blk);
+}
+
+static inline block_t next_blkaddr_of_node(struct f2fs_node *node_blk)
+{
+	return le32_to_cpu(node_blk->footer.next_blkaddr);
+}
+
+static inline int is_node(struct f2fs_node *node_blk, int type)
+{
+	return le32_to_cpu(node_blk->footer.flag) & (1 << type);
+}
+
+#define is_fsync_dnode(node_blk)	is_node(node_blk, FSYNC_BIT_SHIFT)
+#define is_dent_dnode(node_blk)		is_node(node_blk, DENT_BIT_SHIFT)
+
 #endif
fsck/segment.c +5 -6
index b0c6889..e3a90da 100644
@@ -62,6 +62,11 @@ int reserve_new_block(struct f2fs_sb_info *sbi, block_t *to,
 	se->type = type;
 	se->valid_blocks++;
 	f2fs_set_bit(offset, (char *)se->cur_valid_map);
+	if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG)) {
+		se->ckpt_type = type;
+		se->ckpt_valid_blocks++;
+		f2fs_set_bit(offset, (char *)se->ckpt_valid_map);
+	}
 	if (c.func == FSCK) {
 		f2fs_set_main_bitmap(sbi, blkaddr, type);
 		f2fs_set_sit_bitmap(sbi, blkaddr);
@@ -98,14 +103,8 @@ int new_data_block(struct f2fs_sb_info *sbi, void *block,
 	struct f2fs_summary sum;
 	struct node_info ni;
 	unsigned int blkaddr = datablock_addr(dn->node_blk, dn->ofs_in_node);
-	struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
 	int ret;
 
-	if (!is_set_ckpt_flags(cp, CP_UMOUNT_FLAG)) {
-		c.alloc_failed = 1;
-		return -EINVAL;
-	}
-
 	ASSERT(dn->node_blk);
 	memset(block, 0, BLOCK_SZ);
 
Signature · 1 line
2.18.0.rc1