Skip to contentSkip to conversation

sload.f2fs: fix bugs in f2fs_write_block()

PATCH New Jaegeuk Kim 1 participant 4 messages Oct 31, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 sload.f2fs: fix bugs in f2fs_write_block()

From: Hyojun Kim <[email protected]>

Found and fixed following three bugs in f2fs_write_block() function.
- Write (4096 - offset) bytes for the first block even for small count.
- For overwriting, found blkaddr is not used for writing.
- dn.idirty status can be lost by set_new_dnode().
- missing inode_checksum

---
 fsck/segment.c | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)
fsck/segment.c +21 -15
index d568d61..2ea5bf1 100644
@@ -16,6 +16,14 @@
 #include "fsck.h"
 #include "node.h"
 
+static void write_inode(u64 blkaddr, struct f2fs_node *inode)
+{
+	if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
+		inode->i.i_inode_checksum =
+			cpu_to_le32(f2fs_inode_chksum(inode));
+	ASSERT(dev_write_block(inode, blkaddr) >= 0);
+}
+
 void reserve_new_block(struct f2fs_sb_info *sbi, block_t *to,
 			struct f2fs_summary *sum, int type)
 {
@@ -71,6 +79,7 @@ static void f2fs_write_block(struct f2fs_sb_info *sbi, nid_t ino, void *buffer,
 	void *data_blk;
 	struct node_info ni;
 	struct f2fs_node *inode;
+	int idirty = 0;
 	int ret = -1;
 
 	get_node_info(sbi, ino, &ni);
@@ -86,6 +95,8 @@ static void f2fs_write_block(struct f2fs_sb_info *sbi, nid_t ino, void *buffer,
 
 	off_in_block = offset & ((1 << F2FS_BLKSIZE_BITS) - 1);
 	len_in_block = (1 << F2FS_BLKSIZE_BITS) - off_in_block;
+	if (len_in_block > count)
+		len_in_block = count;
 	len_already = 0;
 
 	/*
@@ -115,17 +126,20 @@ static void f2fs_write_block(struct f2fs_sb_info *sbi, nid_t ino, void *buffer,
 			blkaddr = datablock_addr(dn.node_blk, dn.ofs_in_node);
 
 			/* A new page from WARM_DATA */
-			if (blkaddr == NULL_ADDR)
+			if (blkaddr == NULL_ADDR) {
 				new_data_block(sbi, data_blk, &dn,
 							CURSEG_WARM_DATA);
+				blkaddr = dn.data_blkaddr;
+				idirty |= dn.idirty;
+			}
 
 			/* Copy data from buffer to file */
-			ret = dev_read_block(data_blk, dn.data_blkaddr);
+			ret = dev_read_block(data_blk, blkaddr);
 			ASSERT(ret >= 0);
 
 			memcpy(data_blk + off_in_block, buffer, len_in_block);
 
-			ret = dev_write_block(data_blk, dn.data_blkaddr);
+			ret = dev_write_block(data_blk, blkaddr);
 			ASSERT(ret >= 0);
 
 			off_in_block = 0;
@@ -148,13 +162,12 @@ static void f2fs_write_block(struct f2fs_sb_info *sbi, nid_t ino, void *buffer,
 	/* Update the inode info */
 	if (le64_to_cpu(inode->i.i_size) < offset + count) {
 		inode->i.i_size = cpu_to_le64(offset + count);
-		dn.idirty = 1;
+		idirty = 1;
 	}
 
-	if (dn.idirty) {
+	if (idirty) {
 		ASSERT(inode == dn.inode_blk);
-		ret = dev_write_block(inode, ni.blk_addr);
-		ASSERT(ret >= 0);
+		write_inode(ni.blk_addr, inode);
 	}
 
 	if (dn.node_blk && dn.node_blk != dn.inode_blk)
@@ -203,15 +216,8 @@ int f2fs_build_file(struct f2fs_sb_info *sbi, struct dentry *de)
 		n = read(fd, buffer, BLOCK_SZ);
 		ASSERT(n == de->size);
 		memcpy(inline_data_addr(node_blk), buffer, de->size);
-
 		node_blk->i.i_size = cpu_to_le64(de->size);
-
-		if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
-			node_blk->i.i_inode_checksum =
-				cpu_to_le32(f2fs_inode_chksum(node_blk));
-
-		ret = dev_write_block(node_blk, ni.blk_addr);
-		ASSERT(ret >= 0);
+		write_inode(ni.blk_addr, node_blk);
 		free(node_blk);
 	} else {
 		while ((n = read(fd, buffer, BLOCK_SZ)) > 0) {
Signature · 6 lines
2.14.0.rc1.383.gd1ce394fe2-goog


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
#1 lore raw

PATCH 2/4 f2fs-tools: f2fs_read() and f2fs_filesize_update() are added

From: Hyojun Kim <[email protected]>

This patch adds f2fs_read() and f2fs_filesize_update(). It also refactors f2fs_write_block() and renamed as f2fs_write().

---
 fsck/fsck.h    |   6 ++
 fsck/segment.c | 248 ++++++++++++++++++++++++++++++++++++++-------------------
 2 files changed, 171 insertions(+), 83 deletions(-)
fsck/fsck.h +6 -0
index 1e8ed0b..5628906 100644
@@ -219,6 +219,12 @@ void f2fs_alloc_nid(struct f2fs_sb_info *, nid_t *, int);
 void set_data_blkaddr(struct dnode_of_data *);
 block_t new_node_block(struct f2fs_sb_info *,
 					struct dnode_of_data *, unsigned int);
+
+/* segment.c */
+u64 f2fs_read(struct f2fs_sb_info *, nid_t, void *, u64, pgoff_t);
+u64 f2fs_write(struct f2fs_sb_info *, nid_t, void *, u64, pgoff_t);
+void f2fs_filesize_update(struct f2fs_sb_info *, nid_t, u64);
+
 void get_dnode_of_data(struct f2fs_sb_info *, struct dnode_of_data *,
 					pgoff_t, int);
 void make_dentry_ptr(struct f2fs_dentry_ptr *, struct f2fs_node *, void *, int);
fsck/segment.c +165 -83

283 lines not on this page · raw message

Signature · 6 lines
2.14.0.rc1.383.gd1ce394fe2-goog


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
#2 lore raw

PATCH 3/4 mkfs.f2fs: support quota option in mkfs

In reply to Jaegeuk Kim

From: Hyojun Kim <[email protected]>

This patch let mkfs to handle quota option and create quota files.

---
 include/f2fs_fs.h       |  21 +++-
 include/quota.h         |  77 ++++++++++++++
 mkfs/f2fs_format.c      | 278 +++++++++++++++++++++++++++++++++++++++++++++---
 mkfs/f2fs_format_main.c |   2 +
 4 files changed, 363 insertions(+), 15 deletions(-)
 create mode 100644 include/quota.h
include/f2fs_fs.h +20 -1
index 9f56388..d26b7cf 100644
@@ -458,6 +458,13 @@ enum {
 #define F2FS_NODE_INO(sbi)	(sbi->node_ino_num)
 #define F2FS_META_INO(sbi)	(sbi->meta_ino_num)
 
+#define F2FS_QUOTA_INO		3
+#define F2FS_MAX_QUOTAS		3
+#define QUOTA_DATA(i)		(2)
+#define QUOTA_INO(sb,t)	(le32_to_cpu((sb)->qf_ino[t]))
+
+#define FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
+
 /* This flag is used by node and meta inodes, and by recovery */
 #define GFP_F2FS_ZERO	(GFP_NOFS | __GFP_ZERO)
 
@@ -478,6 +485,7 @@ enum {
 #define F2FS_FEATURE_PRJQUOTA		0x0010
 #define F2FS_FEATURE_INODE_CHKSUM	0x0020
 #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
+#define F2FS_FEATURE_QUOTA_INO		0x0080
 
 #define MAX_VOLUME_NAME		512
 
@@ -528,7 +536,8 @@ struct f2fs_super_block {
 	__u8 encryption_level;		/* versioning level for encryption */
 	__u8 encrypt_pw_salt[16];	/* Salt used for string2key algorithm */
 	struct f2fs_device devs[MAX_DEVICES];	/* device list */
-	__u8 reserved[327];		/* valid reserved region */
+	__le32 qf_ino[F2FS_MAX_QUOTAS];	/* quota inode numbers */
+	__u8 reserved[315];		/* valid reserved region */
 } __attribute__((packed));
 
 /*
@@ -1171,4 +1180,14 @@ static inline __le64 get_cp_crc(struct f2fs_checkpoint *cp)
 	return cpu_to_le64(cp_ver);
 }
 
+static inline int is_qf_ino(struct f2fs_super_block *sb, nid_t ino)
+{
+	int i;
+
+	for (i = 0; i < F2FS_MAX_QUOTAS; i++)
+		if (sb->qf_ino[i] == ino)
+			return 1;
+	return 0;
+}
+
 #endif	/*__F2FS_FS_H */
include/quota.h new file +77 -0

77 lines not on this page · raw message

mkfs/f2fs_format.c +264 -14

419 lines not on this page · raw message

mkfs/f2fs_format_main.c +2 -0
index ef62777..50735d3 100644
@@ -88,6 +88,8 @@ static void parse_feature(const char *features)
 		c.feature |= cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM);
 	} else if (!strcmp(features, "flexible_inline_xattr")) {
 		c.feature |= cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR);
+	} else if (!strcmp(features, "quota")) {
+		c.feature |= cpu_to_le32(F2FS_FEATURE_QUOTA_INO);
 	} else {
 		MSG(0, "Error: Wrong features\n");
 		mkfs_usage();
Signature · 6 lines
2.14.0.rc1.383.gd1ce394fe2-goog


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
#3 lore raw

PATCH 4/4 fsck.f2fs: support quota

In reply to Jaegeuk Kim

From: Hyojun Kim <[email protected]>

This patch let fsck to check and fix quota file contents.

---
 fsck/Makefile.am    |    3 +-
 fsck/common.h       |   30 +
 fsck/dict.c         | 1501 +++++++++++++++++++++++++++++++++++++++++++++++++++
 fsck/dict.h         |  144 +++++
 fsck/dqblk_v2.h     |   31 ++
 fsck/fsck.c         |  105 +++-
 fsck/fsck.h         |   10 +
 fsck/main.c         |   18 +-
 fsck/mkquota.c      |  403 ++++++++++++++
 fsck/mount.c        |   15 +-
 fsck/quotaio.c      |  221 ++++++++
 fsck/quotaio.h      |  255 +++++++++
 fsck/quotaio_tree.c |  679 +++++++++++++++++++++++
 fsck/quotaio_tree.h |   66 +++
 fsck/quotaio_v2.c   |  284 ++++++++++
 fsck/quotaio_v2.h   |   54 ++
 fsck/segment.c      |   22 +-
 include/f2fs_fs.h   |   19 +
 mkfs/f2fs_format.c  |   10 +-
 19 files changed, 3852 insertions(+), 18 deletions(-)
 create mode 100644 fsck/common.h
 create mode 100644 fsck/dict.c
 create mode 100644 fsck/dict.h
 create mode 100644 fsck/dqblk_v2.h
 create mode 100644 fsck/mkquota.c
 create mode 100644 fsck/quotaio.c
 create mode 100644 fsck/quotaio.h
 create mode 100644 fsck/quotaio_tree.c
 create mode 100644 fsck/quotaio_tree.h
 create mode 100644 fsck/quotaio_v2.c
 create mode 100644 fsck/quotaio_v2.h
fsck/Makefile.am +2 -1
index 7abcd00..cf0f7d1 100644
@@ -5,7 +5,8 @@ AM_CFLAGS = -Wall
 sbin_PROGRAMS = fsck.f2fs
 fsck_f2fs_SOURCES = main.c fsck.c dump.c mount.c defrag.c f2fs.h fsck.h $(top_srcdir)/include/f2fs_fs.h	\
 		resize.c										\
-		node.c segment.c dir.c sload.c xattr.c
+		node.c segment.c dir.c sload.c xattr.c \
+		dict.c mkquota.c quotaio.c quotaio_tree.c quotaio_v2.c
 fsck_f2fs_LDADD = ${libselinux_LIBS} ${libuuid_LIBS} $(top_builddir)/lib/libf2fs.la
 
 install-data-hook:
fsck/common.h new file +30 -0
new file mode 100644
index 0000000..19a6ecc
@@ -0,0 +1,30 @@
+/**
+ *
+ * Various things common for all utilities
+ *
+ */
+
+#ifndef __QUOTA_COMMON_H__
+#define __QUOTA_COMMON_H__
+
+#undef DEBUG_QUOTA
+
+#ifndef __attribute__
+# if !defined __GNUC__ || __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
+#  define __attribute__(x)
+# endif
+#endif
+
+#define log_err(format, arg ...)					\
+	fprintf(stderr, "[ERROR] %s:%d:%s:: " format "\n",		\
+		__FILE__, __LINE__, __func__, ## arg)
+
+#ifdef DEBUG_QUOTA
+# define log_debug(format, arg ...)					\
+	fprintf(stderr, "[DEBUG] %s:%d:%s:: " format "\n",		\
+		__FILE__, __LINE__, __func__, ## arg)
+#else
+# define log_debug(...)
+#endif
+
+#endif /* __QUOTA_COMMON_H__ */
fsck/dict.c new file +1501 -0

1,501 lines not on this page · raw message

fsck/dict.h new file +144 -0

144 lines not on this page · raw message

fsck/dqblk_v2.h new file +31 -0
new file mode 100644
index 0000000..d12512a
@@ -0,0 +1,31 @@
+/*
+ * Header file for disk format of new quotafile format
+ *
+ * Jan Kara <[email protected]> - sponsored by SuSE CR
+ */
+
+#ifndef __QUOTA_DQBLK_V2_H__
+#define __QUOTA_DQBLK_V2_H__
+
+#include "quotaio_tree.h"
+
+/* Structure for format specific information */
+struct v2_mem_dqinfo {
+	struct qtree_mem_dqinfo dqi_qtree;
+	unsigned int dqi_flags;		/* Flags set in quotafile */
+	unsigned int dqi_used_entries;	/* Number of entries in file -
+					   updated by scan_dquots */
+	unsigned int dqi_data_blocks;	/* Number of data blocks in file -
+					   updated by scan_dquots */
+};
+
+struct v2_mem_dqblk {
+	long long dqb_off;	/* Offset of dquot in file */
+};
+
+struct quotafile_ops;		/* Will be defined later in quotaio.h */
+
+/* Operations above this format */
+extern struct quotafile_ops quotafile_ops_2;
+
+#endif  /* __QUOTA_DQBLK_V2_H__ */
fsck/fsck.c +102 -3

158 lines not on this page · raw message

fsck/fsck.h +10 -0
index 5628906..7b6ac2b 100644
@@ -13,6 +13,8 @@
 
 #include "f2fs.h"
 
+struct quota_ctx;
+
 #define FSCK_UNMATCHED_EXTENT		0x00000001
 
 enum {
@@ -85,6 +87,8 @@ struct f2fs_fsck {
 	u32 dentry_depth;
 	struct f2fs_nat_entry *entries;
 	u32 nat_valid_inode_cnt;
+
+	struct quota_ctx *qctx;
 };
 
 #define BLOCK_SZ		4096
@@ -118,6 +122,8 @@ enum seg_type {
 struct selabel_handle;
 
 extern int fsck_chk_orphan_node(struct f2fs_sb_info *);
+extern int fsck_chk_quota_node(struct f2fs_sb_info *);
+extern int fsck_chk_quota_files(struct f2fs_sb_info *);
 extern int fsck_chk_node_blk(struct f2fs_sb_info *, struct f2fs_inode *, u32,
 		enum FILE_TYPE, enum NODE_TYPE, u32 *,
 		struct child_info *);
@@ -154,6 +160,8 @@ extern void nullify_nat_entry(struct f2fs_sb_info *, u32);
 extern void rewrite_sit_area_bitmap(struct f2fs_sb_info *);
 extern void build_nat_area_bitmap(struct f2fs_sb_info *);
 extern void build_sit_area_bitmap(struct f2fs_sb_info *);
+extern int f2fs_set_main_bitmap(struct f2fs_sb_info *, u32, int);
+extern int f2fs_set_sit_bitmap(struct f2fs_sb_info *, u32);
 extern void fsck_init(struct f2fs_sb_info *);
 extern int fsck_verify(struct f2fs_sb_info *);
 extern void fsck_free(struct f2fs_sb_info *);
@@ -210,6 +218,8 @@ int f2fs_resize(struct f2fs_sb_info *);
 /* sload.c */
 int f2fs_sload(struct f2fs_sb_info *, const char *, const char *,
 		const char *, struct selabel_handle *);
+
+/* segment.c */
 void reserve_new_block(struct f2fs_sb_info *, block_t *,
 					struct f2fs_summary *, int);
 void new_data_block(struct f2fs_sb_info *, void *,
fsck/main.c +15 -3
index c9411eb..eab213d 100644
@@ -18,6 +18,7 @@
 #include "fsck.h"
 #include <libgen.h>
 #include <ctype.h>
+#include "quotaio.h"
 
 struct f2fs_fsck gfsck;
 
@@ -407,6 +408,7 @@ static void do_fsck(struct f2fs_sb_info *sbi)
 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
 	u32 flag = le32_to_cpu(ckpt->ckpt_flags);
 	u32 blk_cnt;
+	errcode_t ret;
 
 	fsck_init(sbi);
 
@@ -429,8 +431,7 @@ static void do_fsck(struct f2fs_sb_info *sbi)
 				c.fix_on = 1;
 			break;
 		}
-	} else {
-		/*
+	} else { /*
 		 * we can hit this in 3 situations:
 		 *  1. fsck -f, fix_on has already been set to 1 when
 		 *     parsing options;
@@ -443,12 +444,23 @@ static void do_fsck(struct f2fs_sb_info *sbi)
 		c.fix_on = 1;
 	}
 
-	fsck_chk_orphan_node(sbi);
+	fsck_chk_quota_node(sbi);
 
 	/* Traverse all block recursively from root inode */
 	blk_cnt = 1;
+
+	if (c.feature & cpu_to_le32(F2FS_FEATURE_QUOTA_INO)) {
+		ret = quota_init_context(sbi);
+		if (ret) {
+			ASSERT_MSG("quota_init_context failure: %d", ret);
+			return;
+		}
+	}
+	fsck_chk_orphan_node(sbi);
 	fsck_chk_node_blk(sbi, NULL, sbi->root_ino_num,
 			F2FS_FT_DIR, TYPE_INODE, &blk_cnt, NULL);
+	fsck_chk_quota_files(sbi);
+
 	fsck_verify(sbi);
 	fsck_free(sbi);
 }
fsck/mkquota.c new file +403 -0

403 lines not on this page · raw message

fsck/mount.c +11 -4
index 29af3b7..d71e107 100644
@@ -297,6 +297,9 @@ void print_sb_state(struct f2fs_super_block *sb)
 	if (f & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) {
 		MSG(0, "%s", " flexible inline xattr");
 	}
+	if (f & cpu_to_le32(F2FS_FEATURE_QUOTA_INO)) {
+		MSG(0, "%s", " quota ino");
+	}
 	MSG(0, "\n");
 	MSG(0, "Info: superblock encrypt level = %d, salt = ",
 					sb->encryption_level);
@@ -739,7 +742,7 @@ static int f2fs_init_nid_bitmap(struct f2fs_sb_info *sbi)
 	nid_t nid;
 	int i;
 
-	if (!(c.func == SLOAD))
+	if (!(c.func == SLOAD || c.func == FSCK))
 		return 0;
 
 	nm_i->nid_bitmap = (char *)calloc(nid_bitmap_size, 1);
@@ -2159,10 +2162,14 @@ int f2fs_do_mount(struct f2fs_sb_info *sbi)
 	if (c.auto_fix || c.preen_mode) {
 		u32 flag = get_cp(ckpt_flags);
 
-		if (flag & CP_FSCK_FLAG)
+		if (flag & CP_FSCK_FLAG ||
+			(exist_qf_ino(sb) && (!(flag & CP_UMOUNT_FLAG) ||
+						flag & CP_ERROR_FLAG))) {
 			c.fix_on = 1;
-		else if (!c.preen_mode)
+		} else if (!c.preen_mode) {
+			print_cp_state(flag);
 			return 1;
+		}
 	}
 
 	c.bug_on = 0;
@@ -2224,7 +2231,7 @@ void f2fs_do_umount(struct f2fs_sb_info *sbi)
 	unsigned int i;
 
 	/* free nm_info */
-	if (c.func == SLOAD)
+	if (c.func == SLOAD || c.func == FSCK)
 		free(nm_i->nid_bitmap);
 	free(nm_i->nat_bitmap);
 	free(sbi->nm_info);
fsck/quotaio.c new file +221 -0

221 lines not on this page · raw message

fsck/quotaio.h new file +255 -0

255 lines not on this page · raw message

fsck/quotaio_tree.c new file +679 -0

120 lines not on this page · raw message

fsck/quotaio_tree.h new file +66 -0

66 lines not on this page · raw message

fsck/quotaio_v2.c new file +284 -0

284 lines not on this page · raw message

fsck/quotaio_v2.h new file +54 -0

54 lines not on this page · raw message

fsck/segment.c +18 -4
index e13f147..efbd667 100644

Lines of this file are not shown. View the raw message.

include/f2fs_fs.h +19 -0
index d26b7cf..c2b2454 100644

Lines of this file are not shown. View the raw message.

mkfs/f2fs_format.c +7 -3
index c809225..2103f9d 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 6 lines
2.14.0.rc1.383.gd1ce394fe2-goog


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

Part of the diff is not shown here. View the raw message.