Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 722458db authored by Jaegeuk Kim's avatar Jaegeuk Kim
Browse files

f2fs: support quota sys files



This patch supports hidden quota files in the system, which will be used for
Android. It requires up-to-date f2fs-tools later than v1.9.0.

Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
Signed-off-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
parent e5617e7a
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -616,6 +616,9 @@ int recover_orphan_inodes(struct f2fs_sb_info *sbi)
	block_t start_blk, orphan_blocks, i, j;
	unsigned int s_flags = sbi->sb->s_flags;
	int err = 0;
#ifdef CONFIG_QUOTA
	int quota_enabled;
#endif

	if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
		return 0;
@@ -628,8 +631,9 @@ int recover_orphan_inodes(struct f2fs_sb_info *sbi)
#ifdef CONFIG_QUOTA
	/* Needed for iput() to work correctly and not trash data */
	sbi->sb->s_flags |= MS_ACTIVE;

	/* Turn on quotas so that they are updated correctly */
	f2fs_enable_quota_files(sbi);
	quota_enabled = f2fs_enable_quota_files(sbi, s_flags & MS_RDONLY);
#endif

	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
@@ -657,6 +661,7 @@ int recover_orphan_inodes(struct f2fs_sb_info *sbi)
out:
#ifdef CONFIG_QUOTA
	/* Turn quotas off */
	if (quota_enabled)
		f2fs_quota_off_umount(sbi->sb);
#endif
	sbi->sb->s_flags = s_flags; /* Restore MS_RDONLY status */
+9 −1
Original line number Diff line number Diff line
@@ -1479,6 +1479,13 @@ static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
	return le64_to_cpu(cp->checkpoint_ver);
}

static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
{
	if (type < F2FS_MAX_QUOTAS)
		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
	return 0;
}

static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
{
	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
@@ -2439,6 +2446,7 @@ static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
	return ret;
}

#define sb_rdonly	f2fs_readonly
static inline int f2fs_readonly(struct super_block *sb)
{
	return sb->s_flags & MS_RDONLY;
@@ -2640,7 +2648,7 @@ static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
 */
int f2fs_inode_dirtied(struct inode *inode, bool sync);
void f2fs_inode_synced(struct inode *inode);
void f2fs_enable_quota_files(struct f2fs_sb_info *sbi);
int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
void f2fs_quota_off_umount(struct super_block *sb);
int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
int f2fs_sync_fs(struct super_block *sb, int sync);
+6 −2
Original line number Diff line number Diff line
@@ -582,6 +582,9 @@ int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
	int ret = 0;
	unsigned long s_flags = sbi->sb->s_flags;
	bool need_writecp = false;
#ifdef CONFIG_QUOTA
	int quota_enabled;
#endif

	if (s_flags & MS_RDONLY) {
		f2fs_msg(sbi->sb, KERN_INFO, "orphan cleanup on readonly fs");
@@ -592,7 +595,7 @@ int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
	/* Needed for iput() to work correctly and not trash data */
	sbi->sb->s_flags |= MS_ACTIVE;
	/* Turn on quotas so that they are updated correctly */
	f2fs_enable_quota_files(sbi);
	quota_enabled = f2fs_enable_quota_files(sbi, s_flags & MS_RDONLY);
#endif

	fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
@@ -653,6 +656,7 @@ skip:
out:
#ifdef CONFIG_QUOTA
	/* Turn quotas off */
	if (quota_enabled)
		f2fs_quota_off_umount(sbi->sb);
#endif
	sbi->sb->s_flags = s_flags; /* Restore MS_RDONLY status */
+175 −13
Original line number Diff line number Diff line
@@ -219,6 +219,12 @@ static int f2fs_set_qf_name(struct super_block *sb, int qtype,
			"quota options when quota turned on");
		return -EINVAL;
	}
	if (f2fs_sb_has_quota_ino(sb)) {
		f2fs_msg(sb, KERN_INFO,
			"QUOTA feature is enabled, so ignore qf_name");
		return 0;
	}

	qname = match_strdup(args);
	if (!qname) {
		f2fs_msg(sb, KERN_ERR,
@@ -302,6 +308,18 @@ static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
			return -1;
		}
	}

	if (f2fs_sb_has_quota_ino(sbi->sb) && sbi->s_jquota_fmt) {
		f2fs_msg(sbi->sb, KERN_INFO,
			"QUOTA feature is enabled, so ignore jquota_fmt");
		sbi->s_jquota_fmt = 0;
	}
	if (f2fs_sb_has_quota_ino(sbi->sb) && sb_rdonly(sbi->sb)) {
		f2fs_msg(sbi->sb, KERN_INFO,
			 "Filesystem with quota feature cannot be mounted RDWR "
			 "without CONFIG_QUOTA");
		return -1;
	}
	return 0;
}
#endif
@@ -1188,6 +1206,9 @@ static void default_options(struct f2fs_sb_info *sbi)
#endif
}

#ifdef CONFIG_QUOTA
static int f2fs_enable_quotas(struct super_block *sb);
#endif
static int f2fs_remount(struct super_block *sb, int *flags, char *data)
{
	struct f2fs_sb_info *sbi = F2FS_SB(sb);
@@ -1254,6 +1275,7 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
	if (f2fs_readonly(sb) && (*flags & MS_RDONLY))
		goto skip;

#ifdef CONFIG_QUOTA
	if (!f2fs_readonly(sb) && (*flags & MS_RDONLY)) {
		err = dquot_suspend(sb, -1);
		if (err < 0)
@@ -1261,9 +1283,15 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
	} else {
		/* dquot_resume needs RW */
		sb->s_flags &= ~MS_RDONLY;
		if (sb_any_quota_suspended(sb)) {
			dquot_resume(sb, -1);
		} else if (f2fs_sb_has_quota_ino(sb)) {
			err = f2fs_enable_quotas(sb);
			if (err)
				goto restore_opts;
		}

	}
#endif
	/* disallow enable/disable extent_cache dynamically */
	if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
		err = -EINVAL;
@@ -1465,19 +1493,93 @@ static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
						sbi->s_jquota_fmt, type);
}

void f2fs_enable_quota_files(struct f2fs_sb_info *sbi)
int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
{
	int i, ret;
	int enabled = 0;
	int i, err;

	if (f2fs_sb_has_quota_ino(sbi->sb) && rdonly) {
		err = f2fs_enable_quotas(sbi->sb);
		if (err) {
			f2fs_msg(sbi->sb, KERN_ERR,
					"Cannot turn on quota_ino: %d", err);
			return 0;
		}
		return 1;
	}

	for (i = 0; i < F2FS_MAXQUOTAS; i++) {
		if (sbi->s_qf_names[i]) {
			ret = f2fs_quota_on_mount(sbi, i);
			if (ret < 0)
			err = f2fs_quota_on_mount(sbi, i);
			if (!err) {
				enabled = 1;
				continue;
			}
			f2fs_msg(sbi->sb, KERN_ERR,
					"Cannot turn on journaled "
					"quota: error %d", ret);
				"Cannot turn on quotas: %d on %d", err, i);
		}
	}
	return enabled;
}

static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
			     unsigned int flags)
{
	struct inode *qf_inode;
	unsigned long qf_inum;
	int err;

	BUG_ON(!f2fs_sb_has_quota_ino(sb));

	qf_inum = f2fs_qf_ino(sb, type);
	if (!qf_inum)
		return -EPERM;

	qf_inode = f2fs_iget(sb, qf_inum);
	if (IS_ERR(qf_inode)) {
		f2fs_msg(sb, KERN_ERR,
			"Bad quota inode %u:%lu", type, qf_inum);
		return PTR_ERR(qf_inode);
	}

	/* Don't account quota for quota files to avoid recursion */
	qf_inode->i_flags |= S_NOQUOTA;
	err = dquot_enable(qf_inode, type, format_id, flags);
	iput(qf_inode);
	return err;
}

static int f2fs_enable_quotas(struct super_block *sb)
{
	int type, err = 0;
	unsigned long qf_inum;
	bool quota_mopt[MAXQUOTAS] = {
		test_opt(F2FS_SB(sb), USRQUOTA),
		test_opt(F2FS_SB(sb), GRPQUOTA),
#if 0	/* not support */
		test_opt(F2FS_SB(sb), PRJQUOTA),
#endif
	};

	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
	for (type = 0; type < MAXQUOTAS; type++) {
		qf_inum = f2fs_qf_ino(sb, type);
		if (qf_inum) {
			err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
				DQUOT_USAGE_ENABLED |
				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
			if (err) {
				f2fs_msg(sb, KERN_ERR,
					"Failed to enable quota tracking "
					"(type=%d, err=%d). Please run "
					"fsck to fix.", type, err);
				for (type--; type >= 0; type--)
					dquot_quota_off(sb, type);
				return err;
			}
		}
	}
	return 0;
}

static int f2fs_quota_sync(struct super_block *sb, int type)
@@ -1537,6 +1639,21 @@ static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
	return 0;
}

/*
 * quota_on function that is used when QUOTA feature is set.
 */
static int f2fs_quota_on_sysfile(struct super_block *sb, int type,
				 int format_id)
{
	if (!f2fs_sb_has_quota_ino(sb))
		return -EINVAL;

	/*
	 * USAGE was enabled at mount time. Only need to enable LIMITS now.
	 */
	return f2fs_quota_enable(sb, type, format_id, DQUOT_LIMITS_ENABLED);
}

static int f2fs_quota_off(struct super_block *sb, int type)
{
	struct inode *inode = sb_dqopt(sb)->files[type];
@@ -1548,7 +1665,7 @@ static int f2fs_quota_off(struct super_block *sb, int type)
	f2fs_quota_sync(sb, type);

	err = dquot_quota_off(sb, type);
	if (err)
	if (err || f2fs_sb_has_quota_ino(sb))
		goto out_put;

	inode_lock(inode);
@@ -1561,6 +1678,18 @@ out_put:
	return err;
}

/*
 * quota_off function that is used when QUOTA feature is set.
 */
static int f2fs_quota_off_sysfile(struct super_block *sb, int type)
{
	if (!f2fs_sb_has_quota_ino(sb))
		return -EINVAL;

	/* Disable only the limits. */
	return dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
}

void f2fs_quota_off_umount(struct super_block *sb)
{
	int type;
@@ -1599,6 +1728,16 @@ static const struct quotactl_ops f2fs_quotactl_ops = {
	.get_dqblk	= dquot_get_dqblk,
	.set_dqblk	= dquot_set_dqblk,
};

static const struct quotactl_ops f2fs_quotactl_sysfile_ops = {
	.quota_on_meta	= f2fs_quota_on_sysfile,
	.quota_off	= f2fs_quota_off_sysfile,
	.quota_sync	= dquot_quota_sync,
	.get_info	= dquot_get_dqinfo,
	.set_info	= dquot_set_dqinfo,
	.get_dqblk	= dquot_get_dqblk,
	.set_dqblk	= dquot_set_dqblk
};
#else
void f2fs_quota_off_umount(struct super_block *sb)
{
@@ -2379,6 +2518,9 @@ try_onemore:

#ifdef CONFIG_QUOTA
	sb->dq_op = &f2fs_quota_operations;
	if (f2fs_sb_has_quota_ino(sb))
		sb->s_qcop = &f2fs_quotactl_sysfile_ops;
	else
		sb->s_qcop = &f2fs_quotactl_ops;
#if 0	/* not support */
	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
@@ -2552,10 +2694,24 @@ try_onemore:
	if (err)
		goto free_root_inode;

#ifdef CONFIG_QUOTA
	/*
	 * Turn on quotas which were not enabled for read-only mounts if
	 * filesystem has quota feature, so that they are updated correctly.
	 */
	if (f2fs_sb_has_quota_ino(sb) && !sb_rdonly(sb)) {
		err = f2fs_enable_quotas(sb);
		if (err) {
			f2fs_msg(sb, KERN_ERR,
				"Cannot turn on quotas: error %d", err);
			goto free_sysfs;
		}
	}
#endif
	/* if there are nt orphan nodes free them */
	err = recover_orphan_inodes(sbi);
	if (err)
		goto free_sysfs;
		goto free_meta;

	/* recover fsynced data */
	if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
@@ -2589,7 +2745,7 @@ try_onemore:
			err = -EINVAL;
			f2fs_msg(sb, KERN_ERR,
				"Need to recover fsync data");
			goto free_sysfs;
			goto free_meta;
		}
	}
skip_recovery:
@@ -2623,6 +2779,10 @@ skip_recovery:
	return 0;

free_meta:
#ifdef CONFIG_QUOTA
	if (f2fs_sb_has_quota_ino(sb) && !sb_rdonly(sb))
		f2fs_quota_off_umount(sbi->sb);
#endif
	f2fs_sync_inode_meta(sbi);
	/*
	 * Some dirty meta pages can be produced by recover_orphan_inodes()
@@ -2631,7 +2791,9 @@ free_meta:
	 * falls into an infinite loop in sync_meta_pages().
	 */
	truncate_inode_pages_final(META_MAPPING(sbi));
#ifdef CONFIG_QUOTA
free_sysfs:
#endif
	f2fs_unregister_sysfs(sbi);
free_root_inode:
	dput(sb->s_root);
+0 −1
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@
#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 F2FS_IO_SIZE(sbi)	(1 << (sbi)->write_io_size_bits) /* Blocks */