aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChao Yu <yuchao0@huawei.com>2018-09-20 08:05:00 -0400
committerJaegeuk Kim <jaegeuk@kernel.org>2018-10-22 20:54:47 -0400
commitaf033b2aa8a874fd5737fafe90d159136527b5b4 (patch)
treea038b3b03568cb20fc678a1183f423d330f97028
parent26b5a079197c8cb6725565968b7fd3299bd1877b (diff)
f2fs: guarantee journalled quota data by checkpoint
For journalled quota mode, let checkpoint to flush dquot dirty data and quota file data to guarntee persistence of all quota sysfile in last checkpoint, by this way, we can avoid corrupting quota sysfile when encountering SPO. The implementation is as below: 1. add a global state SBI_QUOTA_NEED_FLUSH to indicate that there is cached dquot metadata changes in quota subsystem, and later checkpoint should: a) flush dquot metadata into quota file. b) flush quota file to storage to keep file usage be consistent. 2. add a global state SBI_QUOTA_NEED_REPAIR to indicate that quota operation failed due to -EIO or -ENOSPC, so later, a) checkpoint will skip syncing dquot metadata. b) CP_QUOTA_NEED_FSCK_FLAG will be set in last cp pack to give a hint for fsck repairing. 3. add a global state SBI_QUOTA_SKIP_FLUSH, in checkpoint, if quota data updating is very heavy, it may cause hungtask in block_operation(). To avoid this, if our retry time exceed threshold, let's just skip flushing and retry in next checkpoint(). Signed-off-by: Weichao Guo <guoweichao@huawei.com> Signed-off-by: Chao Yu <yuchao0@huawei.com> [Jaegeuk Kim: avoid warnings and set fsck flag] Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
-rw-r--r--fs/f2fs/checkpoint.c62
-rw-r--r--fs/f2fs/data.c16
-rw-r--r--fs/f2fs/f2fs.h49
-rw-r--r--fs/f2fs/file.c31
-rw-r--r--fs/f2fs/inline.c4
-rw-r--r--fs/f2fs/inode.c11
-rw-r--r--fs/f2fs/namei.c4
-rw-r--r--fs/f2fs/recovery.c43
-rw-r--r--fs/f2fs/super.c121
-rw-r--r--include/linux/f2fs_fs.h1
10 files changed, 294 insertions, 48 deletions
diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c
index eb6ac79640f8..9c28ea439e0b 100644
--- a/fs/f2fs/checkpoint.c
+++ b/fs/f2fs/checkpoint.c
@@ -1079,6 +1079,21 @@ static void __prepare_cp_block(struct f2fs_sb_info *sbi)
1079 ckpt->next_free_nid = cpu_to_le32(last_nid); 1079 ckpt->next_free_nid = cpu_to_le32(last_nid);
1080} 1080}
1081 1081
1082static bool __need_flush_quota(struct f2fs_sb_info *sbi)
1083{
1084 if (!is_journalled_quota(sbi))
1085 return false;
1086 if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH))
1087 return false;
1088 if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR))
1089 return false;
1090 if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH))
1091 return true;
1092 if (get_pages(sbi, F2FS_DIRTY_QDATA))
1093 return true;
1094 return false;
1095}
1096
1082/* 1097/*
1083 * Freeze all the FS-operations for checkpoint. 1098 * Freeze all the FS-operations for checkpoint.
1084 */ 1099 */
@@ -1090,12 +1105,36 @@ static int block_operations(struct f2fs_sb_info *sbi)
1090 .for_reclaim = 0, 1105 .for_reclaim = 0,
1091 }; 1106 };
1092 struct blk_plug plug; 1107 struct blk_plug plug;
1093 int err = 0; 1108 int err = 0, cnt = 0;
1094 1109
1095 blk_start_plug(&plug); 1110 blk_start_plug(&plug);
1096 1111
1097retry_flush_dents: 1112retry_flush_quotas:
1113 if (__need_flush_quota(sbi)) {
1114 int locked;
1115
1116 if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) {
1117 set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1118 f2fs_lock_all(sbi);
1119 goto retry_flush_dents;
1120 }
1121 clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
1122
1123 /* only failed during mount/umount/freeze/quotactl */
1124 locked = down_read_trylock(&sbi->sb->s_umount);
1125 f2fs_quota_sync(sbi->sb, -1);
1126 if (locked)
1127 up_read(&sbi->sb->s_umount);
1128 }
1129
1098 f2fs_lock_all(sbi); 1130 f2fs_lock_all(sbi);
1131 if (__need_flush_quota(sbi)) {
1132 f2fs_unlock_all(sbi);
1133 cond_resched();
1134 goto retry_flush_quotas;
1135 }
1136
1137retry_flush_dents:
1099 /* write all the dirty dentry pages */ 1138 /* write all the dirty dentry pages */
1100 if (get_pages(sbi, F2FS_DIRTY_DENTS)) { 1139 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
1101 f2fs_unlock_all(sbi); 1140 f2fs_unlock_all(sbi);
@@ -1103,7 +1142,7 @@ retry_flush_dents:
1103 if (err) 1142 if (err)
1104 goto out; 1143 goto out;
1105 cond_resched(); 1144 cond_resched();
1106 goto retry_flush_dents; 1145 goto retry_flush_quotas;
1107 } 1146 }
1108 1147
1109 /* 1148 /*
@@ -1112,6 +1151,12 @@ retry_flush_dents:
1112 */ 1151 */
1113 down_write(&sbi->node_change); 1152 down_write(&sbi->node_change);
1114 1153
1154 if (__need_flush_quota(sbi)) {
1155 up_write(&sbi->node_change);
1156 f2fs_unlock_all(sbi);
1157 goto retry_flush_quotas;
1158 }
1159
1115 if (get_pages(sbi, F2FS_DIRTY_IMETA)) { 1160 if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
1116 up_write(&sbi->node_change); 1161 up_write(&sbi->node_change);
1117 f2fs_unlock_all(sbi); 1162 f2fs_unlock_all(sbi);
@@ -1119,7 +1164,7 @@ retry_flush_dents:
1119 if (err) 1164 if (err)
1120 goto out; 1165 goto out;
1121 cond_resched(); 1166 cond_resched();
1122 goto retry_flush_dents; 1167 goto retry_flush_quotas;
1123 } 1168 }
1124 1169
1125retry_flush_nodes: 1170retry_flush_nodes:
@@ -1215,6 +1260,14 @@ static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1215 else 1260 else
1216 __clear_ckpt_flags(ckpt, CP_DISABLED_FLAG); 1261 __clear_ckpt_flags(ckpt, CP_DISABLED_FLAG);
1217 1262
1263 if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH))
1264 __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1265 else
1266 __clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1267
1268 if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR))
1269 __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1270
1218 /* set this flag to activate crc|cp_ver for recovery */ 1271 /* set this flag to activate crc|cp_ver for recovery */
1219 __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG); 1272 __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1220 __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG); 1273 __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
@@ -1422,6 +1475,7 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1422 1475
1423 clear_sbi_flag(sbi, SBI_IS_DIRTY); 1476 clear_sbi_flag(sbi, SBI_IS_DIRTY);
1424 clear_sbi_flag(sbi, SBI_NEED_CP); 1477 clear_sbi_flag(sbi, SBI_NEED_CP);
1478 clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1425 sbi->unusable_block_count = 0; 1479 sbi->unusable_block_count = 0;
1426 __set_cp_next_pack(sbi); 1480 __set_cp_next_pack(sbi);
1427 1481
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 00b37a1bd15c..106f116466bf 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -46,7 +46,7 @@ static bool __is_cp_guaranteed(struct page *page)
46 inode->i_ino == F2FS_NODE_INO(sbi) || 46 inode->i_ino == F2FS_NODE_INO(sbi) ||
47 S_ISDIR(inode->i_mode) || 47 S_ISDIR(inode->i_mode) ||
48 (S_ISREG(inode->i_mode) && 48 (S_ISREG(inode->i_mode) &&
49 is_inode_flag_set(inode, FI_ATOMIC_FILE)) || 49 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
50 is_cold_data(page)) 50 is_cold_data(page))
51 return true; 51 return true;
52 return false; 52 return false;
@@ -1766,6 +1766,8 @@ bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
1766 return true; 1766 return true;
1767 if (S_ISDIR(inode->i_mode)) 1767 if (S_ISDIR(inode->i_mode))
1768 return true; 1768 return true;
1769 if (IS_NOQUOTA(inode))
1770 return true;
1769 if (f2fs_is_atomic_file(inode)) 1771 if (f2fs_is_atomic_file(inode))
1770 return true; 1772 return true;
1771 if (fio) { 1773 if (fio) {
@@ -2016,7 +2018,7 @@ out:
2016 } 2018 }
2017 2019
2018 unlock_page(page); 2020 unlock_page(page);
2019 if (!S_ISDIR(inode->i_mode)) 2021 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode))
2020 f2fs_balance_fs(sbi, need_balance_fs); 2022 f2fs_balance_fs(sbi, need_balance_fs);
2021 2023
2022 if (unlikely(f2fs_cp_error(sbi))) { 2024 if (unlikely(f2fs_cp_error(sbi))) {
@@ -2207,6 +2209,8 @@ static inline bool __should_serialize_io(struct inode *inode,
2207{ 2209{
2208 if (!S_ISREG(inode->i_mode)) 2210 if (!S_ISREG(inode->i_mode))
2209 return false; 2211 return false;
2212 if (IS_NOQUOTA(inode))
2213 return false;
2210 if (wbc->sync_mode != WB_SYNC_ALL) 2214 if (wbc->sync_mode != WB_SYNC_ALL)
2211 return true; 2215 return true;
2212 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks) 2216 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
@@ -2236,7 +2240,8 @@ static int __f2fs_write_data_pages(struct address_space *mapping,
2236 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 2240 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2237 goto skip_write; 2241 goto skip_write;
2238 2242
2239 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE && 2243 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
2244 wbc->sync_mode == WB_SYNC_NONE &&
2240 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && 2245 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
2241 f2fs_available_free_memory(sbi, DIRTY_DENTS)) 2246 f2fs_available_free_memory(sbi, DIRTY_DENTS))
2242 goto skip_write; 2247 goto skip_write;
@@ -2301,7 +2306,7 @@ static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2301 down_write(&F2FS_I(inode)->i_mmap_sem); 2306 down_write(&F2FS_I(inode)->i_mmap_sem);
2302 2307
2303 truncate_pagecache(inode, i_size); 2308 truncate_pagecache(inode, i_size);
2304 f2fs_truncate_blocks(inode, i_size, true); 2309 f2fs_truncate_blocks(inode, i_size, true, true);
2305 2310
2306 up_write(&F2FS_I(inode)->i_mmap_sem); 2311 up_write(&F2FS_I(inode)->i_mmap_sem);
2307 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 2312 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
@@ -2440,7 +2445,8 @@ repeat:
2440 if (err) 2445 if (err)
2441 goto fail; 2446 goto fail;
2442 2447
2443 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) { 2448 if (need_balance && !IS_NOQUOTA(inode) &&
2449 has_not_enough_free_secs(sbi, 0, 0)) {
2444 unlock_page(page); 2450 unlock_page(page);
2445 f2fs_balance_fs(sbi, true); 2451 f2fs_balance_fs(sbi, true);
2446 lock_page(page); 2452 lock_page(page);
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 5c80eca194b5..f447cbc2295f 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -531,6 +531,9 @@ enum {
531 531
532#define DEFAULT_RETRY_IO_COUNT 8 /* maximum retry read IO count */ 532#define DEFAULT_RETRY_IO_COUNT 8 /* maximum retry read IO count */
533 533
534/* maximum retry quota flush count */
535#define DEFAULT_RETRY_QUOTA_FLUSH_COUNT 8
536
534#define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */ 537#define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
535 538
536#define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */ 539#define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
@@ -1099,6 +1102,9 @@ enum {
1099 SBI_IS_SHUTDOWN, /* shutdown by ioctl */ 1102 SBI_IS_SHUTDOWN, /* shutdown by ioctl */
1100 SBI_IS_RECOVERED, /* recovered orphan/data */ 1103 SBI_IS_RECOVERED, /* recovered orphan/data */
1101 SBI_CP_DISABLED, /* CP was disabled last mount */ 1104 SBI_CP_DISABLED, /* CP was disabled last mount */
1105 SBI_QUOTA_NEED_FLUSH, /* need to flush quota info in CP */
1106 SBI_QUOTA_SKIP_FLUSH, /* skip flushing quota in current CP */
1107 SBI_QUOTA_NEED_REPAIR, /* quota file may be corrupted */
1102}; 1108};
1103 1109
1104enum { 1110enum {
@@ -1923,12 +1929,18 @@ static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1923{ 1929{
1924 block_t valid_block_count; 1930 block_t valid_block_count;
1925 unsigned int valid_node_count; 1931 unsigned int valid_node_count;
1926 bool quota = inode && !is_inode; 1932 int err;
1927 1933
1928 if (quota) { 1934 if (is_inode) {
1929 int ret = dquot_reserve_block(inode, 1); 1935 if (inode) {
1930 if (ret) 1936 err = dquot_alloc_inode(inode);
1931 return ret; 1937 if (err)
1938 return err;
1939 }
1940 } else {
1941 err = dquot_reserve_block(inode, 1);
1942 if (err)
1943 return err;
1932 } 1944 }
1933 1945
1934 if (time_to_inject(sbi, FAULT_BLOCK)) { 1946 if (time_to_inject(sbi, FAULT_BLOCK)) {
@@ -1972,8 +1984,12 @@ static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1972 return 0; 1984 return 0;
1973 1985
1974enospc: 1986enospc:
1975 if (quota) 1987 if (is_inode) {
1988 if (inode)
1989 dquot_free_inode(inode);
1990 } else {
1976 dquot_release_reservation_block(inode, 1); 1991 dquot_release_reservation_block(inode, 1);
1992 }
1977 return -ENOSPC; 1993 return -ENOSPC;
1978} 1994}
1979 1995
@@ -1994,7 +2010,9 @@ static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1994 2010
1995 spin_unlock(&sbi->stat_lock); 2011 spin_unlock(&sbi->stat_lock);
1996 2012
1997 if (!is_inode) 2013 if (is_inode)
2014 dquot_free_inode(inode);
2015 else
1998 f2fs_i_blocks_write(inode, 1, false, true); 2016 f2fs_i_blocks_write(inode, 1, false, true);
1999} 2017}
2000 2018
@@ -2782,7 +2800,8 @@ static inline bool is_valid_data_blkaddr(struct f2fs_sb_info *sbi,
2782 */ 2800 */
2783int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync); 2801int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2784void f2fs_truncate_data_blocks(struct dnode_of_data *dn); 2802void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
2785int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock); 2803int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock,
2804 bool buf_write);
2786int f2fs_truncate(struct inode *inode); 2805int f2fs_truncate(struct inode *inode);
2787int f2fs_getattr(const struct path *path, struct kstat *stat, 2806int f2fs_getattr(const struct path *path, struct kstat *stat,
2788 u32 request_mask, unsigned int flags); 2807 u32 request_mask, unsigned int flags);
@@ -2870,6 +2889,7 @@ static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2870int f2fs_inode_dirtied(struct inode *inode, bool sync); 2889int f2fs_inode_dirtied(struct inode *inode, bool sync);
2871void f2fs_inode_synced(struct inode *inode); 2890void f2fs_inode_synced(struct inode *inode);
2872int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly); 2891int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
2892int f2fs_quota_sync(struct super_block *sb, int type);
2873void f2fs_quota_off_umount(struct super_block *sb); 2893void f2fs_quota_off_umount(struct super_block *sb);
2874int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover); 2894int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2875int f2fs_sync_fs(struct super_block *sb, int sync); 2895int f2fs_sync_fs(struct super_block *sb, int sync);
@@ -3564,3 +3584,16 @@ extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
3564#endif 3584#endif
3565 3585
3566#endif 3586#endif
3587
3588static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
3589{
3590#ifdef CONFIG_QUOTA
3591 if (f2fs_sb_has_quota_ino(sbi->sb))
3592 return true;
3593 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
3594 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
3595 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
3596 return true;
3597#endif
3598 return false;
3599}
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 543c742f8bd7..971463e0589e 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -586,7 +586,8 @@ truncate_out:
586 return 0; 586 return 0;
587} 587}
588 588
589int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock) 589int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock,
590 bool buf_write)
590{ 591{
591 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 592 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
592 struct dnode_of_data dn; 593 struct dnode_of_data dn;
@@ -594,6 +595,7 @@ int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
594 int count = 0, err = 0; 595 int count = 0, err = 0;
595 struct page *ipage; 596 struct page *ipage;
596 bool truncate_page = false; 597 bool truncate_page = false;
598 int flag = buf_write ? F2FS_GET_BLOCK_PRE_AIO : F2FS_GET_BLOCK_PRE_DIO;
597 599
598 trace_f2fs_truncate_blocks_enter(inode, from); 600 trace_f2fs_truncate_blocks_enter(inode, from);
599 601
@@ -603,7 +605,7 @@ int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
603 goto free_partial; 605 goto free_partial;
604 606
605 if (lock) 607 if (lock)
606 f2fs_lock_op(sbi); 608 __do_map_lock(sbi, flag, true);
607 609
608 ipage = f2fs_get_node_page(sbi, inode->i_ino); 610 ipage = f2fs_get_node_page(sbi, inode->i_ino);
609 if (IS_ERR(ipage)) { 611 if (IS_ERR(ipage)) {
@@ -641,7 +643,7 @@ free_next:
641 err = f2fs_truncate_inode_blocks(inode, free_from); 643 err = f2fs_truncate_inode_blocks(inode, free_from);
642out: 644out:
643 if (lock) 645 if (lock)
644 f2fs_unlock_op(sbi); 646 __do_map_lock(sbi, flag, false);
645free_partial: 647free_partial:
646 /* lastly zero out the first data page */ 648 /* lastly zero out the first data page */
647 if (!err) 649 if (!err)
@@ -676,7 +678,7 @@ int f2fs_truncate(struct inode *inode)
676 return err; 678 return err;
677 } 679 }
678 680
679 err = f2fs_truncate_blocks(inode, i_size_read(inode), true); 681 err = f2fs_truncate_blocks(inode, i_size_read(inode), true, false);
680 if (err) 682 if (err)
681 return err; 683 return err;
682 684
@@ -785,9 +787,24 @@ int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
785 !uid_eq(attr->ia_uid, inode->i_uid)) || 787 !uid_eq(attr->ia_uid, inode->i_uid)) ||
786 (attr->ia_valid & ATTR_GID && 788 (attr->ia_valid & ATTR_GID &&
787 !gid_eq(attr->ia_gid, inode->i_gid))) { 789 !gid_eq(attr->ia_gid, inode->i_gid))) {
790 f2fs_lock_op(F2FS_I_SB(inode));
788 err = dquot_transfer(inode, attr); 791 err = dquot_transfer(inode, attr);
789 if (err) 792 if (err) {
793 set_sbi_flag(F2FS_I_SB(inode),
794 SBI_QUOTA_NEED_REPAIR);
795 f2fs_unlock_op(F2FS_I_SB(inode));
790 return err; 796 return err;
797 }
798 /*
799 * update uid/gid under lock_op(), so that dquot and inode can
800 * be updated atomically.
801 */
802 if (attr->ia_valid & ATTR_UID)
803 inode->i_uid = attr->ia_uid;
804 if (attr->ia_valid & ATTR_GID)
805 inode->i_gid = attr->ia_gid;
806 f2fs_mark_inode_dirty_sync(inode, true);
807 f2fs_unlock_op(F2FS_I_SB(inode));
791 } 808 }
792 809
793 if (attr->ia_valid & ATTR_SIZE) { 810 if (attr->ia_valid & ATTR_SIZE) {
@@ -1242,7 +1259,7 @@ static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1242 new_size = i_size_read(inode) - len; 1259 new_size = i_size_read(inode) - len;
1243 truncate_pagecache(inode, new_size); 1260 truncate_pagecache(inode, new_size);
1244 1261
1245 ret = f2fs_truncate_blocks(inode, new_size, true); 1262 ret = f2fs_truncate_blocks(inode, new_size, true, false);
1246 up_write(&F2FS_I(inode)->i_mmap_sem); 1263 up_write(&F2FS_I(inode)->i_mmap_sem);
1247 if (!ret) 1264 if (!ret)
1248 f2fs_i_size_write(inode, new_size); 1265 f2fs_i_size_write(inode, new_size);
@@ -1427,7 +1444,7 @@ static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1427 f2fs_balance_fs(sbi, true); 1444 f2fs_balance_fs(sbi, true);
1428 1445
1429 down_write(&F2FS_I(inode)->i_mmap_sem); 1446 down_write(&F2FS_I(inode)->i_mmap_sem);
1430 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true); 1447 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true, false);
1431 up_write(&F2FS_I(inode)->i_mmap_sem); 1448 up_write(&F2FS_I(inode)->i_mmap_sem);
1432 if (ret) 1449 if (ret)
1433 return ret; 1450 return ret;
diff --git a/fs/f2fs/inline.c b/fs/f2fs/inline.c
index 425d740f87fd..cb31a719b048 100644
--- a/fs/f2fs/inline.c
+++ b/fs/f2fs/inline.c
@@ -298,7 +298,7 @@ process_inline:
298 clear_inode_flag(inode, FI_INLINE_DATA); 298 clear_inode_flag(inode, FI_INLINE_DATA);
299 f2fs_put_page(ipage, 1); 299 f2fs_put_page(ipage, 1);
300 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) { 300 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
301 if (f2fs_truncate_blocks(inode, 0, false)) 301 if (f2fs_truncate_blocks(inode, 0, false, false))
302 return false; 302 return false;
303 goto process_inline; 303 goto process_inline;
304 } 304 }
@@ -470,7 +470,7 @@ static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry)
470 return 0; 470 return 0;
471punch_dentry_pages: 471punch_dentry_pages:
472 truncate_inode_pages(&dir->i_data, 0); 472 truncate_inode_pages(&dir->i_data, 0);
473 f2fs_truncate_blocks(dir, 0, false); 473 f2fs_truncate_blocks(dir, 0, false, false);
474 f2fs_remove_dirty_inode(dir); 474 f2fs_remove_dirty_inode(dir);
475 return err; 475 return err;
476} 476}
diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
index 57a7a15239d6..91ceee0ed4c4 100644
--- a/fs/f2fs/inode.c
+++ b/fs/f2fs/inode.c
@@ -654,7 +654,11 @@ void f2fs_evict_inode(struct inode *inode)
654 if (inode->i_nlink || is_bad_inode(inode)) 654 if (inode->i_nlink || is_bad_inode(inode))
655 goto no_delete; 655 goto no_delete;
656 656
657 dquot_initialize(inode); 657 err = dquot_initialize(inode);
658 if (err) {
659 err = 0;
660 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
661 }
658 662
659 f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO); 663 f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
660 f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO); 664 f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
@@ -686,9 +690,10 @@ retry:
686 goto retry; 690 goto retry;
687 } 691 }
688 692
689 if (err) 693 if (err) {
690 f2fs_update_inode_page(inode); 694 f2fs_update_inode_page(inode);
691 dquot_free_inode(inode); 695 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
696 }
692 sb_end_intwrite(inode->i_sb); 697 sb_end_intwrite(inode->i_sb);
693no_delete: 698no_delete:
694 dquot_drop(inode); 699 dquot_drop(inode);
diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
index 54295b5c1822..99299ede7429 100644
--- a/fs/f2fs/namei.c
+++ b/fs/f2fs/namei.c
@@ -72,10 +72,6 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
72 if (err) 72 if (err)
73 goto fail_drop; 73 goto fail_drop;
74 74
75 err = dquot_alloc_inode(inode);
76 if (err)
77 goto fail_drop;
78
79 set_inode_flag(inode, FI_NEW_INODE); 75 set_inode_flag(inode, FI_NEW_INODE);
80 76
81 /* If the directory encrypted, then we should encrypt the inode. */ 77 /* If the directory encrypted, then we should encrypt the inode. */
diff --git a/fs/f2fs/recovery.c b/fs/f2fs/recovery.c
index 875d2e205791..df2123759ac7 100644
--- a/fs/f2fs/recovery.c
+++ b/fs/f2fs/recovery.c
@@ -195,6 +195,33 @@ out:
195 return err; 195 return err;
196} 196}
197 197
198static int recover_quota_data(struct inode *inode, struct page *page)
199{
200 struct f2fs_inode *raw = F2FS_INODE(page);
201 struct iattr attr;
202 uid_t i_uid = le32_to_cpu(raw->i_uid);
203 gid_t i_gid = le32_to_cpu(raw->i_gid);
204 int err;
205
206 memset(&attr, 0, sizeof(attr));
207
208 attr.ia_uid = make_kuid(inode->i_sb->s_user_ns, i_uid);
209 attr.ia_gid = make_kgid(inode->i_sb->s_user_ns, i_gid);
210
211 if (!uid_eq(attr.ia_uid, inode->i_uid))
212 attr.ia_valid |= ATTR_UID;
213 if (!gid_eq(attr.ia_gid, inode->i_gid))
214 attr.ia_valid |= ATTR_GID;
215
216 if (!attr.ia_valid)
217 return 0;
218
219 err = dquot_transfer(inode, &attr);
220 if (err)
221 set_sbi_flag(F2FS_I_SB(inode), SBI_QUOTA_NEED_REPAIR);
222 return err;
223}
224
198static void recover_inline_flags(struct inode *inode, struct f2fs_inode *ri) 225static void recover_inline_flags(struct inode *inode, struct f2fs_inode *ri)
199{ 226{
200 if (ri->i_inline & F2FS_PIN_FILE) 227 if (ri->i_inline & F2FS_PIN_FILE)
@@ -207,12 +234,18 @@ static void recover_inline_flags(struct inode *inode, struct f2fs_inode *ri)
207 clear_inode_flag(inode, FI_DATA_EXIST); 234 clear_inode_flag(inode, FI_DATA_EXIST);
208} 235}
209 236
210static void recover_inode(struct inode *inode, struct page *page) 237static int recover_inode(struct inode *inode, struct page *page)
211{ 238{
212 struct f2fs_inode *raw = F2FS_INODE(page); 239 struct f2fs_inode *raw = F2FS_INODE(page);
213 char *name; 240 char *name;
241 int err;
214 242
215 inode->i_mode = le16_to_cpu(raw->i_mode); 243 inode->i_mode = le16_to_cpu(raw->i_mode);
244
245 err = recover_quota_data(inode, page);
246 if (err)
247 return err;
248
216 i_uid_write(inode, le32_to_cpu(raw->i_uid)); 249 i_uid_write(inode, le32_to_cpu(raw->i_uid));
217 i_gid_write(inode, le32_to_cpu(raw->i_gid)); 250 i_gid_write(inode, le32_to_cpu(raw->i_gid));
218 251
@@ -254,6 +287,7 @@ static void recover_inode(struct inode *inode, struct page *page)
254 f2fs_msg(inode->i_sb, KERN_NOTICE, 287 f2fs_msg(inode->i_sb, KERN_NOTICE,
255 "recover_inode: ino = %x, name = %s, inline = %x", 288 "recover_inode: ino = %x, name = %s, inline = %x",
256 ino_of_node(page), name, raw->i_inline); 289 ino_of_node(page), name, raw->i_inline);
290 return 0;
257} 291}
258 292
259static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head, 293static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head,
@@ -622,8 +656,11 @@ static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
622 * In this case, we can lose the latest inode(x). 656 * In this case, we can lose the latest inode(x).
623 * So, call recover_inode for the inode update. 657 * So, call recover_inode for the inode update.
624 */ 658 */
625 if (IS_INODE(page)) 659 if (IS_INODE(page)) {
626 recover_inode(entry->inode, page); 660 err = recover_inode(entry->inode, page);
661 if (err)
662 break;
663 }
627 if (entry->last_dentry == blkaddr) { 664 if (entry->last_dentry == blkaddr) {
628 err = recover_dentry(entry->inode, page, dir_list); 665 err = recover_dentry(entry->inode, page, dir_list);
629 if (err) { 666 if (err) {
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index f7814bb26a13..af58b2cc21b8 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -1711,6 +1711,7 @@ repeat:
1711 congestion_wait(BLK_RW_ASYNC, HZ/50); 1711 congestion_wait(BLK_RW_ASYNC, HZ/50);
1712 goto repeat; 1712 goto repeat;
1713 } 1713 }
1714 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
1714 return PTR_ERR(page); 1715 return PTR_ERR(page);
1715 } 1716 }
1716 1717
@@ -1722,6 +1723,7 @@ repeat:
1722 } 1723 }
1723 if (unlikely(!PageUptodate(page))) { 1724 if (unlikely(!PageUptodate(page))) {
1724 f2fs_put_page(page, 1); 1725 f2fs_put_page(page, 1);
1726 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
1725 return -EIO; 1727 return -EIO;
1726 } 1728 }
1727 1729
@@ -1763,6 +1765,7 @@ retry:
1763 congestion_wait(BLK_RW_ASYNC, HZ/50); 1765 congestion_wait(BLK_RW_ASYNC, HZ/50);
1764 goto retry; 1766 goto retry;
1765 } 1767 }
1768 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
1766 break; 1769 break;
1767 } 1770 }
1768 1771
@@ -1799,6 +1802,12 @@ static qsize_t *f2fs_get_reserved_space(struct inode *inode)
1799 1802
1800static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type) 1803static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
1801{ 1804{
1805 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
1806 f2fs_msg(sbi->sb, KERN_ERR,
1807 "quota sysfile may be corrupted, skip loading it");
1808 return 0;
1809 }
1810
1802 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type], 1811 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
1803 F2FS_OPTION(sbi).s_jquota_fmt, type); 1812 F2FS_OPTION(sbi).s_jquota_fmt, type);
1804} 1813}
@@ -1869,7 +1878,14 @@ static int f2fs_enable_quotas(struct super_block *sb)
1869 test_opt(F2FS_SB(sb), PRJQUOTA), 1878 test_opt(F2FS_SB(sb), PRJQUOTA),
1870 }; 1879 };
1871 1880
1872 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY; 1881 if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) {
1882 f2fs_msg(sb, KERN_ERR,
1883 "quota file may be corrupted, skip loading it");
1884 return 0;
1885 }
1886
1887 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
1888
1873 for (type = 0; type < MAXQUOTAS; type++) { 1889 for (type = 0; type < MAXQUOTAS; type++) {
1874 qf_inum = f2fs_qf_ino(sb, type); 1890 qf_inum = f2fs_qf_ino(sb, type);
1875 if (qf_inum) { 1891 if (qf_inum) {
@@ -1883,6 +1899,8 @@ static int f2fs_enable_quotas(struct super_block *sb)
1883 "fsck to fix.", type, err); 1899 "fsck to fix.", type, err);
1884 for (type--; type >= 0; type--) 1900 for (type--; type >= 0; type--)
1885 dquot_quota_off(sb, type); 1901 dquot_quota_off(sb, type);
1902 set_sbi_flag(F2FS_SB(sb),
1903 SBI_QUOTA_NEED_REPAIR);
1886 return err; 1904 return err;
1887 } 1905 }
1888 } 1906 }
@@ -1890,35 +1908,51 @@ static int f2fs_enable_quotas(struct super_block *sb)
1890 return 0; 1908 return 0;
1891} 1909}
1892 1910
1893static int f2fs_quota_sync(struct super_block *sb, int type) 1911int f2fs_quota_sync(struct super_block *sb, int type)
1894{ 1912{
1913 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1895 struct quota_info *dqopt = sb_dqopt(sb); 1914 struct quota_info *dqopt = sb_dqopt(sb);
1896 int cnt; 1915 int cnt;
1897 int ret; 1916 int ret;
1898 1917
1899 ret = dquot_writeback_dquots(sb, type); 1918 ret = dquot_writeback_dquots(sb, type);
1900 if (ret) 1919 if (ret)
1901 return ret; 1920 goto out;
1902 1921
1903 /* 1922 /*
1904 * Now when everything is written we can discard the pagecache so 1923 * Now when everything is written we can discard the pagecache so
1905 * that userspace sees the changes. 1924 * that userspace sees the changes.
1906 */ 1925 */
1907 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1926 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1927 struct address_space *mapping;
1928
1908 if (type != -1 && cnt != type) 1929 if (type != -1 && cnt != type)
1909 continue; 1930 continue;
1910 if (!sb_has_quota_active(sb, cnt)) 1931 if (!sb_has_quota_active(sb, cnt))
1911 continue; 1932 continue;
1912 1933
1913 ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping); 1934 mapping = dqopt->files[cnt]->i_mapping;
1935
1936 ret = filemap_fdatawrite(mapping);
1914 if (ret) 1937 if (ret)
1915 return ret; 1938 goto out;
1939
1940 /* if we are using journalled quota */
1941 if (is_journalled_quota(sbi))
1942 continue;
1943
1944 ret = filemap_fdatawait(mapping);
1945 if (ret)
1946 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
1916 1947
1917 inode_lock(dqopt->files[cnt]); 1948 inode_lock(dqopt->files[cnt]);
1918 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0); 1949 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
1919 inode_unlock(dqopt->files[cnt]); 1950 inode_unlock(dqopt->files[cnt]);
1920 } 1951 }
1921 return 0; 1952out:
1953 if (ret)
1954 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
1955 return ret;
1922} 1956}
1923 1957
1924static int f2fs_quota_on(struct super_block *sb, int type, int format_id, 1958static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
@@ -1986,7 +2020,7 @@ void f2fs_quota_off_umount(struct super_block *sb)
1986 "Fail to turn off disk quota " 2020 "Fail to turn off disk quota "
1987 "(type: %d, err: %d, ret:%d), Please " 2021 "(type: %d, err: %d, ret:%d), Please "
1988 "run fsck to fix it.", type, err, ret); 2022 "run fsck to fix it.", type, err, ret);
1989 set_sbi_flag(F2FS_SB(sb), SBI_NEED_FSCK); 2023 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
1990 } 2024 }
1991 } 2025 }
1992} 2026}
@@ -2003,6 +2037,61 @@ static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
2003 } 2037 }
2004} 2038}
2005 2039
2040static int f2fs_dquot_commit(struct dquot *dquot)
2041{
2042 int ret;
2043
2044 ret = dquot_commit(dquot);
2045 if (ret < 0)
2046 set_sbi_flag(F2FS_SB(dquot->dq_sb), SBI_QUOTA_NEED_REPAIR);
2047 return ret;
2048}
2049
2050static int f2fs_dquot_acquire(struct dquot *dquot)
2051{
2052 int ret;
2053
2054 ret = dquot_acquire(dquot);
2055 if (ret < 0)
2056 set_sbi_flag(F2FS_SB(dquot->dq_sb), SBI_QUOTA_NEED_REPAIR);
2057
2058 return ret;
2059}
2060
2061static int f2fs_dquot_release(struct dquot *dquot)
2062{
2063 int ret;
2064
2065 ret = dquot_release(dquot);
2066 if (ret < 0)
2067 set_sbi_flag(F2FS_SB(dquot->dq_sb), SBI_QUOTA_NEED_REPAIR);
2068 return ret;
2069}
2070
2071static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
2072{
2073 struct super_block *sb = dquot->dq_sb;
2074 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2075 int ret;
2076
2077 ret = dquot_mark_dquot_dirty(dquot);
2078
2079 /* if we are using journalled quota */
2080 if (is_journalled_quota(sbi))
2081 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
2082
2083 return ret;
2084}
2085
2086static int f2fs_dquot_commit_info(struct super_block *sb, int type)
2087{
2088 int ret;
2089
2090 ret = dquot_commit_info(sb, type);
2091 if (ret < 0)
2092 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2093 return ret;
2094}
2006 2095
2007static int f2fs_get_projid(struct inode *inode, kprojid_t *projid) 2096static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
2008{ 2097{
@@ -2012,11 +2101,11 @@ static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
2012 2101
2013static const struct dquot_operations f2fs_quota_operations = { 2102static const struct dquot_operations f2fs_quota_operations = {
2014 .get_reserved_space = f2fs_get_reserved_space, 2103 .get_reserved_space = f2fs_get_reserved_space,
2015 .write_dquot = dquot_commit, 2104 .write_dquot = f2fs_dquot_commit,
2016 .acquire_dquot = dquot_acquire, 2105 .acquire_dquot = f2fs_dquot_acquire,
2017 .release_dquot = dquot_release, 2106 .release_dquot = f2fs_dquot_release,
2018 .mark_dirty = dquot_mark_dquot_dirty, 2107 .mark_dirty = f2fs_dquot_mark_dquot_dirty,
2019 .write_info = dquot_commit_info, 2108 .write_info = f2fs_dquot_commit_info,
2020 .alloc_dquot = dquot_alloc, 2109 .alloc_dquot = dquot_alloc,
2021 .destroy_dquot = dquot_destroy, 2110 .destroy_dquot = dquot_destroy,
2022 .get_projid = f2fs_get_projid, 2111 .get_projid = f2fs_get_projid,
@@ -2034,6 +2123,11 @@ static const struct quotactl_ops f2fs_quotactl_ops = {
2034 .get_nextdqblk = dquot_get_next_dqblk, 2123 .get_nextdqblk = dquot_get_next_dqblk,
2035}; 2124};
2036#else 2125#else
2126int f2fs_quota_sync(struct super_block *sb, int type)
2127{
2128 return 0;
2129}
2130
2037void f2fs_quota_off_umount(struct super_block *sb) 2131void f2fs_quota_off_umount(struct super_block *sb)
2038{ 2132{
2039} 2133}
@@ -3104,6 +3198,9 @@ try_onemore:
3104 goto free_meta_inode; 3198 goto free_meta_inode;
3105 } 3199 }
3106 3200
3201 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
3202 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3203
3107 /* Initialize device list */ 3204 /* Initialize device list */
3108 err = f2fs_scan_devices(sbi); 3205 err = f2fs_scan_devices(sbi);
3109 if (err) { 3206 if (err) {
diff --git a/include/linux/f2fs_fs.h b/include/linux/f2fs_fs.h
index 8b9c7dc0260c..d7711048ef93 100644
--- a/include/linux/f2fs_fs.h
+++ b/include/linux/f2fs_fs.h
@@ -117,6 +117,7 @@ struct f2fs_super_block {
117 * For checkpoint 117 * For checkpoint
118 */ 118 */
119#define CP_DISABLED_FLAG 0x00001000 119#define CP_DISABLED_FLAG 0x00001000
120#define CP_QUOTA_NEED_FSCK_FLAG 0x00000800
120#define CP_LARGE_NAT_BITMAP_FLAG 0x00000400 121#define CP_LARGE_NAT_BITMAP_FLAG 0x00000400
121#define CP_NOCRC_RECOVERY_FLAG 0x00000200 122#define CP_NOCRC_RECOVERY_FLAG 0x00000200
122#define CP_TRIMMED_FLAG 0x00000100 123#define CP_TRIMMED_FLAG 0x00000100