blob: c47b1ef2685a4143a504575a3898cd0974847d7a [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003 * fs/f2fs/super.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09007 */
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/fs.h>
11#include <linux/statfs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090012#include <linux/buffer_head.h>
13#include <linux/backing-dev.h>
14#include <linux/kthread.h>
15#include <linux/parser.h>
16#include <linux/mount.h>
17#include <linux/seq_file.h>
Jaegeuk Kim5e176d52013-06-28 12:47:01 +090018#include <linux/proc_fs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090019#include <linux/random.h>
20#include <linux/exportfs.h>
Namjae Jeond3ee4562013-03-17 17:26:14 +090021#include <linux/blkdev.h>
Chao Yu0abd6752017-07-09 00:13:07 +080022#include <linux/quotaops.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090023#include <linux/f2fs_fs.h>
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090024#include <linux/sysfs.h>
Chao Yu4b2414d2017-08-08 10:54:31 +080025#include <linux/quota.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090026
27#include "f2fs.h"
28#include "node.h"
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +090029#include "segment.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090030#include "xattr.h"
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090031#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080032#include "trace.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090033
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090034#define CREATE_TRACE_POINTS
35#include <trace/events/f2fs.h>
36
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090037static struct kmem_cache *f2fs_inode_cachep;
38
Jaegeuk Kim73faec42016-04-29 15:34:32 -070039#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070040
Gao Xiang2d3a5852018-06-30 23:57:03 +080041char *f2fs_fault_name[FAULT_MAX] = {
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070042 [FAULT_KMALLOC] = "kmalloc",
Chao Yu628b3d12017-11-30 19:28:18 +080043 [FAULT_KVMALLOC] = "kvmalloc",
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -070044 [FAULT_PAGE_ALLOC] = "page alloc",
Chao Yu01eccef2017-10-28 16:52:30 +080045 [FAULT_PAGE_GET] = "page get",
Chao Yud62fe972017-10-28 16:52:31 +080046 [FAULT_ALLOC_BIO] = "alloc bio",
Jaegeuk Kimcb789422016-04-29 16:29:22 -070047 [FAULT_ALLOC_NID] = "alloc nid",
48 [FAULT_ORPHAN] = "orphan",
49 [FAULT_BLOCK] = "no more block",
50 [FAULT_DIR_DEPTH] = "too big dir depth",
Jaegeuk Kim53aa6bb2016-05-25 15:24:18 -070051 [FAULT_EVICT_INODE] = "evict_inode fail",
Jaegeuk Kim14b44d22017-03-09 15:24:24 -080052 [FAULT_TRUNCATE] = "truncate fail",
Chao Yu6f5c2ed2018-09-12 09:22:29 +080053 [FAULT_READ_IO] = "read IO error",
Chao Yu0f348022016-09-26 19:45:55 +080054 [FAULT_CHECKPOINT] = "checkpoint error",
Chao Yub83dcfe2018-08-06 20:30:18 +080055 [FAULT_DISCARD] = "discard error",
Chao Yu6f5c2ed2018-09-12 09:22:29 +080056 [FAULT_WRITE_IO] = "write IO error",
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070057};
Sheng Yong08796892016-05-16 12:38:50 +080058
Chao Yud4945002018-08-08 17:36:41 +080059void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
60 unsigned int type)
Sheng Yong08796892016-05-16 12:38:50 +080061{
Chao Yu63189b72018-03-08 14:22:56 +080062 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
Chao Yu1ecc0c52016-09-23 21:30:09 +080063
Sheng Yong08796892016-05-16 12:38:50 +080064 if (rate) {
Chao Yu1ecc0c52016-09-23 21:30:09 +080065 atomic_set(&ffi->inject_ops, 0);
66 ffi->inject_rate = rate;
Sheng Yong08796892016-05-16 12:38:50 +080067 }
Chao Yud4945002018-08-08 17:36:41 +080068
69 if (type)
70 ffi->inject_type = type;
71
72 if (!rate && !type)
73 memset(ffi, 0, sizeof(struct f2fs_fault_info));
Sheng Yong08796892016-05-16 12:38:50 +080074}
Jaegeuk Kim73faec42016-04-29 15:34:32 -070075#endif
76
Jaegeuk Kim2658e502015-06-19 12:01:21 -070077/* f2fs-wide shrinker description */
78static struct shrinker f2fs_shrinker_info = {
79 .scan_objects = f2fs_shrink_scan,
80 .count_objects = f2fs_shrink_count,
81 .seeks = DEFAULT_SEEKS,
82};
83
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090084enum {
Namjae Jeon696c0182013-06-16 09:48:48 +090085 Opt_gc_background,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090086 Opt_disable_roll_forward,
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -080087 Opt_norecovery,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090088 Opt_discard,
Chao Yu64058be2016-07-03 22:05:14 +080089 Opt_nodiscard,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090090 Opt_noheap,
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -040091 Opt_heap,
Kelly Anderson4058c512013-10-07 11:36:20 +090092 Opt_user_xattr,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090093 Opt_nouser_xattr,
Kelly Anderson4058c512013-10-07 11:36:20 +090094 Opt_acl,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090095 Opt_noacl,
96 Opt_active_logs,
97 Opt_disable_ext_identify,
Jaegeuk Kim444c5802013-08-08 15:16:22 +090098 Opt_inline_xattr,
Chao Yu23cf7212017-02-15 10:34:45 +080099 Opt_noinline_xattr,
Chao Yu6afc6622017-09-06 21:59:50 +0800100 Opt_inline_xattr_size,
Huajun Li8274de72013-11-10 23:13:17 +0800101 Opt_inline_data,
Chao Yu5efd3c62014-09-24 18:16:13 +0800102 Opt_inline_dentry,
Chao Yu97c17942016-05-09 19:56:34 +0800103 Opt_noinline_dentry,
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900104 Opt_flush_merge,
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700105 Opt_noflush_merge,
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700106 Opt_nobarrier,
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700107 Opt_fastboot,
Chao Yu89672152015-02-05 17:55:51 +0800108 Opt_extent_cache,
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700109 Opt_noextent_cache,
Wanpeng Li75342792015-03-24 10:20:27 +0800110 Opt_noinline_data,
Chao Yu343f40f2015-12-16 13:12:16 +0800111 Opt_data_flush,
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800112 Opt_reserve_root,
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800113 Opt_resgid,
114 Opt_resuid,
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700115 Opt_mode,
Jaegeuk Kimec915382016-12-21 17:09:19 -0800116 Opt_io_size_bits,
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700117 Opt_fault_injection,
Chao Yud4945002018-08-08 17:36:41 +0800118 Opt_fault_type,
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700119 Opt_lazytime,
120 Opt_nolazytime,
Chao Yu4b2414d2017-08-08 10:54:31 +0800121 Opt_quota,
122 Opt_noquota,
Chao Yu0abd6752017-07-09 00:13:07 +0800123 Opt_usrquota,
124 Opt_grpquota,
Chao Yu5c571322017-07-26 00:01:41 +0800125 Opt_prjquota,
Chao Yu4b2414d2017-08-08 10:54:31 +0800126 Opt_usrjquota,
127 Opt_grpjquota,
128 Opt_prjjquota,
129 Opt_offusrjquota,
130 Opt_offgrpjquota,
131 Opt_offprjjquota,
132 Opt_jqfmt_vfsold,
133 Opt_jqfmt_vfsv0,
134 Opt_jqfmt_vfsv1,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900135 Opt_whint,
Jaegeuk Kim07939622018-02-18 08:50:49 -0800136 Opt_alloc,
Junling Zheng93cf93f2018-03-07 12:07:49 +0800137 Opt_fsync,
Sheng Yongff62af22018-03-15 18:51:42 +0800138 Opt_test_dummy_encryption,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900139 Opt_err,
140};
141
142static match_table_t f2fs_tokens = {
Namjae Jeon696c0182013-06-16 09:48:48 +0900143 {Opt_gc_background, "background_gc=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900144 {Opt_disable_roll_forward, "disable_roll_forward"},
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800145 {Opt_norecovery, "norecovery"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900146 {Opt_discard, "discard"},
Chao Yu64058be2016-07-03 22:05:14 +0800147 {Opt_nodiscard, "nodiscard"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900148 {Opt_noheap, "no_heap"},
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400149 {Opt_heap, "heap"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900150 {Opt_user_xattr, "user_xattr"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900151 {Opt_nouser_xattr, "nouser_xattr"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900152 {Opt_acl, "acl"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900153 {Opt_noacl, "noacl"},
154 {Opt_active_logs, "active_logs=%u"},
155 {Opt_disable_ext_identify, "disable_ext_identify"},
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900156 {Opt_inline_xattr, "inline_xattr"},
Chao Yu23cf7212017-02-15 10:34:45 +0800157 {Opt_noinline_xattr, "noinline_xattr"},
Chao Yu6afc6622017-09-06 21:59:50 +0800158 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
Huajun Li8274de72013-11-10 23:13:17 +0800159 {Opt_inline_data, "inline_data"},
Chao Yu5efd3c62014-09-24 18:16:13 +0800160 {Opt_inline_dentry, "inline_dentry"},
Chao Yu97c17942016-05-09 19:56:34 +0800161 {Opt_noinline_dentry, "noinline_dentry"},
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900162 {Opt_flush_merge, "flush_merge"},
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700163 {Opt_noflush_merge, "noflush_merge"},
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700164 {Opt_nobarrier, "nobarrier"},
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700165 {Opt_fastboot, "fastboot"},
Chao Yu89672152015-02-05 17:55:51 +0800166 {Opt_extent_cache, "extent_cache"},
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700167 {Opt_noextent_cache, "noextent_cache"},
Wanpeng Li75342792015-03-24 10:20:27 +0800168 {Opt_noinline_data, "noinline_data"},
Chao Yu343f40f2015-12-16 13:12:16 +0800169 {Opt_data_flush, "data_flush"},
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800170 {Opt_reserve_root, "reserve_root=%u"},
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800171 {Opt_resgid, "resgid=%u"},
172 {Opt_resuid, "resuid=%u"},
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700173 {Opt_mode, "mode=%s"},
Jaegeuk Kimec915382016-12-21 17:09:19 -0800174 {Opt_io_size_bits, "io_bits=%u"},
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700175 {Opt_fault_injection, "fault_injection=%u"},
Chao Yud4945002018-08-08 17:36:41 +0800176 {Opt_fault_type, "fault_type=%u"},
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700177 {Opt_lazytime, "lazytime"},
178 {Opt_nolazytime, "nolazytime"},
Chao Yu4b2414d2017-08-08 10:54:31 +0800179 {Opt_quota, "quota"},
180 {Opt_noquota, "noquota"},
Chao Yu0abd6752017-07-09 00:13:07 +0800181 {Opt_usrquota, "usrquota"},
182 {Opt_grpquota, "grpquota"},
Chao Yu5c571322017-07-26 00:01:41 +0800183 {Opt_prjquota, "prjquota"},
Chao Yu4b2414d2017-08-08 10:54:31 +0800184 {Opt_usrjquota, "usrjquota=%s"},
185 {Opt_grpjquota, "grpjquota=%s"},
186 {Opt_prjjquota, "prjjquota=%s"},
187 {Opt_offusrjquota, "usrjquota="},
188 {Opt_offgrpjquota, "grpjquota="},
189 {Opt_offprjjquota, "prjjquota="},
190 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
191 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
192 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900193 {Opt_whint, "whint_mode=%s"},
Jaegeuk Kim07939622018-02-18 08:50:49 -0800194 {Opt_alloc, "alloc_mode=%s"},
Junling Zheng93cf93f2018-03-07 12:07:49 +0800195 {Opt_fsync, "fsync_mode=%s"},
Sheng Yongff62af22018-03-15 18:51:42 +0800196 {Opt_test_dummy_encryption, "test_dummy_encryption"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900197 {Opt_err, NULL},
198};
199
Namjae Jeona07ef782012-12-30 14:52:05 +0900200void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
201{
202 struct va_format vaf;
203 va_list args;
204
205 va_start(args, fmt);
206 vaf.fmt = fmt;
207 vaf.va = &args;
Chao Yu22d7ea12018-08-22 17:17:47 +0800208 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
Namjae Jeona07ef782012-12-30 14:52:05 +0900209 va_end(args);
210}
211
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800212static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
213{
214 block_t limit = (sbi->user_block_count << 1) / 1000;
215
216 /* limit is 0.2% */
Chao Yu63189b72018-03-08 14:22:56 +0800217 if (test_opt(sbi, RESERVE_ROOT) &&
218 F2FS_OPTION(sbi).root_reserved_blocks > limit) {
219 F2FS_OPTION(sbi).root_reserved_blocks = limit;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800220 f2fs_msg(sbi->sb, KERN_INFO,
221 "Reduce reserved blocks for root = %u",
Chao Yu63189b72018-03-08 14:22:56 +0800222 F2FS_OPTION(sbi).root_reserved_blocks);
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800223 }
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800224 if (!test_opt(sbi, RESERVE_ROOT) &&
Chao Yu63189b72018-03-08 14:22:56 +0800225 (!uid_eq(F2FS_OPTION(sbi).s_resuid,
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800226 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
Chao Yu63189b72018-03-08 14:22:56 +0800227 !gid_eq(F2FS_OPTION(sbi).s_resgid,
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800228 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
229 f2fs_msg(sbi->sb, KERN_INFO,
230 "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
Chao Yu63189b72018-03-08 14:22:56 +0800231 from_kuid_munged(&init_user_ns,
232 F2FS_OPTION(sbi).s_resuid),
233 from_kgid_munged(&init_user_ns,
234 F2FS_OPTION(sbi).s_resgid));
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800235}
236
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900237static void init_once(void *foo)
238{
239 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
240
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900241 inode_init_once(&fi->vfs_inode);
242}
243
Chao Yu4b2414d2017-08-08 10:54:31 +0800244#ifdef CONFIG_QUOTA
245static const char * const quotatypes[] = INITQFNAMES;
246#define QTYPE2NAME(t) (quotatypes[t])
247static int f2fs_set_qf_name(struct super_block *sb, int qtype,
248 substring_t *args)
249{
250 struct f2fs_sb_info *sbi = F2FS_SB(sb);
251 char *qname;
252 int ret = -EINVAL;
253
Chao Yu63189b72018-03-08 14:22:56 +0800254 if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
Chao Yu4b2414d2017-08-08 10:54:31 +0800255 f2fs_msg(sb, KERN_ERR,
256 "Cannot change journaled "
257 "quota options when quota turned on");
258 return -EINVAL;
259 }
Jaegeuk Kimea676732017-10-06 09:14:28 -0700260 if (f2fs_sb_has_quota_ino(sb)) {
261 f2fs_msg(sb, KERN_INFO,
262 "QUOTA feature is enabled, so ignore qf_name");
263 return 0;
264 }
265
Chao Yu4b2414d2017-08-08 10:54:31 +0800266 qname = match_strdup(args);
267 if (!qname) {
268 f2fs_msg(sb, KERN_ERR,
269 "Not enough memory for storing quotafile name");
270 return -EINVAL;
271 }
Chao Yu63189b72018-03-08 14:22:56 +0800272 if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
273 if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
Chao Yu4b2414d2017-08-08 10:54:31 +0800274 ret = 0;
275 else
276 f2fs_msg(sb, KERN_ERR,
277 "%s quota file already specified",
278 QTYPE2NAME(qtype));
279 goto errout;
280 }
281 if (strchr(qname, '/')) {
282 f2fs_msg(sb, KERN_ERR,
283 "quotafile must be on filesystem root");
284 goto errout;
285 }
Chao Yu63189b72018-03-08 14:22:56 +0800286 F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
Chao Yu4b2414d2017-08-08 10:54:31 +0800287 set_opt(sbi, QUOTA);
288 return 0;
289errout:
290 kfree(qname);
291 return ret;
292}
293
294static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
295{
296 struct f2fs_sb_info *sbi = F2FS_SB(sb);
297
Chao Yu63189b72018-03-08 14:22:56 +0800298 if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
Chao Yu4b2414d2017-08-08 10:54:31 +0800299 f2fs_msg(sb, KERN_ERR, "Cannot change journaled quota options"
300 " when quota turned on");
301 return -EINVAL;
302 }
Chao Yu63189b72018-03-08 14:22:56 +0800303 kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
304 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
Chao Yu4b2414d2017-08-08 10:54:31 +0800305 return 0;
306}
307
308static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
309{
310 /*
311 * We do the test below only for project quotas. 'usrquota' and
312 * 'grpquota' mount options are allowed even without quota feature
313 * to support legacy quotas in quota files.
314 */
315 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi->sb)) {
316 f2fs_msg(sbi->sb, KERN_ERR, "Project quota feature not enabled. "
317 "Cannot enable project quota enforcement.");
318 return -1;
319 }
Chao Yu63189b72018-03-08 14:22:56 +0800320 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
321 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
322 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
323 if (test_opt(sbi, USRQUOTA) &&
324 F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
Chao Yu4b2414d2017-08-08 10:54:31 +0800325 clear_opt(sbi, USRQUOTA);
326
Chao Yu63189b72018-03-08 14:22:56 +0800327 if (test_opt(sbi, GRPQUOTA) &&
328 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
Chao Yu4b2414d2017-08-08 10:54:31 +0800329 clear_opt(sbi, GRPQUOTA);
330
Chao Yu63189b72018-03-08 14:22:56 +0800331 if (test_opt(sbi, PRJQUOTA) &&
332 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
Chao Yu4b2414d2017-08-08 10:54:31 +0800333 clear_opt(sbi, PRJQUOTA);
334
335 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
336 test_opt(sbi, PRJQUOTA)) {
337 f2fs_msg(sbi->sb, KERN_ERR, "old and new quota "
338 "format mixing");
339 return -1;
340 }
341
Chao Yu63189b72018-03-08 14:22:56 +0800342 if (!F2FS_OPTION(sbi).s_jquota_fmt) {
Chao Yu4b2414d2017-08-08 10:54:31 +0800343 f2fs_msg(sbi->sb, KERN_ERR, "journaled quota format "
344 "not specified");
345 return -1;
346 }
347 }
Jaegeuk Kimea676732017-10-06 09:14:28 -0700348
Chao Yu63189b72018-03-08 14:22:56 +0800349 if (f2fs_sb_has_quota_ino(sbi->sb) && F2FS_OPTION(sbi).s_jquota_fmt) {
Jaegeuk Kimea676732017-10-06 09:14:28 -0700350 f2fs_msg(sbi->sb, KERN_INFO,
351 "QUOTA feature is enabled, so ignore jquota_fmt");
Chao Yu63189b72018-03-08 14:22:56 +0800352 F2FS_OPTION(sbi).s_jquota_fmt = 0;
Jaegeuk Kimea676732017-10-06 09:14:28 -0700353 }
Chao Yu4b2414d2017-08-08 10:54:31 +0800354 return 0;
355}
356#endif
357
Namjae Jeon696c0182013-06-16 09:48:48 +0900358static int parse_options(struct super_block *sb, char *options)
359{
360 struct f2fs_sb_info *sbi = F2FS_SB(sb);
361 substring_t args[MAX_OPT_ARGS];
362 char *p, *name;
363 int arg = 0;
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800364 kuid_t uid;
365 kgid_t gid;
Chao Yu4b2414d2017-08-08 10:54:31 +0800366#ifdef CONFIG_QUOTA
367 int ret;
368#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900369
370 if (!options)
371 return 0;
372
373 while ((p = strsep(&options, ",")) != NULL) {
374 int token;
375 if (!*p)
376 continue;
377 /*
378 * Initialize args struct so we know whether arg was
379 * found; some options take optional arguments.
380 */
381 args[0].to = args[0].from = NULL;
382 token = match_token(p, f2fs_tokens, args);
383
384 switch (token) {
385 case Opt_gc_background:
386 name = match_strdup(&args[0]);
387
388 if (!name)
389 return -ENOMEM;
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700390 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900391 set_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700392 clear_opt(sbi, FORCE_FG_GC);
393 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900394 clear_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700395 clear_opt(sbi, FORCE_FG_GC);
396 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
397 set_opt(sbi, BG_GC);
398 set_opt(sbi, FORCE_FG_GC);
399 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +0900400 kfree(name);
401 return -EINVAL;
402 }
403 kfree(name);
404 break;
405 case Opt_disable_roll_forward:
406 set_opt(sbi, DISABLE_ROLL_FORWARD);
407 break;
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800408 case Opt_norecovery:
409 /* this option mounts f2fs with ro */
410 set_opt(sbi, DISABLE_ROLL_FORWARD);
411 if (!f2fs_readonly(sb))
412 return -EINVAL;
413 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900414 case Opt_discard:
Chao Yu7d20c8a2018-09-04 03:52:17 +0800415 set_opt(sbi, DISCARD);
Namjae Jeon696c0182013-06-16 09:48:48 +0900416 break;
Chao Yu64058be2016-07-03 22:05:14 +0800417 case Opt_nodiscard:
Sheng Yongccd31cb2018-02-06 12:31:17 +0800418 if (f2fs_sb_has_blkzoned(sb)) {
Damien Le Moal96ba2de2016-10-28 17:45:03 +0900419 f2fs_msg(sb, KERN_WARNING,
420 "discard is required for zoned block devices");
421 return -EINVAL;
422 }
Chao Yu64058be2016-07-03 22:05:14 +0800423 clear_opt(sbi, DISCARD);
Damien Le Moal487df612016-10-28 17:44:59 +0900424 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900425 case Opt_noheap:
426 set_opt(sbi, NOHEAP);
427 break;
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400428 case Opt_heap:
429 clear_opt(sbi, NOHEAP);
430 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900431#ifdef CONFIG_F2FS_FS_XATTR
Kelly Anderson4058c512013-10-07 11:36:20 +0900432 case Opt_user_xattr:
433 set_opt(sbi, XATTR_USER);
434 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900435 case Opt_nouser_xattr:
436 clear_opt(sbi, XATTR_USER);
437 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900438 case Opt_inline_xattr:
439 set_opt(sbi, INLINE_XATTR);
440 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800441 case Opt_noinline_xattr:
442 clear_opt(sbi, INLINE_XATTR);
443 break;
Chao Yu6afc6622017-09-06 21:59:50 +0800444 case Opt_inline_xattr_size:
445 if (args->from && match_int(args, &arg))
446 return -EINVAL;
447 set_opt(sbi, INLINE_XATTR_SIZE);
Chao Yu63189b72018-03-08 14:22:56 +0800448 F2FS_OPTION(sbi).inline_xattr_size = arg;
Chao Yu6afc6622017-09-06 21:59:50 +0800449 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900450#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900451 case Opt_user_xattr:
452 f2fs_msg(sb, KERN_INFO,
453 "user_xattr options not supported");
454 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900455 case Opt_nouser_xattr:
456 f2fs_msg(sb, KERN_INFO,
457 "nouser_xattr options not supported");
458 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900459 case Opt_inline_xattr:
460 f2fs_msg(sb, KERN_INFO,
461 "inline_xattr options not supported");
462 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800463 case Opt_noinline_xattr:
464 f2fs_msg(sb, KERN_INFO,
465 "noinline_xattr options not supported");
466 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900467#endif
468#ifdef CONFIG_F2FS_FS_POSIX_ACL
Kelly Anderson4058c512013-10-07 11:36:20 +0900469 case Opt_acl:
470 set_opt(sbi, POSIX_ACL);
471 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900472 case Opt_noacl:
473 clear_opt(sbi, POSIX_ACL);
474 break;
475#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900476 case Opt_acl:
477 f2fs_msg(sb, KERN_INFO, "acl options not supported");
478 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900479 case Opt_noacl:
480 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
481 break;
482#endif
483 case Opt_active_logs:
484 if (args->from && match_int(args, &arg))
485 return -EINVAL;
486 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
487 return -EINVAL;
Chao Yu63189b72018-03-08 14:22:56 +0800488 F2FS_OPTION(sbi).active_logs = arg;
Namjae Jeon696c0182013-06-16 09:48:48 +0900489 break;
490 case Opt_disable_ext_identify:
491 set_opt(sbi, DISABLE_EXT_IDENTIFY);
492 break;
Huajun Li8274de72013-11-10 23:13:17 +0800493 case Opt_inline_data:
494 set_opt(sbi, INLINE_DATA);
495 break;
Chao Yu5efd3c62014-09-24 18:16:13 +0800496 case Opt_inline_dentry:
497 set_opt(sbi, INLINE_DENTRY);
498 break;
Chao Yu97c17942016-05-09 19:56:34 +0800499 case Opt_noinline_dentry:
500 clear_opt(sbi, INLINE_DENTRY);
501 break;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900502 case Opt_flush_merge:
503 set_opt(sbi, FLUSH_MERGE);
504 break;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700505 case Opt_noflush_merge:
506 clear_opt(sbi, FLUSH_MERGE);
507 break;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700508 case Opt_nobarrier:
509 set_opt(sbi, NOBARRIER);
510 break;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700511 case Opt_fastboot:
512 set_opt(sbi, FASTBOOT);
513 break;
Chao Yu89672152015-02-05 17:55:51 +0800514 case Opt_extent_cache:
515 set_opt(sbi, EXTENT_CACHE);
516 break;
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700517 case Opt_noextent_cache:
518 clear_opt(sbi, EXTENT_CACHE);
519 break;
Wanpeng Li75342792015-03-24 10:20:27 +0800520 case Opt_noinline_data:
521 clear_opt(sbi, INLINE_DATA);
522 break;
Chao Yu343f40f2015-12-16 13:12:16 +0800523 case Opt_data_flush:
524 set_opt(sbi, DATA_FLUSH);
525 break;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800526 case Opt_reserve_root:
527 if (args->from && match_int(args, &arg))
528 return -EINVAL;
529 if (test_opt(sbi, RESERVE_ROOT)) {
530 f2fs_msg(sb, KERN_INFO,
531 "Preserve previous reserve_root=%u",
Chao Yu63189b72018-03-08 14:22:56 +0800532 F2FS_OPTION(sbi).root_reserved_blocks);
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800533 } else {
Chao Yu63189b72018-03-08 14:22:56 +0800534 F2FS_OPTION(sbi).root_reserved_blocks = arg;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800535 set_opt(sbi, RESERVE_ROOT);
536 }
537 break;
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800538 case Opt_resuid:
539 if (args->from && match_int(args, &arg))
540 return -EINVAL;
541 uid = make_kuid(current_user_ns(), arg);
542 if (!uid_valid(uid)) {
543 f2fs_msg(sb, KERN_ERR,
544 "Invalid uid value %d", arg);
545 return -EINVAL;
546 }
Chao Yu63189b72018-03-08 14:22:56 +0800547 F2FS_OPTION(sbi).s_resuid = uid;
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800548 break;
549 case Opt_resgid:
550 if (args->from && match_int(args, &arg))
551 return -EINVAL;
552 gid = make_kgid(current_user_ns(), arg);
553 if (!gid_valid(gid)) {
554 f2fs_msg(sb, KERN_ERR,
555 "Invalid gid value %d", arg);
556 return -EINVAL;
557 }
Chao Yu63189b72018-03-08 14:22:56 +0800558 F2FS_OPTION(sbi).s_resgid = gid;
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800559 break;
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700560 case Opt_mode:
561 name = match_strdup(&args[0]);
562
563 if (!name)
564 return -ENOMEM;
565 if (strlen(name) == 8 &&
566 !strncmp(name, "adaptive", 8)) {
Sheng Yongccd31cb2018-02-06 12:31:17 +0800567 if (f2fs_sb_has_blkzoned(sb)) {
Damien Le Moal3adc57e2016-10-28 17:45:04 +0900568 f2fs_msg(sb, KERN_WARNING,
569 "adaptive mode is not allowed with "
570 "zoned block device feature");
571 kfree(name);
572 return -EINVAL;
573 }
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700574 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700575 } else if (strlen(name) == 3 &&
576 !strncmp(name, "lfs", 3)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700577 set_opt_mode(sbi, F2FS_MOUNT_LFS);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700578 } else {
579 kfree(name);
580 return -EINVAL;
581 }
582 kfree(name);
583 break;
Jaegeuk Kimec915382016-12-21 17:09:19 -0800584 case Opt_io_size_bits:
585 if (args->from && match_int(args, &arg))
586 return -EINVAL;
587 if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
588 f2fs_msg(sb, KERN_WARNING,
589 "Not support %d, larger than %d",
590 1 << arg, BIO_MAX_PAGES);
591 return -EINVAL;
592 }
Chao Yu63189b72018-03-08 14:22:56 +0800593 F2FS_OPTION(sbi).write_io_size_bits = arg;
Jaegeuk Kimec915382016-12-21 17:09:19 -0800594 break;
Chengguang Xu4cb037e2018-09-12 13:32:52 +0800595#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700596 case Opt_fault_injection:
597 if (args->from && match_int(args, &arg))
598 return -EINVAL;
Chao Yud4945002018-08-08 17:36:41 +0800599 f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
600 set_opt(sbi, FAULT_INJECTION);
Chao Yud4945002018-08-08 17:36:41 +0800601 break;
Chengguang Xu4cb037e2018-09-12 13:32:52 +0800602
Chao Yud4945002018-08-08 17:36:41 +0800603 case Opt_fault_type:
604 if (args->from && match_int(args, &arg))
605 return -EINVAL;
Chao Yud4945002018-08-08 17:36:41 +0800606 f2fs_build_fault_attr(sbi, 0, arg);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800607 set_opt(sbi, FAULT_INJECTION);
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700608 break;
Chengguang Xu4cb037e2018-09-12 13:32:52 +0800609#else
610 case Opt_fault_injection:
611 f2fs_msg(sb, KERN_INFO,
612 "fault_injection options not supported");
613 break;
614
615 case Opt_fault_type:
616 f2fs_msg(sb, KERN_INFO,
617 "fault_type options not supported");
618 break;
619#endif
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700620 case Opt_lazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800621 sb->s_flags |= SB_LAZYTIME;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700622 break;
623 case Opt_nolazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800624 sb->s_flags &= ~SB_LAZYTIME;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700625 break;
Chao Yu0abd6752017-07-09 00:13:07 +0800626#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +0800627 case Opt_quota:
Chao Yu0abd6752017-07-09 00:13:07 +0800628 case Opt_usrquota:
629 set_opt(sbi, USRQUOTA);
630 break;
631 case Opt_grpquota:
632 set_opt(sbi, GRPQUOTA);
633 break;
Chao Yu5c571322017-07-26 00:01:41 +0800634 case Opt_prjquota:
635 set_opt(sbi, PRJQUOTA);
636 break;
Chao Yu4b2414d2017-08-08 10:54:31 +0800637 case Opt_usrjquota:
638 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
639 if (ret)
640 return ret;
641 break;
642 case Opt_grpjquota:
643 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
644 if (ret)
645 return ret;
646 break;
647 case Opt_prjjquota:
648 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
649 if (ret)
650 return ret;
651 break;
652 case Opt_offusrjquota:
653 ret = f2fs_clear_qf_name(sb, USRQUOTA);
654 if (ret)
655 return ret;
656 break;
657 case Opt_offgrpjquota:
658 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
659 if (ret)
660 return ret;
661 break;
662 case Opt_offprjjquota:
663 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
664 if (ret)
665 return ret;
666 break;
667 case Opt_jqfmt_vfsold:
Chao Yu63189b72018-03-08 14:22:56 +0800668 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
Chao Yu4b2414d2017-08-08 10:54:31 +0800669 break;
670 case Opt_jqfmt_vfsv0:
Chao Yu63189b72018-03-08 14:22:56 +0800671 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
Chao Yu4b2414d2017-08-08 10:54:31 +0800672 break;
673 case Opt_jqfmt_vfsv1:
Chao Yu63189b72018-03-08 14:22:56 +0800674 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
Chao Yu4b2414d2017-08-08 10:54:31 +0800675 break;
676 case Opt_noquota:
677 clear_opt(sbi, QUOTA);
678 clear_opt(sbi, USRQUOTA);
679 clear_opt(sbi, GRPQUOTA);
680 clear_opt(sbi, PRJQUOTA);
681 break;
Chao Yu0abd6752017-07-09 00:13:07 +0800682#else
Chao Yu4b2414d2017-08-08 10:54:31 +0800683 case Opt_quota:
Chao Yu0abd6752017-07-09 00:13:07 +0800684 case Opt_usrquota:
685 case Opt_grpquota:
Chao Yu5c571322017-07-26 00:01:41 +0800686 case Opt_prjquota:
Chao Yu4b2414d2017-08-08 10:54:31 +0800687 case Opt_usrjquota:
688 case Opt_grpjquota:
689 case Opt_prjjquota:
690 case Opt_offusrjquota:
691 case Opt_offgrpjquota:
692 case Opt_offprjjquota:
693 case Opt_jqfmt_vfsold:
694 case Opt_jqfmt_vfsv0:
695 case Opt_jqfmt_vfsv1:
696 case Opt_noquota:
Chao Yu0abd6752017-07-09 00:13:07 +0800697 f2fs_msg(sb, KERN_INFO,
698 "quota operations not supported");
699 break;
700#endif
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900701 case Opt_whint:
702 name = match_strdup(&args[0]);
703 if (!name)
704 return -ENOMEM;
705 if (strlen(name) == 10 &&
706 !strncmp(name, "user-based", 10)) {
Chao Yu63189b72018-03-08 14:22:56 +0800707 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_USER;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900708 } else if (strlen(name) == 3 &&
709 !strncmp(name, "off", 3)) {
Chao Yu63189b72018-03-08 14:22:56 +0800710 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
Hyunchul Leef2e703f2018-01-31 11:36:58 +0900711 } else if (strlen(name) == 8 &&
712 !strncmp(name, "fs-based", 8)) {
Chao Yu63189b72018-03-08 14:22:56 +0800713 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_FS;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900714 } else {
715 kfree(name);
716 return -EINVAL;
717 }
718 kfree(name);
719 break;
Jaegeuk Kim07939622018-02-18 08:50:49 -0800720 case Opt_alloc:
721 name = match_strdup(&args[0]);
722 if (!name)
723 return -ENOMEM;
724
725 if (strlen(name) == 7 &&
726 !strncmp(name, "default", 7)) {
Chao Yu63189b72018-03-08 14:22:56 +0800727 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
Jaegeuk Kim07939622018-02-18 08:50:49 -0800728 } else if (strlen(name) == 5 &&
729 !strncmp(name, "reuse", 5)) {
Chao Yu63189b72018-03-08 14:22:56 +0800730 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
Jaegeuk Kim07939622018-02-18 08:50:49 -0800731 } else {
732 kfree(name);
733 return -EINVAL;
734 }
735 kfree(name);
736 break;
Junling Zheng93cf93f2018-03-07 12:07:49 +0800737 case Opt_fsync:
738 name = match_strdup(&args[0]);
739 if (!name)
740 return -ENOMEM;
741 if (strlen(name) == 5 &&
742 !strncmp(name, "posix", 5)) {
Chao Yu63189b72018-03-08 14:22:56 +0800743 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
Junling Zheng93cf93f2018-03-07 12:07:49 +0800744 } else if (strlen(name) == 6 &&
745 !strncmp(name, "strict", 6)) {
Chao Yu63189b72018-03-08 14:22:56 +0800746 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
Jaegeuk Kimd6290812018-05-25 18:02:58 -0700747 } else if (strlen(name) == 9 &&
748 !strncmp(name, "nobarrier", 9)) {
749 F2FS_OPTION(sbi).fsync_mode =
750 FSYNC_MODE_NOBARRIER;
Junling Zheng93cf93f2018-03-07 12:07:49 +0800751 } else {
752 kfree(name);
753 return -EINVAL;
754 }
755 kfree(name);
756 break;
Sheng Yongff62af22018-03-15 18:51:42 +0800757 case Opt_test_dummy_encryption:
758#ifdef CONFIG_F2FS_FS_ENCRYPTION
759 if (!f2fs_sb_has_encrypt(sb)) {
760 f2fs_msg(sb, KERN_ERR, "Encrypt feature is off");
761 return -EINVAL;
762 }
763
764 F2FS_OPTION(sbi).test_dummy_encryption = true;
765 f2fs_msg(sb, KERN_INFO,
766 "Test dummy encryption mode enabled");
767#else
768 f2fs_msg(sb, KERN_INFO,
769 "Test dummy encryption mount option ignored");
770#endif
771 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900772 default:
773 f2fs_msg(sb, KERN_ERR,
774 "Unrecognized mount option \"%s\" or missing value",
775 p);
776 return -EINVAL;
777 }
778 }
Chao Yu4b2414d2017-08-08 10:54:31 +0800779#ifdef CONFIG_QUOTA
780 if (f2fs_check_quota_options(sbi))
781 return -EINVAL;
Sheng Yong00960c22018-07-24 20:17:52 +0800782#else
783 if (f2fs_sb_has_quota_ino(sbi->sb) && !f2fs_readonly(sbi->sb)) {
784 f2fs_msg(sbi->sb, KERN_INFO,
785 "Filesystem with quota feature cannot be mounted RDWR "
786 "without CONFIG_QUOTA");
787 return -EINVAL;
788 }
Chao Yu4ddc1b22018-07-26 07:19:48 +0800789 if (f2fs_sb_has_project_quota(sbi->sb) && !f2fs_readonly(sbi->sb)) {
790 f2fs_msg(sb, KERN_ERR,
791 "Filesystem with project quota feature cannot be "
792 "mounted RDWR without CONFIG_QUOTA");
793 return -EINVAL;
794 }
Chao Yu4b2414d2017-08-08 10:54:31 +0800795#endif
Jaegeuk Kimec915382016-12-21 17:09:19 -0800796
797 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
798 f2fs_msg(sb, KERN_ERR,
799 "Should set mode=lfs with %uKB-sized IO",
800 F2FS_IO_SIZE_KB(sbi));
801 return -EINVAL;
802 }
Chao Yu6afc6622017-09-06 21:59:50 +0800803
804 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
Chao Yu4d817ae2018-01-27 17:29:48 +0800805 if (!f2fs_sb_has_extra_attr(sb) ||
806 !f2fs_sb_has_flexible_inline_xattr(sb)) {
807 f2fs_msg(sb, KERN_ERR,
808 "extra_attr or flexible_inline_xattr "
809 "feature is off");
810 return -EINVAL;
811 }
Chao Yu6afc6622017-09-06 21:59:50 +0800812 if (!test_opt(sbi, INLINE_XATTR)) {
813 f2fs_msg(sb, KERN_ERR,
814 "inline_xattr_size option should be "
815 "set with inline_xattr option");
816 return -EINVAL;
817 }
Chao Yu63189b72018-03-08 14:22:56 +0800818 if (!F2FS_OPTION(sbi).inline_xattr_size ||
819 F2FS_OPTION(sbi).inline_xattr_size >=
820 DEF_ADDRS_PER_INODE -
Chao Yu6afc6622017-09-06 21:59:50 +0800821 F2FS_TOTAL_EXTRA_ATTR_SIZE -
822 DEF_INLINE_RESERVED_SIZE -
823 DEF_MIN_INLINE_SIZE) {
824 f2fs_msg(sb, KERN_ERR,
825 "inline xattr size is out of range");
826 return -EINVAL;
827 }
828 }
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900829
830 /* Not pass down write hints if the number of active logs is lesser
831 * than NR_CURSEG_TYPE.
832 */
Chao Yu63189b72018-03-08 14:22:56 +0800833 if (F2FS_OPTION(sbi).active_logs != NR_CURSEG_TYPE)
834 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
Namjae Jeon696c0182013-06-16 09:48:48 +0900835 return 0;
836}
837
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900838static struct inode *f2fs_alloc_inode(struct super_block *sb)
839{
840 struct f2fs_inode_info *fi;
841
Chao Yua0acdfe2013-12-05 09:54:00 +0800842 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900843 if (!fi)
844 return NULL;
845
846 init_once((void *) fi);
847
Masanari Iida434720f2013-03-19 08:03:35 +0900848 /* Initialize f2fs-specific inode info */
Jaegeuk Kim204706c2016-12-02 15:11:32 -0800849 atomic_set(&fi->dirty_pages, 0);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900850 init_rwsem(&fi->i_sem);
Chao Yu2710fd72015-12-15 13:30:45 +0800851 INIT_LIST_HEAD(&fi->dirty_list);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700852 INIT_LIST_HEAD(&fi->gdirty_list);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700853 INIT_LIST_HEAD(&fi->inmem_ilist);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700854 INIT_LIST_HEAD(&fi->inmem_pages);
855 mutex_init(&fi->inmem_lock);
Chao Yub2532c62018-04-24 10:55:28 +0800856 init_rwsem(&fi->i_gc_rwsem[READ]);
857 init_rwsem(&fi->i_gc_rwsem[WRITE]);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800858 init_rwsem(&fi->i_mmap_sem);
Yunlei He27161f12017-09-07 10:40:54 +0800859 init_rwsem(&fi->i_xattr_sem);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900860
Jaegeuk Kimab9fa662014-02-27 20:09:05 +0900861 /* Will be used by directory only */
862 fi->i_dir_level = F2FS_SB(sb)->dir_level;
Chao Yuf2470372017-07-19 00:19:05 +0800863
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900864 return &fi->vfs_inode;
865}
866
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900867static int f2fs_drop_inode(struct inode *inode)
868{
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000869 int ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900870 /*
871 * This is to avoid a deadlock condition like below.
872 * writeback_single_inode(inode)
873 * - f2fs_write_data_page
874 * - f2fs_gc -> iput -> evict
875 * - inode_wait_for_writeback(inode)
876 */
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700877 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700878 if (!inode->i_nlink && !is_bad_inode(inode)) {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700879 /* to avoid evict_inode call simultaneously */
880 atomic_inc(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700881 spin_unlock(&inode->i_lock);
882
883 /* some remained atomic pages should discarded */
884 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +0800885 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700886
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700887 /* should remain fi->extent_tree for writepage */
888 f2fs_destroy_extent_node(inode);
889
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700890 sb_start_intwrite(inode->i_sb);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700891 f2fs_i_size_write(inode, 0);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700892
893 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700894 f2fs_truncate(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700895
896 sb_end_intwrite(inode->i_sb);
897
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700898 spin_lock(&inode->i_lock);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700899 atomic_dec(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700900 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000901 trace_f2fs_drop_inode(inode, 0);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900902 return 0;
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700903 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000904 ret = generic_drop_inode(inode);
905 trace_f2fs_drop_inode(inode, ret);
906 return ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900907}
908
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700909int f2fs_inode_dirtied(struct inode *inode, bool sync)
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700910{
911 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700912 int ret = 0;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700913
914 spin_lock(&sbi->inode_lock[DIRTY_META]);
915 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700916 ret = 1;
917 } else {
918 set_inode_flag(inode, FI_DIRTY_INODE);
919 stat_inc_dirty_inode(sbi, DIRTY_META);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700920 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700921 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
922 list_add_tail(&F2FS_I(inode)->gdirty_list,
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700923 &sbi->inode_list[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700924 inc_page_count(sbi, F2FS_DIRTY_IMETA);
925 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700926 spin_unlock(&sbi->inode_lock[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700927 return ret;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700928}
929
930void f2fs_inode_synced(struct inode *inode)
931{
932 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
933
934 spin_lock(&sbi->inode_lock[DIRTY_META]);
935 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
936 spin_unlock(&sbi->inode_lock[DIRTY_META]);
937 return;
938 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700939 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
940 list_del_init(&F2FS_I(inode)->gdirty_list);
941 dec_page_count(sbi, F2FS_DIRTY_IMETA);
942 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700943 clear_inode_flag(inode, FI_DIRTY_INODE);
944 clear_inode_flag(inode, FI_AUTO_RECOVER);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700945 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
946 spin_unlock(&sbi->inode_lock[DIRTY_META]);
947}
948
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900949/*
950 * f2fs_dirty_inode() is called from __mark_inode_dirty()
951 *
952 * We should call set_dirty_inode to write the dirty inode through write_inode.
953 */
954static void f2fs_dirty_inode(struct inode *inode, int flags)
955{
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700956 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
957
958 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
959 inode->i_ino == F2FS_META_INO(sbi))
960 return;
961
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700962 if (flags == I_DIRTY_TIME)
963 return;
964
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700965 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
966 clear_inode_flag(inode, FI_AUTO_RECOVER);
967
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700968 f2fs_inode_dirtied(inode, false);
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900969}
970
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900971static void f2fs_i_callback(struct rcu_head *head)
972{
973 struct inode *inode = container_of(head, struct inode, i_rcu);
974 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
975}
976
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900977static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900978{
979 call_rcu(&inode->i_rcu, f2fs_i_callback);
980}
981
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700982static void destroy_percpu_info(struct f2fs_sb_info *sbi)
983{
Jaegeuk Kim41382ec2016-05-16 11:06:50 -0700984 percpu_counter_destroy(&sbi->alloc_valid_block_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -0700985 percpu_counter_destroy(&sbi->total_valid_inode_count);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700986}
987
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700988static void destroy_device_list(struct f2fs_sb_info *sbi)
989{
990 int i;
991
992 for (i = 0; i < sbi->s_ndevs; i++) {
993 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
994#ifdef CONFIG_BLK_DEV_ZONED
995 kfree(FDEV(i).blkz_type);
996#endif
997 }
998 kfree(sbi->devs);
999}
1000
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001001static void f2fs_put_super(struct super_block *sb)
1002{
1003 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yua3981012017-06-14 17:39:46 +08001004 int i;
Chao Yucf5c7592017-10-04 09:08:37 +08001005 bool dropped;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001006
Chao Yu0abd6752017-07-09 00:13:07 +08001007 f2fs_quota_off_umount(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001008
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001009 /* prevent remaining shrinker jobs */
1010 mutex_lock(&sbi->umount_mutex);
1011
Jaegeuk Kim85dc2f22015-01-14 17:41:41 -08001012 /*
1013 * We don't need to do checkpoint when superblock is clean.
1014 * But, the previous checkpoint was not done by umount, it needs to do
1015 * clean checkpoint again.
1016 */
Chao Yucaf00472015-01-28 17:48:42 +08001017 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
Chao Yuaaec2b12016-09-20 11:04:18 +08001018 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -07001019 struct cp_control cpc = {
1020 .reason = CP_UMOUNT,
1021 };
Chao Yu4d57b862018-05-30 00:20:41 +08001022 f2fs_write_checkpoint(sbi, &cpc);
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -07001023 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001024
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08001025 /* be sure to wait for any on-going discard commands */
Chao Yucf5c7592017-10-04 09:08:37 +08001026 dropped = f2fs_wait_discard_bios(sbi);
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08001027
Chao Yu7d20c8a2018-09-04 03:52:17 +08001028 if ((f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) &&
1029 !sbi->discard_blks && !dropped) {
Chao Yu1f43e2a2017-04-28 13:56:08 +08001030 struct cp_control cpc = {
1031 .reason = CP_UMOUNT | CP_TRIMMED,
1032 };
Chao Yu4d57b862018-05-30 00:20:41 +08001033 f2fs_write_checkpoint(sbi, &cpc);
Chao Yu1f43e2a2017-04-28 13:56:08 +08001034 }
1035
Chao Yu4d57b862018-05-30 00:20:41 +08001036 /* f2fs_write_checkpoint can update stat informaion */
Jaegeuk Kimeca616f2015-06-15 14:52:29 -07001037 f2fs_destroy_stats(sbi);
1038
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001039 /*
1040 * normally superblock is clean, so we need to release this.
1041 * In addition, EIO will skip do checkpoint, we need this as well.
1042 */
Chao Yu4d57b862018-05-30 00:20:41 +08001043 f2fs_release_ino_entry(sbi, true);
Jaegeuk Kim6f12ac22014-08-19 09:48:22 -07001044
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001045 f2fs_leave_shrinker(sbi);
1046 mutex_unlock(&sbi->umount_mutex);
1047
Jaegeuk Kim17c19122016-01-29 08:57:59 -08001048 /* our cp_error case, we can wait for any writeback page */
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001049 f2fs_flush_merged_writes(sbi);
Jaegeuk Kim17c19122016-01-29 08:57:59 -08001050
Chao Yu50fa53e2018-08-02 23:03:19 +08001051 f2fs_wait_on_all_pages_writeback(sbi);
1052
1053 f2fs_bug_on(sbi, sbi->fsync_node_num);
1054
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001055 iput(sbi->node_inode);
1056 iput(sbi->meta_inode);
1057
1058 /* destroy f2fs internal modules */
Chao Yu4d57b862018-05-30 00:20:41 +08001059 f2fs_destroy_node_manager(sbi);
1060 f2fs_destroy_segment_manager(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001061
1062 kfree(sbi->ckpt);
Chao Yua3981012017-06-14 17:39:46 +08001063
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07001064 f2fs_unregister_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001065
1066 sb->s_fs_info = NULL;
Keith Mok43b65732016-03-02 12:04:24 -08001067 if (sbi->s_chksum_driver)
1068 crypto_free_shash(sbi->s_chksum_driver);
Yunlei Heb39f0de2015-12-15 17:17:20 +08001069 kfree(sbi->raw_super);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001070
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001071 destroy_device_list(sbi);
Chao Yub6895e82017-02-27 18:43:12 +08001072 mempool_destroy(sbi->write_io_dummy);
Chao Yu4b2414d2017-08-08 10:54:31 +08001073#ifdef CONFIG_QUOTA
1074 for (i = 0; i < MAXQUOTAS; i++)
Chao Yu63189b72018-03-08 14:22:56 +08001075 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
Chao Yu4b2414d2017-08-08 10:54:31 +08001076#endif
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001077 destroy_percpu_info(sbi);
Jaegeuk Kima912b542017-05-10 11:18:25 -07001078 for (i = 0; i < NR_PAGE_TYPE; i++)
1079 kfree(sbi->write_io[i]);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001080 kfree(sbi);
1081}
1082
1083int f2fs_sync_fs(struct super_block *sb, int sync)
1084{
1085 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yuc34f42e2015-12-23 17:50:30 +08001086 int err = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001087
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001088 if (unlikely(f2fs_cp_error(sbi)))
1089 return 0;
1090
Namjae Jeona2a4a7e2013-04-20 01:28:40 +09001091 trace_f2fs_sync_fs(sb, sync);
1092
Chao Yu4b2414d2017-08-08 10:54:31 +08001093 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1094 return -EAGAIN;
1095
Jaegeuk Kimb7473752013-04-01 08:32:21 +09001096 if (sync) {
Jaegeuk Kimd5053a342014-10-30 22:47:03 -07001097 struct cp_control cpc;
1098
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001099 cpc.reason = __get_cp_reason(sbi);
1100
Jaegeuk Kimb7473752013-04-01 08:32:21 +09001101 mutex_lock(&sbi->gc_mutex);
Chao Yu4d57b862018-05-30 00:20:41 +08001102 err = f2fs_write_checkpoint(sbi, &cpc);
Jaegeuk Kimb7473752013-04-01 08:32:21 +09001103 mutex_unlock(&sbi->gc_mutex);
Jaegeuk Kimb7473752013-04-01 08:32:21 +09001104 }
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001105 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001106
Chao Yuc34f42e2015-12-23 17:50:30 +08001107 return err;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001108}
1109
Changman Leed6212a52013-01-29 18:30:07 +09001110static int f2fs_freeze(struct super_block *sb)
1111{
Jaegeuk Kim77888c12013-05-20 20:28:47 +09001112 if (f2fs_readonly(sb))
Changman Leed6212a52013-01-29 18:30:07 +09001113 return 0;
1114
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -07001115 /* IO error happened before */
1116 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1117 return -EIO;
1118
1119 /* must be clean, since sync_filesystem() was already called */
1120 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1121 return -EINVAL;
1122 return 0;
Changman Leed6212a52013-01-29 18:30:07 +09001123}
1124
1125static int f2fs_unfreeze(struct super_block *sb)
1126{
1127 return 0;
1128}
1129
Chao Yuddc34e32017-07-29 00:32:53 +08001130#ifdef CONFIG_QUOTA
1131static int f2fs_statfs_project(struct super_block *sb,
1132 kprojid_t projid, struct kstatfs *buf)
1133{
1134 struct kqid qid;
1135 struct dquot *dquot;
1136 u64 limit;
1137 u64 curblock;
1138
1139 qid = make_kqid_projid(projid);
1140 dquot = dqget(sb, qid);
1141 if (IS_ERR(dquot))
1142 return PTR_ERR(dquot);
Sheng Yong955ac6e2018-07-24 20:17:53 +08001143 spin_lock(&dquot->dq_dqb_lock);
Chao Yuddc34e32017-07-29 00:32:53 +08001144
1145 limit = (dquot->dq_dqb.dqb_bsoftlimit ?
1146 dquot->dq_dqb.dqb_bsoftlimit :
1147 dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
1148 if (limit && buf->f_blocks > limit) {
1149 curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits;
1150 buf->f_blocks = limit;
1151 buf->f_bfree = buf->f_bavail =
1152 (buf->f_blocks > curblock) ?
1153 (buf->f_blocks - curblock) : 0;
1154 }
1155
1156 limit = dquot->dq_dqb.dqb_isoftlimit ?
1157 dquot->dq_dqb.dqb_isoftlimit :
1158 dquot->dq_dqb.dqb_ihardlimit;
1159 if (limit && buf->f_files > limit) {
1160 buf->f_files = limit;
1161 buf->f_ffree =
1162 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1163 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1164 }
1165
Sheng Yong955ac6e2018-07-24 20:17:53 +08001166 spin_unlock(&dquot->dq_dqb_lock);
Chao Yuddc34e32017-07-29 00:32:53 +08001167 dqput(dquot);
1168 return 0;
1169}
1170#endif
1171
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001172static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1173{
1174 struct super_block *sb = dentry->d_sb;
1175 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1176 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Jaegeuk Kimf66c0272018-01-03 10:55:07 -08001177 block_t total_count, user_block_count, start_count;
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -07001178 u64 avail_node_count;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001179
1180 total_count = le64_to_cpu(sbi->raw_super->block_count);
1181 user_block_count = sbi->user_block_count;
1182 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001183 buf->f_type = F2FS_SUPER_MAGIC;
1184 buf->f_bsize = sbi->blocksize;
1185
1186 buf->f_blocks = total_count - start_count;
Jaegeuk Kimf66c0272018-01-03 10:55:07 -08001187 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
Yunlong Song80d42142017-10-27 20:45:05 +08001188 sbi->current_reserved_blocks;
Chao Yu63189b72018-03-08 14:22:56 +08001189 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1190 buf->f_bavail = buf->f_bfree -
1191 F2FS_OPTION(sbi).root_reserved_blocks;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08001192 else
1193 buf->f_bavail = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001194
Chao Yu292c196a2017-11-16 16:59:14 +08001195 avail_node_count = sbi->total_node_count - sbi->nquota_files -
1196 F2FS_RESERVED_NODE_NUM;
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -07001197
1198 if (avail_node_count > user_block_count) {
1199 buf->f_files = user_block_count;
1200 buf->f_ffree = buf->f_bavail;
1201 } else {
1202 buf->f_files = avail_node_count;
1203 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
1204 buf->f_bavail);
1205 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001206
Jaegeuk Kim5a20d332013-03-03 13:58:05 +09001207 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001208 buf->f_fsid.val[0] = (u32)id;
1209 buf->f_fsid.val[1] = (u32)(id >> 32);
1210
Chao Yuddc34e32017-07-29 00:32:53 +08001211#ifdef CONFIG_QUOTA
1212 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1213 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1214 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1215 }
1216#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001217 return 0;
1218}
1219
Chao Yu4b2414d2017-08-08 10:54:31 +08001220static inline void f2fs_show_quota_options(struct seq_file *seq,
1221 struct super_block *sb)
1222{
1223#ifdef CONFIG_QUOTA
1224 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1225
Chao Yu63189b72018-03-08 14:22:56 +08001226 if (F2FS_OPTION(sbi).s_jquota_fmt) {
Chao Yu4b2414d2017-08-08 10:54:31 +08001227 char *fmtname = "";
1228
Chao Yu63189b72018-03-08 14:22:56 +08001229 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
Chao Yu4b2414d2017-08-08 10:54:31 +08001230 case QFMT_VFS_OLD:
1231 fmtname = "vfsold";
1232 break;
1233 case QFMT_VFS_V0:
1234 fmtname = "vfsv0";
1235 break;
1236 case QFMT_VFS_V1:
1237 fmtname = "vfsv1";
1238 break;
1239 }
1240 seq_printf(seq, ",jqfmt=%s", fmtname);
1241 }
1242
Chao Yu63189b72018-03-08 14:22:56 +08001243 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1244 seq_show_option(seq, "usrjquota",
1245 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
Chao Yu4b2414d2017-08-08 10:54:31 +08001246
Chao Yu63189b72018-03-08 14:22:56 +08001247 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1248 seq_show_option(seq, "grpjquota",
1249 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
Chao Yu4b2414d2017-08-08 10:54:31 +08001250
Chao Yu63189b72018-03-08 14:22:56 +08001251 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1252 seq_show_option(seq, "prjjquota",
1253 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
Chao Yu4b2414d2017-08-08 10:54:31 +08001254#endif
1255}
1256
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001257static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1258{
1259 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1260
Jaegeuk Kim6aefd932015-10-05 11:02:54 -07001261 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
1262 if (test_opt(sbi, FORCE_FG_GC))
1263 seq_printf(seq, ",background_gc=%s", "sync");
1264 else
1265 seq_printf(seq, ",background_gc=%s", "on");
1266 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +09001267 seq_printf(seq, ",background_gc=%s", "off");
Jaegeuk Kim6aefd932015-10-05 11:02:54 -07001268 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001269 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1270 seq_puts(seq, ",disable_roll_forward");
1271 if (test_opt(sbi, DISCARD))
1272 seq_puts(seq, ",discard");
1273 if (test_opt(sbi, NOHEAP))
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001274 seq_puts(seq, ",no_heap");
1275 else
1276 seq_puts(seq, ",heap");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001277#ifdef CONFIG_F2FS_FS_XATTR
1278 if (test_opt(sbi, XATTR_USER))
1279 seq_puts(seq, ",user_xattr");
1280 else
1281 seq_puts(seq, ",nouser_xattr");
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001282 if (test_opt(sbi, INLINE_XATTR))
1283 seq_puts(seq, ",inline_xattr");
Chao Yu23cf7212017-02-15 10:34:45 +08001284 else
1285 seq_puts(seq, ",noinline_xattr");
Chao Yu6afc6622017-09-06 21:59:50 +08001286 if (test_opt(sbi, INLINE_XATTR_SIZE))
1287 seq_printf(seq, ",inline_xattr_size=%u",
Chao Yu63189b72018-03-08 14:22:56 +08001288 F2FS_OPTION(sbi).inline_xattr_size);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001289#endif
1290#ifdef CONFIG_F2FS_FS_POSIX_ACL
1291 if (test_opt(sbi, POSIX_ACL))
1292 seq_puts(seq, ",acl");
1293 else
1294 seq_puts(seq, ",noacl");
1295#endif
1296 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +01001297 seq_puts(seq, ",disable_ext_identify");
Huajun Li8274de72013-11-10 23:13:17 +08001298 if (test_opt(sbi, INLINE_DATA))
1299 seq_puts(seq, ",inline_data");
Wanpeng Li75342792015-03-24 10:20:27 +08001300 else
1301 seq_puts(seq, ",noinline_data");
Chao Yu5efd3c62014-09-24 18:16:13 +08001302 if (test_opt(sbi, INLINE_DENTRY))
1303 seq_puts(seq, ",inline_dentry");
Chao Yu97c17942016-05-09 19:56:34 +08001304 else
1305 seq_puts(seq, ",noinline_dentry");
Gu Zhengb270ad62014-04-11 17:49:55 +08001306 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09001307 seq_puts(seq, ",flush_merge");
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -07001308 if (test_opt(sbi, NOBARRIER))
1309 seq_puts(seq, ",nobarrier");
Jaegeuk Kimd5053a342014-10-30 22:47:03 -07001310 if (test_opt(sbi, FASTBOOT))
1311 seq_puts(seq, ",fastboot");
Chao Yu89672152015-02-05 17:55:51 +08001312 if (test_opt(sbi, EXTENT_CACHE))
1313 seq_puts(seq, ",extent_cache");
Jaegeuk Kim7daaea22015-06-25 17:43:04 -07001314 else
1315 seq_puts(seq, ",noextent_cache");
Chao Yu343f40f2015-12-16 13:12:16 +08001316 if (test_opt(sbi, DATA_FLUSH))
1317 seq_puts(seq, ",data_flush");
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001318
1319 seq_puts(seq, ",mode=");
1320 if (test_opt(sbi, ADAPTIVE))
1321 seq_puts(seq, "adaptive");
1322 else if (test_opt(sbi, LFS))
1323 seq_puts(seq, "lfs");
Chao Yu63189b72018-03-08 14:22:56 +08001324 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08001325 if (test_opt(sbi, RESERVE_ROOT))
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -08001326 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
Chao Yu63189b72018-03-08 14:22:56 +08001327 F2FS_OPTION(sbi).root_reserved_blocks,
1328 from_kuid_munged(&init_user_ns,
1329 F2FS_OPTION(sbi).s_resuid),
1330 from_kgid_munged(&init_user_ns,
1331 F2FS_OPTION(sbi).s_resgid));
Jaegeuk Kimec915382016-12-21 17:09:19 -08001332 if (F2FS_IO_SIZE_BITS(sbi))
Chengguang Xuc6b18672018-09-22 22:43:09 +08001333 seq_printf(seq, ",io_bits=%u",
1334 F2FS_OPTION(sbi).write_io_size_bits);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +08001335#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yud4945002018-08-08 17:36:41 +08001336 if (test_opt(sbi, FAULT_INJECTION)) {
Chao Yu44529f82017-06-12 09:44:24 +08001337 seq_printf(seq, ",fault_injection=%u",
Chao Yu63189b72018-03-08 14:22:56 +08001338 F2FS_OPTION(sbi).fault_info.inject_rate);
Chao Yud4945002018-08-08 17:36:41 +08001339 seq_printf(seq, ",fault_type=%u",
1340 F2FS_OPTION(sbi).fault_info.inject_type);
1341 }
Kaixu Xia0cc0dec2017-01-27 09:35:37 +08001342#endif
Chao Yu0abd6752017-07-09 00:13:07 +08001343#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +08001344 if (test_opt(sbi, QUOTA))
1345 seq_puts(seq, ",quota");
Chao Yu0abd6752017-07-09 00:13:07 +08001346 if (test_opt(sbi, USRQUOTA))
1347 seq_puts(seq, ",usrquota");
1348 if (test_opt(sbi, GRPQUOTA))
1349 seq_puts(seq, ",grpquota");
Chao Yu5c571322017-07-26 00:01:41 +08001350 if (test_opt(sbi, PRJQUOTA))
1351 seq_puts(seq, ",prjquota");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001352#endif
Chao Yu4b2414d2017-08-08 10:54:31 +08001353 f2fs_show_quota_options(seq, sbi->sb);
Chao Yu63189b72018-03-08 14:22:56 +08001354 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER)
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001355 seq_printf(seq, ",whint_mode=%s", "user-based");
Chao Yu63189b72018-03-08 14:22:56 +08001356 else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS)
Hyunchul Leef2e703f2018-01-31 11:36:58 +09001357 seq_printf(seq, ",whint_mode=%s", "fs-based");
Sheng Yongff62af22018-03-15 18:51:42 +08001358#ifdef CONFIG_F2FS_FS_ENCRYPTION
1359 if (F2FS_OPTION(sbi).test_dummy_encryption)
1360 seq_puts(seq, ",test_dummy_encryption");
1361#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001362
Chao Yu63189b72018-03-08 14:22:56 +08001363 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
Jaegeuk Kim07939622018-02-18 08:50:49 -08001364 seq_printf(seq, ",alloc_mode=%s", "default");
Chao Yu63189b72018-03-08 14:22:56 +08001365 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
Jaegeuk Kim07939622018-02-18 08:50:49 -08001366 seq_printf(seq, ",alloc_mode=%s", "reuse");
Junling Zheng93cf93f2018-03-07 12:07:49 +08001367
Chao Yu63189b72018-03-08 14:22:56 +08001368 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
Junling Zheng93cf93f2018-03-07 12:07:49 +08001369 seq_printf(seq, ",fsync_mode=%s", "posix");
Chao Yu63189b72018-03-08 14:22:56 +08001370 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
Junling Zheng93cf93f2018-03-07 12:07:49 +08001371 seq_printf(seq, ",fsync_mode=%s", "strict");
Sahitya Tummaladc132802018-07-02 11:37:40 +05301372 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
1373 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001374 return 0;
1375}
1376
Yunlei He498c5e92015-05-07 18:11:37 +08001377static void default_options(struct f2fs_sb_info *sbi)
1378{
1379 /* init some FS parameters */
Chao Yu63189b72018-03-08 14:22:56 +08001380 F2FS_OPTION(sbi).active_logs = NR_CURSEG_TYPE;
1381 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
1382 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1383 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
1384 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
Sheng Yongff62af22018-03-15 18:51:42 +08001385 F2FS_OPTION(sbi).test_dummy_encryption = false;
Chao Yu0aa7e0f2018-06-06 23:55:02 +08001386 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
1387 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
Yunlei He498c5e92015-05-07 18:11:37 +08001388
1389 set_opt(sbi, BG_GC);
Chao Yu39133a52017-02-08 17:39:44 +08001390 set_opt(sbi, INLINE_XATTR);
Yunlei He498c5e92015-05-07 18:11:37 +08001391 set_opt(sbi, INLINE_DATA);
Chao Yu97c17942016-05-09 19:56:34 +08001392 set_opt(sbi, INLINE_DENTRY);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07001393 set_opt(sbi, EXTENT_CACHE);
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001394 set_opt(sbi, NOHEAP);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001395 sbi->sb->s_flags |= SB_LAZYTIME;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -07001396 set_opt(sbi, FLUSH_MERGE);
Chao Yu7d20c8a2018-09-04 03:52:17 +08001397 set_opt(sbi, DISCARD);
Chao Yua39e53652018-07-05 14:24:11 +08001398 if (f2fs_sb_has_blkzoned(sbi->sb))
1399 set_opt_mode(sbi, F2FS_MOUNT_LFS);
1400 else
Jaegeuk Kim52763a42016-06-13 09:47:48 -07001401 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
Yunlei He498c5e92015-05-07 18:11:37 +08001402
1403#ifdef CONFIG_F2FS_FS_XATTR
1404 set_opt(sbi, XATTR_USER);
1405#endif
1406#ifdef CONFIG_F2FS_FS_POSIX_ACL
1407 set_opt(sbi, POSIX_ACL);
1408#endif
Chao Yu36dbd322016-09-26 19:45:05 +08001409
Chao Yud4945002018-08-08 17:36:41 +08001410 f2fs_build_fault_attr(sbi, 0, 0);
Yunlei He498c5e92015-05-07 18:11:37 +08001411}
1412
Jaegeuk Kimea676732017-10-06 09:14:28 -07001413#ifdef CONFIG_QUOTA
1414static int f2fs_enable_quotas(struct super_block *sb);
1415#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001416static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1417{
1418 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1419 struct f2fs_mount_info org_mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +08001420 unsigned long old_sb_flags;
Chao Yu63189b72018-03-08 14:22:56 +08001421 int err;
Gu Zheng876dc592014-04-11 17:50:00 +08001422 bool need_restart_gc = false;
1423 bool need_stop_gc = false;
Chao Yu9cd81ce2015-09-18 16:55:26 +08001424 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
Chao Yu4b2414d2017-08-08 10:54:31 +08001425#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +08001426 int i, j;
1427#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001428
1429 /*
1430 * Save the old mount options in case we
1431 * need to restore them.
1432 */
1433 org_mount_opt = sbi->mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +08001434 old_sb_flags = sb->s_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +09001435
Chao Yu4b2414d2017-08-08 10:54:31 +08001436#ifdef CONFIG_QUOTA
Chao Yu63189b72018-03-08 14:22:56 +08001437 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
Chao Yu4b2414d2017-08-08 10:54:31 +08001438 for (i = 0; i < MAXQUOTAS; i++) {
Chao Yu63189b72018-03-08 14:22:56 +08001439 if (F2FS_OPTION(sbi).s_qf_names[i]) {
1440 org_mount_opt.s_qf_names[i] =
1441 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
1442 GFP_KERNEL);
1443 if (!org_mount_opt.s_qf_names[i]) {
Chao Yu4b2414d2017-08-08 10:54:31 +08001444 for (j = 0; j < i; j++)
Chao Yu63189b72018-03-08 14:22:56 +08001445 kfree(org_mount_opt.s_qf_names[j]);
Chao Yu4b2414d2017-08-08 10:54:31 +08001446 return -ENOMEM;
1447 }
1448 } else {
Chao Yu63189b72018-03-08 14:22:56 +08001449 org_mount_opt.s_qf_names[i] = NULL;
Chao Yu4b2414d2017-08-08 10:54:31 +08001450 }
1451 }
1452#endif
1453
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001454 /* recover superblocks we couldn't write due to previous RO mount */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001455 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001456 err = f2fs_commit_super(sbi, false);
1457 f2fs_msg(sb, KERN_INFO,
1458 "Try to recover all the superblocks, ret: %d", err);
1459 if (!err)
1460 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1461 }
1462
Yunlei He498c5e92015-05-07 18:11:37 +08001463 default_options(sbi);
Chao Yu26666c82014-09-15 18:04:44 +08001464
Namjae Jeon696c0182013-06-16 09:48:48 +09001465 /* parse mount options */
1466 err = parse_options(sb, data);
1467 if (err)
1468 goto restore_opts;
1469
1470 /*
1471 * Previous and new state of filesystem is RO,
Gu Zheng876dc592014-04-11 17:50:00 +08001472 * so skip checking GC and FLUSH_MERGE conditions.
Namjae Jeon696c0182013-06-16 09:48:48 +09001473 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001474 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
Namjae Jeon696c0182013-06-16 09:48:48 +09001475 goto skip;
1476
Jaegeuk Kimea676732017-10-06 09:14:28 -07001477#ifdef CONFIG_QUOTA
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001478 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
Chao Yu0abd6752017-07-09 00:13:07 +08001479 err = dquot_suspend(sb, -1);
1480 if (err < 0)
1481 goto restore_opts;
Jaegeuk Kim62793982018-01-02 11:03:19 -08001482 } else if (f2fs_readonly(sb) && !(*flags & MS_RDONLY)) {
Chao Yu0abd6752017-07-09 00:13:07 +08001483 /* dquot_resume needs RW */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001484 sb->s_flags &= ~SB_RDONLY;
Jaegeuk Kimea676732017-10-06 09:14:28 -07001485 if (sb_any_quota_suspended(sb)) {
1486 dquot_resume(sb, -1);
1487 } else if (f2fs_sb_has_quota_ino(sb)) {
1488 err = f2fs_enable_quotas(sb);
1489 if (err)
1490 goto restore_opts;
1491 }
Chao Yu0abd6752017-07-09 00:13:07 +08001492 }
Jaegeuk Kimea676732017-10-06 09:14:28 -07001493#endif
Chao Yu9cd81ce2015-09-18 16:55:26 +08001494 /* disallow enable/disable extent_cache dynamically */
1495 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
1496 err = -EINVAL;
1497 f2fs_msg(sbi->sb, KERN_WARNING,
1498 "switch extent_cache option is not allowed");
1499 goto restore_opts;
1500 }
1501
Namjae Jeon696c0182013-06-16 09:48:48 +09001502 /*
1503 * We stop the GC thread if FS is mounted as RO
1504 * or if background_gc = off is passed in mount
1505 * option. Also sync the filesystem.
1506 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001507 if ((*flags & SB_RDONLY) || !test_opt(sbi, BG_GC)) {
Namjae Jeon696c0182013-06-16 09:48:48 +09001508 if (sbi->gc_thread) {
Chao Yu4d57b862018-05-30 00:20:41 +08001509 f2fs_stop_gc_thread(sbi);
Gu Zheng876dc592014-04-11 17:50:00 +08001510 need_restart_gc = true;
Namjae Jeon696c0182013-06-16 09:48:48 +09001511 }
Chao Yuaba291b2014-11-18 11:17:20 +08001512 } else if (!sbi->gc_thread) {
Chao Yu4d57b862018-05-30 00:20:41 +08001513 err = f2fs_start_gc_thread(sbi);
Namjae Jeon696c0182013-06-16 09:48:48 +09001514 if (err)
1515 goto restore_opts;
Gu Zheng876dc592014-04-11 17:50:00 +08001516 need_stop_gc = true;
1517 }
1518
Chao Yu63189b72018-03-08 14:22:56 +08001519 if (*flags & SB_RDONLY ||
1520 F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
Jaegeuk Kimfaa0e552016-03-24 10:29:39 -07001521 writeback_inodes_sb(sb, WB_REASON_SYNC);
1522 sync_inodes_sb(sb);
1523
1524 set_sbi_flag(sbi, SBI_IS_DIRTY);
1525 set_sbi_flag(sbi, SBI_IS_CLOSE);
1526 f2fs_sync_fs(sb, 1);
1527 clear_sbi_flag(sbi, SBI_IS_CLOSE);
1528 }
1529
Gu Zheng876dc592014-04-11 17:50:00 +08001530 /*
1531 * We stop issue flush thread if FS is mounted as RO
1532 * or if flush_merge is not passed in mount option.
1533 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001534 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -08001535 clear_opt(sbi, FLUSH_MERGE);
Chao Yu4d57b862018-05-30 00:20:41 +08001536 f2fs_destroy_flush_cmd_control(sbi, false);
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -08001537 } else {
Chao Yu4d57b862018-05-30 00:20:41 +08001538 err = f2fs_create_flush_cmd_control(sbi);
Gu Zheng2163d192014-04-27 14:21:33 +08001539 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08001540 goto restore_gc;
Namjae Jeon696c0182013-06-16 09:48:48 +09001541 }
1542skip:
Chao Yu4b2414d2017-08-08 10:54:31 +08001543#ifdef CONFIG_QUOTA
1544 /* Release old quota file names */
1545 for (i = 0; i < MAXQUOTAS; i++)
Chao Yu63189b72018-03-08 14:22:56 +08001546 kfree(org_mount_opt.s_qf_names[i]);
Chao Yu4b2414d2017-08-08 10:54:31 +08001547#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001548 /* Update the POSIXACL Flag */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001549 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
1550 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001551
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08001552 limit_reserve_root(sbi);
Namjae Jeon696c0182013-06-16 09:48:48 +09001553 return 0;
Gu Zheng876dc592014-04-11 17:50:00 +08001554restore_gc:
1555 if (need_restart_gc) {
Chao Yu4d57b862018-05-30 00:20:41 +08001556 if (f2fs_start_gc_thread(sbi))
Gu Zheng876dc592014-04-11 17:50:00 +08001557 f2fs_msg(sbi->sb, KERN_WARNING,
arter97e1c42042014-08-06 23:22:50 +09001558 "background gc thread has stopped");
Gu Zheng876dc592014-04-11 17:50:00 +08001559 } else if (need_stop_gc) {
Chao Yu4d57b862018-05-30 00:20:41 +08001560 f2fs_stop_gc_thread(sbi);
Gu Zheng876dc592014-04-11 17:50:00 +08001561 }
Namjae Jeon696c0182013-06-16 09:48:48 +09001562restore_opts:
Chao Yu4b2414d2017-08-08 10:54:31 +08001563#ifdef CONFIG_QUOTA
Chao Yu63189b72018-03-08 14:22:56 +08001564 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
Chao Yu4b2414d2017-08-08 10:54:31 +08001565 for (i = 0; i < MAXQUOTAS; i++) {
Chao Yu63189b72018-03-08 14:22:56 +08001566 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1567 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
Chao Yu4b2414d2017-08-08 10:54:31 +08001568 }
1569#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001570 sbi->mount_opt = org_mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +08001571 sb->s_flags = old_sb_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +09001572 return err;
1573}
1574
Chao Yu0abd6752017-07-09 00:13:07 +08001575#ifdef CONFIG_QUOTA
1576/* Read data from quotafile */
1577static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
1578 size_t len, loff_t off)
1579{
1580 struct inode *inode = sb_dqopt(sb)->files[type];
1581 struct address_space *mapping = inode->i_mapping;
1582 block_t blkidx = F2FS_BYTES_TO_BLK(off);
1583 int offset = off & (sb->s_blocksize - 1);
1584 int tocopy;
1585 size_t toread;
1586 loff_t i_size = i_size_read(inode);
1587 struct page *page;
1588 char *kaddr;
1589
1590 if (off > i_size)
1591 return 0;
1592
1593 if (off + len > i_size)
1594 len = i_size - off;
1595 toread = len;
1596 while (toread > 0) {
1597 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
1598repeat:
Ritesh Harjani02117b82018-03-16 18:53:53 +05301599 page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001600 if (IS_ERR(page)) {
1601 if (PTR_ERR(page) == -ENOMEM) {
1602 congestion_wait(BLK_RW_ASYNC, HZ/50);
1603 goto repeat;
1604 }
Chao Yu0abd6752017-07-09 00:13:07 +08001605 return PTR_ERR(page);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001606 }
Chao Yu0abd6752017-07-09 00:13:07 +08001607
1608 lock_page(page);
1609
1610 if (unlikely(page->mapping != mapping)) {
1611 f2fs_put_page(page, 1);
1612 goto repeat;
1613 }
1614 if (unlikely(!PageUptodate(page))) {
1615 f2fs_put_page(page, 1);
1616 return -EIO;
1617 }
1618
1619 kaddr = kmap_atomic(page);
1620 memcpy(data, kaddr + offset, tocopy);
1621 kunmap_atomic(kaddr);
1622 f2fs_put_page(page, 1);
1623
1624 offset = 0;
1625 toread -= tocopy;
1626 data += tocopy;
1627 blkidx++;
1628 }
1629 return len;
1630}
1631
1632/* Write to quotafile */
1633static ssize_t f2fs_quota_write(struct super_block *sb, int type,
1634 const char *data, size_t len, loff_t off)
1635{
1636 struct inode *inode = sb_dqopt(sb)->files[type];
1637 struct address_space *mapping = inode->i_mapping;
1638 const struct address_space_operations *a_ops = mapping->a_ops;
1639 int offset = off & (sb->s_blocksize - 1);
1640 size_t towrite = len;
1641 struct page *page;
1642 char *kaddr;
1643 int err = 0;
1644 int tocopy;
1645
1646 while (towrite > 0) {
1647 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
1648 towrite);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001649retry:
Chao Yu0abd6752017-07-09 00:13:07 +08001650 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
1651 &page, NULL);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001652 if (unlikely(err)) {
1653 if (err == -ENOMEM) {
1654 congestion_wait(BLK_RW_ASYNC, HZ/50);
1655 goto retry;
1656 }
Chao Yu0abd6752017-07-09 00:13:07 +08001657 break;
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001658 }
Chao Yu0abd6752017-07-09 00:13:07 +08001659
1660 kaddr = kmap_atomic(page);
1661 memcpy(kaddr + offset, data, tocopy);
1662 kunmap_atomic(kaddr);
1663 flush_dcache_page(page);
1664
1665 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
1666 page, NULL);
1667 offset = 0;
1668 towrite -= tocopy;
1669 off += tocopy;
1670 data += tocopy;
1671 cond_resched();
1672 }
1673
1674 if (len == towrite)
Jaegeuk Kim6e5b5d42017-10-19 12:07:11 -07001675 return err;
Chao Yu0abd6752017-07-09 00:13:07 +08001676 inode->i_mtime = inode->i_ctime = current_time(inode);
1677 f2fs_mark_inode_dirty_sync(inode, false);
1678 return len - towrite;
1679}
1680
1681static struct dquot **f2fs_get_dquots(struct inode *inode)
1682{
1683 return F2FS_I(inode)->i_dquot;
1684}
1685
1686static qsize_t *f2fs_get_reserved_space(struct inode *inode)
1687{
1688 return &F2FS_I(inode)->i_reserved_quota;
1689}
1690
Chao Yu4b2414d2017-08-08 10:54:31 +08001691static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
1692{
Chao Yu63189b72018-03-08 14:22:56 +08001693 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
1694 F2FS_OPTION(sbi).s_jquota_fmt, type);
Chao Yu4b2414d2017-08-08 10:54:31 +08001695}
1696
Jaegeuk Kimea676732017-10-06 09:14:28 -07001697int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
Chao Yu4b2414d2017-08-08 10:54:31 +08001698{
Jaegeuk Kimea676732017-10-06 09:14:28 -07001699 int enabled = 0;
1700 int i, err;
1701
1702 if (f2fs_sb_has_quota_ino(sbi->sb) && rdonly) {
1703 err = f2fs_enable_quotas(sbi->sb);
1704 if (err) {
1705 f2fs_msg(sbi->sb, KERN_ERR,
1706 "Cannot turn on quota_ino: %d", err);
1707 return 0;
1708 }
1709 return 1;
1710 }
Chao Yu4b2414d2017-08-08 10:54:31 +08001711
1712 for (i = 0; i < MAXQUOTAS; i++) {
Chao Yu63189b72018-03-08 14:22:56 +08001713 if (F2FS_OPTION(sbi).s_qf_names[i]) {
Jaegeuk Kimea676732017-10-06 09:14:28 -07001714 err = f2fs_quota_on_mount(sbi, i);
1715 if (!err) {
1716 enabled = 1;
1717 continue;
1718 }
1719 f2fs_msg(sbi->sb, KERN_ERR,
1720 "Cannot turn on quotas: %d on %d", err, i);
Chao Yu4b2414d2017-08-08 10:54:31 +08001721 }
1722 }
Jaegeuk Kimea676732017-10-06 09:14:28 -07001723 return enabled;
1724}
1725
1726static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
1727 unsigned int flags)
1728{
1729 struct inode *qf_inode;
1730 unsigned long qf_inum;
1731 int err;
1732
1733 BUG_ON(!f2fs_sb_has_quota_ino(sb));
1734
1735 qf_inum = f2fs_qf_ino(sb, type);
1736 if (!qf_inum)
1737 return -EPERM;
1738
1739 qf_inode = f2fs_iget(sb, qf_inum);
1740 if (IS_ERR(qf_inode)) {
1741 f2fs_msg(sb, KERN_ERR,
1742 "Bad quota inode %u:%lu", type, qf_inum);
1743 return PTR_ERR(qf_inode);
1744 }
1745
1746 /* Don't account quota for quota files to avoid recursion */
1747 qf_inode->i_flags |= S_NOQUOTA;
1748 err = dquot_enable(qf_inode, type, format_id, flags);
1749 iput(qf_inode);
1750 return err;
1751}
1752
1753static int f2fs_enable_quotas(struct super_block *sb)
1754{
1755 int type, err = 0;
1756 unsigned long qf_inum;
1757 bool quota_mopt[MAXQUOTAS] = {
1758 test_opt(F2FS_SB(sb), USRQUOTA),
1759 test_opt(F2FS_SB(sb), GRPQUOTA),
1760 test_opt(F2FS_SB(sb), PRJQUOTA),
1761 };
1762
1763 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
1764 for (type = 0; type < MAXQUOTAS; type++) {
1765 qf_inum = f2fs_qf_ino(sb, type);
1766 if (qf_inum) {
1767 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
1768 DQUOT_USAGE_ENABLED |
1769 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
1770 if (err) {
1771 f2fs_msg(sb, KERN_ERR,
1772 "Failed to enable quota tracking "
1773 "(type=%d, err=%d). Please run "
1774 "fsck to fix.", type, err);
1775 for (type--; type >= 0; type--)
1776 dquot_quota_off(sb, type);
1777 return err;
1778 }
1779 }
1780 }
1781 return 0;
Chao Yu4b2414d2017-08-08 10:54:31 +08001782}
1783
Chao Yu0abd6752017-07-09 00:13:07 +08001784static int f2fs_quota_sync(struct super_block *sb, int type)
1785{
1786 struct quota_info *dqopt = sb_dqopt(sb);
1787 int cnt;
1788 int ret;
1789
1790 ret = dquot_writeback_dquots(sb, type);
1791 if (ret)
1792 return ret;
1793
1794 /*
1795 * Now when everything is written we can discard the pagecache so
1796 * that userspace sees the changes.
1797 */
1798 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1799 if (type != -1 && cnt != type)
1800 continue;
1801 if (!sb_has_quota_active(sb, cnt))
1802 continue;
1803
1804 ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping);
1805 if (ret)
1806 return ret;
1807
1808 inode_lock(dqopt->files[cnt]);
1809 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
1810 inode_unlock(dqopt->files[cnt]);
1811 }
1812 return 0;
1813}
1814
1815static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
1816 const struct path *path)
1817{
1818 struct inode *inode;
1819 int err;
1820
Chao Yu9a20d392017-08-07 16:37:59 +08001821 err = f2fs_quota_sync(sb, type);
Chao Yu0abd6752017-07-09 00:13:07 +08001822 if (err)
1823 return err;
1824
1825 err = dquot_quota_on(sb, type, format_id, path);
1826 if (err)
1827 return err;
1828
1829 inode = d_inode(path->dentry);
1830
1831 inode_lock(inode);
Chao Yu59c84402018-04-03 15:08:17 +08001832 F2FS_I(inode)->i_flags |= F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
Chao Yu0abd6752017-07-09 00:13:07 +08001833 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
1834 S_NOATIME | S_IMMUTABLE);
1835 inode_unlock(inode);
1836 f2fs_mark_inode_dirty_sync(inode, false);
1837
1838 return 0;
1839}
1840
1841static int f2fs_quota_off(struct super_block *sb, int type)
1842{
1843 struct inode *inode = sb_dqopt(sb)->files[type];
1844 int err;
1845
1846 if (!inode || !igrab(inode))
1847 return dquot_quota_off(sb, type);
1848
Yunlei Hecda9cc52018-06-26 13:12:43 +08001849 err = f2fs_quota_sync(sb, type);
1850 if (err)
1851 goto out_put;
Chao Yu0abd6752017-07-09 00:13:07 +08001852
1853 err = dquot_quota_off(sb, type);
Jaegeuk Kimea676732017-10-06 09:14:28 -07001854 if (err || f2fs_sb_has_quota_ino(sb))
Chao Yu0abd6752017-07-09 00:13:07 +08001855 goto out_put;
1856
1857 inode_lock(inode);
Chao Yu59c84402018-04-03 15:08:17 +08001858 F2FS_I(inode)->i_flags &= ~(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL);
Chao Yu0abd6752017-07-09 00:13:07 +08001859 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
1860 inode_unlock(inode);
1861 f2fs_mark_inode_dirty_sync(inode, false);
1862out_put:
1863 iput(inode);
1864 return err;
1865}
1866
Chao Yu4b2414d2017-08-08 10:54:31 +08001867void f2fs_quota_off_umount(struct super_block *sb)
Chao Yu0abd6752017-07-09 00:13:07 +08001868{
1869 int type;
Yunlei Hecda9cc52018-06-26 13:12:43 +08001870 int err;
Chao Yu0abd6752017-07-09 00:13:07 +08001871
Yunlei Hecda9cc52018-06-26 13:12:43 +08001872 for (type = 0; type < MAXQUOTAS; type++) {
1873 err = f2fs_quota_off(sb, type);
1874 if (err) {
1875 int ret = dquot_quota_off(sb, type);
1876
1877 f2fs_msg(sb, KERN_ERR,
1878 "Fail to turn off disk quota "
1879 "(type: %d, err: %d, ret:%d), Please "
1880 "run fsck to fix it.", type, err, ret);
1881 set_sbi_flag(F2FS_SB(sb), SBI_NEED_FSCK);
1882 }
1883 }
Chao Yu0abd6752017-07-09 00:13:07 +08001884}
1885
Wei Yongjun94b1e102018-01-05 09:41:20 +00001886static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
Chao Yu5c571322017-07-26 00:01:41 +08001887{
1888 *projid = F2FS_I(inode)->i_projid;
1889 return 0;
1890}
1891
Chao Yu0abd6752017-07-09 00:13:07 +08001892static const struct dquot_operations f2fs_quota_operations = {
1893 .get_reserved_space = f2fs_get_reserved_space,
1894 .write_dquot = dquot_commit,
1895 .acquire_dquot = dquot_acquire,
1896 .release_dquot = dquot_release,
1897 .mark_dirty = dquot_mark_dquot_dirty,
1898 .write_info = dquot_commit_info,
1899 .alloc_dquot = dquot_alloc,
1900 .destroy_dquot = dquot_destroy,
Chao Yu5c571322017-07-26 00:01:41 +08001901 .get_projid = f2fs_get_projid,
Chao Yu0abd6752017-07-09 00:13:07 +08001902 .get_next_id = dquot_get_next_id,
1903};
1904
1905static const struct quotactl_ops f2fs_quotactl_ops = {
1906 .quota_on = f2fs_quota_on,
1907 .quota_off = f2fs_quota_off,
1908 .quota_sync = f2fs_quota_sync,
1909 .get_state = dquot_get_state,
1910 .set_info = dquot_set_dqinfo,
1911 .get_dqblk = dquot_get_dqblk,
1912 .set_dqblk = dquot_set_dqblk,
1913 .get_nextdqblk = dquot_get_next_dqblk,
1914};
1915#else
Chao Yu4b2414d2017-08-08 10:54:31 +08001916void f2fs_quota_off_umount(struct super_block *sb)
Chao Yu0abd6752017-07-09 00:13:07 +08001917{
1918}
1919#endif
1920
Arvind Yadavf62fc9f2017-08-31 15:06:24 +05301921static const struct super_operations f2fs_sops = {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001922 .alloc_inode = f2fs_alloc_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001923 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001924 .destroy_inode = f2fs_destroy_inode,
1925 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +09001926 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001927 .show_options = f2fs_show_options,
Chao Yu0abd6752017-07-09 00:13:07 +08001928#ifdef CONFIG_QUOTA
1929 .quota_read = f2fs_quota_read,
1930 .quota_write = f2fs_quota_write,
1931 .get_dquots = f2fs_get_dquots,
1932#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001933 .evict_inode = f2fs_evict_inode,
1934 .put_super = f2fs_put_super,
1935 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +09001936 .freeze_fs = f2fs_freeze,
1937 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001938 .statfs = f2fs_statfs,
Namjae Jeon696c0182013-06-16 09:48:48 +09001939 .remount_fs = f2fs_remount,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001940};
1941
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001942#ifdef CONFIG_F2FS_FS_ENCRYPTION
1943static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
1944{
1945 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1946 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1947 ctx, len, NULL);
1948}
1949
1950static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
1951 void *fs_data)
1952{
Sheng Yongb7c409d2018-03-15 18:51:41 +08001953 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1954
1955 /*
1956 * Encrypting the root directory is not allowed because fsck
1957 * expects lost+found directory to exist and remain unencrypted
1958 * if LOST_FOUND feature is enabled.
1959 *
1960 */
1961 if (f2fs_sb_has_lost_found(sbi->sb) &&
1962 inode->i_ino == F2FS_ROOT_INO(sbi))
1963 return -EPERM;
1964
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001965 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1966 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1967 ctx, len, fs_data, XATTR_CREATE);
1968}
1969
Sheng Yongff62af22018-03-15 18:51:42 +08001970static bool f2fs_dummy_context(struct inode *inode)
1971{
1972 return DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(inode));
1973}
1974
Eric Biggers6f69f0e2017-02-07 12:42:10 -08001975static const struct fscrypt_operations f2fs_cryptops = {
Eric Biggersa5d431e2017-01-05 13:51:18 -08001976 .key_prefix = "f2fs:",
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001977 .get_context = f2fs_get_context,
1978 .set_context = f2fs_set_context,
Sheng Yongff62af22018-03-15 18:51:42 +08001979 .dummy_context = f2fs_dummy_context,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001980 .empty_dir = f2fs_empty_dir,
Eric Biggerse12ee682018-04-30 15:51:44 -07001981 .max_namelen = F2FS_NAME_LEN,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001982};
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001983#endif
1984
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001985static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
1986 u64 ino, u32 generation)
1987{
1988 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1989 struct inode *inode;
1990
Chao Yu4d57b862018-05-30 00:20:41 +08001991 if (f2fs_check_nid_range(sbi, ino))
Chao Yu910bb122014-03-12 17:08:36 +08001992 return ERR_PTR(-ESTALE);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001993
1994 /*
1995 * f2fs_iget isn't quite right if the inode is currently unallocated!
1996 * However f2fs_iget currently does appropriate checks to handle stale
1997 * inodes so everything is OK.
1998 */
1999 inode = f2fs_iget(sb, ino);
2000 if (IS_ERR(inode))
2001 return ERR_CAST(inode);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002002 if (unlikely(generation && inode->i_generation != generation)) {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002003 /* we didn't find the right inode.. */
2004 iput(inode);
2005 return ERR_PTR(-ESTALE);
2006 }
2007 return inode;
2008}
2009
2010static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
2011 int fh_len, int fh_type)
2012{
2013 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2014 f2fs_nfs_get_inode);
2015}
2016
2017static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
2018 int fh_len, int fh_type)
2019{
2020 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2021 f2fs_nfs_get_inode);
2022}
2023
2024static const struct export_operations f2fs_export_ops = {
2025 .fh_to_dentry = f2fs_fh_to_dentry,
2026 .fh_to_parent = f2fs_fh_to_parent,
2027 .get_parent = f2fs_get_parent,
2028};
2029
Chao Yue0afc4d2015-12-31 14:35:37 +08002030static loff_t max_file_blocks(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002031{
Chao Yu7a2af762017-07-19 00:19:06 +08002032 loff_t result = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002033 loff_t leaf_count = ADDRS_PER_BLOCK;
2034
Chao Yu7a2af762017-07-19 00:19:06 +08002035 /*
2036 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
Chao Yu6afc6622017-09-06 21:59:50 +08002037 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
Chao Yu7a2af762017-07-19 00:19:06 +08002038 * space in inode.i_addr, it will be more safe to reassign
2039 * result as zero.
2040 */
2041
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002042 /* two direct node blocks */
2043 result += (leaf_count * 2);
2044
2045 /* two indirect node blocks */
2046 leaf_count *= NIDS_PER_BLOCK;
2047 result += (leaf_count * 2);
2048
2049 /* one double indirect node block */
2050 leaf_count *= NIDS_PER_BLOCK;
2051 result += leaf_count;
2052
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002053 return result;
2054}
2055
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002056static int __f2fs_commit_super(struct buffer_head *bh,
2057 struct f2fs_super_block *super)
Chao Yu9a59b622015-12-15 09:58:18 +08002058{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002059 lock_buffer(bh);
2060 if (super)
2061 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002062 set_buffer_dirty(bh);
2063 unlock_buffer(bh);
2064
2065 /* it's rare case, we can do fua all the time */
Jan Kara3adc5fcb2017-05-02 17:03:47 +02002066 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002067}
2068
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002069static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002070 struct buffer_head *bh)
2071{
2072 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2073 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002074 struct super_block *sb = sbi->sb;
Chao Yu9a59b622015-12-15 09:58:18 +08002075 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2076 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
2077 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
2078 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
2079 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2080 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2081 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
2082 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
2083 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
2084 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
2085 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2086 u32 segment_count = le32_to_cpu(raw_super->segment_count);
2087 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002088 u64 main_end_blkaddr = main_blkaddr +
2089 (segment_count_main << log_blocks_per_seg);
2090 u64 seg_end_blkaddr = segment0_blkaddr +
2091 (segment_count << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08002092
2093 if (segment0_blkaddr != cp_blkaddr) {
2094 f2fs_msg(sb, KERN_INFO,
2095 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
2096 segment0_blkaddr, cp_blkaddr);
2097 return true;
2098 }
2099
2100 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
2101 sit_blkaddr) {
2102 f2fs_msg(sb, KERN_INFO,
2103 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
2104 cp_blkaddr, sit_blkaddr,
2105 segment_count_ckpt << log_blocks_per_seg);
2106 return true;
2107 }
2108
2109 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
2110 nat_blkaddr) {
2111 f2fs_msg(sb, KERN_INFO,
2112 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
2113 sit_blkaddr, nat_blkaddr,
2114 segment_count_sit << log_blocks_per_seg);
2115 return true;
2116 }
2117
2118 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
2119 ssa_blkaddr) {
2120 f2fs_msg(sb, KERN_INFO,
2121 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
2122 nat_blkaddr, ssa_blkaddr,
2123 segment_count_nat << log_blocks_per_seg);
2124 return true;
2125 }
2126
2127 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
2128 main_blkaddr) {
2129 f2fs_msg(sb, KERN_INFO,
2130 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
2131 ssa_blkaddr, main_blkaddr,
2132 segment_count_ssa << log_blocks_per_seg);
2133 return true;
2134 }
2135
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002136 if (main_end_blkaddr > seg_end_blkaddr) {
Chao Yu9a59b622015-12-15 09:58:18 +08002137 f2fs_msg(sb, KERN_INFO,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002138 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
Chao Yu9a59b622015-12-15 09:58:18 +08002139 main_blkaddr,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002140 segment0_blkaddr +
2141 (segment_count << log_blocks_per_seg),
Chao Yu9a59b622015-12-15 09:58:18 +08002142 segment_count_main << log_blocks_per_seg);
2143 return true;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002144 } else if (main_end_blkaddr < seg_end_blkaddr) {
2145 int err = 0;
2146 char *res;
Chao Yu9a59b622015-12-15 09:58:18 +08002147
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002148 /* fix in-memory information all the time */
2149 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
2150 segment0_blkaddr) >> log_blocks_per_seg);
2151
2152 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002153 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002154 res = "internally";
2155 } else {
2156 err = __f2fs_commit_super(bh, NULL);
2157 res = err ? "failed" : "done";
2158 }
2159 f2fs_msg(sb, KERN_INFO,
2160 "Fix alignment : %s, start(%u) end(%u) block(%u)",
2161 res, main_blkaddr,
2162 segment0_blkaddr +
2163 (segment_count << log_blocks_per_seg),
2164 segment_count_main << log_blocks_per_seg);
2165 if (err)
2166 return true;
2167 }
Chao Yu9a59b622015-12-15 09:58:18 +08002168 return false;
2169}
2170
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002171static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002172 struct buffer_head *bh)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002173{
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07002174 block_t segment_count, segs_per_sec, secs_per_zone;
2175 block_t total_sections, blocks_per_seg;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002176 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2177 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002178 struct super_block *sb = sbi->sb;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002179 unsigned int blocksize;
2180
Namjae Jeona07ef782012-12-30 14:52:05 +09002181 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
2182 f2fs_msg(sb, KERN_INFO,
2183 "Magic Mismatch, valid(0x%x) - read(0x%x)",
2184 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002185 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09002186 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002187
majianpeng5c9b4692013-02-01 19:07:57 +08002188 /* Currently, support only 4KB page cache size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002189 if (F2FS_BLKSIZE != PAGE_SIZE) {
majianpeng5c9b4692013-02-01 19:07:57 +08002190 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +08002191 "Invalid page_cache_size (%lu), supports only 4KB\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002192 PAGE_SIZE);
majianpeng5c9b4692013-02-01 19:07:57 +08002193 return 1;
2194 }
2195
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002196 /* Currently, support only 4KB block size */
2197 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +08002198 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002199 f2fs_msg(sb, KERN_INFO,
2200 "Invalid blocksize (%u), supports only 4KB\n",
2201 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002202 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09002203 }
majianpeng5c9b4692013-02-01 19:07:57 +08002204
Chao Yu9a59b622015-12-15 09:58:18 +08002205 /* check log blocks per segment */
2206 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
2207 f2fs_msg(sb, KERN_INFO,
2208 "Invalid log blocks per segment (%u)\n",
2209 le32_to_cpu(raw_super->log_blocks_per_seg));
2210 return 1;
2211 }
2212
Chao Yu55cf9cb2014-09-15 18:01:10 +08002213 /* Currently, support 512/1024/2048/4096 bytes sector size */
2214 if (le32_to_cpu(raw_super->log_sectorsize) >
2215 F2FS_MAX_LOG_SECTOR_SIZE ||
2216 le32_to_cpu(raw_super->log_sectorsize) <
2217 F2FS_MIN_LOG_SECTOR_SIZE) {
2218 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
2219 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002220 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09002221 }
Chao Yu55cf9cb2014-09-15 18:01:10 +08002222 if (le32_to_cpu(raw_super->log_sectors_per_block) +
2223 le32_to_cpu(raw_super->log_sectorsize) !=
2224 F2FS_MAX_LOG_SECTOR_SIZE) {
2225 f2fs_msg(sb, KERN_INFO,
2226 "Invalid log sectors per block(%u) log sectorsize(%u)",
2227 le32_to_cpu(raw_super->log_sectors_per_block),
2228 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002229 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09002230 }
Chao Yu9a59b622015-12-15 09:58:18 +08002231
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07002232 segment_count = le32_to_cpu(raw_super->segment_count);
2233 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2234 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2235 total_sections = le32_to_cpu(raw_super->section_count);
2236
2237 /* blocks_per_seg should be 512, given the above check */
2238 blocks_per_seg = 1 << le32_to_cpu(raw_super->log_blocks_per_seg);
2239
2240 if (segment_count > F2FS_MAX_SEGMENT ||
2241 segment_count < F2FS_MIN_SEGMENTS) {
2242 f2fs_msg(sb, KERN_INFO,
2243 "Invalid segment count (%u)",
2244 segment_count);
2245 return 1;
2246 }
2247
2248 if (total_sections > segment_count ||
2249 total_sections < F2FS_MIN_SEGMENTS ||
2250 segs_per_sec > segment_count || !segs_per_sec) {
2251 f2fs_msg(sb, KERN_INFO,
2252 "Invalid segment/section count (%u, %u x %u)",
2253 segment_count, total_sections, segs_per_sec);
2254 return 1;
2255 }
2256
2257 if ((segment_count / segs_per_sec) < total_sections) {
2258 f2fs_msg(sb, KERN_INFO,
2259 "Small segment_count (%u < %u * %u)",
2260 segment_count, segs_per_sec, total_sections);
2261 return 1;
2262 }
2263
2264 if (segment_count > (le32_to_cpu(raw_super->block_count) >> 9)) {
2265 f2fs_msg(sb, KERN_INFO,
2266 "Wrong segment_count / block_count (%u > %u)",
2267 segment_count, le32_to_cpu(raw_super->block_count));
2268 return 1;
2269 }
2270
Chao Yu42bf5462018-06-23 00:12:36 +08002271 if (secs_per_zone > total_sections || !secs_per_zone) {
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07002272 f2fs_msg(sb, KERN_INFO,
Chao Yu42bf5462018-06-23 00:12:36 +08002273 "Wrong secs_per_zone / total_sections (%u, %u)",
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07002274 secs_per_zone, total_sections);
2275 return 1;
2276 }
2277 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
2278 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
2279 (le32_to_cpu(raw_super->extension_count) +
2280 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
2281 f2fs_msg(sb, KERN_INFO,
2282 "Corrupted extension count (%u + %u > %u)",
2283 le32_to_cpu(raw_super->extension_count),
2284 raw_super->hot_ext_count,
2285 F2FS_MAX_EXTENSION);
2286 return 1;
2287 }
2288
2289 if (le32_to_cpu(raw_super->cp_payload) >
2290 (blocks_per_seg - F2FS_CP_PACKS)) {
2291 f2fs_msg(sb, KERN_INFO,
2292 "Insane cp_payload (%u > %u)",
2293 le32_to_cpu(raw_super->cp_payload),
2294 blocks_per_seg - F2FS_CP_PACKS);
2295 return 1;
2296 }
2297
Chao Yu9a59b622015-12-15 09:58:18 +08002298 /* check reserved ino info */
2299 if (le32_to_cpu(raw_super->node_ino) != 1 ||
2300 le32_to_cpu(raw_super->meta_ino) != 2 ||
2301 le32_to_cpu(raw_super->root_ino) != 3) {
2302 f2fs_msg(sb, KERN_INFO,
2303 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
2304 le32_to_cpu(raw_super->node_ino),
2305 le32_to_cpu(raw_super->meta_ino),
2306 le32_to_cpu(raw_super->root_ino));
2307 return 1;
2308 }
2309
2310 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002311 if (sanity_check_area_boundary(sbi, bh))
Chao Yu9a59b622015-12-15 09:58:18 +08002312 return 1;
2313
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002314 return 0;
2315}
2316
Chao Yu4d57b862018-05-30 00:20:41 +08002317int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002318{
2319 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09002320 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2321 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08002322 unsigned int ovp_segments, reserved_segments;
Jin Qian15d30422017-05-15 10:45:08 -07002323 unsigned int main_segs, blocks_per_seg;
Chao Yuc77ec612018-06-23 11:25:19 +08002324 unsigned int sit_segs, nat_segs;
2325 unsigned int sit_bitmap_size, nat_bitmap_size;
2326 unsigned int log_blocks_per_seg;
Chao Yu9dc956b2018-06-27 18:05:54 +08002327 unsigned int segment_count_main;
Chao Yue494c2f2018-08-01 19:16:11 +08002328 unsigned int cp_pack_start_sum, cp_payload;
Chao Yu9dc956b2018-06-27 18:05:54 +08002329 block_t user_block_count;
Chao Yu042be0f2018-09-06 20:34:12 +08002330 int i, j;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002331
2332 total = le32_to_cpu(raw_super->segment_count);
2333 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
Chao Yuc77ec612018-06-23 11:25:19 +08002334 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
2335 fsmeta += sit_segs;
2336 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
2337 fsmeta += nat_segs;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002338 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
2339 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
2340
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002341 if (unlikely(fsmeta >= total))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002342 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09002343
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08002344 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2345 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2346
2347 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
2348 ovp_segments == 0 || reserved_segments == 0)) {
2349 f2fs_msg(sbi->sb, KERN_ERR,
2350 "Wrong layout: check mkfs.f2fs version");
2351 return 1;
2352 }
2353
Chao Yu9dc956b2018-06-27 18:05:54 +08002354 user_block_count = le64_to_cpu(ckpt->user_block_count);
2355 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2356 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2357 if (!user_block_count || user_block_count >=
2358 segment_count_main << log_blocks_per_seg) {
2359 f2fs_msg(sbi->sb, KERN_ERR,
2360 "Wrong user_block_count: %u", user_block_count);
2361 return 1;
2362 }
2363
Jin Qian15d30422017-05-15 10:45:08 -07002364 main_segs = le32_to_cpu(raw_super->segment_count_main);
2365 blocks_per_seg = sbi->blocks_per_seg;
2366
2367 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2368 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
2369 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
2370 return 1;
Chao Yu042be0f2018-09-06 20:34:12 +08002371 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
2372 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
2373 le32_to_cpu(ckpt->cur_node_segno[j])) {
2374 f2fs_msg(sbi->sb, KERN_ERR,
2375 "Node segment (%u, %u) has the same "
2376 "segno: %u", i, j,
2377 le32_to_cpu(ckpt->cur_node_segno[i]));
2378 return 1;
2379 }
2380 }
Jin Qian15d30422017-05-15 10:45:08 -07002381 }
2382 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
2383 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
2384 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
2385 return 1;
Chao Yu042be0f2018-09-06 20:34:12 +08002386 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
2387 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
2388 le32_to_cpu(ckpt->cur_data_segno[j])) {
2389 f2fs_msg(sbi->sb, KERN_ERR,
2390 "Data segment (%u, %u) has the same "
2391 "segno: %u", i, j,
2392 le32_to_cpu(ckpt->cur_data_segno[i]));
2393 return 1;
2394 }
2395 }
2396 }
2397 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2398 for (j = i; j < NR_CURSEG_DATA_TYPE; j++) {
2399 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
2400 le32_to_cpu(ckpt->cur_data_segno[j])) {
2401 f2fs_msg(sbi->sb, KERN_ERR,
2402 "Data segment (%u) and Data segment (%u)"
2403 " has the same segno: %u", i, j,
2404 le32_to_cpu(ckpt->cur_node_segno[i]));
2405 return 1;
2406 }
2407 }
Jin Qian15d30422017-05-15 10:45:08 -07002408 }
2409
Chao Yuc77ec612018-06-23 11:25:19 +08002410 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
2411 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
Chao Yuc77ec612018-06-23 11:25:19 +08002412
2413 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
2414 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
2415 f2fs_msg(sbi->sb, KERN_ERR,
2416 "Wrong bitmap size: sit: %u, nat:%u",
2417 sit_bitmap_size, nat_bitmap_size);
2418 return 1;
2419 }
2420
Chao Yue494c2f2018-08-01 19:16:11 +08002421 cp_pack_start_sum = __start_sum_addr(sbi);
2422 cp_payload = __cp_payload(sbi);
2423 if (cp_pack_start_sum < cp_payload + 1 ||
2424 cp_pack_start_sum > blocks_per_seg - 1 -
2425 NR_CURSEG_TYPE) {
2426 f2fs_msg(sbi->sb, KERN_ERR,
2427 "Wrong cp_pack_start_sum: %u",
2428 cp_pack_start_sum);
2429 return 1;
2430 }
2431
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07002432 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim577e3492013-01-24 19:56:11 +09002433 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
2434 return 1;
2435 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002436 return 0;
2437}
2438
2439static void init_sb_info(struct f2fs_sb_info *sbi)
2440{
2441 struct f2fs_super_block *raw_super = sbi->raw_super;
Chao Yue41e6d72017-05-19 23:37:00 +08002442 int i, j;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002443
2444 sbi->log_sectors_per_block =
2445 le32_to_cpu(raw_super->log_sectors_per_block);
2446 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
2447 sbi->blocksize = 1 << sbi->log_blocksize;
2448 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2449 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
2450 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2451 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2452 sbi->total_sections = le32_to_cpu(raw_super->section_count);
2453 sbi->total_node_count =
2454 (le32_to_cpu(raw_super->segment_count_nat) / 2)
2455 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
2456 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
2457 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
2458 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002459 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimb1c57c12014-01-08 13:45:08 +09002460 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002461
Jaegeuk Kimab9fa662014-02-27 20:09:05 +09002462 sbi->dir_level = DEF_DIR_LEVEL;
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08002463 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002464 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +05302465 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
2466 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
Chao Yucaf00472015-01-28 17:48:42 +08002467 clear_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002468
Jaegeuk Kim35782b22016-10-20 19:09:57 -07002469 for (i = 0; i < NR_COUNT_TYPE; i++)
2470 atomic_set(&sbi->nr_pages[i], 0);
2471
Chao Yuc29fd0c2018-06-04 23:20:36 +08002472 for (i = 0; i < META; i++)
2473 atomic_set(&sbi->wb_sync_req[i], 0);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002474
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002475 INIT_LIST_HEAD(&sbi->s_list);
2476 mutex_init(&sbi->umount_mutex);
Chao Yue41e6d72017-05-19 23:37:00 +08002477 for (i = 0; i < NR_PAGE_TYPE - 1; i++)
2478 for (j = HOT; j < NR_TEMP_TYPE; j++)
2479 mutex_init(&sbi->wio_mutex[i][j]);
Chao Yu107a8052018-05-26 09:00:13 +08002480 init_rwsem(&sbi->io_order_lock);
Chao Yuaaec2b12016-09-20 11:04:18 +08002481 spin_lock_init(&sbi->cp_lock);
Chao Yu1228b482017-09-29 13:59:39 +08002482
2483 sbi->dirty_device = 0;
2484 spin_lock_init(&sbi->dev_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002485
Chao Yu846ae672018-02-26 22:04:13 +08002486 init_rwsem(&sbi->sb_lock);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002487}
2488
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002489static int init_percpu_info(struct f2fs_sb_info *sbi)
2490{
Jaegeuk Kim35782b22016-10-20 19:09:57 -07002491 int err;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002492
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07002493 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
2494 if (err)
2495 return err;
2496
Chao Yu4a70e252018-09-05 14:54:02 +08002497 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002498 GFP_KERNEL);
Chao Yu4a70e252018-09-05 14:54:02 +08002499 if (err)
2500 percpu_counter_destroy(&sbi->alloc_valid_block_count);
2501
2502 return err;
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002503}
2504
Damien Le Moal178053e2016-10-28 17:45:05 +09002505#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002506static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
Damien Le Moal178053e2016-10-28 17:45:05 +09002507{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002508 struct block_device *bdev = FDEV(devi).bdev;
Damien Le Moal178053e2016-10-28 17:45:05 +09002509 sector_t nr_sectors = bdev->bd_part->nr_sects;
2510 sector_t sector = 0;
2511 struct blk_zone *zones;
2512 unsigned int i, nr_zones;
2513 unsigned int n = 0;
2514 int err = -EIO;
2515
Sheng Yongccd31cb2018-02-06 12:31:17 +08002516 if (!f2fs_sb_has_blkzoned(sbi->sb))
Damien Le Moal178053e2016-10-28 17:45:05 +09002517 return 0;
2518
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002519 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
Damien Le Moalf99e8642017-01-12 07:58:32 -07002520 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002521 return -EINVAL;
Damien Le Moalf99e8642017-01-12 07:58:32 -07002522 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002523 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
2524 __ilog2_u32(sbi->blocks_per_blkz))
2525 return -EINVAL;
Damien Le Moal178053e2016-10-28 17:45:05 +09002526 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002527 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
2528 sbi->log_blocks_per_blkz;
Damien Le Moalf99e8642017-01-12 07:58:32 -07002529 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002530 FDEV(devi).nr_blkz++;
Damien Le Moal178053e2016-10-28 17:45:05 +09002531
Chao Yu4e6aad22017-11-30 19:28:19 +08002532 FDEV(devi).blkz_type = f2fs_kmalloc(sbi, FDEV(devi).nr_blkz,
2533 GFP_KERNEL);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002534 if (!FDEV(devi).blkz_type)
Damien Le Moal178053e2016-10-28 17:45:05 +09002535 return -ENOMEM;
2536
2537#define F2FS_REPORT_NR_ZONES 4096
2538
Kees Cook026f0502018-06-12 14:28:23 -07002539 zones = f2fs_kzalloc(sbi,
2540 array_size(F2FS_REPORT_NR_ZONES,
2541 sizeof(struct blk_zone)),
2542 GFP_KERNEL);
Damien Le Moal178053e2016-10-28 17:45:05 +09002543 if (!zones)
2544 return -ENOMEM;
2545
2546 /* Get block zones type */
2547 while (zones && sector < nr_sectors) {
2548
2549 nr_zones = F2FS_REPORT_NR_ZONES;
2550 err = blkdev_report_zones(bdev, sector,
2551 zones, &nr_zones,
2552 GFP_KERNEL);
2553 if (err)
2554 break;
2555 if (!nr_zones) {
2556 err = -EIO;
2557 break;
2558 }
2559
2560 for (i = 0; i < nr_zones; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002561 FDEV(devi).blkz_type[n] = zones[i].type;
Damien Le Moal178053e2016-10-28 17:45:05 +09002562 sector += zones[i].len;
2563 n++;
2564 }
2565 }
2566
2567 kfree(zones);
2568
2569 return err;
2570}
2571#endif
2572
Gu Zheng9076a752013-10-14 18:47:11 +08002573/*
2574 * Read f2fs raw super block.
Shawn Lin2b39e902016-02-17 08:59:01 +08002575 * Because we have two copies of super block, so read both of them
2576 * to get the first valid one. If any one of them is broken, we pass
2577 * them recovery flag back to the caller.
Gu Zheng9076a752013-10-14 18:47:11 +08002578 */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002579static int read_raw_super_block(struct f2fs_sb_info *sbi,
Gu Zheng9076a752013-10-14 18:47:11 +08002580 struct f2fs_super_block **raw_super,
Chao Yue8240f62015-12-15 17:19:26 +08002581 int *valid_super_block, int *recovery)
majianpeng14d7e9d2013-02-01 19:07:03 +08002582{
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002583 struct super_block *sb = sbi->sb;
Shawn Lin2b39e902016-02-17 08:59:01 +08002584 int block;
Chao Yue8240f62015-12-15 17:19:26 +08002585 struct buffer_head *bh;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002586 struct f2fs_super_block *super;
hujianyangda554e42015-05-21 14:42:53 +08002587 int err = 0;
majianpeng14d7e9d2013-02-01 19:07:03 +08002588
Yunlei Heb39f0de2015-12-15 17:17:20 +08002589 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
2590 if (!super)
2591 return -ENOMEM;
Shawn Lin2b39e902016-02-17 08:59:01 +08002592
2593 for (block = 0; block < 2; block++) {
2594 bh = sb_bread(sb, block);
2595 if (!bh) {
2596 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
Gu Zheng9076a752013-10-14 18:47:11 +08002597 block + 1);
Shawn Lin2b39e902016-02-17 08:59:01 +08002598 err = -EIO;
2599 continue;
2600 }
majianpeng14d7e9d2013-02-01 19:07:03 +08002601
Shawn Lin2b39e902016-02-17 08:59:01 +08002602 /* sanity checking of raw super */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002603 if (sanity_check_raw_super(sbi, bh)) {
Shawn Lin2b39e902016-02-17 08:59:01 +08002604 f2fs_msg(sb, KERN_ERR,
2605 "Can't find valid F2FS filesystem in %dth superblock",
2606 block + 1);
2607 err = -EINVAL;
2608 brelse(bh);
2609 continue;
2610 }
2611
2612 if (!*raw_super) {
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002613 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
2614 sizeof(*super));
Shawn Lin2b39e902016-02-17 08:59:01 +08002615 *valid_super_block = block;
2616 *raw_super = super;
2617 }
Chao Yue8240f62015-12-15 17:19:26 +08002618 brelse(bh);
Shawn Lin2b39e902016-02-17 08:59:01 +08002619 }
2620
2621 /* Fail to read any one of the superblocks*/
2622 if (err < 0)
hujianyangda554e42015-05-21 14:42:53 +08002623 *recovery = 1;
hujianyangda554e42015-05-21 14:42:53 +08002624
hujianyangda554e42015-05-21 14:42:53 +08002625 /* No valid superblock */
Shawn Lin2b39e902016-02-17 08:59:01 +08002626 if (!*raw_super)
Yunlei Heb39f0de2015-12-15 17:17:20 +08002627 kfree(super);
Shawn Lin2b39e902016-02-17 08:59:01 +08002628 else
2629 err = 0;
hujianyangda554e42015-05-21 14:42:53 +08002630
Shawn Lin2b39e902016-02-17 08:59:01 +08002631 return err;
majianpeng14d7e9d2013-02-01 19:07:03 +08002632}
2633
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002634int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002635{
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002636 struct buffer_head *bh;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002637 int err;
2638
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002639 if ((recover && f2fs_readonly(sbi->sb)) ||
2640 bdev_read_only(sbi->sb->s_bdev)) {
2641 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07002642 return -EROFS;
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002643 }
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07002644
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002645 /* write back-up superblock first */
Sheng Yong0964fc12018-01-29 19:13:15 +08002646 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002647 if (!bh)
2648 return -EIO;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002649 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002650 brelse(bh);
Chao Yuc5bda1c2015-06-08 13:28:03 +08002651
2652 /* if we are in recovery path, skip writing valid superblock */
2653 if (recover || err)
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002654 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002655
Chao Yue8240f62015-12-15 17:19:26 +08002656 /* write current valid superblock */
Sheng Yong0964fc12018-01-29 19:13:15 +08002657 bh = sb_bread(sbi->sb, sbi->valid_super_block);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002658 if (!bh)
2659 return -EIO;
2660 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
2661 brelse(bh);
2662 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002663}
2664
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002665static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
2666{
2667 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002668 unsigned int max_devices = MAX_DEVICES;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002669 int i;
2670
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002671 /* Initialize single device information */
2672 if (!RDEV(0).path[0]) {
2673 if (!bdev_is_zoned(sbi->sb->s_bdev))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002674 return 0;
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002675 max_devices = 1;
2676 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002677
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002678 /*
2679 * Initialize multiple devices information, or single
2680 * zoned block device information.
2681 */
Kees Cook026f0502018-06-12 14:28:23 -07002682 sbi->devs = f2fs_kzalloc(sbi,
2683 array_size(max_devices,
2684 sizeof(struct f2fs_dev_info)),
2685 GFP_KERNEL);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002686 if (!sbi->devs)
2687 return -ENOMEM;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002688
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002689 for (i = 0; i < max_devices; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002690
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002691 if (i > 0 && !RDEV(i).path[0])
2692 break;
2693
2694 if (max_devices == 1) {
2695 /* Single zoned block device mount */
2696 FDEV(0).bdev =
2697 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002698 sbi->sb->s_mode, sbi->sb->s_type);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002699 } else {
2700 /* Multi-device mount */
2701 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
2702 FDEV(i).total_segments =
2703 le32_to_cpu(RDEV(i).total_segments);
2704 if (i == 0) {
2705 FDEV(i).start_blk = 0;
2706 FDEV(i).end_blk = FDEV(i).start_blk +
2707 (FDEV(i).total_segments <<
2708 sbi->log_blocks_per_seg) - 1 +
2709 le32_to_cpu(raw_super->segment0_blkaddr);
2710 } else {
2711 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
2712 FDEV(i).end_blk = FDEV(i).start_blk +
2713 (FDEV(i).total_segments <<
2714 sbi->log_blocks_per_seg) - 1;
2715 }
2716 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
2717 sbi->sb->s_mode, sbi->sb->s_type);
2718 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002719 if (IS_ERR(FDEV(i).bdev))
2720 return PTR_ERR(FDEV(i).bdev);
2721
2722 /* to release errored devices */
2723 sbi->s_ndevs = i + 1;
2724
2725#ifdef CONFIG_BLK_DEV_ZONED
2726 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
Sheng Yongccd31cb2018-02-06 12:31:17 +08002727 !f2fs_sb_has_blkzoned(sbi->sb)) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002728 f2fs_msg(sbi->sb, KERN_ERR,
2729 "Zoned block device feature not enabled\n");
2730 return -EINVAL;
2731 }
2732 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
2733 if (init_blkz_info(sbi, i)) {
2734 f2fs_msg(sbi->sb, KERN_ERR,
2735 "Failed to initialize F2FS blkzone information");
2736 return -EINVAL;
2737 }
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002738 if (max_devices == 1)
2739 break;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002740 f2fs_msg(sbi->sb, KERN_INFO,
2741 "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
2742 i, FDEV(i).path,
2743 FDEV(i).total_segments,
2744 FDEV(i).start_blk, FDEV(i).end_blk,
2745 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
2746 "Host-aware" : "Host-managed");
2747 continue;
2748 }
2749#endif
2750 f2fs_msg(sbi->sb, KERN_INFO,
2751 "Mount Device [%2d]: %20s, %8u, %8x - %8x",
2752 i, FDEV(i).path,
2753 FDEV(i).total_segments,
2754 FDEV(i).start_blk, FDEV(i).end_blk);
2755 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002756 f2fs_msg(sbi->sb, KERN_INFO,
2757 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002758 return 0;
2759}
2760
Jaegeuk Kim84b89e52018-02-22 14:09:30 -08002761static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
2762{
2763 struct f2fs_sm_info *sm_i = SM_I(sbi);
2764
2765 /* adjust parameters according to the volume size */
2766 if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
Chao Yu63189b72018-03-08 14:22:56 +08002767 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
Jaegeuk Kim84b89e52018-02-22 14:09:30 -08002768 sm_i->dcc_info->discard_granularity = 1;
2769 sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
2770 }
Chao Yu4cac90d2018-06-11 18:02:01 +08002771
2772 sbi->readdir_ra = 1;
Jaegeuk Kim84b89e52018-02-22 14:09:30 -08002773}
2774
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002775static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
2776{
2777 struct f2fs_sb_info *sbi;
hujianyangda554e42015-05-21 14:42:53 +08002778 struct f2fs_super_block *raw_super;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002779 struct inode *root;
Sheng Yong99e3e852016-05-11 17:08:14 +08002780 int err;
Chao Yu2adc3502015-03-16 21:08:44 +08002781 bool retry = true, need_fsck = false;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002782 char *options = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08002783 int recovery, i, valid_super_block;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002784 struct curseg_info *seg_i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002785
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002786try_onemore:
hujianyangda554e42015-05-21 14:42:53 +08002787 err = -EINVAL;
2788 raw_super = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08002789 valid_super_block = -1;
hujianyangda554e42015-05-21 14:42:53 +08002790 recovery = 0;
2791
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002792 /* allocate memory for f2fs-specific super block info */
2793 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
2794 if (!sbi)
2795 return -ENOMEM;
2796
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002797 sbi->sb = sb;
2798
Keith Mok43b65732016-03-02 12:04:24 -08002799 /* Load the checksum driver */
2800 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
2801 if (IS_ERR(sbi->s_chksum_driver)) {
2802 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
2803 err = PTR_ERR(sbi->s_chksum_driver);
2804 sbi->s_chksum_driver = NULL;
2805 goto free_sbi;
2806 }
2807
Namjae Jeonff9234a2013-01-12 14:41:13 +09002808 /* set a block size */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002809 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002810 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002811 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +09002812 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002813
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002814 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
Chao Yue8240f62015-12-15 17:19:26 +08002815 &recovery);
Gu Zheng9076a752013-10-14 18:47:11 +08002816 if (err)
2817 goto free_sbi;
2818
Gu Zheng5fb08372013-06-07 14:16:53 +08002819 sb->s_fs_info = sbi;
Jaegeuk Kim52763a42016-06-13 09:47:48 -07002820 sbi->raw_super = raw_super;
2821
Chao Yu704956e2017-07-31 20:19:09 +08002822 /* precompute checksum seed for metadata */
2823 if (f2fs_sb_has_inode_chksum(sb))
2824 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
2825 sizeof(raw_super->uuid));
2826
Damien Le Moald1b959c2016-10-28 17:45:01 +09002827 /*
2828 * The BLKZONED feature indicates that the drive was formatted with
2829 * zone alignment optimization. This is optional for host-aware
2830 * devices, but mandatory for host-managed zoned block devices.
2831 */
2832#ifndef CONFIG_BLK_DEV_ZONED
Sheng Yongccd31cb2018-02-06 12:31:17 +08002833 if (f2fs_sb_has_blkzoned(sb)) {
Damien Le Moald1b959c2016-10-28 17:45:01 +09002834 f2fs_msg(sb, KERN_ERR,
2835 "Zoned block device support is not enabled\n");
Chao Yu1727f312017-06-12 09:44:27 +08002836 err = -EOPNOTSUPP;
Damien Le Moald1b959c2016-10-28 17:45:01 +09002837 goto free_sb_buf;
2838 }
Damien Le Moald1b959c2016-10-28 17:45:01 +09002839#endif
Yunlei He498c5e92015-05-07 18:11:37 +08002840 default_options(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002841 /* parse mount options */
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002842 options = kstrdup((const char *)data, GFP_KERNEL);
2843 if (data && !options) {
2844 err = -ENOMEM;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002845 goto free_sb_buf;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002846 }
2847
2848 err = parse_options(sb, options);
2849 if (err)
2850 goto free_options;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002851
Chao Yue0afc4d2015-12-31 14:35:37 +08002852 sbi->max_file_blocks = max_file_blocks();
2853 sb->s_maxbytes = sbi->max_file_blocks <<
2854 le32_to_cpu(raw_super->log_blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002855 sb->s_max_links = F2FS_LINK_MAX;
2856 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2857
Chao Yu0abd6752017-07-09 00:13:07 +08002858#ifdef CONFIG_QUOTA
2859 sb->dq_op = &f2fs_quota_operations;
Jaegeuk Kimea676732017-10-06 09:14:28 -07002860 if (f2fs_sb_has_quota_ino(sb))
2861 sb->s_qcop = &dquot_quotactl_sysfile_ops;
2862 else
2863 sb->s_qcop = &f2fs_quotactl_ops;
Chao Yu5c571322017-07-26 00:01:41 +08002864 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
Chao Yu292c196a2017-11-16 16:59:14 +08002865
2866 if (f2fs_sb_has_quota_ino(sbi->sb)) {
2867 for (i = 0; i < MAXQUOTAS; i++) {
2868 if (f2fs_qf_ino(sbi->sb, i))
2869 sbi->nquota_files++;
2870 }
2871 }
Chao Yu0abd6752017-07-09 00:13:07 +08002872#endif
2873
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002874 sb->s_op = &f2fs_sops;
Eric Biggersffcc41822017-10-09 12:15:38 -07002875#ifdef CONFIG_F2FS_FS_ENCRYPTION
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002876 sb->s_cop = &f2fs_cryptops;
Eric Biggersffcc41822017-10-09 12:15:38 -07002877#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002878 sb->s_xattr = f2fs_xattr_handlers;
2879 sb->s_export_op = &f2fs_export_ops;
2880 sb->s_magic = F2FS_SUPER_MAGIC;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002881 sb->s_time_gran = 1;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002882 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2883 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
Christoph Hellwig85787092017-05-10 15:06:33 +02002884 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
Yufen Yu578c6472018-01-09 19:33:39 +08002885 sb->s_iflags |= SB_I_CGROUPWB;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002886
2887 /* init f2fs-specific super block info */
Chao Yue8240f62015-12-15 17:19:26 +08002888 sbi->valid_super_block = valid_super_block;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002889 mutex_init(&sbi->gc_mutex);
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002890 mutex_init(&sbi->writepages);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002891 mutex_init(&sbi->cp_mutex);
Chao Yub3582c62014-07-03 18:58:39 +08002892 init_rwsem(&sbi->node_write);
Yunlei He59c90812017-03-13 20:22:18 +08002893 init_rwsem(&sbi->node_change);
Jaegeuk Kim315df832015-08-11 12:45:39 -07002894
2895 /* disallow all the data/node/meta page writes */
2896 set_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002897 spin_lock_init(&sbi->stat_lock);
Jaegeuk Kim971767c2013-11-18 17:16:17 +09002898
Chao Yub0af6d42017-08-02 23:21:48 +08002899 /* init iostat info */
2900 spin_lock_init(&sbi->iostat_lock);
2901 sbi->iostat_enable = false;
2902
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002903 for (i = 0; i < NR_PAGE_TYPE; i++) {
Jaegeuk Kima912b542017-05-10 11:18:25 -07002904 int n = (i == META) ? 1: NR_TEMP_TYPE;
2905 int j;
2906
Kees Cookc8606592018-06-12 14:28:16 -07002907 sbi->write_io[i] =
2908 f2fs_kmalloc(sbi,
2909 array_size(n,
2910 sizeof(struct f2fs_bio_info)),
2911 GFP_KERNEL);
Christophe JAILLETb63def92017-06-11 09:21:11 +02002912 if (!sbi->write_io[i]) {
2913 err = -ENOMEM;
Chao Yu0b2103e82018-09-05 14:54:01 +08002914 goto free_bio_info;
Christophe JAILLETb63def92017-06-11 09:21:11 +02002915 }
Jaegeuk Kima912b542017-05-10 11:18:25 -07002916
2917 for (j = HOT; j < n; j++) {
2918 init_rwsem(&sbi->write_io[i][j].io_rwsem);
2919 sbi->write_io[i][j].sbi = sbi;
2920 sbi->write_io[i][j].bio = NULL;
Chao Yufb830fc2017-05-19 23:37:01 +08002921 spin_lock_init(&sbi->write_io[i][j].io_lock);
2922 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
Jaegeuk Kima912b542017-05-10 11:18:25 -07002923 }
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002924 }
Jaegeuk Kim971767c2013-11-18 17:16:17 +09002925
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002926 init_rwsem(&sbi->cp_rwsem);
Changman Leefb51b5e2013-11-07 12:48:25 +09002927 init_waitqueue_head(&sbi->cp_wait);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002928 init_sb_info(sbi);
2929
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002930 err = init_percpu_info(sbi);
2931 if (err)
Chao Yud7997e62018-01-17 16:31:35 +08002932 goto free_bio_info;
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002933
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002934 if (F2FS_IO_SIZE(sbi) > 1) {
2935 sbi->write_io_dummy =
Chao Yua3ebfe42017-02-27 18:43:13 +08002936 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
Chao Yu1727f312017-06-12 09:44:27 +08002937 if (!sbi->write_io_dummy) {
2938 err = -ENOMEM;
Chao Yud7997e62018-01-17 16:31:35 +08002939 goto free_percpu;
Chao Yu1727f312017-06-12 09:44:27 +08002940 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002941 }
2942
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002943 /* get an inode for meta space */
2944 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
2945 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002946 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002947 err = PTR_ERR(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002948 goto free_io_dummy;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002949 }
2950
Chao Yu4d57b862018-05-30 00:20:41 +08002951 err = f2fs_get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002952 if (err) {
2953 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002954 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +09002955 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002956
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002957 /* Initialize device list */
2958 err = f2fs_scan_devices(sbi);
2959 if (err) {
2960 f2fs_msg(sb, KERN_ERR, "Failed to find devices");
2961 goto free_devices;
2962 }
2963
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002964 sbi->total_valid_node_count =
2965 le32_to_cpu(sbi->ckpt->valid_node_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07002966 percpu_counter_set(&sbi->total_valid_inode_count,
2967 le32_to_cpu(sbi->ckpt->valid_inode_count));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002968 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
2969 sbi->total_valid_block_count =
2970 le64_to_cpu(sbi->ckpt->valid_block_count);
2971 sbi->last_valid_block_count = sbi->total_valid_block_count;
Chao Yudaeb4332017-06-26 16:24:41 +08002972 sbi->reserved_blocks = 0;
Yunlong Song80d42142017-10-27 20:45:05 +08002973 sbi->current_reserved_blocks = 0;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08002974 limit_reserve_root(sbi);
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002975
Chao Yuc227f912015-12-16 13:09:20 +08002976 for (i = 0; i < NR_INODE_TYPE; i++) {
2977 INIT_LIST_HEAD(&sbi->inode_list[i]);
2978 spin_lock_init(&sbi->inode_lock[i]);
2979 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002980
Chao Yu4d57b862018-05-30 00:20:41 +08002981 f2fs_init_extent_cache_info(sbi);
Chao Yu1dcc3362015-02-05 17:57:31 +08002982
Chao Yu4d57b862018-05-30 00:20:41 +08002983 f2fs_init_ino_entry_info(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002984
Chao Yu50fa53e2018-08-02 23:03:19 +08002985 f2fs_init_fsync_node_info(sbi);
2986
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002987 /* setup f2fs internal modules */
Chao Yu4d57b862018-05-30 00:20:41 +08002988 err = f2fs_build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002989 if (err) {
2990 f2fs_msg(sb, KERN_ERR,
2991 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002992 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +09002993 }
Chao Yu4d57b862018-05-30 00:20:41 +08002994 err = f2fs_build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002995 if (err) {
2996 f2fs_msg(sb, KERN_ERR,
2997 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002998 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +09002999 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003000
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08003001 /* For write statistics */
3002 if (sb->s_bdev->bd_part)
3003 sbi->sectors_written_start =
Michael Callahandbae2c52018-07-18 04:47:38 -07003004 (u64)part_stat_read(sb->s_bdev->bd_part,
3005 sectors[STAT_WRITE]);
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08003006
3007 /* Read accumulated write IO statistics if exists */
3008 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
3009 if (__exist_node_summaries(sbi))
3010 sbi->kbytes_written =
Shuoran Liub2dde6f2016-03-29 18:00:15 +08003011 le64_to_cpu(seg_i->journal->info.kbytes_written);
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08003012
Chao Yu4d57b862018-05-30 00:20:41 +08003013 f2fs_build_gc_manager(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003014
3015 /* get an inode for node space */
3016 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
3017 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09003018 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003019 err = PTR_ERR(sbi->node_inode);
3020 goto free_nm;
3021 }
3022
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08003023 err = f2fs_build_stats(sbi);
3024 if (err)
Chao Yubae01ed2017-11-30 19:28:20 +08003025 goto free_node_inode;
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08003026
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003027 /* read root inode and dentry */
3028 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
3029 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09003030 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003031 err = PTR_ERR(root);
Chao Yubae01ed2017-11-30 19:28:20 +08003032 goto free_stats;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003033 }
Chao Yubcbfbd62018-06-29 00:19:25 +08003034 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
3035 !root->i_size || !root->i_nlink) {
Chao Yu9d847952014-07-25 12:55:09 +08003036 iput(root);
Chao Yu8f99a942013-11-28 15:43:43 +08003037 err = -EINVAL;
Chao Yu4e423832018-06-11 18:02:02 +08003038 goto free_stats;
Chao Yu8f99a942013-11-28 15:43:43 +08003039 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003040
3041 sb->s_root = d_make_root(root); /* allocate root dentry */
3042 if (!sb->s_root) {
3043 err = -ENOMEM;
3044 goto free_root_inode;
3045 }
3046
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07003047 err = f2fs_register_sysfs(sbi);
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09003048 if (err)
Chao Yua3981012017-06-14 17:39:46 +08003049 goto free_root_inode;
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09003050
Jaegeuk Kimea676732017-10-06 09:14:28 -07003051#ifdef CONFIG_QUOTA
Sheng Yong76cf05d2018-07-26 19:24:25 +08003052 /* Enable quota usage during mount */
Chao Yudeeedd72018-03-01 23:40:31 +08003053 if (f2fs_sb_has_quota_ino(sb) && !f2fs_readonly(sb)) {
Jaegeuk Kimea676732017-10-06 09:14:28 -07003054 err = f2fs_enable_quotas(sb);
3055 if (err) {
3056 f2fs_msg(sb, KERN_ERR,
3057 "Cannot turn on quotas: error %d", err);
3058 goto free_sysfs;
3059 }
3060 }
3061#endif
Chao Yu4b2414d2017-08-08 10:54:31 +08003062 /* if there are nt orphan nodes free them */
Chao Yu4d57b862018-05-30 00:20:41 +08003063 err = f2fs_recover_orphan_inodes(sbi);
Chao Yu4b2414d2017-08-08 10:54:31 +08003064 if (err)
Jaegeuk Kimea676732017-10-06 09:14:28 -07003065 goto free_meta;
Chao Yu4b2414d2017-08-08 10:54:31 +08003066
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09003067 /* recover fsynced data */
3068 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08003069 /*
3070 * mount should be failed, when device has readonly mode, and
3071 * previous checkpoint was not done by clean system shutdown.
3072 */
3073 if (bdev_read_only(sb->s_bdev) &&
Chao Yuaaec2b12016-09-20 11:04:18 +08003074 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08003075 err = -EROFS;
Chao Yu4b2414d2017-08-08 10:54:31 +08003076 goto free_meta;
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08003077 }
Chao Yu2adc3502015-03-16 21:08:44 +08003078
3079 if (need_fsck)
3080 set_sbi_flag(sbi, SBI_NEED_FSCK);
3081
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07003082 if (!retry)
3083 goto skip_recovery;
3084
Chao Yu4d57b862018-05-30 00:20:41 +08003085 err = f2fs_recover_fsync_data(sbi, false);
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07003086 if (err < 0) {
Chao Yu2adc3502015-03-16 21:08:44 +08003087 need_fsck = true;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09003088 f2fs_msg(sb, KERN_ERR,
Sheng Yong99e3e852016-05-11 17:08:14 +08003089 "Cannot recover all fsync data errno=%d", err);
Chao Yu4b2414d2017-08-08 10:54:31 +08003090 goto free_meta;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07003091 }
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07003092 } else {
Chao Yu4d57b862018-05-30 00:20:41 +08003093 err = f2fs_recover_fsync_data(sbi, true);
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07003094
3095 if (!f2fs_readonly(sb) && err > 0) {
3096 err = -EINVAL;
3097 f2fs_msg(sb, KERN_ERR,
3098 "Need to recover fsync data");
Jaegeuk Kimea676732017-10-06 09:14:28 -07003099 goto free_meta;
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07003100 }
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09003101 }
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07003102skip_recovery:
Chao Yu4d57b862018-05-30 00:20:41 +08003103 /* f2fs_recover_fsync_data() cleared this already */
Jaegeuk Kim315df832015-08-11 12:45:39 -07003104 clear_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09003105
3106 /*
3107 * If filesystem is not mounted as read-only then
3108 * do start the gc_thread.
3109 */
Chao Yu6c029932014-11-18 11:16:01 +08003110 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09003111 /* After POR, we can run background GC thread.*/
Chao Yu4d57b862018-05-30 00:20:41 +08003112 err = f2fs_start_gc_thread(sbi);
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09003113 if (err)
Chao Yu4b2414d2017-08-08 10:54:31 +08003114 goto free_meta;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09003115 }
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08003116 kfree(options);
hujianyangda554e42015-05-21 14:42:53 +08003117
3118 /* recover broken superblock */
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07003119 if (recovery) {
Chao Yu41214b32016-02-22 18:33:20 +08003120 err = f2fs_commit_super(sbi, true);
3121 f2fs_msg(sb, KERN_INFO,
Sheng Yong99e3e852016-05-11 17:08:14 +08003122 "Try to recover %dth superblock, ret: %d",
Chao Yu41214b32016-02-22 18:33:20 +08003123 sbi->valid_super_block ? 1 : 2, err);
hujianyangda554e42015-05-21 14:42:53 +08003124 }
3125
Chao Yubae01ed2017-11-30 19:28:20 +08003126 f2fs_join_shrinker(sbi);
3127
Jaegeuk Kim84b89e52018-02-22 14:09:30 -08003128 f2fs_tuning_parameters(sbi);
3129
Jaegeuk Kim1200abb2017-02-09 13:25:35 -08003130 f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
3131 cur_cp_version(F2FS_CKPT(sbi)));
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08003132 f2fs_update_time(sbi, CP_TIME);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08003133 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003134 return 0;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09003135
Chao Yu4b2414d2017-08-08 10:54:31 +08003136free_meta:
Jaegeuk Kimea676732017-10-06 09:14:28 -07003137#ifdef CONFIG_QUOTA
Chao Yudeeedd72018-03-01 23:40:31 +08003138 if (f2fs_sb_has_quota_ino(sb) && !f2fs_readonly(sb))
Jaegeuk Kimea676732017-10-06 09:14:28 -07003139 f2fs_quota_off_umount(sbi->sb);
3140#endif
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07003141 f2fs_sync_inode_meta(sbi);
Chao Yu4b2414d2017-08-08 10:54:31 +08003142 /*
Chao Yu4d57b862018-05-30 00:20:41 +08003143 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
Chao Yu4b2414d2017-08-08 10:54:31 +08003144 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
Chao Yu4d57b862018-05-30 00:20:41 +08003145 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
3146 * falls into an infinite loop in f2fs_sync_meta_pages().
Chao Yu4b2414d2017-08-08 10:54:31 +08003147 */
3148 truncate_inode_pages_final(META_MAPPING(sbi));
Jaegeuk Kimea676732017-10-06 09:14:28 -07003149#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +08003150free_sysfs:
Jaegeuk Kimea676732017-10-06 09:14:28 -07003151#endif
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07003152 f2fs_unregister_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003153free_root_inode:
3154 dput(sb->s_root);
3155 sb->s_root = NULL;
Chao Yubae01ed2017-11-30 19:28:20 +08003156free_stats:
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08003157 f2fs_destroy_stats(sbi);
Chao Yubae01ed2017-11-30 19:28:20 +08003158free_node_inode:
Chao Yu4d57b862018-05-30 00:20:41 +08003159 f2fs_release_ino_entry(sbi, true);
Chao Yubae01ed2017-11-30 19:28:20 +08003160 truncate_inode_pages_final(NODE_MAPPING(sbi));
3161 iput(sbi->node_inode);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003162free_nm:
Chao Yu4d57b862018-05-30 00:20:41 +08003163 f2fs_destroy_node_manager(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003164free_sm:
Chao Yu4d57b862018-05-30 00:20:41 +08003165 f2fs_destroy_segment_manager(sbi);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003166free_devices:
3167 destroy_device_list(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003168 kfree(sbi->ckpt);
3169free_meta_inode:
3170 make_bad_inode(sbi->meta_inode);
3171 iput(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08003172free_io_dummy:
3173 mempool_destroy(sbi->write_io_dummy);
Chao Yud7997e62018-01-17 16:31:35 +08003174free_percpu:
3175 destroy_percpu_info(sbi);
3176free_bio_info:
Jaegeuk Kima912b542017-05-10 11:18:25 -07003177 for (i = 0; i < NR_PAGE_TYPE; i++)
3178 kfree(sbi->write_io[i]);
Chao Yud7997e62018-01-17 16:31:35 +08003179free_options:
Chao Yu4b2414d2017-08-08 10:54:31 +08003180#ifdef CONFIG_QUOTA
3181 for (i = 0; i < MAXQUOTAS; i++)
Chao Yu63189b72018-03-08 14:22:56 +08003182 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
Chao Yu4b2414d2017-08-08 10:54:31 +08003183#endif
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08003184 kfree(options);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003185free_sb_buf:
Yunlei Heb39f0de2015-12-15 17:17:20 +08003186 kfree(raw_super);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003187free_sbi:
Keith Mok43b65732016-03-02 12:04:24 -08003188 if (sbi->s_chksum_driver)
3189 crypto_free_shash(sbi->s_chksum_driver);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003190 kfree(sbi);
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07003191
3192 /* give only one another chance */
3193 if (retry) {
Taehee Yoo9df47ba2015-04-13 21:48:06 +09003194 retry = false;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07003195 shrink_dcache_sb(sb);
3196 goto try_onemore;
3197 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003198 return err;
3199}
3200
3201static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
3202 const char *dev_name, void *data)
3203{
3204 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
3205}
3206
Jaegeuk Kim30a55372015-01-14 16:34:24 -08003207static void kill_f2fs_super(struct super_block *sb)
3208{
Chao Yucce13252017-06-29 23:17:45 +08003209 if (sb->s_root) {
Jaegeuk Kim1cb50f82018-07-06 16:47:34 -07003210 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3211
3212 set_sbi_flag(sbi, SBI_IS_CLOSE);
3213 f2fs_stop_gc_thread(sbi);
3214 f2fs_stop_discard_thread(sbi);
3215
3216 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
3217 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3218 struct cp_control cpc = {
3219 .reason = CP_UMOUNT,
3220 };
3221 f2fs_write_checkpoint(sbi, &cpc);
3222 }
Chao Yu13787522018-08-22 17:11:05 +08003223
3224 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
3225 sb->s_flags &= ~SB_RDONLY;
Chao Yucce13252017-06-29 23:17:45 +08003226 }
Jaegeuk Kim30a55372015-01-14 16:34:24 -08003227 kill_block_super(sb);
3228}
3229
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003230static struct file_system_type f2fs_fs_type = {
3231 .owner = THIS_MODULE,
3232 .name = "f2fs",
3233 .mount = f2fs_mount,
Jaegeuk Kim30a55372015-01-14 16:34:24 -08003234 .kill_sb = kill_f2fs_super,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003235 .fs_flags = FS_REQUIRES_DEV,
3236};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08003237MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003238
Namjae Jeon6e6093a2013-01-17 00:08:30 +09003239static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003240{
Vladimir Davydov5d097052016-01-14 15:18:21 -08003241 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
3242 sizeof(struct f2fs_inode_info), 0,
3243 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09003244 if (!f2fs_inode_cachep)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003245 return -ENOMEM;
3246 return 0;
3247}
3248
3249static void destroy_inodecache(void)
3250{
3251 /*
3252 * Make sure all delayed rcu free inodes are flushed before we
3253 * destroy cache.
3254 */
3255 rcu_barrier();
3256 kmem_cache_destroy(f2fs_inode_cachep);
3257}
3258
3259static int __init init_f2fs_fs(void)
3260{
3261 int err;
3262
Anatoly Pugachev4071e672018-05-28 02:06:37 +03003263 if (PAGE_SIZE != F2FS_BLKSIZE) {
3264 printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
3265 PAGE_SIZE, F2FS_BLKSIZE);
3266 return -EINVAL;
3267 }
3268
Jaegeuk Kimc0508652015-01-07 14:07:36 -08003269 f2fs_build_trace_ios();
3270
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003271 err = init_inodecache();
3272 if (err)
3273 goto fail;
Chao Yu4d57b862018-05-30 00:20:41 +08003274 err = f2fs_create_node_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003275 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08003276 goto free_inodecache;
Chao Yu4d57b862018-05-30 00:20:41 +08003277 err = f2fs_create_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003278 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08003279 goto free_node_manager_caches;
Chao Yu4d57b862018-05-30 00:20:41 +08003280 err = f2fs_create_checkpoint_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003281 if (err)
Chao Yu06292072014-12-29 15:56:18 +08003282 goto free_segment_manager_caches;
Chao Yu4d57b862018-05-30 00:20:41 +08003283 err = f2fs_create_extent_cache();
Chao Yu1dcc3362015-02-05 17:57:31 +08003284 if (err)
3285 goto free_checkpoint_caches;
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07003286 err = f2fs_init_sysfs();
Chao Yua3981012017-06-14 17:39:46 +08003287 if (err)
Chao Yu1dcc3362015-02-05 17:57:31 +08003288 goto free_extent_cache;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07003289 err = register_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimcfc4d972015-05-15 15:37:24 -07003290 if (err)
Chao Yua3981012017-06-14 17:39:46 +08003291 goto free_sysfs;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07003292 err = register_filesystem(&f2fs_fs_type);
3293 if (err)
3294 goto free_shrinker;
Chao Yu787c7b8c2015-10-29 09:13:04 +08003295 err = f2fs_create_root_stats();
3296 if (err)
3297 goto free_filesystem;
Eric Biggers6dbb1792018-04-18 11:09:48 -07003298 err = f2fs_init_post_read_processing();
3299 if (err)
3300 goto free_root_stats;
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08003301 return 0;
3302
Eric Biggers6dbb1792018-04-18 11:09:48 -07003303free_root_stats:
3304 f2fs_destroy_root_stats();
Chao Yu787c7b8c2015-10-29 09:13:04 +08003305free_filesystem:
3306 unregister_filesystem(&f2fs_fs_type);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07003307free_shrinker:
3308 unregister_shrinker(&f2fs_shrinker_info);
Chao Yua3981012017-06-14 17:39:46 +08003309free_sysfs:
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07003310 f2fs_exit_sysfs();
Chao Yu1dcc3362015-02-05 17:57:31 +08003311free_extent_cache:
Chao Yu4d57b862018-05-30 00:20:41 +08003312 f2fs_destroy_extent_cache();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08003313free_checkpoint_caches:
Chao Yu4d57b862018-05-30 00:20:41 +08003314 f2fs_destroy_checkpoint_caches();
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003315free_segment_manager_caches:
Chao Yu4d57b862018-05-30 00:20:41 +08003316 f2fs_destroy_segment_manager_caches();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08003317free_node_manager_caches:
Chao Yu4d57b862018-05-30 00:20:41 +08003318 f2fs_destroy_node_manager_caches();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08003319free_inodecache:
3320 destroy_inodecache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003321fail:
3322 return err;
3323}
3324
3325static void __exit exit_f2fs_fs(void)
3326{
Eric Biggers6dbb1792018-04-18 11:09:48 -07003327 f2fs_destroy_post_read_processing();
Namjae Jeon4589d252013-01-15 19:58:47 +09003328 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003329 unregister_filesystem(&f2fs_fs_type);
Tiezhu Yangb8bef792016-05-18 08:02:25 +08003330 unregister_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07003331 f2fs_exit_sysfs();
Chao Yu4d57b862018-05-30 00:20:41 +08003332 f2fs_destroy_extent_cache();
3333 f2fs_destroy_checkpoint_caches();
3334 f2fs_destroy_segment_manager_caches();
3335 f2fs_destroy_node_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003336 destroy_inodecache();
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -08003337 f2fs_destroy_trace_ios();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003338}
3339
3340module_init(init_f2fs_fs)
3341module_exit(exit_f2fs_fs)
3342
3343MODULE_AUTHOR("Samsung Electronics's Praesto Team");
3344MODULE_DESCRIPTION("Flash Friendly File System");
3345MODULE_LICENSE("GPL");
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -07003346