Skip to content

Commit 7e65be4

Browse files
author
Jaegeuk Kim
committed
f2fs: add reserved blocks for root user
This patch allows root to reserve some blocks via mount option. "-o reserve_root=N" means N x 4KB-sized blocks for root only. Reviewed-by: Chao Yu <[email protected]> Signed-off-by: Jaegeuk Kim <[email protected]>
1 parent 2c19050 commit 7e65be4

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

fs/f2fs/f2fs.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ extern char *fault_name[FAULT_MAX];
9595
#define F2FS_MOUNT_PRJQUOTA 0x00200000
9696
#define F2FS_MOUNT_QUOTA 0x00400000
9797
#define F2FS_MOUNT_INLINE_XATTR_SIZE 0x00800000
98+
#define F2FS_MOUNT_RESERVE_ROOT 0x01000000
9899

99100
#define clear_opt(sbi, option) ((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option)
100101
#define set_opt(sbi, option) ((sbi)->mount_opt.opt |= F2FS_MOUNT_##option)
@@ -1105,6 +1106,7 @@ struct f2fs_sb_info {
11051106
block_t last_valid_block_count; /* for recovery */
11061107
block_t reserved_blocks; /* configurable reserved blocks */
11071108
block_t current_reserved_blocks; /* current reserved blocks */
1109+
block_t root_reserved_blocks; /* root reserved blocks */
11081110

11091111
unsigned int nquota_files; /* # of quota sysfile */
11101112

@@ -1583,11 +1585,17 @@ static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
15831585
sbi->total_valid_block_count += (block_t)(*count);
15841586
avail_user_block_count = sbi->user_block_count -
15851587
sbi->current_reserved_blocks;
1588+
1589+
if (!(test_opt(sbi, RESERVE_ROOT) && capable(CAP_SYS_RESOURCE)))
1590+
avail_user_block_count -= sbi->root_reserved_blocks;
1591+
15861592
if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
15871593
diff = sbi->total_valid_block_count - avail_user_block_count;
1594+
if (diff > *count)
1595+
diff = *count;
15881596
*count -= diff;
15891597
release = diff;
1590-
sbi->total_valid_block_count = avail_user_block_count;
1598+
sbi->total_valid_block_count -= diff;
15911599
if (!*count) {
15921600
spin_unlock(&sbi->stat_lock);
15931601
percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
@@ -1776,9 +1784,13 @@ static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
17761784

17771785
spin_lock(&sbi->stat_lock);
17781786

1779-
valid_block_count = sbi->total_valid_block_count + 1;
1780-
if (unlikely(valid_block_count + sbi->current_reserved_blocks >
1781-
sbi->user_block_count)) {
1787+
valid_block_count = sbi->total_valid_block_count +
1788+
sbi->current_reserved_blocks + 1;
1789+
1790+
if (!(test_opt(sbi, RESERVE_ROOT) && capable(CAP_SYS_RESOURCE)))
1791+
valid_block_count += sbi->root_reserved_blocks;
1792+
1793+
if (unlikely(valid_block_count > sbi->user_block_count)) {
17821794
spin_unlock(&sbi->stat_lock);
17831795
goto enospc;
17841796
}

fs/f2fs/super.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ enum {
107107
Opt_noextent_cache,
108108
Opt_noinline_data,
109109
Opt_data_flush,
110+
Opt_reserve_root,
110111
Opt_mode,
111112
Opt_io_size_bits,
112113
Opt_fault_injection,
@@ -157,6 +158,7 @@ static match_table_t f2fs_tokens = {
157158
{Opt_noextent_cache, "noextent_cache"},
158159
{Opt_noinline_data, "noinline_data"},
159160
{Opt_data_flush, "data_flush"},
161+
{Opt_reserve_root, "reserve_root=%u"},
160162
{Opt_mode, "mode=%s"},
161163
{Opt_io_size_bits, "io_bits=%u"},
162164
{Opt_fault_injection, "fault_injection=%u"},
@@ -191,6 +193,19 @@ void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
191193
va_end(args);
192194
}
193195

196+
static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
197+
{
198+
block_t limit = (sbi->user_block_count << 1) / 1000;
199+
200+
/* limit is 0.2% */
201+
if (test_opt(sbi, RESERVE_ROOT) && sbi->root_reserved_blocks > limit) {
202+
sbi->root_reserved_blocks = limit;
203+
f2fs_msg(sbi->sb, KERN_INFO,
204+
"Reduce reserved blocks for root = %u",
205+
sbi->root_reserved_blocks);
206+
}
207+
}
208+
194209
static void init_once(void *foo)
195210
{
196211
struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
@@ -488,6 +503,18 @@ static int parse_options(struct super_block *sb, char *options)
488503
case Opt_data_flush:
489504
set_opt(sbi, DATA_FLUSH);
490505
break;
506+
case Opt_reserve_root:
507+
if (args->from && match_int(args, &arg))
508+
return -EINVAL;
509+
if (test_opt(sbi, RESERVE_ROOT)) {
510+
f2fs_msg(sb, KERN_INFO,
511+
"Preserve previous reserve_root=%u",
512+
sbi->root_reserved_blocks);
513+
} else {
514+
sbi->root_reserved_blocks = arg;
515+
set_opt(sbi, RESERVE_ROOT);
516+
}
517+
break;
491518
case Opt_mode:
492519
name = match_strdup(&args[0]);
493520

@@ -1006,7 +1033,10 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
10061033
buf->f_blocks = total_count - start_count;
10071034
buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
10081035
sbi->current_reserved_blocks;
1009-
buf->f_bavail = buf->f_bfree;
1036+
if (buf->f_bfree > sbi->root_reserved_blocks)
1037+
buf->f_bavail = buf->f_bfree - sbi->root_reserved_blocks;
1038+
else
1039+
buf->f_bavail = 0;
10101040

10111041
avail_node_count = sbi->total_node_count - sbi->nquota_files -
10121042
F2FS_RESERVED_NODE_NUM;
@@ -1135,6 +1165,9 @@ static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
11351165
else if (test_opt(sbi, LFS))
11361166
seq_puts(seq, "lfs");
11371167
seq_printf(seq, ",active_logs=%u", sbi->active_logs);
1168+
if (test_opt(sbi, RESERVE_ROOT))
1169+
seq_printf(seq, ",reserve_root=%u",
1170+
sbi->root_reserved_blocks);
11381171
if (F2FS_IO_SIZE_BITS(sbi))
11391172
seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
11401173
#ifdef CONFIG_F2FS_FAULT_INJECTION
@@ -1333,6 +1366,7 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
13331366
sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
13341367
(test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
13351368

1369+
limit_reserve_root(sbi);
13361370
return 0;
13371371
restore_gc:
13381372
if (need_restart_gc) {
@@ -2569,6 +2603,7 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
25692603
sbi->last_valid_block_count = sbi->total_valid_block_count;
25702604
sbi->reserved_blocks = 0;
25712605
sbi->current_reserved_blocks = 0;
2606+
limit_reserve_root(sbi);
25722607

25732608
for (i = 0; i < NR_INODE_TYPE; i++) {
25742609
INIT_LIST_HEAD(&sbi->inode_list[i]);

fs/f2fs/sysfs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
162162
#endif
163163
if (a->struct_type == RESERVED_BLOCKS) {
164164
spin_lock(&sbi->stat_lock);
165-
if (t > (unsigned long)sbi->user_block_count) {
165+
if (t > (unsigned long)(sbi->user_block_count -
166+
sbi->root_reserved_blocks)) {
166167
spin_unlock(&sbi->stat_lock);
167168
return -EINVAL;
168169
}

0 commit comments

Comments
 (0)