Skip to contentSkip to conversation

f2fs: introduce F2FS_FEATURE_LOST_FOUND feature

RFC PATCH Superseded Sheng Yong 2 participants 3 messages Mar 6, 2018 – Mar 7, 2018

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Sheng Yong

author
#0 lore raw

RFC PATCH v3 1/1 f2fs: introduce F2FS_FEATURE_LOST_FOUND feature

This patch introduces a new feature, F2FS_FEATURE_LOST_FOUND, which is set by mkfs. It creates a directory named lost+found, which saves unreachable files. If fsck finds a file which has no parent, or its parent is removed by fsck, the file will be placed under lost+found directory by fsck.

lost+found directory could not be encrypted. As a result, the root directory cannot be encrypted too. So if LOST_FOUND feature is enabled, let's avoid to encrypt root directory.

This patch also introduces a new mount option `test_dummy_encryption' to allow fscrypt to create a fake fscrypt context. This is used by xfstests.

---
v3->v2:
* fix test_dummy_encryption
v2->v1:
* introduce new mount option test_dummy_encryption
* add sysfs entry for LOST_FOUND feature

 fs/f2fs/dir.c   |  6 ++++--
 fs/f2fs/f2fs.h  | 17 +++++++++++++++++
 fs/f2fs/namei.c |  9 ++++++---
 fs/f2fs/super.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 fs/f2fs/sysfs.c |  7 +++++++
 5 files changed, 85 insertions(+), 10 deletions(-)
fs/f2fs/dir.c +4 -2
index 797eb05cb538..7f908be8d525 100644
@@ -361,6 +361,7 @@ struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
 			struct page *dpage)
 {
 	struct page *page;
+	int encrypt = DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(dir));
 	int err;
 
 	if (is_inode_flag_set(inode, FI_NEW_INODE)) {
@@ -387,7 +388,8 @@ struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
 		if (err)
 			goto put_error;
 
-		if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode)) {
+		if ((f2fs_encrypted_inode(dir) || encrypt) &&
+					f2fs_may_encrypt(inode)) {
 			err = fscrypt_inherit_context(dir, inode, page, false);
 			if (err)
 				goto put_error;
@@ -402,7 +404,7 @@ struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
 
 	if (new_name) {
 		init_dent_inode(new_name, page);
-		if (f2fs_encrypted_inode(dir))
+		if (f2fs_encrypted_inode(dir) || encrypt)
 			file_set_enc_name(inode);
 	}
 
fs/f2fs/f2fs.h +17 -0
index f6dc70666ebb..77af79d0376b 100644
@@ -125,6 +125,7 @@ struct f2fs_mount_info {
 #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
 #define F2FS_FEATURE_QUOTA_INO		0x0080
 #define F2FS_FEATURE_INODE_CRTIME	0x0100
+#define F2FS_FEATURE_LOST_FOUND		0x0200
 
 #define F2FS_HAS_FEATURE(sb, mask)					\
 	((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
@@ -1052,6 +1053,15 @@ enum {
 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
 };
 
+#define F2FS_MF_TEST_DUMMY_ENCRYPTION	0x0001
+
+#ifdef CONFIG_F2FS_FS_ENCRYPTION
+#define DUMMY_ENCRYPTION_ENABLED(sbi) (unlikely((sbi)->mount_flags & \
+						F2FS_MF_TEST_DUMMY_ENCRYPTION))
+#else
+#define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
+#endif
+
 struct f2fs_sb_info {
 	struct super_block *sb;			/* pointer to VFS super block */
 	struct proc_dir_entry *s_proc;		/* proc entry */
@@ -1241,6 +1251,8 @@ struct f2fs_sb_info {
 
 	/* segment allocation policy */
 	int alloc_mode;
+
+	unsigned int mount_flags;
 };
 
 #ifdef CONFIG_F2FS_FAULT_INJECTION
@@ -3201,6 +3213,10 @@ static inline bool f2fs_bio_encrypted(struct bio *bio)
 static inline int f2fs_sb_has_##name(struct super_block *sb) \
 { \
 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_##flagname); \
+} \
+static inline int f2fs_sb_set_##name(struct super_block *sb) \
+{ \
+	return F2FS_SET_FEATURE(sb, F2FS_FEATURE_##flagname); \
 }
 
 F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
@@ -3211,6 +3227,7 @@ F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
 F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
 F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
+F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
 
 #ifdef CONFIG_BLK_DEV_ZONED
 static inline int get_blkz_type(struct f2fs_sb_info *sbi,
fs/f2fs/namei.c +6 -3
index 318dfe870cb5..5dd6a112e13d 100644
@@ -78,7 +78,8 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
 	set_inode_flag(inode, FI_NEW_INODE);
 
 	/* If the directory encrypted, then we should encrypt the inode. */
-	if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode))
+	if ((f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
+				f2fs_may_encrypt(inode))
 		f2fs_set_encrypted_inode(inode);
 
 	if (f2fs_sb_has_extra_attr(sbi->sb)) {
@@ -788,10 +789,12 @@ static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
 
 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
 {
-	if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
+	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
+
+	if (unlikely(f2fs_cp_error(sbi)))
 		return -EIO;
 
-	if (f2fs_encrypted_inode(dir)) {
+	if (f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) {
 		int err = fscrypt_get_encryption_info(dir);
 		if (err)
 			return err;
fs/f2fs/super.c +51 -5

110 lines not on this page · raw message

fs/f2fs/sysfs.c +7 -0
index 23a2d8d66c43..53c6785c1916 100644
@@ -116,6 +116,9 @@ static ssize_t features_show(struct f2fs_attr *a,
 	if (f2fs_sb_has_inode_crtime(sb))
 		len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
 				len ? ", " : "", "inode_crtime");
+	if (f2fs_sb_has_lost_found(sb))
+		len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
+				len ? ", " : "", "lost_found");
 	len += snprintf(buf + len, PAGE_SIZE - len, "\n");
 	return len;
 }
@@ -292,6 +295,7 @@ enum feat_id {
 	FEAT_FLEXIBLE_INLINE_XATTR,
 	FEAT_QUOTA_INO,
 	FEAT_INODE_CRTIME,
+	FEAT_LOST_FOUND,
 };
 
 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
@@ -307,6 +311,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
 	case FEAT_FLEXIBLE_INLINE_XATTR:
 	case FEAT_QUOTA_INO:
 	case FEAT_INODE_CRTIME:
+	case FEAT_LOST_FOUND:
 		return snprintf(buf, PAGE_SIZE, "supported\n");
 	}
 	return 0;
@@ -386,6 +391,7 @@ F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
 F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
+F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
 
 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
 static struct attribute *f2fs_attrs[] = {
@@ -441,6 +447,7 @@ static struct attribute *f2fs_feat_attrs[] = {
 	ATTR_LIST(flexible_inline_xattr),
 	ATTR_LIST(quota_ino),
 	ATTR_LIST(inode_crtime),
+	ATTR_LIST(lost_found),
 	NULL,
 };
 
Signature · 6 lines
2.14.1


------------------------------------------------------------------------------
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

[+Cc linux-fscrypt]

Hi Sheng,

Quoted 6 lines · Sheng Yong

This patch introduces a new feature, F2FS_FEATURE_LOST_FOUND, which is set by mkfs. It creates a directory named lost+found, which saves unreachable files. If fsck finds a file which has no parent, or its parent is removed by fsck, the file will be placed under lost+found directory by fsck.

Quoted 7 lines

lost+found directory could not be encrypted. As a result, the root directory cannot be encrypted too. So if LOST_FOUND feature is enabled, let's avoid to encrypt root directory.

This patch also introduces a new mount option `test_dummy_encryption' to allow fscrypt to create a fake fscrypt context. This is used by xfstests.

It would be a bit easier to review if this was split into 2 patches:

1. Add LOST_FOUND feature
2. Add test_dummy_encryption mount option

Quoted 23 lines
fs/f2fs/dir.c +3 -1
index 797eb05cb538..7f908be8d525 100644
@@ -361,6 +361,7 @@ struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
 			struct page *dpage)
 {
 	struct page *page;
+	int encrypt = DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(dir));
 	int err;
 
 	if (is_inode_flag_set(inode, FI_NEW_INODE)) {
@@ -387,7 +388,8 @@ struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
 		if (err)
 			goto put_error;
 
-		if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode)) {
+		if ((f2fs_encrypted_inode(dir) || encrypt) &&
+					f2fs_may_encrypt(inode)) {
 			err = fscrypt_inherit_context(dir, inode, page, false);
 			if (err)
 				goto put_error;

fs/f2fs/dir.c struct page *init_inode_metadata(struct inode *inode, struct inode *dir, · line 410

Quoted 8 lines
fs/f2fs/dir.c +1 -1
@@ -402,7 +404,7 @@ struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
 
 	if (new_name) {
 		init_dent_inode(new_name, page);
-		if (f2fs_encrypted_inode(dir))
+		if (f2fs_encrypted_inode(dir) || encrypt)
 			file_set_enc_name(inode);
 	}
 

'enc_name' should only be set if the parent directory is encrypted, right? So the condition for file_set_enc_name() should stay 'f2fs_encrypted_inode(dir)'.

Quoted 7 lines
fs/f2fs/f2fs.h +2 -0
@@ -719,6 +721,18 @@ static int parse_options(struct super_block *sb, char *options)
 			}
 			kfree(name);
 			break;
+#ifdef CONFIG_F2FS_FS_ENCRYPTION
+		case Opt_test_dummy_encryption:

fs/f2fs/f2fs.h static int parse_options(struct super_block *sb, char *options) · line 735

Quoted 10 lines
fs/f2fs/f2fs.h +10 -0
+			sbi->mount_flags |= F2FS_MF_TEST_DUMMY_ENCRYPTION;
+			f2fs_msg(sb, KERN_INFO,
+					"Test dummy encryption mode enabled");
+			break;
+#else
+		case Opt_test_dummy_encryption:
+			f2fs_msg(sb, KERN_INFO,
+					"Test dummy encryption mount option ignored");
+			break;
+#endif

You could write the 'case Opt_test_dummy_encryption:" and break just once, and put the #ifdef inside it.

Quoted 10 lines
fs/f2fs/f2fs.h +6 -0
@@ -2696,6 +2732,12 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
 		}
 	}
 
+	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !f2fs_readonly(sb) &&
+				!f2fs_sb_has_encrypt(sb)) {
+		f2fs_sb_set_encrypt(sb);
+		recovery = 1;
+	}
+

[...]

Quoted 13 lines
fs/f2fs/f2fs.h +1 -4
 	/* get an inode for meta space */
 	sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
 	if (IS_ERR(sbi->meta_inode)) {
@@ -2870,12 +2912,16 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
 	}
 	kfree(options);
 
-	/* recover broken superblock */
+	/* recover broken superblock or enable encrypt feature */
 	if (recovery) {
-		err = f2fs_commit_super(sbi, true);
-		f2fs_msg(sb, KERN_INFO,
-			"Try to recover %dth superblock, ret: %d",

fs/f2fs/f2fs.h static int f2fs_fill_super(struct super_block *sb, void *data, int silent) · line 2925

Quoted 10 lines
fs/f2fs/f2fs.h +8 -1
-			sbi->valid_super_block ? 1 : 2, err);
+		if (sbi->mount_flags & F2FS_MF_TEST_DUMMY_ENCRYPTION) {
+			f2fs_commit_super(sbi, false);
+		} else {
+			err = f2fs_commit_super(sbi, true);
+			f2fs_msg(sb, KERN_INFO,
+				"Try to recover %dth superblock, ret: %d",
+				sbi->valid_super_block ? 1 : 2, err);
+		}
 	}

How about just not allowing mounting with test_dummy_encryption if the 'encrypt' feature flag isn't already set? That would be simpler. I think it was a mistake that ext4 automatically turns the 'encrypt' flag on when test_dummy_encryption is specified.

- Eric

author
#2 lore raw

Hi, Eric

Quoted 15 lines · Eric Biggers

[+Cc linux-fscrypt]

Hi Sheng,

Quoted 10 lines · Sheng Yong

This patch introduces a new feature, F2FS_FEATURE_LOST_FOUND, which is set by mkfs. It creates a directory named lost+found, which saves unreachable files. If fsck finds a file which has no parent, or its parent is removed by fsck, the file will be placed under lost+found directory by fsck.

lost+found directory could not be encrypted. As a result, the root directory cannot be encrypted too. So if LOST_FOUND feature is enabled, let's avoid to encrypt root directory.

Quoted 9 lines
Quoted 3 lines

This patch also introduces a new mount option `test_dummy_encryption' to allow fscrypt to create a fake fscrypt context. This is used by xfstests.

It would be a bit easier to review if this was split into 2 patches:

1. Add LOST_FOUND feature
2. Add test_dummy_encryption mount option

Thanks for your review, I'll split this into 2 patches.

Quoted 24 lines
Quoted 24 lines
fs/f2fs/dir.c +3 -1
index 797eb05cb538..7f908be8d525 100644
@@ -361,6 +361,7 @@ struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
  			struct page *dpage)
  {
  	struct page *page;
+	int encrypt = DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(dir));
  	int err;
 
  	if (is_inode_flag_set(inode, FI_NEW_INODE)) {
@@ -387,7 +388,8 @@ struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
  		if (err)
  			goto put_error;
 
-		if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode)) {
+		if ((f2fs_encrypted_inode(dir) || encrypt) &&
+					f2fs_may_encrypt(inode)) {
  			err = fscrypt_inherit_context(dir, inode, page, false);
  			if (err)
  				goto put_error;
@@ -402,7 +404,7 @@ struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
 
Quoted 10 lines

fs/f2fs/dir.c struct page *init_inode_metadata(struct inode *inode, struct inode *dir, · line 410

Quoted 7 lines
fs/f2fs/dir.c +1 -1
  	if (new_name) {
  		init_dent_inode(new_name, page);
-		if (f2fs_encrypted_inode(dir))
+		if (f2fs_encrypted_inode(dir) || encrypt)
  			file_set_enc_name(inode);
  	}
 

'enc_name' should only be set if the parent directory is encrypted, right? So the condition for file_set_enc_name() should stay 'f2fs_encrypted_inode(dir)'.

Right, should not set enc_name here :)

Quoted 53 lines
Quoted 8 lines
fs/f2fs/f2fs.h +2 -0
@@ -719,6 +721,18 @@ static int parse_options(struct super_block *sb, char *options)
  			}
  			kfree(name);
  			break;
+#ifdef CONFIG_F2FS_FS_ENCRYPTION
+		case Opt_test_dummy_encryption:

fs/f2fs/f2fs.h static int parse_options(struct super_block *sb, char *options) · line 735

Quoted 10 lines
fs/f2fs/f2fs.h +10 -0
+			sbi->mount_flags |= F2FS_MF_TEST_DUMMY_ENCRYPTION;
+			f2fs_msg(sb, KERN_INFO,
+					"Test dummy encryption mode enabled");
+			break;
+#else
+		case Opt_test_dummy_encryption:
+			f2fs_msg(sb, KERN_INFO,
+					"Test dummy encryption mount option ignored");
+			break;
+#endif

You could write the 'case Opt_test_dummy_encryption:" and break just once, and put the #ifdef inside it.

Quoted 10 lines
fs/f2fs/f2fs.h +6 -0
@@ -2696,6 +2732,12 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
  		}
  	}
 
+	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !f2fs_readonly(sb) &&
+				!f2fs_sb_has_encrypt(sb)) {
+		f2fs_sb_set_encrypt(sb);
+		recovery = 1;
+	}
+

[...]

Quoted 20 lines
  	/* get an inode for meta space */
  	sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
  	if (IS_ERR(sbi->meta_inode)) {
fs/f2fs/f2fs.h +7 -5
@@ -2870,12 +2912,16 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
  	}
  	kfree(options);
 
-	/* recover broken superblock */
+	/* recover broken superblock or enable encrypt feature */
  	if (recovery) {
-		err = f2fs_commit_super(sbi, true);
-		f2fs_msg(sb, KERN_INFO,
-			"Try to recover %dth superblock, ret: %d",
-			sbi->valid_super_block ? 1 : 2, err);
+		if (sbi->mount_flags & F2FS_MF_TEST_DUMMY_ENCRYPTION) {
+			f2fs_commit_super(sbi, false);
+		} else {
+			err = f2fs_commit_super(sbi, true);
+			f2fs_msg(sb, KERN_INFO,
+				"Try to recover %dth superblock, ret: %d",
Quoted 9 lines

fs/f2fs/f2fs.h static int f2fs_fill_super(struct super_block *sb, void *data, int silent) · line 2925

Quoted 3 lines
fs/f2fs/f2fs.h +2 -0
+				sbi->valid_super_block ? 1 : 2, err);
+		}
  	}

How about just not allowing mounting with test_dummy_encryption if the 'encrypt' feature flag isn't already set? That would be simpler. I think it was a mistake that ext4 automatically turns the 'encrypt' flag on when test_dummy_encryption is specified.

OK. It's better to keep ext4 and f2fs behave the same way. I'll fix this in next version.

thanks,
Sheng

Quoted 4 lines

- Eric

.