Skip to content

Commit 4354994

Browse files
drosen-googleJaegeuk Kim
authored andcommitted
f2fs: checkpoint disabling
Note that, it requires "f2fs: return correct errno in f2fs_gc". This adds a lightweight non-persistent snapshotting scheme to f2fs. To use, mount with the option checkpoint=disable, and to return to normal operation, remount with checkpoint=enable. If the filesystem is shut down before remounting with checkpoint=enable, it will revert back to its apparent state when it was first mounted with checkpoint=disable. This is useful for situations where you wish to be able to roll back the state of the disk in case of some critical failure. Signed-off-by: Daniel Rosenberg <drosen@google.com> [Jaegeuk Kim: use SB_RDONLY instead of MS_RDONLY] Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent fb7d70d commit 4354994

13 files changed

Lines changed: 324 additions & 14 deletions

File tree

‎Documentation/filesystems/f2fs.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ fsync_mode=%s Control the policy of fsync. Currently supports "posix",
212212
non-atomic files likewise "nobarrier" mount option.
213213
test_dummy_encryption Enable dummy encryption, which provides a fake fscrypt
214214
context. The fake fscrypt context is used by xfstests.
215+
checkpoint=%s Set to "disable" to turn off checkpointing. Set to "enable"
216+
to reenable checkpointing. Is enabled by default. While
217+
disabled, any unmounting or unexpected shutdowns will cause
218+
the filesystem contents to appear as they did when the
219+
filesystem was mounted with that option.
215220

216221
================================================================================
217222
DEBUGFS ENTRIES

‎fs/f2fs/checkpoint.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,11 @@ static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
12101210
if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
12111211
__set_ckpt_flags(ckpt, CP_FSCK_FLAG);
12121212

1213+
if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
1214+
__set_ckpt_flags(ckpt, CP_DISABLED_FLAG);
1215+
else
1216+
__clear_ckpt_flags(ckpt, CP_DISABLED_FLAG);
1217+
12131218
/* set this flag to activate crc|cp_ver for recovery */
12141219
__set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
12151220
__clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
@@ -1417,6 +1422,7 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
14171422

14181423
clear_sbi_flag(sbi, SBI_IS_DIRTY);
14191424
clear_sbi_flag(sbi, SBI_NEED_CP);
1425+
sbi->unusable_block_count = 0;
14201426
__set_cp_next_pack(sbi);
14211427

14221428
/*
@@ -1441,6 +1447,12 @@ int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
14411447
unsigned long long ckpt_ver;
14421448
int err = 0;
14431449

1450+
if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1451+
if (cpc->reason != CP_PAUSE)
1452+
return 0;
1453+
f2fs_msg(sbi->sb, KERN_WARNING,
1454+
"Start checkpoint disabled!");
1455+
}
14441456
mutex_lock(&sbi->cp_mutex);
14451457

14461458
if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&

‎fs/f2fs/data.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,8 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
537537
if (fio->in_list)
538538
goto next;
539539
out:
540-
if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN))
540+
if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
541+
f2fs_is_checkpoint_ready(sbi))
541542
__submit_merged_bio(io);
542543
up_write(&io->io_rwsem);
543544
}
@@ -1703,6 +1704,10 @@ static inline bool check_inplace_update_policy(struct inode *inode,
17031704
is_inode_flag_set(inode, FI_NEED_IPU))
17041705
return true;
17051706

1707+
if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1708+
!f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1709+
return true;
1710+
17061711
return false;
17071712
}
17081713

@@ -1733,6 +1738,9 @@ bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
17331738
return true;
17341739
if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
17351740
return true;
1741+
if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1742+
f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1743+
return true;
17361744
}
17371745
return false;
17381746
}
@@ -2353,6 +2361,10 @@ static int f2fs_write_begin(struct file *file, struct address_space *mapping,
23532361

23542362
trace_f2fs_write_begin(inode, pos, len, flags);
23552363

2364+
err = f2fs_is_checkpoint_ready(sbi);
2365+
if (err)
2366+
goto fail;
2367+
23562368
if ((f2fs_is_atomic_file(inode) &&
23572369
!f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
23582370
is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {

‎fs/f2fs/debug.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ static int stat_show(struct seq_file *s, void *v)
272272
seq_printf(s, "\n=====[ partition info(%pg). #%d, %s, CP: %s]=====\n",
273273
si->sbi->sb->s_bdev, i++,
274274
f2fs_readonly(si->sbi->sb) ? "RO": "RW",
275-
f2fs_cp_error(si->sbi) ? "Error": "Good");
275+
is_set_ckpt_flags(si->sbi, CP_DISABLED_FLAG) ?
276+
"Disabled": (f2fs_cp_error(si->sbi) ? "Error": "Good"));
276277
seq_printf(s, "[SB: 1] [CP: 2] [SIT: %d] [NAT: %d] ",
277278
si->sit_area_segs, si->nat_area_segs);
278279
seq_printf(s, "[SSA: %d] [MAIN: %d",

‎fs/f2fs/f2fs.h‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ extern char *f2fs_fault_name[FAULT_MAX];
9999
#define F2FS_MOUNT_QUOTA 0x00400000
100100
#define F2FS_MOUNT_INLINE_XATTR_SIZE 0x00800000
101101
#define F2FS_MOUNT_RESERVE_ROOT 0x01000000
102+
#define F2FS_MOUNT_DISABLE_CHECKPOINT 0x02000000
102103

103104
#define F2FS_OPTION(sbi) ((sbi)->mount_opt)
104105
#define clear_opt(sbi, option) (F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
@@ -178,6 +179,7 @@ enum {
178179
#define CP_RECOVERY 0x00000008
179180
#define CP_DISCARD 0x00000010
180181
#define CP_TRIMMED 0x00000020
182+
#define CP_PAUSE 0x00000040
181183

182184
#define MAX_DISCARD_BLOCKS(sbi) BLKS_PER_SEC(sbi)
183185
#define DEF_MAX_DISCARD_REQUEST 8 /* issue 8 discards per round */
@@ -187,6 +189,7 @@ enum {
187189
#define DEF_DISCARD_URGENT_UTIL 80 /* do more discard over 80% */
188190
#define DEF_CP_INTERVAL 60 /* 60 secs */
189191
#define DEF_IDLE_INTERVAL 5 /* 5 secs */
192+
#define DEF_DISABLE_INTERVAL 5 /* 5 secs */
190193

191194
struct cp_control {
192195
int reason;
@@ -1092,13 +1095,15 @@ enum {
10921095
SBI_NEED_CP, /* need to checkpoint */
10931096
SBI_IS_SHUTDOWN, /* shutdown by ioctl */
10941097
SBI_IS_RECOVERED, /* recovered orphan/data */
1098+
SBI_CP_DISABLED, /* CP was disabled last mount */
10951099
};
10961100

10971101
enum {
10981102
CP_TIME,
10991103
REQ_TIME,
11001104
DISCARD_TIME,
11011105
GC_TIME,
1106+
DISABLE_TIME,
11021107
MAX_TIME,
11031108
};
11041109

@@ -1225,6 +1230,9 @@ struct f2fs_sb_info {
12251230
block_t reserved_blocks; /* configurable reserved blocks */
12261231
block_t current_reserved_blocks; /* current reserved blocks */
12271232

1233+
/* Additional tracking for no checkpoint mode */
1234+
block_t unusable_block_count; /* # of blocks saved by last cp */
1235+
12281236
unsigned int nquota_files; /* # of quota sysfile */
12291237

12301238
u32 s_next_generation; /* for NFS support */
@@ -1735,7 +1743,8 @@ static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
17351743

17361744
if (!__allow_reserved_blocks(sbi, inode, true))
17371745
avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
1738-
1746+
if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1747+
avail_user_block_count -= sbi->unusable_block_count;
17391748
if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
17401749
diff = sbi->total_valid_block_count - avail_user_block_count;
17411750
if (diff > *count)
@@ -1942,6 +1951,8 @@ static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
19421951

19431952
if (!__allow_reserved_blocks(sbi, inode, false))
19441953
valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
1954+
if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1955+
valid_block_count += sbi->unusable_block_count;
19451956

19461957
if (unlikely(valid_block_count > sbi->user_block_count)) {
19471958
spin_unlock(&sbi->stat_lock);
@@ -2945,6 +2956,8 @@ void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
29452956
bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
29462957
void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
29472958
struct cp_control *cpc);
2959+
void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
2960+
int f2fs_disable_cp_again(struct f2fs_sb_info *sbi);
29482961
void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
29492962
int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
29502963
void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
@@ -3532,6 +3545,9 @@ static inline bool f2fs_force_buffered_io(struct inode *inode,
35323545
if (test_opt(sbi, LFS) && (rw == WRITE) &&
35333546
block_unaligned_IO(inode, iocb, iter))
35343547
return true;
3548+
if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED))
3549+
return true;
3550+
35353551
return false;
35363552
}
35373553

‎fs/f2fs/file.c‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
210210
};
211211
unsigned int seq_id = 0;
212212

213-
if (unlikely(f2fs_readonly(inode->i_sb)))
213+
if (unlikely(f2fs_readonly(inode->i_sb) ||
214+
is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
214215
return 0;
215216

216217
trace_f2fs_sync_file_enter(inode);
@@ -2157,6 +2158,12 @@ static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
21572158
if (f2fs_readonly(sbi->sb))
21582159
return -EROFS;
21592160

2161+
if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2162+
f2fs_msg(sbi->sb, KERN_INFO,
2163+
"Skipping Checkpoint. Checkpoints currently disabled.");
2164+
return -EINVAL;
2165+
}
2166+
21602167
ret = mnt_want_write_file(filp);
21612168
if (ret)
21622169
return ret;
@@ -2528,6 +2535,9 @@ static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
25282535
if (f2fs_readonly(sbi->sb))
25292536
return -EROFS;
25302537

2538+
if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2539+
return -EINVAL;
2540+
25312541
if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
25322542
sizeof(range)))
25332543
return -EFAULT;

‎fs/f2fs/gc.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ static int get_victim_by_default(struct f2fs_sb_info *sbi,
370370

371371
if (sec_usage_check(sbi, secno))
372372
goto next;
373+
/* Don't touch checkpointed data */
374+
if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
375+
get_ckpt_valid_blocks(sbi, segno)))
376+
goto next;
373377
if (gc_type == BG_GC && test_bit(secno, dirty_i->victim_secmap))
374378
goto next;
375379

@@ -1189,7 +1193,8 @@ int f2fs_gc(struct f2fs_sb_info *sbi, bool sync,
11891193
* threshold, we can make them free by checkpoint. Then, we
11901194
* secure free segments which doesn't need fggc any more.
11911195
*/
1192-
if (prefree_segments(sbi)) {
1196+
if (prefree_segments(sbi) &&
1197+
!is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
11931198
ret = f2fs_write_checkpoint(sbi, &cpc);
11941199
if (ret)
11951200
goto stop;
@@ -1241,7 +1246,7 @@ int f2fs_gc(struct f2fs_sb_info *sbi, bool sync,
12411246
segno = NULL_SEGNO;
12421247
goto gc_more;
12431248
}
1244-
if (gc_type == FG_GC)
1249+
if (gc_type == FG_GC && !is_sbi_flag_set(sbi, SBI_CP_DISABLED))
12451250
ret = f2fs_write_checkpoint(sbi, &cpc);
12461251
}
12471252
stop:

‎fs/f2fs/inode.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,9 @@ int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
607607
if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
608608
return 0;
609609

610+
if (f2fs_is_checkpoint_ready(sbi))
611+
return -ENOSPC;
612+
610613
/*
611614
* We need to balance fs here to prevent from producing dirty node pages
612615
* during the urgent cleaning time when runing out of free sections.
@@ -688,7 +691,8 @@ void f2fs_evict_inode(struct inode *inode)
688691
stat_dec_inline_dir(inode);
689692
stat_dec_inline_inode(inode);
690693

691-
if (likely(!is_set_ckpt_flags(sbi, CP_ERROR_FLAG)))
694+
if (likely(!is_set_ckpt_flags(sbi, CP_ERROR_FLAG) &&
695+
!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
692696
f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
693697
else
694698
f2fs_inode_synced(inode);

‎fs/f2fs/namei.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "f2fs.h"
1818
#include "node.h"
19+
#include "segment.h"
1920
#include "xattr.h"
2021
#include "acl.h"
2122
#include <trace/events/f2fs.h>
@@ -269,6 +270,9 @@ static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
269270

270271
if (unlikely(f2fs_cp_error(sbi)))
271272
return -EIO;
273+
err = f2fs_is_checkpoint_ready(sbi);
274+
if (err)
275+
return err;
272276

273277
err = dquot_initialize(dir);
274278
if (err)
@@ -315,6 +319,9 @@ static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
315319

316320
if (unlikely(f2fs_cp_error(sbi)))
317321
return -EIO;
322+
err = f2fs_is_checkpoint_ready(sbi);
323+
if (err)
324+
return err;
318325

319326
err = fscrypt_prepare_link(old_dentry, dir, dentry);
320327
if (err)
@@ -561,6 +568,9 @@ static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
561568

562569
if (unlikely(f2fs_cp_error(sbi)))
563570
return -EIO;
571+
err = f2fs_is_checkpoint_ready(sbi);
572+
if (err)
573+
return err;
564574

565575
err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
566576
&disk_link);
@@ -690,6 +700,9 @@ static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
690700

691701
if (unlikely(f2fs_cp_error(sbi)))
692702
return -EIO;
703+
err = f2fs_is_checkpoint_ready(sbi);
704+
if (err)
705+
return err;
693706

694707
err = dquot_initialize(dir);
695708
if (err)
@@ -824,6 +837,9 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
824837

825838
if (unlikely(f2fs_cp_error(sbi)))
826839
return -EIO;
840+
err = f2fs_is_checkpoint_ready(sbi);
841+
if (err)
842+
return err;
827843

828844
if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
829845
(!projid_eq(F2FS_I(new_dir)->i_projid,
@@ -1014,6 +1030,9 @@ static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
10141030

10151031
if (unlikely(f2fs_cp_error(sbi)))
10161032
return -EIO;
1033+
err = f2fs_is_checkpoint_ready(sbi);
1034+
if (err)
1035+
return err;
10171036

10181037
if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
10191038
!projid_eq(F2FS_I(new_dir)->i_projid,

0 commit comments

Comments
 (0)