blob: 9b6c53b695353c570f05923d09524b7588777872 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002 * fs/f2fs/super.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/fs.h>
14#include <linux/statfs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090015#include <linux/buffer_head.h>
16#include <linux/backing-dev.h>
17#include <linux/kthread.h>
18#include <linux/parser.h>
19#include <linux/mount.h>
20#include <linux/seq_file.h>
Jaegeuk Kim5e176d52013-06-28 12:47:01 +090021#include <linux/proc_fs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090022#include <linux/random.h>
23#include <linux/exportfs.h>
Namjae Jeond3ee4562013-03-17 17:26:14 +090024#include <linux/blkdev.h>
Chao Yu0abd6752017-07-09 00:13:07 +080025#include <linux/quotaops.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090026#include <linux/f2fs_fs.h>
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090027#include <linux/sysfs.h>
Chao Yu4b2414d2017-08-08 10:54:31 +080028#include <linux/quota.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090029
30#include "f2fs.h"
31#include "node.h"
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +090032#include "segment.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090033#include "xattr.h"
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090034#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080035#include "trace.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090036
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090037#define CREATE_TRACE_POINTS
38#include <trace/events/f2fs.h>
39
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090040static struct kmem_cache *f2fs_inode_cachep;
41
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -070042#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070043
44char *fault_name[FAULT_MAX] = {
45 [FAULT_KMALLOC] = "kmalloc",
Chao Yu628b3d12017-11-30 19:28:18 +080046 [FAULT_KVMALLOC] = "kvmalloc",
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -070047 [FAULT_PAGE_ALLOC] = "page alloc",
Chao Yu01eccef2017-10-28 16:52:30 +080048 [FAULT_PAGE_GET] = "page get",
Chao Yud62fe972017-10-28 16:52:31 +080049 [FAULT_ALLOC_BIO] = "alloc bio",
Jaegeuk Kimcb789422016-04-29 16:29:22 -070050 [FAULT_ALLOC_NID] = "alloc nid",
51 [FAULT_ORPHAN] = "orphan",
52 [FAULT_BLOCK] = "no more block",
53 [FAULT_DIR_DEPTH] = "too big dir depth",
Jaegeuk Kim53aa6bb2016-05-25 15:24:18 -070054 [FAULT_EVICT_INODE] = "evict_inode fail",
Jaegeuk Kim14b44d22017-03-09 15:24:24 -080055 [FAULT_TRUNCATE] = "truncate fail",
Chao Yu8b038c72016-09-18 23:30:07 +080056 [FAULT_IO] = "IO error",
Chao Yu0f348022016-09-26 19:45:55 +080057 [FAULT_CHECKPOINT] = "checkpoint error",
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070058};
Sheng Yong08796892016-05-16 12:38:50 +080059
Chao Yu1ecc0c52016-09-23 21:30:09 +080060static void f2fs_build_fault_attr(struct f2fs_sb_info *sbi,
61 unsigned int rate)
Sheng Yong08796892016-05-16 12:38:50 +080062{
Chao Yu1ecc0c52016-09-23 21:30:09 +080063 struct f2fs_fault_info *ffi = &sbi->fault_info;
64
Sheng Yong08796892016-05-16 12:38:50 +080065 if (rate) {
Chao Yu1ecc0c52016-09-23 21:30:09 +080066 atomic_set(&ffi->inject_ops, 0);
67 ffi->inject_rate = rate;
68 ffi->inject_type = (1 << FAULT_MAX) - 1;
Sheng Yong08796892016-05-16 12:38:50 +080069 } else {
Chao Yu1ecc0c52016-09-23 21:30:09 +080070 memset(ffi, 0, sizeof(struct f2fs_fault_info));
Sheng Yong08796892016-05-16 12:38:50 +080071 }
72}
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -070073#endif
74
Jaegeuk Kim2658e502015-06-19 12:01:21 -070075/* f2fs-wide shrinker description */
76static struct shrinker f2fs_shrinker_info = {
77 .scan_objects = f2fs_shrink_scan,
78 .count_objects = f2fs_shrink_count,
79 .seeks = DEFAULT_SEEKS,
80};
81
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090082enum {
Namjae Jeon696c0182013-06-16 09:48:48 +090083 Opt_gc_background,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090084 Opt_disable_roll_forward,
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -080085 Opt_norecovery,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090086 Opt_discard,
Chao Yu64058be2016-07-03 22:05:14 +080087 Opt_nodiscard,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090088 Opt_noheap,
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -040089 Opt_heap,
Kelly Anderson4058c512013-10-07 11:36:20 +090090 Opt_user_xattr,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090091 Opt_nouser_xattr,
Kelly Anderson4058c512013-10-07 11:36:20 +090092 Opt_acl,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090093 Opt_noacl,
94 Opt_active_logs,
95 Opt_disable_ext_identify,
Jaegeuk Kim444c5802013-08-08 15:16:22 +090096 Opt_inline_xattr,
Chao Yu23cf7212017-02-15 10:34:45 +080097 Opt_noinline_xattr,
Chao Yu6afc6622017-09-06 21:59:50 +080098 Opt_inline_xattr_size,
Huajun Li8274de72013-11-10 23:13:17 +080099 Opt_inline_data,
Chao Yu5efd3c62014-09-24 18:16:13 +0800100 Opt_inline_dentry,
Chao Yu97c17942016-05-09 19:56:34 +0800101 Opt_noinline_dentry,
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900102 Opt_flush_merge,
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700103 Opt_noflush_merge,
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700104 Opt_nobarrier,
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700105 Opt_fastboot,
Chao Yu89672152015-02-05 17:55:51 +0800106 Opt_extent_cache,
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700107 Opt_noextent_cache,
Wanpeng Li75342792015-03-24 10:20:27 +0800108 Opt_noinline_data,
Chao Yu343f40f2015-12-16 13:12:16 +0800109 Opt_data_flush,
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800110 Opt_reserve_root,
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800111 Opt_resgid,
112 Opt_resuid,
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700113 Opt_mode,
Jaegeuk Kimec915382016-12-21 17:09:19 -0800114 Opt_io_size_bits,
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700115 Opt_fault_injection,
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700116 Opt_lazytime,
117 Opt_nolazytime,
Chao Yu4b2414d2017-08-08 10:54:31 +0800118 Opt_quota,
119 Opt_noquota,
Chao Yu0abd6752017-07-09 00:13:07 +0800120 Opt_usrquota,
121 Opt_grpquota,
Chao Yu5c571322017-07-26 00:01:41 +0800122 Opt_prjquota,
Chao Yu4b2414d2017-08-08 10:54:31 +0800123 Opt_usrjquota,
124 Opt_grpjquota,
125 Opt_prjjquota,
126 Opt_offusrjquota,
127 Opt_offgrpjquota,
128 Opt_offprjjquota,
129 Opt_jqfmt_vfsold,
130 Opt_jqfmt_vfsv0,
131 Opt_jqfmt_vfsv1,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900132 Opt_whint,
Jaegeuk Kim07939622018-02-18 08:50:49 -0800133 Opt_alloc,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900134 Opt_err,
135};
136
137static match_table_t f2fs_tokens = {
Namjae Jeon696c0182013-06-16 09:48:48 +0900138 {Opt_gc_background, "background_gc=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900139 {Opt_disable_roll_forward, "disable_roll_forward"},
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800140 {Opt_norecovery, "norecovery"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900141 {Opt_discard, "discard"},
Chao Yu64058be2016-07-03 22:05:14 +0800142 {Opt_nodiscard, "nodiscard"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900143 {Opt_noheap, "no_heap"},
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400144 {Opt_heap, "heap"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900145 {Opt_user_xattr, "user_xattr"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900146 {Opt_nouser_xattr, "nouser_xattr"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900147 {Opt_acl, "acl"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900148 {Opt_noacl, "noacl"},
149 {Opt_active_logs, "active_logs=%u"},
150 {Opt_disable_ext_identify, "disable_ext_identify"},
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900151 {Opt_inline_xattr, "inline_xattr"},
Chao Yu23cf7212017-02-15 10:34:45 +0800152 {Opt_noinline_xattr, "noinline_xattr"},
Chao Yu6afc6622017-09-06 21:59:50 +0800153 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
Huajun Li8274de72013-11-10 23:13:17 +0800154 {Opt_inline_data, "inline_data"},
Chao Yu5efd3c62014-09-24 18:16:13 +0800155 {Opt_inline_dentry, "inline_dentry"},
Chao Yu97c17942016-05-09 19:56:34 +0800156 {Opt_noinline_dentry, "noinline_dentry"},
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900157 {Opt_flush_merge, "flush_merge"},
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700158 {Opt_noflush_merge, "noflush_merge"},
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700159 {Opt_nobarrier, "nobarrier"},
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700160 {Opt_fastboot, "fastboot"},
Chao Yu89672152015-02-05 17:55:51 +0800161 {Opt_extent_cache, "extent_cache"},
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700162 {Opt_noextent_cache, "noextent_cache"},
Wanpeng Li75342792015-03-24 10:20:27 +0800163 {Opt_noinline_data, "noinline_data"},
Chao Yu343f40f2015-12-16 13:12:16 +0800164 {Opt_data_flush, "data_flush"},
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800165 {Opt_reserve_root, "reserve_root=%u"},
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800166 {Opt_resgid, "resgid=%u"},
167 {Opt_resuid, "resuid=%u"},
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700168 {Opt_mode, "mode=%s"},
Jaegeuk Kimec915382016-12-21 17:09:19 -0800169 {Opt_io_size_bits, "io_bits=%u"},
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700170 {Opt_fault_injection, "fault_injection=%u"},
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700171 {Opt_lazytime, "lazytime"},
172 {Opt_nolazytime, "nolazytime"},
Chao Yu4b2414d2017-08-08 10:54:31 +0800173 {Opt_quota, "quota"},
174 {Opt_noquota, "noquota"},
Chao Yu0abd6752017-07-09 00:13:07 +0800175 {Opt_usrquota, "usrquota"},
176 {Opt_grpquota, "grpquota"},
Chao Yu5c571322017-07-26 00:01:41 +0800177 {Opt_prjquota, "prjquota"},
Chao Yu4b2414d2017-08-08 10:54:31 +0800178 {Opt_usrjquota, "usrjquota=%s"},
179 {Opt_grpjquota, "grpjquota=%s"},
180 {Opt_prjjquota, "prjjquota=%s"},
181 {Opt_offusrjquota, "usrjquota="},
182 {Opt_offgrpjquota, "grpjquota="},
183 {Opt_offprjjquota, "prjjquota="},
184 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
185 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
186 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900187 {Opt_whint, "whint_mode=%s"},
Jaegeuk Kim07939622018-02-18 08:50:49 -0800188 {Opt_alloc, "alloc_mode=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900189 {Opt_err, NULL},
190};
191
Namjae Jeona07ef782012-12-30 14:52:05 +0900192void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
193{
194 struct va_format vaf;
195 va_list args;
196
197 va_start(args, fmt);
198 vaf.fmt = fmt;
199 vaf.va = &args;
Jaegeuk Kima36c1062017-08-02 20:58:29 -0700200 printk_ratelimited("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
Namjae Jeona07ef782012-12-30 14:52:05 +0900201 va_end(args);
202}
203
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800204static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
205{
206 block_t limit = (sbi->user_block_count << 1) / 1000;
207
208 /* limit is 0.2% */
209 if (test_opt(sbi, RESERVE_ROOT) && sbi->root_reserved_blocks > limit) {
210 sbi->root_reserved_blocks = limit;
211 f2fs_msg(sbi->sb, KERN_INFO,
212 "Reduce reserved blocks for root = %u",
213 sbi->root_reserved_blocks);
214 }
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800215 if (!test_opt(sbi, RESERVE_ROOT) &&
216 (!uid_eq(sbi->s_resuid,
217 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
218 !gid_eq(sbi->s_resgid,
219 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
220 f2fs_msg(sbi->sb, KERN_INFO,
221 "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
222 from_kuid_munged(&init_user_ns, sbi->s_resuid),
223 from_kgid_munged(&init_user_ns, sbi->s_resgid));
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800224}
225
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900226static void init_once(void *foo)
227{
228 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
229
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900230 inode_init_once(&fi->vfs_inode);
231}
232
Chao Yu4b2414d2017-08-08 10:54:31 +0800233#ifdef CONFIG_QUOTA
234static const char * const quotatypes[] = INITQFNAMES;
235#define QTYPE2NAME(t) (quotatypes[t])
236static int f2fs_set_qf_name(struct super_block *sb, int qtype,
237 substring_t *args)
238{
239 struct f2fs_sb_info *sbi = F2FS_SB(sb);
240 char *qname;
241 int ret = -EINVAL;
242
243 if (sb_any_quota_loaded(sb) && !sbi->s_qf_names[qtype]) {
244 f2fs_msg(sb, KERN_ERR,
245 "Cannot change journaled "
246 "quota options when quota turned on");
247 return -EINVAL;
248 }
Jaegeuk Kimea676732017-10-06 09:14:28 -0700249 if (f2fs_sb_has_quota_ino(sb)) {
250 f2fs_msg(sb, KERN_INFO,
251 "QUOTA feature is enabled, so ignore qf_name");
252 return 0;
253 }
254
Chao Yu4b2414d2017-08-08 10:54:31 +0800255 qname = match_strdup(args);
256 if (!qname) {
257 f2fs_msg(sb, KERN_ERR,
258 "Not enough memory for storing quotafile name");
259 return -EINVAL;
260 }
261 if (sbi->s_qf_names[qtype]) {
262 if (strcmp(sbi->s_qf_names[qtype], qname) == 0)
263 ret = 0;
264 else
265 f2fs_msg(sb, KERN_ERR,
266 "%s quota file already specified",
267 QTYPE2NAME(qtype));
268 goto errout;
269 }
270 if (strchr(qname, '/')) {
271 f2fs_msg(sb, KERN_ERR,
272 "quotafile must be on filesystem root");
273 goto errout;
274 }
275 sbi->s_qf_names[qtype] = qname;
276 set_opt(sbi, QUOTA);
277 return 0;
278errout:
279 kfree(qname);
280 return ret;
281}
282
283static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
284{
285 struct f2fs_sb_info *sbi = F2FS_SB(sb);
286
287 if (sb_any_quota_loaded(sb) && sbi->s_qf_names[qtype]) {
288 f2fs_msg(sb, KERN_ERR, "Cannot change journaled quota options"
289 " when quota turned on");
290 return -EINVAL;
291 }
292 kfree(sbi->s_qf_names[qtype]);
293 sbi->s_qf_names[qtype] = NULL;
294 return 0;
295}
296
297static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
298{
299 /*
300 * We do the test below only for project quotas. 'usrquota' and
301 * 'grpquota' mount options are allowed even without quota feature
302 * to support legacy quotas in quota files.
303 */
304 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi->sb)) {
305 f2fs_msg(sbi->sb, KERN_ERR, "Project quota feature not enabled. "
306 "Cannot enable project quota enforcement.");
307 return -1;
308 }
309 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA] ||
310 sbi->s_qf_names[PRJQUOTA]) {
311 if (test_opt(sbi, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
312 clear_opt(sbi, USRQUOTA);
313
314 if (test_opt(sbi, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
315 clear_opt(sbi, GRPQUOTA);
316
317 if (test_opt(sbi, PRJQUOTA) && sbi->s_qf_names[PRJQUOTA])
318 clear_opt(sbi, PRJQUOTA);
319
320 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
321 test_opt(sbi, PRJQUOTA)) {
322 f2fs_msg(sbi->sb, KERN_ERR, "old and new quota "
323 "format mixing");
324 return -1;
325 }
326
327 if (!sbi->s_jquota_fmt) {
328 f2fs_msg(sbi->sb, KERN_ERR, "journaled quota format "
329 "not specified");
330 return -1;
331 }
332 }
Jaegeuk Kimea676732017-10-06 09:14:28 -0700333
334 if (f2fs_sb_has_quota_ino(sbi->sb) && sbi->s_jquota_fmt) {
335 f2fs_msg(sbi->sb, KERN_INFO,
336 "QUOTA feature is enabled, so ignore jquota_fmt");
337 sbi->s_jquota_fmt = 0;
338 }
339 if (f2fs_sb_has_quota_ino(sbi->sb) && sb_rdonly(sbi->sb)) {
340 f2fs_msg(sbi->sb, KERN_INFO,
341 "Filesystem with quota feature cannot be mounted RDWR "
342 "without CONFIG_QUOTA");
343 return -1;
344 }
Chao Yu4b2414d2017-08-08 10:54:31 +0800345 return 0;
346}
347#endif
348
Namjae Jeon696c0182013-06-16 09:48:48 +0900349static int parse_options(struct super_block *sb, char *options)
350{
351 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yu09d54cd2015-06-08 13:20:10 +0800352 struct request_queue *q;
Namjae Jeon696c0182013-06-16 09:48:48 +0900353 substring_t args[MAX_OPT_ARGS];
354 char *p, *name;
355 int arg = 0;
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800356 kuid_t uid;
357 kgid_t gid;
Chao Yu4b2414d2017-08-08 10:54:31 +0800358#ifdef CONFIG_QUOTA
359 int ret;
360#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900361
362 if (!options)
363 return 0;
364
365 while ((p = strsep(&options, ",")) != NULL) {
366 int token;
367 if (!*p)
368 continue;
369 /*
370 * Initialize args struct so we know whether arg was
371 * found; some options take optional arguments.
372 */
373 args[0].to = args[0].from = NULL;
374 token = match_token(p, f2fs_tokens, args);
375
376 switch (token) {
377 case Opt_gc_background:
378 name = match_strdup(&args[0]);
379
380 if (!name)
381 return -ENOMEM;
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700382 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900383 set_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700384 clear_opt(sbi, FORCE_FG_GC);
385 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900386 clear_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700387 clear_opt(sbi, FORCE_FG_GC);
388 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
389 set_opt(sbi, BG_GC);
390 set_opt(sbi, FORCE_FG_GC);
391 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +0900392 kfree(name);
393 return -EINVAL;
394 }
395 kfree(name);
396 break;
397 case Opt_disable_roll_forward:
398 set_opt(sbi, DISABLE_ROLL_FORWARD);
399 break;
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800400 case Opt_norecovery:
401 /* this option mounts f2fs with ro */
402 set_opt(sbi, DISABLE_ROLL_FORWARD);
403 if (!f2fs_readonly(sb))
404 return -EINVAL;
405 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900406 case Opt_discard:
Chao Yu09d54cd2015-06-08 13:20:10 +0800407 q = bdev_get_queue(sb->s_bdev);
408 if (blk_queue_discard(q)) {
409 set_opt(sbi, DISCARD);
Sheng Yongccd31cb2018-02-06 12:31:17 +0800410 } else if (!f2fs_sb_has_blkzoned(sb)) {
Chao Yu09d54cd2015-06-08 13:20:10 +0800411 f2fs_msg(sb, KERN_WARNING,
412 "mounting with \"discard\" option, but "
413 "the device does not support discard");
414 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900415 break;
Chao Yu64058be2016-07-03 22:05:14 +0800416 case Opt_nodiscard:
Sheng Yongccd31cb2018-02-06 12:31:17 +0800417 if (f2fs_sb_has_blkzoned(sb)) {
Damien Le Moal96ba2de2016-10-28 17:45:03 +0900418 f2fs_msg(sb, KERN_WARNING,
419 "discard is required for zoned block devices");
420 return -EINVAL;
421 }
Chao Yu64058be2016-07-03 22:05:14 +0800422 clear_opt(sbi, DISCARD);
Damien Le Moal487df612016-10-28 17:44:59 +0900423 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900424 case Opt_noheap:
425 set_opt(sbi, NOHEAP);
426 break;
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400427 case Opt_heap:
428 clear_opt(sbi, NOHEAP);
429 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900430#ifdef CONFIG_F2FS_FS_XATTR
Kelly Anderson4058c512013-10-07 11:36:20 +0900431 case Opt_user_xattr:
432 set_opt(sbi, XATTR_USER);
433 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900434 case Opt_nouser_xattr:
435 clear_opt(sbi, XATTR_USER);
436 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900437 case Opt_inline_xattr:
438 set_opt(sbi, INLINE_XATTR);
439 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800440 case Opt_noinline_xattr:
441 clear_opt(sbi, INLINE_XATTR);
442 break;
Chao Yu6afc6622017-09-06 21:59:50 +0800443 case Opt_inline_xattr_size:
444 if (args->from && match_int(args, &arg))
445 return -EINVAL;
446 set_opt(sbi, INLINE_XATTR_SIZE);
447 sbi->inline_xattr_size = arg;
448 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900449#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900450 case Opt_user_xattr:
451 f2fs_msg(sb, KERN_INFO,
452 "user_xattr options not supported");
453 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900454 case Opt_nouser_xattr:
455 f2fs_msg(sb, KERN_INFO,
456 "nouser_xattr options not supported");
457 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900458 case Opt_inline_xattr:
459 f2fs_msg(sb, KERN_INFO,
460 "inline_xattr options not supported");
461 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800462 case Opt_noinline_xattr:
463 f2fs_msg(sb, KERN_INFO,
464 "noinline_xattr options not supported");
465 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900466#endif
467#ifdef CONFIG_F2FS_FS_POSIX_ACL
Kelly Anderson4058c512013-10-07 11:36:20 +0900468 case Opt_acl:
469 set_opt(sbi, POSIX_ACL);
470 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900471 case Opt_noacl:
472 clear_opt(sbi, POSIX_ACL);
473 break;
474#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900475 case Opt_acl:
476 f2fs_msg(sb, KERN_INFO, "acl options not supported");
477 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900478 case Opt_noacl:
479 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
480 break;
481#endif
482 case Opt_active_logs:
483 if (args->from && match_int(args, &arg))
484 return -EINVAL;
485 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
486 return -EINVAL;
487 sbi->active_logs = arg;
488 break;
489 case Opt_disable_ext_identify:
490 set_opt(sbi, DISABLE_EXT_IDENTIFY);
491 break;
Huajun Li8274de72013-11-10 23:13:17 +0800492 case Opt_inline_data:
493 set_opt(sbi, INLINE_DATA);
494 break;
Chao Yu5efd3c62014-09-24 18:16:13 +0800495 case Opt_inline_dentry:
496 set_opt(sbi, INLINE_DENTRY);
497 break;
Chao Yu97c17942016-05-09 19:56:34 +0800498 case Opt_noinline_dentry:
499 clear_opt(sbi, INLINE_DENTRY);
500 break;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900501 case Opt_flush_merge:
502 set_opt(sbi, FLUSH_MERGE);
503 break;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700504 case Opt_noflush_merge:
505 clear_opt(sbi, FLUSH_MERGE);
506 break;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700507 case Opt_nobarrier:
508 set_opt(sbi, NOBARRIER);
509 break;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700510 case Opt_fastboot:
511 set_opt(sbi, FASTBOOT);
512 break;
Chao Yu89672152015-02-05 17:55:51 +0800513 case Opt_extent_cache:
514 set_opt(sbi, EXTENT_CACHE);
515 break;
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700516 case Opt_noextent_cache:
517 clear_opt(sbi, EXTENT_CACHE);
518 break;
Wanpeng Li75342792015-03-24 10:20:27 +0800519 case Opt_noinline_data:
520 clear_opt(sbi, INLINE_DATA);
521 break;
Chao Yu343f40f2015-12-16 13:12:16 +0800522 case Opt_data_flush:
523 set_opt(sbi, DATA_FLUSH);
524 break;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800525 case Opt_reserve_root:
526 if (args->from && match_int(args, &arg))
527 return -EINVAL;
528 if (test_opt(sbi, RESERVE_ROOT)) {
529 f2fs_msg(sb, KERN_INFO,
530 "Preserve previous reserve_root=%u",
531 sbi->root_reserved_blocks);
532 } else {
533 sbi->root_reserved_blocks = arg;
534 set_opt(sbi, RESERVE_ROOT);
535 }
536 break;
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800537 case Opt_resuid:
538 if (args->from && match_int(args, &arg))
539 return -EINVAL;
540 uid = make_kuid(current_user_ns(), arg);
541 if (!uid_valid(uid)) {
542 f2fs_msg(sb, KERN_ERR,
543 "Invalid uid value %d", arg);
544 return -EINVAL;
545 }
546 sbi->s_resuid = uid;
547 break;
548 case Opt_resgid:
549 if (args->from && match_int(args, &arg))
550 return -EINVAL;
551 gid = make_kgid(current_user_ns(), arg);
552 if (!gid_valid(gid)) {
553 f2fs_msg(sb, KERN_ERR,
554 "Invalid gid value %d", arg);
555 return -EINVAL;
556 }
557 sbi->s_resgid = gid;
558 break;
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700559 case Opt_mode:
560 name = match_strdup(&args[0]);
561
562 if (!name)
563 return -ENOMEM;
564 if (strlen(name) == 8 &&
565 !strncmp(name, "adaptive", 8)) {
Sheng Yongccd31cb2018-02-06 12:31:17 +0800566 if (f2fs_sb_has_blkzoned(sb)) {
Damien Le Moal3adc57e2016-10-28 17:45:04 +0900567 f2fs_msg(sb, KERN_WARNING,
568 "adaptive mode is not allowed with "
569 "zoned block device feature");
570 kfree(name);
571 return -EINVAL;
572 }
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700573 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700574 } else if (strlen(name) == 3 &&
575 !strncmp(name, "lfs", 3)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700576 set_opt_mode(sbi, F2FS_MOUNT_LFS);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700577 } else {
578 kfree(name);
579 return -EINVAL;
580 }
581 kfree(name);
582 break;
Jaegeuk Kimec915382016-12-21 17:09:19 -0800583 case Opt_io_size_bits:
584 if (args->from && match_int(args, &arg))
585 return -EINVAL;
586 if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
587 f2fs_msg(sb, KERN_WARNING,
588 "Not support %d, larger than %d",
589 1 << arg, BIO_MAX_PAGES);
590 return -EINVAL;
591 }
592 sbi->write_io_size_bits = arg;
593 break;
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700594 case Opt_fault_injection:
595 if (args->from && match_int(args, &arg))
596 return -EINVAL;
597#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu1ecc0c52016-09-23 21:30:09 +0800598 f2fs_build_fault_attr(sbi, arg);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800599 set_opt(sbi, FAULT_INJECTION);
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700600#else
601 f2fs_msg(sb, KERN_INFO,
602 "FAULT_INJECTION was not selected");
603#endif
604 break;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700605 case Opt_lazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800606 sb->s_flags |= SB_LAZYTIME;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700607 break;
608 case Opt_nolazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800609 sb->s_flags &= ~SB_LAZYTIME;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700610 break;
Chao Yu0abd6752017-07-09 00:13:07 +0800611#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +0800612 case Opt_quota:
Chao Yu0abd6752017-07-09 00:13:07 +0800613 case Opt_usrquota:
614 set_opt(sbi, USRQUOTA);
615 break;
616 case Opt_grpquota:
617 set_opt(sbi, GRPQUOTA);
618 break;
Chao Yu5c571322017-07-26 00:01:41 +0800619 case Opt_prjquota:
620 set_opt(sbi, PRJQUOTA);
621 break;
Chao Yu4b2414d2017-08-08 10:54:31 +0800622 case Opt_usrjquota:
623 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
624 if (ret)
625 return ret;
626 break;
627 case Opt_grpjquota:
628 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
629 if (ret)
630 return ret;
631 break;
632 case Opt_prjjquota:
633 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
634 if (ret)
635 return ret;
636 break;
637 case Opt_offusrjquota:
638 ret = f2fs_clear_qf_name(sb, USRQUOTA);
639 if (ret)
640 return ret;
641 break;
642 case Opt_offgrpjquota:
643 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
644 if (ret)
645 return ret;
646 break;
647 case Opt_offprjjquota:
648 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
649 if (ret)
650 return ret;
651 break;
652 case Opt_jqfmt_vfsold:
653 sbi->s_jquota_fmt = QFMT_VFS_OLD;
654 break;
655 case Opt_jqfmt_vfsv0:
656 sbi->s_jquota_fmt = QFMT_VFS_V0;
657 break;
658 case Opt_jqfmt_vfsv1:
659 sbi->s_jquota_fmt = QFMT_VFS_V1;
660 break;
661 case Opt_noquota:
662 clear_opt(sbi, QUOTA);
663 clear_opt(sbi, USRQUOTA);
664 clear_opt(sbi, GRPQUOTA);
665 clear_opt(sbi, PRJQUOTA);
666 break;
Chao Yu0abd6752017-07-09 00:13:07 +0800667#else
Chao Yu4b2414d2017-08-08 10:54:31 +0800668 case Opt_quota:
Chao Yu0abd6752017-07-09 00:13:07 +0800669 case Opt_usrquota:
670 case Opt_grpquota:
Chao Yu5c571322017-07-26 00:01:41 +0800671 case Opt_prjquota:
Chao Yu4b2414d2017-08-08 10:54:31 +0800672 case Opt_usrjquota:
673 case Opt_grpjquota:
674 case Opt_prjjquota:
675 case Opt_offusrjquota:
676 case Opt_offgrpjquota:
677 case Opt_offprjjquota:
678 case Opt_jqfmt_vfsold:
679 case Opt_jqfmt_vfsv0:
680 case Opt_jqfmt_vfsv1:
681 case Opt_noquota:
Chao Yu0abd6752017-07-09 00:13:07 +0800682 f2fs_msg(sb, KERN_INFO,
683 "quota operations not supported");
684 break;
685#endif
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900686 case Opt_whint:
687 name = match_strdup(&args[0]);
688 if (!name)
689 return -ENOMEM;
690 if (strlen(name) == 10 &&
691 !strncmp(name, "user-based", 10)) {
692 sbi->whint_mode = WHINT_MODE_USER;
693 } else if (strlen(name) == 3 &&
694 !strncmp(name, "off", 3)) {
695 sbi->whint_mode = WHINT_MODE_OFF;
Hyunchul Leef2e703f2018-01-31 11:36:58 +0900696 } else if (strlen(name) == 8 &&
697 !strncmp(name, "fs-based", 8)) {
698 sbi->whint_mode = WHINT_MODE_FS;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900699 } else {
700 kfree(name);
701 return -EINVAL;
702 }
703 kfree(name);
704 break;
Jaegeuk Kim07939622018-02-18 08:50:49 -0800705 case Opt_alloc:
706 name = match_strdup(&args[0]);
707 if (!name)
708 return -ENOMEM;
709
710 if (strlen(name) == 7 &&
711 !strncmp(name, "default", 7)) {
712 sbi->alloc_mode = ALLOC_MODE_DEFAULT;
713 } else if (strlen(name) == 5 &&
714 !strncmp(name, "reuse", 5)) {
715 sbi->alloc_mode = ALLOC_MODE_REUSE;
716 } else {
717 kfree(name);
718 return -EINVAL;
719 }
720 kfree(name);
721 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900722 default:
723 f2fs_msg(sb, KERN_ERR,
724 "Unrecognized mount option \"%s\" or missing value",
725 p);
726 return -EINVAL;
727 }
728 }
Chao Yu4b2414d2017-08-08 10:54:31 +0800729#ifdef CONFIG_QUOTA
730 if (f2fs_check_quota_options(sbi))
731 return -EINVAL;
732#endif
Jaegeuk Kimec915382016-12-21 17:09:19 -0800733
734 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
735 f2fs_msg(sb, KERN_ERR,
736 "Should set mode=lfs with %uKB-sized IO",
737 F2FS_IO_SIZE_KB(sbi));
738 return -EINVAL;
739 }
Chao Yu6afc6622017-09-06 21:59:50 +0800740
741 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
Chao Yu4d817ae2018-01-27 17:29:48 +0800742 if (!f2fs_sb_has_extra_attr(sb) ||
743 !f2fs_sb_has_flexible_inline_xattr(sb)) {
744 f2fs_msg(sb, KERN_ERR,
745 "extra_attr or flexible_inline_xattr "
746 "feature is off");
747 return -EINVAL;
748 }
Chao Yu6afc6622017-09-06 21:59:50 +0800749 if (!test_opt(sbi, INLINE_XATTR)) {
750 f2fs_msg(sb, KERN_ERR,
751 "inline_xattr_size option should be "
752 "set with inline_xattr option");
753 return -EINVAL;
754 }
755 if (!sbi->inline_xattr_size ||
756 sbi->inline_xattr_size >= DEF_ADDRS_PER_INODE -
757 F2FS_TOTAL_EXTRA_ATTR_SIZE -
758 DEF_INLINE_RESERVED_SIZE -
759 DEF_MIN_INLINE_SIZE) {
760 f2fs_msg(sb, KERN_ERR,
761 "inline xattr size is out of range");
762 return -EINVAL;
763 }
764 }
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900765
766 /* Not pass down write hints if the number of active logs is lesser
767 * than NR_CURSEG_TYPE.
768 */
769 if (sbi->active_logs != NR_CURSEG_TYPE)
770 sbi->whint_mode = WHINT_MODE_OFF;
Namjae Jeon696c0182013-06-16 09:48:48 +0900771 return 0;
772}
773
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900774static struct inode *f2fs_alloc_inode(struct super_block *sb)
775{
776 struct f2fs_inode_info *fi;
777
Chao Yua0acdfe2013-12-05 09:54:00 +0800778 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900779 if (!fi)
780 return NULL;
781
782 init_once((void *) fi);
783
Masanari Iida434720f2013-03-19 08:03:35 +0900784 /* Initialize f2fs-specific inode info */
Jaegeuk Kim204706c2016-12-02 15:11:32 -0800785 atomic_set(&fi->dirty_pages, 0);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900786 fi->i_current_depth = 1;
787 fi->i_advise = 0;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900788 init_rwsem(&fi->i_sem);
Chao Yu2710fd72015-12-15 13:30:45 +0800789 INIT_LIST_HEAD(&fi->dirty_list);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700790 INIT_LIST_HEAD(&fi->gdirty_list);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700791 INIT_LIST_HEAD(&fi->inmem_ilist);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700792 INIT_LIST_HEAD(&fi->inmem_pages);
793 mutex_init(&fi->inmem_lock);
Chao Yu82e0a5a2016-07-13 09:18:29 +0800794 init_rwsem(&fi->dio_rwsem[READ]);
795 init_rwsem(&fi->dio_rwsem[WRITE]);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800796 init_rwsem(&fi->i_mmap_sem);
Yunlei He27161f12017-09-07 10:40:54 +0800797 init_rwsem(&fi->i_xattr_sem);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900798
Chao Yu0abd6752017-07-09 00:13:07 +0800799#ifdef CONFIG_QUOTA
800 memset(&fi->i_dquot, 0, sizeof(fi->i_dquot));
801 fi->i_reserved_quota = 0;
802#endif
Jaegeuk Kimab9fa662014-02-27 20:09:05 +0900803 /* Will be used by directory only */
804 fi->i_dir_level = F2FS_SB(sb)->dir_level;
Chao Yuf2470372017-07-19 00:19:05 +0800805
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900806 return &fi->vfs_inode;
807}
808
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900809static int f2fs_drop_inode(struct inode *inode)
810{
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000811 int ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900812 /*
813 * This is to avoid a deadlock condition like below.
814 * writeback_single_inode(inode)
815 * - f2fs_write_data_page
816 * - f2fs_gc -> iput -> evict
817 * - inode_wait_for_writeback(inode)
818 */
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700819 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700820 if (!inode->i_nlink && !is_bad_inode(inode)) {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700821 /* to avoid evict_inode call simultaneously */
822 atomic_inc(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700823 spin_unlock(&inode->i_lock);
824
825 /* some remained atomic pages should discarded */
826 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +0800827 drop_inmem_pages(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700828
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700829 /* should remain fi->extent_tree for writepage */
830 f2fs_destroy_extent_node(inode);
831
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700832 sb_start_intwrite(inode->i_sb);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700833 f2fs_i_size_write(inode, 0);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700834
835 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700836 f2fs_truncate(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700837
838 sb_end_intwrite(inode->i_sb);
839
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700840 spin_lock(&inode->i_lock);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700841 atomic_dec(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700842 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000843 trace_f2fs_drop_inode(inode, 0);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900844 return 0;
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700845 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000846 ret = generic_drop_inode(inode);
847 trace_f2fs_drop_inode(inode, ret);
848 return ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900849}
850
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700851int f2fs_inode_dirtied(struct inode *inode, bool sync)
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700852{
853 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700854 int ret = 0;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700855
856 spin_lock(&sbi->inode_lock[DIRTY_META]);
857 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700858 ret = 1;
859 } else {
860 set_inode_flag(inode, FI_DIRTY_INODE);
861 stat_inc_dirty_inode(sbi, DIRTY_META);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700862 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700863 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
864 list_add_tail(&F2FS_I(inode)->gdirty_list,
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700865 &sbi->inode_list[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700866 inc_page_count(sbi, F2FS_DIRTY_IMETA);
867 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700868 spin_unlock(&sbi->inode_lock[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700869 return ret;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700870}
871
872void f2fs_inode_synced(struct inode *inode)
873{
874 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
875
876 spin_lock(&sbi->inode_lock[DIRTY_META]);
877 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
878 spin_unlock(&sbi->inode_lock[DIRTY_META]);
879 return;
880 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700881 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
882 list_del_init(&F2FS_I(inode)->gdirty_list);
883 dec_page_count(sbi, F2FS_DIRTY_IMETA);
884 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700885 clear_inode_flag(inode, FI_DIRTY_INODE);
886 clear_inode_flag(inode, FI_AUTO_RECOVER);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700887 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
888 spin_unlock(&sbi->inode_lock[DIRTY_META]);
889}
890
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900891/*
892 * f2fs_dirty_inode() is called from __mark_inode_dirty()
893 *
894 * We should call set_dirty_inode to write the dirty inode through write_inode.
895 */
896static void f2fs_dirty_inode(struct inode *inode, int flags)
897{
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700898 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
899
900 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
901 inode->i_ino == F2FS_META_INO(sbi))
902 return;
903
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700904 if (flags == I_DIRTY_TIME)
905 return;
906
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700907 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
908 clear_inode_flag(inode, FI_AUTO_RECOVER);
909
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700910 f2fs_inode_dirtied(inode, false);
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900911}
912
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900913static void f2fs_i_callback(struct rcu_head *head)
914{
915 struct inode *inode = container_of(head, struct inode, i_rcu);
916 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
917}
918
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900919static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900920{
921 call_rcu(&inode->i_rcu, f2fs_i_callback);
922}
923
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700924static void destroy_percpu_info(struct f2fs_sb_info *sbi)
925{
Jaegeuk Kim41382ec2016-05-16 11:06:50 -0700926 percpu_counter_destroy(&sbi->alloc_valid_block_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -0700927 percpu_counter_destroy(&sbi->total_valid_inode_count);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700928}
929
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700930static void destroy_device_list(struct f2fs_sb_info *sbi)
931{
932 int i;
933
934 for (i = 0; i < sbi->s_ndevs; i++) {
935 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
936#ifdef CONFIG_BLK_DEV_ZONED
937 kfree(FDEV(i).blkz_type);
938#endif
939 }
940 kfree(sbi->devs);
941}
942
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900943static void f2fs_put_super(struct super_block *sb)
944{
945 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yua3981012017-06-14 17:39:46 +0800946 int i;
Chao Yucf5c7592017-10-04 09:08:37 +0800947 bool dropped;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900948
Chao Yu0abd6752017-07-09 00:13:07 +0800949 f2fs_quota_off_umount(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900950
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700951 /* prevent remaining shrinker jobs */
952 mutex_lock(&sbi->umount_mutex);
953
Jaegeuk Kim85dc2f22015-01-14 17:41:41 -0800954 /*
955 * We don't need to do checkpoint when superblock is clean.
956 * But, the previous checkpoint was not done by umount, it needs to do
957 * clean checkpoint again.
958 */
Chao Yucaf00472015-01-28 17:48:42 +0800959 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
Chao Yuaaec2b12016-09-20 11:04:18 +0800960 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700961 struct cp_control cpc = {
962 .reason = CP_UMOUNT,
963 };
964 write_checkpoint(sbi, &cpc);
965 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900966
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -0800967 /* be sure to wait for any on-going discard commands */
Chao Yucf5c7592017-10-04 09:08:37 +0800968 dropped = f2fs_wait_discard_bios(sbi);
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -0800969
Chao Yucf5c7592017-10-04 09:08:37 +0800970 if (f2fs_discard_en(sbi) && !sbi->discard_blks && !dropped) {
Chao Yu1f43e2a2017-04-28 13:56:08 +0800971 struct cp_control cpc = {
972 .reason = CP_UMOUNT | CP_TRIMMED,
973 };
974 write_checkpoint(sbi, &cpc);
975 }
976
Jaegeuk Kimeca616f2015-06-15 14:52:29 -0700977 /* write_checkpoint can update stat informaion */
978 f2fs_destroy_stats(sbi);
979
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700980 /*
981 * normally superblock is clean, so we need to release this.
982 * In addition, EIO will skip do checkpoint, we need this as well.
983 */
Jaegeuk Kim74ef9242016-05-02 22:09:56 -0700984 release_ino_entry(sbi, true);
Jaegeuk Kim6f12ac22014-08-19 09:48:22 -0700985
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700986 f2fs_leave_shrinker(sbi);
987 mutex_unlock(&sbi->umount_mutex);
988
Jaegeuk Kim17c19122016-01-29 08:57:59 -0800989 /* our cp_error case, we can wait for any writeback page */
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700990 f2fs_flush_merged_writes(sbi);
Jaegeuk Kim17c19122016-01-29 08:57:59 -0800991
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900992 iput(sbi->node_inode);
993 iput(sbi->meta_inode);
994
995 /* destroy f2fs internal modules */
996 destroy_node_manager(sbi);
997 destroy_segment_manager(sbi);
998
999 kfree(sbi->ckpt);
Chao Yua3981012017-06-14 17:39:46 +08001000
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07001001 f2fs_unregister_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001002
1003 sb->s_fs_info = NULL;
Keith Mok43b65732016-03-02 12:04:24 -08001004 if (sbi->s_chksum_driver)
1005 crypto_free_shash(sbi->s_chksum_driver);
Yunlei Heb39f0de2015-12-15 17:17:20 +08001006 kfree(sbi->raw_super);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001007
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001008 destroy_device_list(sbi);
Chao Yub6895e82017-02-27 18:43:12 +08001009 mempool_destroy(sbi->write_io_dummy);
Chao Yu4b2414d2017-08-08 10:54:31 +08001010#ifdef CONFIG_QUOTA
1011 for (i = 0; i < MAXQUOTAS; i++)
1012 kfree(sbi->s_qf_names[i]);
1013#endif
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001014 destroy_percpu_info(sbi);
Jaegeuk Kima912b542017-05-10 11:18:25 -07001015 for (i = 0; i < NR_PAGE_TYPE; i++)
1016 kfree(sbi->write_io[i]);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001017 kfree(sbi);
1018}
1019
1020int f2fs_sync_fs(struct super_block *sb, int sync)
1021{
1022 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yuc34f42e2015-12-23 17:50:30 +08001023 int err = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001024
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001025 if (unlikely(f2fs_cp_error(sbi)))
1026 return 0;
1027
Namjae Jeona2a4a7e2013-04-20 01:28:40 +09001028 trace_f2fs_sync_fs(sb, sync);
1029
Chao Yu4b2414d2017-08-08 10:54:31 +08001030 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1031 return -EAGAIN;
1032
Jaegeuk Kimb7473752013-04-01 08:32:21 +09001033 if (sync) {
Jaegeuk Kimd5053a342014-10-30 22:47:03 -07001034 struct cp_control cpc;
1035
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001036 cpc.reason = __get_cp_reason(sbi);
1037
Jaegeuk Kimb7473752013-04-01 08:32:21 +09001038 mutex_lock(&sbi->gc_mutex);
Chao Yuc34f42e2015-12-23 17:50:30 +08001039 err = write_checkpoint(sbi, &cpc);
Jaegeuk Kimb7473752013-04-01 08:32:21 +09001040 mutex_unlock(&sbi->gc_mutex);
Jaegeuk Kimb7473752013-04-01 08:32:21 +09001041 }
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001042 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001043
Chao Yuc34f42e2015-12-23 17:50:30 +08001044 return err;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001045}
1046
Changman Leed6212a52013-01-29 18:30:07 +09001047static int f2fs_freeze(struct super_block *sb)
1048{
Jaegeuk Kim77888c12013-05-20 20:28:47 +09001049 if (f2fs_readonly(sb))
Changman Leed6212a52013-01-29 18:30:07 +09001050 return 0;
1051
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -07001052 /* IO error happened before */
1053 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1054 return -EIO;
1055
1056 /* must be clean, since sync_filesystem() was already called */
1057 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1058 return -EINVAL;
1059 return 0;
Changman Leed6212a52013-01-29 18:30:07 +09001060}
1061
1062static int f2fs_unfreeze(struct super_block *sb)
1063{
1064 return 0;
1065}
1066
Chao Yuddc34e32017-07-29 00:32:53 +08001067#ifdef CONFIG_QUOTA
1068static int f2fs_statfs_project(struct super_block *sb,
1069 kprojid_t projid, struct kstatfs *buf)
1070{
1071 struct kqid qid;
1072 struct dquot *dquot;
1073 u64 limit;
1074 u64 curblock;
1075
1076 qid = make_kqid_projid(projid);
1077 dquot = dqget(sb, qid);
1078 if (IS_ERR(dquot))
1079 return PTR_ERR(dquot);
1080 spin_lock(&dq_data_lock);
1081
1082 limit = (dquot->dq_dqb.dqb_bsoftlimit ?
1083 dquot->dq_dqb.dqb_bsoftlimit :
1084 dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
1085 if (limit && buf->f_blocks > limit) {
1086 curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits;
1087 buf->f_blocks = limit;
1088 buf->f_bfree = buf->f_bavail =
1089 (buf->f_blocks > curblock) ?
1090 (buf->f_blocks - curblock) : 0;
1091 }
1092
1093 limit = dquot->dq_dqb.dqb_isoftlimit ?
1094 dquot->dq_dqb.dqb_isoftlimit :
1095 dquot->dq_dqb.dqb_ihardlimit;
1096 if (limit && buf->f_files > limit) {
1097 buf->f_files = limit;
1098 buf->f_ffree =
1099 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1100 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1101 }
1102
1103 spin_unlock(&dq_data_lock);
1104 dqput(dquot);
1105 return 0;
1106}
1107#endif
1108
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001109static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1110{
1111 struct super_block *sb = dentry->d_sb;
1112 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1113 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Jaegeuk Kimf66c0272018-01-03 10:55:07 -08001114 block_t total_count, user_block_count, start_count;
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -07001115 u64 avail_node_count;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001116
1117 total_count = le64_to_cpu(sbi->raw_super->block_count);
1118 user_block_count = sbi->user_block_count;
1119 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001120 buf->f_type = F2FS_SUPER_MAGIC;
1121 buf->f_bsize = sbi->blocksize;
1122
1123 buf->f_blocks = total_count - start_count;
Jaegeuk Kimf66c0272018-01-03 10:55:07 -08001124 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
Yunlong Song80d42142017-10-27 20:45:05 +08001125 sbi->current_reserved_blocks;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08001126 if (buf->f_bfree > sbi->root_reserved_blocks)
1127 buf->f_bavail = buf->f_bfree - sbi->root_reserved_blocks;
1128 else
1129 buf->f_bavail = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001130
Chao Yu292c196a2017-11-16 16:59:14 +08001131 avail_node_count = sbi->total_node_count - sbi->nquota_files -
1132 F2FS_RESERVED_NODE_NUM;
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -07001133
1134 if (avail_node_count > user_block_count) {
1135 buf->f_files = user_block_count;
1136 buf->f_ffree = buf->f_bavail;
1137 } else {
1138 buf->f_files = avail_node_count;
1139 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
1140 buf->f_bavail);
1141 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001142
Jaegeuk Kim5a20d332013-03-03 13:58:05 +09001143 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001144 buf->f_fsid.val[0] = (u32)id;
1145 buf->f_fsid.val[1] = (u32)(id >> 32);
1146
Chao Yuddc34e32017-07-29 00:32:53 +08001147#ifdef CONFIG_QUOTA
1148 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1149 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1150 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1151 }
1152#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001153 return 0;
1154}
1155
Chao Yu4b2414d2017-08-08 10:54:31 +08001156static inline void f2fs_show_quota_options(struct seq_file *seq,
1157 struct super_block *sb)
1158{
1159#ifdef CONFIG_QUOTA
1160 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1161
1162 if (sbi->s_jquota_fmt) {
1163 char *fmtname = "";
1164
1165 switch (sbi->s_jquota_fmt) {
1166 case QFMT_VFS_OLD:
1167 fmtname = "vfsold";
1168 break;
1169 case QFMT_VFS_V0:
1170 fmtname = "vfsv0";
1171 break;
1172 case QFMT_VFS_V1:
1173 fmtname = "vfsv1";
1174 break;
1175 }
1176 seq_printf(seq, ",jqfmt=%s", fmtname);
1177 }
1178
1179 if (sbi->s_qf_names[USRQUOTA])
1180 seq_show_option(seq, "usrjquota", sbi->s_qf_names[USRQUOTA]);
1181
1182 if (sbi->s_qf_names[GRPQUOTA])
1183 seq_show_option(seq, "grpjquota", sbi->s_qf_names[GRPQUOTA]);
1184
1185 if (sbi->s_qf_names[PRJQUOTA])
1186 seq_show_option(seq, "prjjquota", sbi->s_qf_names[PRJQUOTA]);
1187#endif
1188}
1189
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001190static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1191{
1192 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1193
Jaegeuk Kim6aefd932015-10-05 11:02:54 -07001194 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
1195 if (test_opt(sbi, FORCE_FG_GC))
1196 seq_printf(seq, ",background_gc=%s", "sync");
1197 else
1198 seq_printf(seq, ",background_gc=%s", "on");
1199 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +09001200 seq_printf(seq, ",background_gc=%s", "off");
Jaegeuk Kim6aefd932015-10-05 11:02:54 -07001201 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001202 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1203 seq_puts(seq, ",disable_roll_forward");
1204 if (test_opt(sbi, DISCARD))
1205 seq_puts(seq, ",discard");
1206 if (test_opt(sbi, NOHEAP))
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001207 seq_puts(seq, ",no_heap");
1208 else
1209 seq_puts(seq, ",heap");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001210#ifdef CONFIG_F2FS_FS_XATTR
1211 if (test_opt(sbi, XATTR_USER))
1212 seq_puts(seq, ",user_xattr");
1213 else
1214 seq_puts(seq, ",nouser_xattr");
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001215 if (test_opt(sbi, INLINE_XATTR))
1216 seq_puts(seq, ",inline_xattr");
Chao Yu23cf7212017-02-15 10:34:45 +08001217 else
1218 seq_puts(seq, ",noinline_xattr");
Chao Yu6afc6622017-09-06 21:59:50 +08001219 if (test_opt(sbi, INLINE_XATTR_SIZE))
1220 seq_printf(seq, ",inline_xattr_size=%u",
1221 sbi->inline_xattr_size);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001222#endif
1223#ifdef CONFIG_F2FS_FS_POSIX_ACL
1224 if (test_opt(sbi, POSIX_ACL))
1225 seq_puts(seq, ",acl");
1226 else
1227 seq_puts(seq, ",noacl");
1228#endif
1229 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +01001230 seq_puts(seq, ",disable_ext_identify");
Huajun Li8274de72013-11-10 23:13:17 +08001231 if (test_opt(sbi, INLINE_DATA))
1232 seq_puts(seq, ",inline_data");
Wanpeng Li75342792015-03-24 10:20:27 +08001233 else
1234 seq_puts(seq, ",noinline_data");
Chao Yu5efd3c62014-09-24 18:16:13 +08001235 if (test_opt(sbi, INLINE_DENTRY))
1236 seq_puts(seq, ",inline_dentry");
Chao Yu97c17942016-05-09 19:56:34 +08001237 else
1238 seq_puts(seq, ",noinline_dentry");
Gu Zhengb270ad62014-04-11 17:49:55 +08001239 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09001240 seq_puts(seq, ",flush_merge");
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -07001241 if (test_opt(sbi, NOBARRIER))
1242 seq_puts(seq, ",nobarrier");
Jaegeuk Kimd5053a342014-10-30 22:47:03 -07001243 if (test_opt(sbi, FASTBOOT))
1244 seq_puts(seq, ",fastboot");
Chao Yu89672152015-02-05 17:55:51 +08001245 if (test_opt(sbi, EXTENT_CACHE))
1246 seq_puts(seq, ",extent_cache");
Jaegeuk Kim7daaea22015-06-25 17:43:04 -07001247 else
1248 seq_puts(seq, ",noextent_cache");
Chao Yu343f40f2015-12-16 13:12:16 +08001249 if (test_opt(sbi, DATA_FLUSH))
1250 seq_puts(seq, ",data_flush");
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001251
1252 seq_puts(seq, ",mode=");
1253 if (test_opt(sbi, ADAPTIVE))
1254 seq_puts(seq, "adaptive");
1255 else if (test_opt(sbi, LFS))
1256 seq_puts(seq, "lfs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001257 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08001258 if (test_opt(sbi, RESERVE_ROOT))
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -08001259 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
1260 sbi->root_reserved_blocks,
1261 from_kuid_munged(&init_user_ns, sbi->s_resuid),
1262 from_kgid_munged(&init_user_ns, sbi->s_resgid));
Jaegeuk Kimec915382016-12-21 17:09:19 -08001263 if (F2FS_IO_SIZE_BITS(sbi))
1264 seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
Kaixu Xia0cc0dec2017-01-27 09:35:37 +08001265#ifdef CONFIG_F2FS_FAULT_INJECTION
1266 if (test_opt(sbi, FAULT_INJECTION))
Chao Yu44529f82017-06-12 09:44:24 +08001267 seq_printf(seq, ",fault_injection=%u",
1268 sbi->fault_info.inject_rate);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +08001269#endif
Chao Yu0abd6752017-07-09 00:13:07 +08001270#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +08001271 if (test_opt(sbi, QUOTA))
1272 seq_puts(seq, ",quota");
Chao Yu0abd6752017-07-09 00:13:07 +08001273 if (test_opt(sbi, USRQUOTA))
1274 seq_puts(seq, ",usrquota");
1275 if (test_opt(sbi, GRPQUOTA))
1276 seq_puts(seq, ",grpquota");
Chao Yu5c571322017-07-26 00:01:41 +08001277 if (test_opt(sbi, PRJQUOTA))
1278 seq_puts(seq, ",prjquota");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001279#endif
Chao Yu4b2414d2017-08-08 10:54:31 +08001280 f2fs_show_quota_options(seq, sbi->sb);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001281 if (sbi->whint_mode == WHINT_MODE_USER)
1282 seq_printf(seq, ",whint_mode=%s", "user-based");
Hyunchul Leef2e703f2018-01-31 11:36:58 +09001283 else if (sbi->whint_mode == WHINT_MODE_FS)
1284 seq_printf(seq, ",whint_mode=%s", "fs-based");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001285
Jaegeuk Kim07939622018-02-18 08:50:49 -08001286 if (sbi->alloc_mode == ALLOC_MODE_DEFAULT)
1287 seq_printf(seq, ",alloc_mode=%s", "default");
1288 else if (sbi->alloc_mode == ALLOC_MODE_REUSE)
1289 seq_printf(seq, ",alloc_mode=%s", "reuse");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001290 return 0;
1291}
1292
Yunlei He498c5e92015-05-07 18:11:37 +08001293static void default_options(struct f2fs_sb_info *sbi)
1294{
1295 /* init some FS parameters */
1296 sbi->active_logs = NR_CURSEG_TYPE;
Chao Yu6afc6622017-09-06 21:59:50 +08001297 sbi->inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001298 sbi->whint_mode = WHINT_MODE_OFF;
Jaegeuk Kim07939622018-02-18 08:50:49 -08001299 sbi->alloc_mode = ALLOC_MODE_DEFAULT;
Yunlei He498c5e92015-05-07 18:11:37 +08001300
1301 set_opt(sbi, BG_GC);
Chao Yu39133a52017-02-08 17:39:44 +08001302 set_opt(sbi, INLINE_XATTR);
Yunlei He498c5e92015-05-07 18:11:37 +08001303 set_opt(sbi, INLINE_DATA);
Chao Yu97c17942016-05-09 19:56:34 +08001304 set_opt(sbi, INLINE_DENTRY);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07001305 set_opt(sbi, EXTENT_CACHE);
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001306 set_opt(sbi, NOHEAP);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001307 sbi->sb->s_flags |= SB_LAZYTIME;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -07001308 set_opt(sbi, FLUSH_MERGE);
Sheng Yongccd31cb2018-02-06 12:31:17 +08001309 if (f2fs_sb_has_blkzoned(sbi->sb)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -07001310 set_opt_mode(sbi, F2FS_MOUNT_LFS);
1311 set_opt(sbi, DISCARD);
1312 } else {
1313 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
1314 }
Yunlei He498c5e92015-05-07 18:11:37 +08001315
1316#ifdef CONFIG_F2FS_FS_XATTR
1317 set_opt(sbi, XATTR_USER);
1318#endif
1319#ifdef CONFIG_F2FS_FS_POSIX_ACL
1320 set_opt(sbi, POSIX_ACL);
1321#endif
Chao Yu36dbd322016-09-26 19:45:05 +08001322
1323#ifdef CONFIG_F2FS_FAULT_INJECTION
1324 f2fs_build_fault_attr(sbi, 0);
1325#endif
Yunlei He498c5e92015-05-07 18:11:37 +08001326}
1327
Jaegeuk Kimea676732017-10-06 09:14:28 -07001328#ifdef CONFIG_QUOTA
1329static int f2fs_enable_quotas(struct super_block *sb);
1330#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001331static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1332{
1333 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1334 struct f2fs_mount_info org_mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +08001335 unsigned long old_sb_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +09001336 int err, active_logs;
Gu Zheng876dc592014-04-11 17:50:00 +08001337 bool need_restart_gc = false;
1338 bool need_stop_gc = false;
Chao Yu9cd81ce2015-09-18 16:55:26 +08001339 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001340 int old_whint_mode = sbi->whint_mode;
Jaegeuk Kim07939622018-02-18 08:50:49 -08001341 int old_alloc_mode = sbi->alloc_mode;
Chao Yu2443b8b2016-09-26 19:45:06 +08001342#ifdef CONFIG_F2FS_FAULT_INJECTION
1343 struct f2fs_fault_info ffi = sbi->fault_info;
1344#endif
Chao Yu4b2414d2017-08-08 10:54:31 +08001345#ifdef CONFIG_QUOTA
1346 int s_jquota_fmt;
1347 char *s_qf_names[MAXQUOTAS];
1348 int i, j;
1349#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001350
1351 /*
1352 * Save the old mount options in case we
1353 * need to restore them.
1354 */
1355 org_mount_opt = sbi->mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +08001356 old_sb_flags = sb->s_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +09001357 active_logs = sbi->active_logs;
1358
Chao Yu4b2414d2017-08-08 10:54:31 +08001359#ifdef CONFIG_QUOTA
1360 s_jquota_fmt = sbi->s_jquota_fmt;
1361 for (i = 0; i < MAXQUOTAS; i++) {
1362 if (sbi->s_qf_names[i]) {
1363 s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
1364 GFP_KERNEL);
1365 if (!s_qf_names[i]) {
1366 for (j = 0; j < i; j++)
1367 kfree(s_qf_names[j]);
1368 return -ENOMEM;
1369 }
1370 } else {
1371 s_qf_names[i] = NULL;
1372 }
1373 }
1374#endif
1375
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001376 /* recover superblocks we couldn't write due to previous RO mount */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001377 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001378 err = f2fs_commit_super(sbi, false);
1379 f2fs_msg(sb, KERN_INFO,
1380 "Try to recover all the superblocks, ret: %d", err);
1381 if (!err)
1382 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1383 }
1384
Yunlei He498c5e92015-05-07 18:11:37 +08001385 default_options(sbi);
Chao Yu26666c82014-09-15 18:04:44 +08001386
Namjae Jeon696c0182013-06-16 09:48:48 +09001387 /* parse mount options */
1388 err = parse_options(sb, data);
1389 if (err)
1390 goto restore_opts;
1391
1392 /*
1393 * Previous and new state of filesystem is RO,
Gu Zheng876dc592014-04-11 17:50:00 +08001394 * so skip checking GC and FLUSH_MERGE conditions.
Namjae Jeon696c0182013-06-16 09:48:48 +09001395 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001396 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
Namjae Jeon696c0182013-06-16 09:48:48 +09001397 goto skip;
1398
Jaegeuk Kimea676732017-10-06 09:14:28 -07001399#ifdef CONFIG_QUOTA
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001400 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
Chao Yu0abd6752017-07-09 00:13:07 +08001401 err = dquot_suspend(sb, -1);
1402 if (err < 0)
1403 goto restore_opts;
Jaegeuk Kim62793982018-01-02 11:03:19 -08001404 } else if (f2fs_readonly(sb) && !(*flags & MS_RDONLY)) {
Chao Yu0abd6752017-07-09 00:13:07 +08001405 /* dquot_resume needs RW */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001406 sb->s_flags &= ~SB_RDONLY;
Jaegeuk Kimea676732017-10-06 09:14:28 -07001407 if (sb_any_quota_suspended(sb)) {
1408 dquot_resume(sb, -1);
1409 } else if (f2fs_sb_has_quota_ino(sb)) {
1410 err = f2fs_enable_quotas(sb);
1411 if (err)
1412 goto restore_opts;
1413 }
Chao Yu0abd6752017-07-09 00:13:07 +08001414 }
Jaegeuk Kimea676732017-10-06 09:14:28 -07001415#endif
Chao Yu9cd81ce2015-09-18 16:55:26 +08001416 /* disallow enable/disable extent_cache dynamically */
1417 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
1418 err = -EINVAL;
1419 f2fs_msg(sbi->sb, KERN_WARNING,
1420 "switch extent_cache option is not allowed");
1421 goto restore_opts;
1422 }
1423
Namjae Jeon696c0182013-06-16 09:48:48 +09001424 /*
1425 * We stop the GC thread if FS is mounted as RO
1426 * or if background_gc = off is passed in mount
1427 * option. Also sync the filesystem.
1428 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001429 if ((*flags & SB_RDONLY) || !test_opt(sbi, BG_GC)) {
Namjae Jeon696c0182013-06-16 09:48:48 +09001430 if (sbi->gc_thread) {
1431 stop_gc_thread(sbi);
Gu Zheng876dc592014-04-11 17:50:00 +08001432 need_restart_gc = true;
Namjae Jeon696c0182013-06-16 09:48:48 +09001433 }
Chao Yuaba291b2014-11-18 11:17:20 +08001434 } else if (!sbi->gc_thread) {
Namjae Jeon696c0182013-06-16 09:48:48 +09001435 err = start_gc_thread(sbi);
1436 if (err)
1437 goto restore_opts;
Gu Zheng876dc592014-04-11 17:50:00 +08001438 need_stop_gc = true;
1439 }
1440
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001441 if (*flags & SB_RDONLY || sbi->whint_mode != old_whint_mode) {
Jaegeuk Kimfaa0e552016-03-24 10:29:39 -07001442 writeback_inodes_sb(sb, WB_REASON_SYNC);
1443 sync_inodes_sb(sb);
1444
1445 set_sbi_flag(sbi, SBI_IS_DIRTY);
1446 set_sbi_flag(sbi, SBI_IS_CLOSE);
1447 f2fs_sync_fs(sb, 1);
1448 clear_sbi_flag(sbi, SBI_IS_CLOSE);
1449 }
1450
Gu Zheng876dc592014-04-11 17:50:00 +08001451 /*
1452 * We stop issue flush thread if FS is mounted as RO
1453 * or if flush_merge is not passed in mount option.
1454 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001455 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -08001456 clear_opt(sbi, FLUSH_MERGE);
1457 destroy_flush_cmd_control(sbi, false);
1458 } else {
Gu Zheng2163d192014-04-27 14:21:33 +08001459 err = create_flush_cmd_control(sbi);
1460 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08001461 goto restore_gc;
Namjae Jeon696c0182013-06-16 09:48:48 +09001462 }
1463skip:
Chao Yu4b2414d2017-08-08 10:54:31 +08001464#ifdef CONFIG_QUOTA
1465 /* Release old quota file names */
1466 for (i = 0; i < MAXQUOTAS; i++)
1467 kfree(s_qf_names[i]);
1468#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001469 /* Update the POSIXACL Flag */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001470 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
1471 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001472
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08001473 limit_reserve_root(sbi);
Namjae Jeon696c0182013-06-16 09:48:48 +09001474 return 0;
Gu Zheng876dc592014-04-11 17:50:00 +08001475restore_gc:
1476 if (need_restart_gc) {
1477 if (start_gc_thread(sbi))
1478 f2fs_msg(sbi->sb, KERN_WARNING,
arter97e1c42042014-08-06 23:22:50 +09001479 "background gc thread has stopped");
Gu Zheng876dc592014-04-11 17:50:00 +08001480 } else if (need_stop_gc) {
1481 stop_gc_thread(sbi);
1482 }
Namjae Jeon696c0182013-06-16 09:48:48 +09001483restore_opts:
Chao Yu4b2414d2017-08-08 10:54:31 +08001484#ifdef CONFIG_QUOTA
1485 sbi->s_jquota_fmt = s_jquota_fmt;
1486 for (i = 0; i < MAXQUOTAS; i++) {
1487 kfree(sbi->s_qf_names[i]);
1488 sbi->s_qf_names[i] = s_qf_names[i];
1489 }
1490#endif
Jaegeuk Kim07939622018-02-18 08:50:49 -08001491 sbi->alloc_mode = old_alloc_mode;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001492 sbi->whint_mode = old_whint_mode;
Namjae Jeon696c0182013-06-16 09:48:48 +09001493 sbi->mount_opt = org_mount_opt;
1494 sbi->active_logs = active_logs;
Chao Yu0abd6752017-07-09 00:13:07 +08001495 sb->s_flags = old_sb_flags;
Chao Yu2443b8b2016-09-26 19:45:06 +08001496#ifdef CONFIG_F2FS_FAULT_INJECTION
1497 sbi->fault_info = ffi;
1498#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001499 return err;
1500}
1501
Chao Yu0abd6752017-07-09 00:13:07 +08001502#ifdef CONFIG_QUOTA
1503/* Read data from quotafile */
1504static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
1505 size_t len, loff_t off)
1506{
1507 struct inode *inode = sb_dqopt(sb)->files[type];
1508 struct address_space *mapping = inode->i_mapping;
1509 block_t blkidx = F2FS_BYTES_TO_BLK(off);
1510 int offset = off & (sb->s_blocksize - 1);
1511 int tocopy;
1512 size_t toread;
1513 loff_t i_size = i_size_read(inode);
1514 struct page *page;
1515 char *kaddr;
1516
1517 if (off > i_size)
1518 return 0;
1519
1520 if (off + len > i_size)
1521 len = i_size - off;
1522 toread = len;
1523 while (toread > 0) {
1524 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
1525repeat:
1526 page = read_mapping_page(mapping, blkidx, NULL);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001527 if (IS_ERR(page)) {
1528 if (PTR_ERR(page) == -ENOMEM) {
1529 congestion_wait(BLK_RW_ASYNC, HZ/50);
1530 goto repeat;
1531 }
Chao Yu0abd6752017-07-09 00:13:07 +08001532 return PTR_ERR(page);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001533 }
Chao Yu0abd6752017-07-09 00:13:07 +08001534
1535 lock_page(page);
1536
1537 if (unlikely(page->mapping != mapping)) {
1538 f2fs_put_page(page, 1);
1539 goto repeat;
1540 }
1541 if (unlikely(!PageUptodate(page))) {
1542 f2fs_put_page(page, 1);
1543 return -EIO;
1544 }
1545
1546 kaddr = kmap_atomic(page);
1547 memcpy(data, kaddr + offset, tocopy);
1548 kunmap_atomic(kaddr);
1549 f2fs_put_page(page, 1);
1550
1551 offset = 0;
1552 toread -= tocopy;
1553 data += tocopy;
1554 blkidx++;
1555 }
1556 return len;
1557}
1558
1559/* Write to quotafile */
1560static ssize_t f2fs_quota_write(struct super_block *sb, int type,
1561 const char *data, size_t len, loff_t off)
1562{
1563 struct inode *inode = sb_dqopt(sb)->files[type];
1564 struct address_space *mapping = inode->i_mapping;
1565 const struct address_space_operations *a_ops = mapping->a_ops;
1566 int offset = off & (sb->s_blocksize - 1);
1567 size_t towrite = len;
1568 struct page *page;
1569 char *kaddr;
1570 int err = 0;
1571 int tocopy;
1572
1573 while (towrite > 0) {
1574 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
1575 towrite);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001576retry:
Chao Yu0abd6752017-07-09 00:13:07 +08001577 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
1578 &page, NULL);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001579 if (unlikely(err)) {
1580 if (err == -ENOMEM) {
1581 congestion_wait(BLK_RW_ASYNC, HZ/50);
1582 goto retry;
1583 }
Chao Yu0abd6752017-07-09 00:13:07 +08001584 break;
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001585 }
Chao Yu0abd6752017-07-09 00:13:07 +08001586
1587 kaddr = kmap_atomic(page);
1588 memcpy(kaddr + offset, data, tocopy);
1589 kunmap_atomic(kaddr);
1590 flush_dcache_page(page);
1591
1592 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
1593 page, NULL);
1594 offset = 0;
1595 towrite -= tocopy;
1596 off += tocopy;
1597 data += tocopy;
1598 cond_resched();
1599 }
1600
1601 if (len == towrite)
Jaegeuk Kim6e5b5d42017-10-19 12:07:11 -07001602 return err;
Chao Yu0abd6752017-07-09 00:13:07 +08001603 inode->i_mtime = inode->i_ctime = current_time(inode);
1604 f2fs_mark_inode_dirty_sync(inode, false);
1605 return len - towrite;
1606}
1607
1608static struct dquot **f2fs_get_dquots(struct inode *inode)
1609{
1610 return F2FS_I(inode)->i_dquot;
1611}
1612
1613static qsize_t *f2fs_get_reserved_space(struct inode *inode)
1614{
1615 return &F2FS_I(inode)->i_reserved_quota;
1616}
1617
Chao Yu4b2414d2017-08-08 10:54:31 +08001618static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
1619{
1620 return dquot_quota_on_mount(sbi->sb, sbi->s_qf_names[type],
1621 sbi->s_jquota_fmt, type);
1622}
1623
Jaegeuk Kimea676732017-10-06 09:14:28 -07001624int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
Chao Yu4b2414d2017-08-08 10:54:31 +08001625{
Jaegeuk Kimea676732017-10-06 09:14:28 -07001626 int enabled = 0;
1627 int i, err;
1628
1629 if (f2fs_sb_has_quota_ino(sbi->sb) && rdonly) {
1630 err = f2fs_enable_quotas(sbi->sb);
1631 if (err) {
1632 f2fs_msg(sbi->sb, KERN_ERR,
1633 "Cannot turn on quota_ino: %d", err);
1634 return 0;
1635 }
1636 return 1;
1637 }
Chao Yu4b2414d2017-08-08 10:54:31 +08001638
1639 for (i = 0; i < MAXQUOTAS; i++) {
1640 if (sbi->s_qf_names[i]) {
Jaegeuk Kimea676732017-10-06 09:14:28 -07001641 err = f2fs_quota_on_mount(sbi, i);
1642 if (!err) {
1643 enabled = 1;
1644 continue;
1645 }
1646 f2fs_msg(sbi->sb, KERN_ERR,
1647 "Cannot turn on quotas: %d on %d", err, i);
Chao Yu4b2414d2017-08-08 10:54:31 +08001648 }
1649 }
Jaegeuk Kimea676732017-10-06 09:14:28 -07001650 return enabled;
1651}
1652
1653static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
1654 unsigned int flags)
1655{
1656 struct inode *qf_inode;
1657 unsigned long qf_inum;
1658 int err;
1659
1660 BUG_ON(!f2fs_sb_has_quota_ino(sb));
1661
1662 qf_inum = f2fs_qf_ino(sb, type);
1663 if (!qf_inum)
1664 return -EPERM;
1665
1666 qf_inode = f2fs_iget(sb, qf_inum);
1667 if (IS_ERR(qf_inode)) {
1668 f2fs_msg(sb, KERN_ERR,
1669 "Bad quota inode %u:%lu", type, qf_inum);
1670 return PTR_ERR(qf_inode);
1671 }
1672
1673 /* Don't account quota for quota files to avoid recursion */
1674 qf_inode->i_flags |= S_NOQUOTA;
1675 err = dquot_enable(qf_inode, type, format_id, flags);
1676 iput(qf_inode);
1677 return err;
1678}
1679
1680static int f2fs_enable_quotas(struct super_block *sb)
1681{
1682 int type, err = 0;
1683 unsigned long qf_inum;
1684 bool quota_mopt[MAXQUOTAS] = {
1685 test_opt(F2FS_SB(sb), USRQUOTA),
1686 test_opt(F2FS_SB(sb), GRPQUOTA),
1687 test_opt(F2FS_SB(sb), PRJQUOTA),
1688 };
1689
1690 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
1691 for (type = 0; type < MAXQUOTAS; type++) {
1692 qf_inum = f2fs_qf_ino(sb, type);
1693 if (qf_inum) {
1694 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
1695 DQUOT_USAGE_ENABLED |
1696 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
1697 if (err) {
1698 f2fs_msg(sb, KERN_ERR,
1699 "Failed to enable quota tracking "
1700 "(type=%d, err=%d). Please run "
1701 "fsck to fix.", type, err);
1702 for (type--; type >= 0; type--)
1703 dquot_quota_off(sb, type);
1704 return err;
1705 }
1706 }
1707 }
1708 return 0;
Chao Yu4b2414d2017-08-08 10:54:31 +08001709}
1710
Chao Yu0abd6752017-07-09 00:13:07 +08001711static int f2fs_quota_sync(struct super_block *sb, int type)
1712{
1713 struct quota_info *dqopt = sb_dqopt(sb);
1714 int cnt;
1715 int ret;
1716
1717 ret = dquot_writeback_dquots(sb, type);
1718 if (ret)
1719 return ret;
1720
1721 /*
1722 * Now when everything is written we can discard the pagecache so
1723 * that userspace sees the changes.
1724 */
1725 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1726 if (type != -1 && cnt != type)
1727 continue;
1728 if (!sb_has_quota_active(sb, cnt))
1729 continue;
1730
1731 ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping);
1732 if (ret)
1733 return ret;
1734
1735 inode_lock(dqopt->files[cnt]);
1736 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
1737 inode_unlock(dqopt->files[cnt]);
1738 }
1739 return 0;
1740}
1741
1742static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
1743 const struct path *path)
1744{
1745 struct inode *inode;
1746 int err;
1747
Chao Yu9a20d392017-08-07 16:37:59 +08001748 err = f2fs_quota_sync(sb, type);
Chao Yu0abd6752017-07-09 00:13:07 +08001749 if (err)
1750 return err;
1751
1752 err = dquot_quota_on(sb, type, format_id, path);
1753 if (err)
1754 return err;
1755
1756 inode = d_inode(path->dentry);
1757
1758 inode_lock(inode);
1759 F2FS_I(inode)->i_flags |= FS_NOATIME_FL | FS_IMMUTABLE_FL;
1760 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
1761 S_NOATIME | S_IMMUTABLE);
1762 inode_unlock(inode);
1763 f2fs_mark_inode_dirty_sync(inode, false);
1764
1765 return 0;
1766}
1767
1768static int f2fs_quota_off(struct super_block *sb, int type)
1769{
1770 struct inode *inode = sb_dqopt(sb)->files[type];
1771 int err;
1772
1773 if (!inode || !igrab(inode))
1774 return dquot_quota_off(sb, type);
1775
Chao Yu9a20d392017-08-07 16:37:59 +08001776 f2fs_quota_sync(sb, type);
Chao Yu0abd6752017-07-09 00:13:07 +08001777
1778 err = dquot_quota_off(sb, type);
Jaegeuk Kimea676732017-10-06 09:14:28 -07001779 if (err || f2fs_sb_has_quota_ino(sb))
Chao Yu0abd6752017-07-09 00:13:07 +08001780 goto out_put;
1781
1782 inode_lock(inode);
1783 F2FS_I(inode)->i_flags &= ~(FS_NOATIME_FL | FS_IMMUTABLE_FL);
1784 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
1785 inode_unlock(inode);
1786 f2fs_mark_inode_dirty_sync(inode, false);
1787out_put:
1788 iput(inode);
1789 return err;
1790}
1791
Chao Yu4b2414d2017-08-08 10:54:31 +08001792void f2fs_quota_off_umount(struct super_block *sb)
Chao Yu0abd6752017-07-09 00:13:07 +08001793{
1794 int type;
1795
1796 for (type = 0; type < MAXQUOTAS; type++)
1797 f2fs_quota_off(sb, type);
1798}
1799
Wei Yongjun94b1e102018-01-05 09:41:20 +00001800static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
Chao Yu5c571322017-07-26 00:01:41 +08001801{
1802 *projid = F2FS_I(inode)->i_projid;
1803 return 0;
1804}
1805
Chao Yu0abd6752017-07-09 00:13:07 +08001806static const struct dquot_operations f2fs_quota_operations = {
1807 .get_reserved_space = f2fs_get_reserved_space,
1808 .write_dquot = dquot_commit,
1809 .acquire_dquot = dquot_acquire,
1810 .release_dquot = dquot_release,
1811 .mark_dirty = dquot_mark_dquot_dirty,
1812 .write_info = dquot_commit_info,
1813 .alloc_dquot = dquot_alloc,
1814 .destroy_dquot = dquot_destroy,
Chao Yu5c571322017-07-26 00:01:41 +08001815 .get_projid = f2fs_get_projid,
Chao Yu0abd6752017-07-09 00:13:07 +08001816 .get_next_id = dquot_get_next_id,
1817};
1818
1819static const struct quotactl_ops f2fs_quotactl_ops = {
1820 .quota_on = f2fs_quota_on,
1821 .quota_off = f2fs_quota_off,
1822 .quota_sync = f2fs_quota_sync,
1823 .get_state = dquot_get_state,
1824 .set_info = dquot_set_dqinfo,
1825 .get_dqblk = dquot_get_dqblk,
1826 .set_dqblk = dquot_set_dqblk,
1827 .get_nextdqblk = dquot_get_next_dqblk,
1828};
1829#else
Chao Yu4b2414d2017-08-08 10:54:31 +08001830void f2fs_quota_off_umount(struct super_block *sb)
Chao Yu0abd6752017-07-09 00:13:07 +08001831{
1832}
1833#endif
1834
Arvind Yadavf62fc9f2017-08-31 15:06:24 +05301835static const struct super_operations f2fs_sops = {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001836 .alloc_inode = f2fs_alloc_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001837 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001838 .destroy_inode = f2fs_destroy_inode,
1839 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +09001840 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001841 .show_options = f2fs_show_options,
Chao Yu0abd6752017-07-09 00:13:07 +08001842#ifdef CONFIG_QUOTA
1843 .quota_read = f2fs_quota_read,
1844 .quota_write = f2fs_quota_write,
1845 .get_dquots = f2fs_get_dquots,
1846#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001847 .evict_inode = f2fs_evict_inode,
1848 .put_super = f2fs_put_super,
1849 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +09001850 .freeze_fs = f2fs_freeze,
1851 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001852 .statfs = f2fs_statfs,
Namjae Jeon696c0182013-06-16 09:48:48 +09001853 .remount_fs = f2fs_remount,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001854};
1855
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001856#ifdef CONFIG_F2FS_FS_ENCRYPTION
1857static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
1858{
1859 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1860 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1861 ctx, len, NULL);
1862}
1863
1864static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
1865 void *fs_data)
1866{
1867 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1868 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1869 ctx, len, fs_data, XATTR_CREATE);
1870}
1871
1872static unsigned f2fs_max_namelen(struct inode *inode)
1873{
1874 return S_ISLNK(inode->i_mode) ?
1875 inode->i_sb->s_blocksize : F2FS_NAME_LEN;
1876}
1877
Eric Biggers6f69f0e2017-02-07 12:42:10 -08001878static const struct fscrypt_operations f2fs_cryptops = {
Eric Biggersa5d431e2017-01-05 13:51:18 -08001879 .key_prefix = "f2fs:",
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001880 .get_context = f2fs_get_context,
1881 .set_context = f2fs_set_context,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001882 .empty_dir = f2fs_empty_dir,
1883 .max_namelen = f2fs_max_namelen,
1884};
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001885#endif
1886
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001887static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
1888 u64 ino, u32 generation)
1889{
1890 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1891 struct inode *inode;
1892
Chao Yud6b7d4b2014-06-12 13:23:41 +08001893 if (check_nid_range(sbi, ino))
Chao Yu910bb122014-03-12 17:08:36 +08001894 return ERR_PTR(-ESTALE);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001895
1896 /*
1897 * f2fs_iget isn't quite right if the inode is currently unallocated!
1898 * However f2fs_iget currently does appropriate checks to handle stale
1899 * inodes so everything is OK.
1900 */
1901 inode = f2fs_iget(sb, ino);
1902 if (IS_ERR(inode))
1903 return ERR_CAST(inode);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001904 if (unlikely(generation && inode->i_generation != generation)) {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001905 /* we didn't find the right inode.. */
1906 iput(inode);
1907 return ERR_PTR(-ESTALE);
1908 }
1909 return inode;
1910}
1911
1912static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1913 int fh_len, int fh_type)
1914{
1915 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1916 f2fs_nfs_get_inode);
1917}
1918
1919static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
1920 int fh_len, int fh_type)
1921{
1922 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1923 f2fs_nfs_get_inode);
1924}
1925
1926static const struct export_operations f2fs_export_ops = {
1927 .fh_to_dentry = f2fs_fh_to_dentry,
1928 .fh_to_parent = f2fs_fh_to_parent,
1929 .get_parent = f2fs_get_parent,
1930};
1931
Chao Yue0afc4d2015-12-31 14:35:37 +08001932static loff_t max_file_blocks(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001933{
Chao Yu7a2af762017-07-19 00:19:06 +08001934 loff_t result = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001935 loff_t leaf_count = ADDRS_PER_BLOCK;
1936
Chao Yu7a2af762017-07-19 00:19:06 +08001937 /*
1938 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
Chao Yu6afc6622017-09-06 21:59:50 +08001939 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
Chao Yu7a2af762017-07-19 00:19:06 +08001940 * space in inode.i_addr, it will be more safe to reassign
1941 * result as zero.
1942 */
1943
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001944 /* two direct node blocks */
1945 result += (leaf_count * 2);
1946
1947 /* two indirect node blocks */
1948 leaf_count *= NIDS_PER_BLOCK;
1949 result += (leaf_count * 2);
1950
1951 /* one double indirect node block */
1952 leaf_count *= NIDS_PER_BLOCK;
1953 result += leaf_count;
1954
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001955 return result;
1956}
1957
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001958static int __f2fs_commit_super(struct buffer_head *bh,
1959 struct f2fs_super_block *super)
Chao Yu9a59b622015-12-15 09:58:18 +08001960{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001961 lock_buffer(bh);
1962 if (super)
1963 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001964 set_buffer_dirty(bh);
1965 unlock_buffer(bh);
1966
1967 /* it's rare case, we can do fua all the time */
Jan Kara3adc5fcb2017-05-02 17:03:47 +02001968 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001969}
1970
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001971static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001972 struct buffer_head *bh)
1973{
1974 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1975 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001976 struct super_block *sb = sbi->sb;
Chao Yu9a59b622015-12-15 09:58:18 +08001977 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1978 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
1979 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
1980 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
1981 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1982 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1983 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
1984 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
1985 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
1986 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
1987 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
1988 u32 segment_count = le32_to_cpu(raw_super->segment_count);
1989 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001990 u64 main_end_blkaddr = main_blkaddr +
1991 (segment_count_main << log_blocks_per_seg);
1992 u64 seg_end_blkaddr = segment0_blkaddr +
1993 (segment_count << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08001994
1995 if (segment0_blkaddr != cp_blkaddr) {
1996 f2fs_msg(sb, KERN_INFO,
1997 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
1998 segment0_blkaddr, cp_blkaddr);
1999 return true;
2000 }
2001
2002 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
2003 sit_blkaddr) {
2004 f2fs_msg(sb, KERN_INFO,
2005 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
2006 cp_blkaddr, sit_blkaddr,
2007 segment_count_ckpt << log_blocks_per_seg);
2008 return true;
2009 }
2010
2011 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
2012 nat_blkaddr) {
2013 f2fs_msg(sb, KERN_INFO,
2014 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
2015 sit_blkaddr, nat_blkaddr,
2016 segment_count_sit << log_blocks_per_seg);
2017 return true;
2018 }
2019
2020 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
2021 ssa_blkaddr) {
2022 f2fs_msg(sb, KERN_INFO,
2023 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
2024 nat_blkaddr, ssa_blkaddr,
2025 segment_count_nat << log_blocks_per_seg);
2026 return true;
2027 }
2028
2029 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
2030 main_blkaddr) {
2031 f2fs_msg(sb, KERN_INFO,
2032 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
2033 ssa_blkaddr, main_blkaddr,
2034 segment_count_ssa << log_blocks_per_seg);
2035 return true;
2036 }
2037
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002038 if (main_end_blkaddr > seg_end_blkaddr) {
Chao Yu9a59b622015-12-15 09:58:18 +08002039 f2fs_msg(sb, KERN_INFO,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002040 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
Chao Yu9a59b622015-12-15 09:58:18 +08002041 main_blkaddr,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002042 segment0_blkaddr +
2043 (segment_count << log_blocks_per_seg),
Chao Yu9a59b622015-12-15 09:58:18 +08002044 segment_count_main << log_blocks_per_seg);
2045 return true;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002046 } else if (main_end_blkaddr < seg_end_blkaddr) {
2047 int err = 0;
2048 char *res;
Chao Yu9a59b622015-12-15 09:58:18 +08002049
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002050 /* fix in-memory information all the time */
2051 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
2052 segment0_blkaddr) >> log_blocks_per_seg);
2053
2054 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002055 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002056 res = "internally";
2057 } else {
2058 err = __f2fs_commit_super(bh, NULL);
2059 res = err ? "failed" : "done";
2060 }
2061 f2fs_msg(sb, KERN_INFO,
2062 "Fix alignment : %s, start(%u) end(%u) block(%u)",
2063 res, main_blkaddr,
2064 segment0_blkaddr +
2065 (segment_count << log_blocks_per_seg),
2066 segment_count_main << log_blocks_per_seg);
2067 if (err)
2068 return true;
2069 }
Chao Yu9a59b622015-12-15 09:58:18 +08002070 return false;
2071}
2072
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002073static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002074 struct buffer_head *bh)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002075{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002076 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2077 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002078 struct super_block *sb = sbi->sb;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002079 unsigned int blocksize;
2080
Namjae Jeona07ef782012-12-30 14:52:05 +09002081 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
2082 f2fs_msg(sb, KERN_INFO,
2083 "Magic Mismatch, valid(0x%x) - read(0x%x)",
2084 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002085 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09002086 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002087
majianpeng5c9b4692013-02-01 19:07:57 +08002088 /* Currently, support only 4KB page cache size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002089 if (F2FS_BLKSIZE != PAGE_SIZE) {
majianpeng5c9b4692013-02-01 19:07:57 +08002090 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +08002091 "Invalid page_cache_size (%lu), supports only 4KB\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002092 PAGE_SIZE);
majianpeng5c9b4692013-02-01 19:07:57 +08002093 return 1;
2094 }
2095
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002096 /* Currently, support only 4KB block size */
2097 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +08002098 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002099 f2fs_msg(sb, KERN_INFO,
2100 "Invalid blocksize (%u), supports only 4KB\n",
2101 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002102 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09002103 }
majianpeng5c9b4692013-02-01 19:07:57 +08002104
Chao Yu9a59b622015-12-15 09:58:18 +08002105 /* check log blocks per segment */
2106 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
2107 f2fs_msg(sb, KERN_INFO,
2108 "Invalid log blocks per segment (%u)\n",
2109 le32_to_cpu(raw_super->log_blocks_per_seg));
2110 return 1;
2111 }
2112
Chao Yu55cf9cb2014-09-15 18:01:10 +08002113 /* Currently, support 512/1024/2048/4096 bytes sector size */
2114 if (le32_to_cpu(raw_super->log_sectorsize) >
2115 F2FS_MAX_LOG_SECTOR_SIZE ||
2116 le32_to_cpu(raw_super->log_sectorsize) <
2117 F2FS_MIN_LOG_SECTOR_SIZE) {
2118 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
2119 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002120 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09002121 }
Chao Yu55cf9cb2014-09-15 18:01:10 +08002122 if (le32_to_cpu(raw_super->log_sectors_per_block) +
2123 le32_to_cpu(raw_super->log_sectorsize) !=
2124 F2FS_MAX_LOG_SECTOR_SIZE) {
2125 f2fs_msg(sb, KERN_INFO,
2126 "Invalid log sectors per block(%u) log sectorsize(%u)",
2127 le32_to_cpu(raw_super->log_sectors_per_block),
2128 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002129 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09002130 }
Chao Yu9a59b622015-12-15 09:58:18 +08002131
2132 /* check reserved ino info */
2133 if (le32_to_cpu(raw_super->node_ino) != 1 ||
2134 le32_to_cpu(raw_super->meta_ino) != 2 ||
2135 le32_to_cpu(raw_super->root_ino) != 3) {
2136 f2fs_msg(sb, KERN_INFO,
2137 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
2138 le32_to_cpu(raw_super->node_ino),
2139 le32_to_cpu(raw_super->meta_ino),
2140 le32_to_cpu(raw_super->root_ino));
2141 return 1;
2142 }
2143
Jin Qianb9dd4612017-04-25 16:28:48 -07002144 if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) {
2145 f2fs_msg(sb, KERN_INFO,
2146 "Invalid segment count (%u)",
2147 le32_to_cpu(raw_super->segment_count));
2148 return 1;
2149 }
2150
Chao Yu9a59b622015-12-15 09:58:18 +08002151 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002152 if (sanity_check_area_boundary(sbi, bh))
Chao Yu9a59b622015-12-15 09:58:18 +08002153 return 1;
2154
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002155 return 0;
2156}
2157
Shawn Lin984ec632016-02-17 11:26:32 +08002158int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002159{
2160 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09002161 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2162 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08002163 unsigned int ovp_segments, reserved_segments;
Jin Qian15d30422017-05-15 10:45:08 -07002164 unsigned int main_segs, blocks_per_seg;
2165 int i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002166
2167 total = le32_to_cpu(raw_super->segment_count);
2168 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
2169 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
2170 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
2171 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
2172 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
2173
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002174 if (unlikely(fsmeta >= total))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002175 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09002176
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08002177 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2178 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2179
2180 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
2181 ovp_segments == 0 || reserved_segments == 0)) {
2182 f2fs_msg(sbi->sb, KERN_ERR,
2183 "Wrong layout: check mkfs.f2fs version");
2184 return 1;
2185 }
2186
Jin Qian15d30422017-05-15 10:45:08 -07002187 main_segs = le32_to_cpu(raw_super->segment_count_main);
2188 blocks_per_seg = sbi->blocks_per_seg;
2189
2190 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2191 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
2192 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
2193 return 1;
2194 }
2195 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
2196 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
2197 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
2198 return 1;
2199 }
2200
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07002201 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim577e3492013-01-24 19:56:11 +09002202 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
2203 return 1;
2204 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002205 return 0;
2206}
2207
2208static void init_sb_info(struct f2fs_sb_info *sbi)
2209{
2210 struct f2fs_super_block *raw_super = sbi->raw_super;
Chao Yue41e6d72017-05-19 23:37:00 +08002211 int i, j;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002212
2213 sbi->log_sectors_per_block =
2214 le32_to_cpu(raw_super->log_sectors_per_block);
2215 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
2216 sbi->blocksize = 1 << sbi->log_blocksize;
2217 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2218 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
2219 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2220 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2221 sbi->total_sections = le32_to_cpu(raw_super->section_count);
2222 sbi->total_node_count =
2223 (le32_to_cpu(raw_super->segment_count_nat) / 2)
2224 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
2225 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
2226 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
2227 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002228 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimb1c57c12014-01-08 13:45:08 +09002229 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002230
Jaegeuk Kimab9fa662014-02-27 20:09:05 +09002231 sbi->dir_level = DEF_DIR_LEVEL;
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08002232 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002233 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
Chao Yucaf00472015-01-28 17:48:42 +08002234 clear_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002235
Jaegeuk Kim35782b22016-10-20 19:09:57 -07002236 for (i = 0; i < NR_COUNT_TYPE; i++)
2237 atomic_set(&sbi->nr_pages[i], 0);
2238
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002239 atomic_set(&sbi->wb_sync_req, 0);
2240
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002241 INIT_LIST_HEAD(&sbi->s_list);
2242 mutex_init(&sbi->umount_mutex);
Chao Yue41e6d72017-05-19 23:37:00 +08002243 for (i = 0; i < NR_PAGE_TYPE - 1; i++)
2244 for (j = HOT; j < NR_TEMP_TYPE; j++)
2245 mutex_init(&sbi->wio_mutex[i][j]);
Chao Yuaaec2b12016-09-20 11:04:18 +08002246 spin_lock_init(&sbi->cp_lock);
Chao Yu1228b482017-09-29 13:59:39 +08002247
2248 sbi->dirty_device = 0;
2249 spin_lock_init(&sbi->dev_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002250
Chao Yu846ae672018-02-26 22:04:13 +08002251 init_rwsem(&sbi->sb_lock);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002252}
2253
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002254static int init_percpu_info(struct f2fs_sb_info *sbi)
2255{
Jaegeuk Kim35782b22016-10-20 19:09:57 -07002256 int err;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002257
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07002258 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
2259 if (err)
2260 return err;
2261
2262 return percpu_counter_init(&sbi->total_valid_inode_count, 0,
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002263 GFP_KERNEL);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002264}
2265
Damien Le Moal178053e2016-10-28 17:45:05 +09002266#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002267static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
Damien Le Moal178053e2016-10-28 17:45:05 +09002268{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002269 struct block_device *bdev = FDEV(devi).bdev;
Damien Le Moal178053e2016-10-28 17:45:05 +09002270 sector_t nr_sectors = bdev->bd_part->nr_sects;
2271 sector_t sector = 0;
2272 struct blk_zone *zones;
2273 unsigned int i, nr_zones;
2274 unsigned int n = 0;
2275 int err = -EIO;
2276
Sheng Yongccd31cb2018-02-06 12:31:17 +08002277 if (!f2fs_sb_has_blkzoned(sbi->sb))
Damien Le Moal178053e2016-10-28 17:45:05 +09002278 return 0;
2279
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002280 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
Damien Le Moalf99e8642017-01-12 07:58:32 -07002281 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002282 return -EINVAL;
Damien Le Moalf99e8642017-01-12 07:58:32 -07002283 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002284 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
2285 __ilog2_u32(sbi->blocks_per_blkz))
2286 return -EINVAL;
Damien Le Moal178053e2016-10-28 17:45:05 +09002287 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002288 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
2289 sbi->log_blocks_per_blkz;
Damien Le Moalf99e8642017-01-12 07:58:32 -07002290 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002291 FDEV(devi).nr_blkz++;
Damien Le Moal178053e2016-10-28 17:45:05 +09002292
Chao Yu4e6aad22017-11-30 19:28:19 +08002293 FDEV(devi).blkz_type = f2fs_kmalloc(sbi, FDEV(devi).nr_blkz,
2294 GFP_KERNEL);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002295 if (!FDEV(devi).blkz_type)
Damien Le Moal178053e2016-10-28 17:45:05 +09002296 return -ENOMEM;
2297
2298#define F2FS_REPORT_NR_ZONES 4096
2299
Chao Yu4e6aad22017-11-30 19:28:19 +08002300 zones = f2fs_kzalloc(sbi, sizeof(struct blk_zone) *
2301 F2FS_REPORT_NR_ZONES, GFP_KERNEL);
Damien Le Moal178053e2016-10-28 17:45:05 +09002302 if (!zones)
2303 return -ENOMEM;
2304
2305 /* Get block zones type */
2306 while (zones && sector < nr_sectors) {
2307
2308 nr_zones = F2FS_REPORT_NR_ZONES;
2309 err = blkdev_report_zones(bdev, sector,
2310 zones, &nr_zones,
2311 GFP_KERNEL);
2312 if (err)
2313 break;
2314 if (!nr_zones) {
2315 err = -EIO;
2316 break;
2317 }
2318
2319 for (i = 0; i < nr_zones; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002320 FDEV(devi).blkz_type[n] = zones[i].type;
Damien Le Moal178053e2016-10-28 17:45:05 +09002321 sector += zones[i].len;
2322 n++;
2323 }
2324 }
2325
2326 kfree(zones);
2327
2328 return err;
2329}
2330#endif
2331
Gu Zheng9076a752013-10-14 18:47:11 +08002332/*
2333 * Read f2fs raw super block.
Shawn Lin2b39e902016-02-17 08:59:01 +08002334 * Because we have two copies of super block, so read both of them
2335 * to get the first valid one. If any one of them is broken, we pass
2336 * them recovery flag back to the caller.
Gu Zheng9076a752013-10-14 18:47:11 +08002337 */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002338static int read_raw_super_block(struct f2fs_sb_info *sbi,
Gu Zheng9076a752013-10-14 18:47:11 +08002339 struct f2fs_super_block **raw_super,
Chao Yue8240f62015-12-15 17:19:26 +08002340 int *valid_super_block, int *recovery)
majianpeng14d7e9d2013-02-01 19:07:03 +08002341{
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002342 struct super_block *sb = sbi->sb;
Shawn Lin2b39e902016-02-17 08:59:01 +08002343 int block;
Chao Yue8240f62015-12-15 17:19:26 +08002344 struct buffer_head *bh;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002345 struct f2fs_super_block *super;
hujianyangda554e42015-05-21 14:42:53 +08002346 int err = 0;
majianpeng14d7e9d2013-02-01 19:07:03 +08002347
Yunlei Heb39f0de2015-12-15 17:17:20 +08002348 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
2349 if (!super)
2350 return -ENOMEM;
Shawn Lin2b39e902016-02-17 08:59:01 +08002351
2352 for (block = 0; block < 2; block++) {
2353 bh = sb_bread(sb, block);
2354 if (!bh) {
2355 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
Gu Zheng9076a752013-10-14 18:47:11 +08002356 block + 1);
Shawn Lin2b39e902016-02-17 08:59:01 +08002357 err = -EIO;
2358 continue;
2359 }
majianpeng14d7e9d2013-02-01 19:07:03 +08002360
Shawn Lin2b39e902016-02-17 08:59:01 +08002361 /* sanity checking of raw super */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002362 if (sanity_check_raw_super(sbi, bh)) {
Shawn Lin2b39e902016-02-17 08:59:01 +08002363 f2fs_msg(sb, KERN_ERR,
2364 "Can't find valid F2FS filesystem in %dth superblock",
2365 block + 1);
2366 err = -EINVAL;
2367 brelse(bh);
2368 continue;
2369 }
2370
2371 if (!*raw_super) {
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002372 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
2373 sizeof(*super));
Shawn Lin2b39e902016-02-17 08:59:01 +08002374 *valid_super_block = block;
2375 *raw_super = super;
2376 }
Chao Yue8240f62015-12-15 17:19:26 +08002377 brelse(bh);
Shawn Lin2b39e902016-02-17 08:59:01 +08002378 }
2379
2380 /* Fail to read any one of the superblocks*/
2381 if (err < 0)
hujianyangda554e42015-05-21 14:42:53 +08002382 *recovery = 1;
hujianyangda554e42015-05-21 14:42:53 +08002383
hujianyangda554e42015-05-21 14:42:53 +08002384 /* No valid superblock */
Shawn Lin2b39e902016-02-17 08:59:01 +08002385 if (!*raw_super)
Yunlei Heb39f0de2015-12-15 17:17:20 +08002386 kfree(super);
Shawn Lin2b39e902016-02-17 08:59:01 +08002387 else
2388 err = 0;
hujianyangda554e42015-05-21 14:42:53 +08002389
Shawn Lin2b39e902016-02-17 08:59:01 +08002390 return err;
majianpeng14d7e9d2013-02-01 19:07:03 +08002391}
2392
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002393int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002394{
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002395 struct buffer_head *bh;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002396 int err;
2397
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002398 if ((recover && f2fs_readonly(sbi->sb)) ||
2399 bdev_read_only(sbi->sb->s_bdev)) {
2400 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07002401 return -EROFS;
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002402 }
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07002403
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002404 /* write back-up superblock first */
Sheng Yong0964fc12018-01-29 19:13:15 +08002405 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002406 if (!bh)
2407 return -EIO;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002408 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002409 brelse(bh);
Chao Yuc5bda1c2015-06-08 13:28:03 +08002410
2411 /* if we are in recovery path, skip writing valid superblock */
2412 if (recover || err)
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002413 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002414
Chao Yue8240f62015-12-15 17:19:26 +08002415 /* write current valid superblock */
Sheng Yong0964fc12018-01-29 19:13:15 +08002416 bh = sb_bread(sbi->sb, sbi->valid_super_block);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002417 if (!bh)
2418 return -EIO;
2419 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
2420 brelse(bh);
2421 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002422}
2423
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002424static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
2425{
2426 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002427 unsigned int max_devices = MAX_DEVICES;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002428 int i;
2429
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002430 /* Initialize single device information */
2431 if (!RDEV(0).path[0]) {
2432 if (!bdev_is_zoned(sbi->sb->s_bdev))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002433 return 0;
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002434 max_devices = 1;
2435 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002436
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002437 /*
2438 * Initialize multiple devices information, or single
2439 * zoned block device information.
2440 */
Chao Yu4e6aad22017-11-30 19:28:19 +08002441 sbi->devs = f2fs_kzalloc(sbi, sizeof(struct f2fs_dev_info) *
2442 max_devices, GFP_KERNEL);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002443 if (!sbi->devs)
2444 return -ENOMEM;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002445
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002446 for (i = 0; i < max_devices; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002447
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002448 if (i > 0 && !RDEV(i).path[0])
2449 break;
2450
2451 if (max_devices == 1) {
2452 /* Single zoned block device mount */
2453 FDEV(0).bdev =
2454 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002455 sbi->sb->s_mode, sbi->sb->s_type);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002456 } else {
2457 /* Multi-device mount */
2458 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
2459 FDEV(i).total_segments =
2460 le32_to_cpu(RDEV(i).total_segments);
2461 if (i == 0) {
2462 FDEV(i).start_blk = 0;
2463 FDEV(i).end_blk = FDEV(i).start_blk +
2464 (FDEV(i).total_segments <<
2465 sbi->log_blocks_per_seg) - 1 +
2466 le32_to_cpu(raw_super->segment0_blkaddr);
2467 } else {
2468 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
2469 FDEV(i).end_blk = FDEV(i).start_blk +
2470 (FDEV(i).total_segments <<
2471 sbi->log_blocks_per_seg) - 1;
2472 }
2473 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
2474 sbi->sb->s_mode, sbi->sb->s_type);
2475 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002476 if (IS_ERR(FDEV(i).bdev))
2477 return PTR_ERR(FDEV(i).bdev);
2478
2479 /* to release errored devices */
2480 sbi->s_ndevs = i + 1;
2481
2482#ifdef CONFIG_BLK_DEV_ZONED
2483 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
Sheng Yongccd31cb2018-02-06 12:31:17 +08002484 !f2fs_sb_has_blkzoned(sbi->sb)) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002485 f2fs_msg(sbi->sb, KERN_ERR,
2486 "Zoned block device feature not enabled\n");
2487 return -EINVAL;
2488 }
2489 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
2490 if (init_blkz_info(sbi, i)) {
2491 f2fs_msg(sbi->sb, KERN_ERR,
2492 "Failed to initialize F2FS blkzone information");
2493 return -EINVAL;
2494 }
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002495 if (max_devices == 1)
2496 break;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002497 f2fs_msg(sbi->sb, KERN_INFO,
2498 "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
2499 i, FDEV(i).path,
2500 FDEV(i).total_segments,
2501 FDEV(i).start_blk, FDEV(i).end_blk,
2502 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
2503 "Host-aware" : "Host-managed");
2504 continue;
2505 }
2506#endif
2507 f2fs_msg(sbi->sb, KERN_INFO,
2508 "Mount Device [%2d]: %20s, %8u, %8x - %8x",
2509 i, FDEV(i).path,
2510 FDEV(i).total_segments,
2511 FDEV(i).start_blk, FDEV(i).end_blk);
2512 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002513 f2fs_msg(sbi->sb, KERN_INFO,
2514 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002515 return 0;
2516}
2517
Jaegeuk Kim84b89e52018-02-22 14:09:30 -08002518static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
2519{
2520 struct f2fs_sm_info *sm_i = SM_I(sbi);
2521
2522 /* adjust parameters according to the volume size */
2523 if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
2524 sbi->alloc_mode = ALLOC_MODE_REUSE;
2525 sm_i->dcc_info->discard_granularity = 1;
2526 sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
2527 }
2528}
2529
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002530static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
2531{
2532 struct f2fs_sb_info *sbi;
hujianyangda554e42015-05-21 14:42:53 +08002533 struct f2fs_super_block *raw_super;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002534 struct inode *root;
Sheng Yong99e3e852016-05-11 17:08:14 +08002535 int err;
Chao Yu2adc3502015-03-16 21:08:44 +08002536 bool retry = true, need_fsck = false;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002537 char *options = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08002538 int recovery, i, valid_super_block;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002539 struct curseg_info *seg_i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002540
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002541try_onemore:
hujianyangda554e42015-05-21 14:42:53 +08002542 err = -EINVAL;
2543 raw_super = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08002544 valid_super_block = -1;
hujianyangda554e42015-05-21 14:42:53 +08002545 recovery = 0;
2546
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002547 /* allocate memory for f2fs-specific super block info */
2548 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
2549 if (!sbi)
2550 return -ENOMEM;
2551
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002552 sbi->sb = sb;
2553
Keith Mok43b65732016-03-02 12:04:24 -08002554 /* Load the checksum driver */
2555 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
2556 if (IS_ERR(sbi->s_chksum_driver)) {
2557 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
2558 err = PTR_ERR(sbi->s_chksum_driver);
2559 sbi->s_chksum_driver = NULL;
2560 goto free_sbi;
2561 }
2562
Namjae Jeonff9234a2013-01-12 14:41:13 +09002563 /* set a block size */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002564 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002565 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002566 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +09002567 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002568
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002569 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
Chao Yue8240f62015-12-15 17:19:26 +08002570 &recovery);
Gu Zheng9076a752013-10-14 18:47:11 +08002571 if (err)
2572 goto free_sbi;
2573
Gu Zheng5fb08372013-06-07 14:16:53 +08002574 sb->s_fs_info = sbi;
Jaegeuk Kim52763a42016-06-13 09:47:48 -07002575 sbi->raw_super = raw_super;
2576
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -08002577 sbi->s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
2578 sbi->s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
2579
Chao Yu704956e2017-07-31 20:19:09 +08002580 /* precompute checksum seed for metadata */
2581 if (f2fs_sb_has_inode_chksum(sb))
2582 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
2583 sizeof(raw_super->uuid));
2584
Damien Le Moald1b959c2016-10-28 17:45:01 +09002585 /*
2586 * The BLKZONED feature indicates that the drive was formatted with
2587 * zone alignment optimization. This is optional for host-aware
2588 * devices, but mandatory for host-managed zoned block devices.
2589 */
2590#ifndef CONFIG_BLK_DEV_ZONED
Sheng Yongccd31cb2018-02-06 12:31:17 +08002591 if (f2fs_sb_has_blkzoned(sb)) {
Damien Le Moald1b959c2016-10-28 17:45:01 +09002592 f2fs_msg(sb, KERN_ERR,
2593 "Zoned block device support is not enabled\n");
Chao Yu1727f312017-06-12 09:44:27 +08002594 err = -EOPNOTSUPP;
Damien Le Moald1b959c2016-10-28 17:45:01 +09002595 goto free_sb_buf;
2596 }
Damien Le Moald1b959c2016-10-28 17:45:01 +09002597#endif
Yunlei He498c5e92015-05-07 18:11:37 +08002598 default_options(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002599 /* parse mount options */
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002600 options = kstrdup((const char *)data, GFP_KERNEL);
2601 if (data && !options) {
2602 err = -ENOMEM;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002603 goto free_sb_buf;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002604 }
2605
2606 err = parse_options(sb, options);
2607 if (err)
2608 goto free_options;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002609
Chao Yue0afc4d2015-12-31 14:35:37 +08002610 sbi->max_file_blocks = max_file_blocks();
2611 sb->s_maxbytes = sbi->max_file_blocks <<
2612 le32_to_cpu(raw_super->log_blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002613 sb->s_max_links = F2FS_LINK_MAX;
2614 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2615
Chao Yu0abd6752017-07-09 00:13:07 +08002616#ifdef CONFIG_QUOTA
2617 sb->dq_op = &f2fs_quota_operations;
Jaegeuk Kimea676732017-10-06 09:14:28 -07002618 if (f2fs_sb_has_quota_ino(sb))
2619 sb->s_qcop = &dquot_quotactl_sysfile_ops;
2620 else
2621 sb->s_qcop = &f2fs_quotactl_ops;
Chao Yu5c571322017-07-26 00:01:41 +08002622 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
Chao Yu292c196a2017-11-16 16:59:14 +08002623
2624 if (f2fs_sb_has_quota_ino(sbi->sb)) {
2625 for (i = 0; i < MAXQUOTAS; i++) {
2626 if (f2fs_qf_ino(sbi->sb, i))
2627 sbi->nquota_files++;
2628 }
2629 }
Chao Yu0abd6752017-07-09 00:13:07 +08002630#endif
2631
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002632 sb->s_op = &f2fs_sops;
Eric Biggersffcc41822017-10-09 12:15:38 -07002633#ifdef CONFIG_F2FS_FS_ENCRYPTION
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002634 sb->s_cop = &f2fs_cryptops;
Eric Biggersffcc41822017-10-09 12:15:38 -07002635#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002636 sb->s_xattr = f2fs_xattr_handlers;
2637 sb->s_export_op = &f2fs_export_ops;
2638 sb->s_magic = F2FS_SUPER_MAGIC;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002639 sb->s_time_gran = 1;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002640 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2641 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
Christoph Hellwig85787092017-05-10 15:06:33 +02002642 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
Yufen Yu578c6472018-01-09 19:33:39 +08002643 sb->s_iflags |= SB_I_CGROUPWB;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002644
2645 /* init f2fs-specific super block info */
Chao Yue8240f62015-12-15 17:19:26 +08002646 sbi->valid_super_block = valid_super_block;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002647 mutex_init(&sbi->gc_mutex);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002648 mutex_init(&sbi->cp_mutex);
Chao Yub3582c62014-07-03 18:58:39 +08002649 init_rwsem(&sbi->node_write);
Yunlei He59c90812017-03-13 20:22:18 +08002650 init_rwsem(&sbi->node_change);
Jaegeuk Kim315df832015-08-11 12:45:39 -07002651
2652 /* disallow all the data/node/meta page writes */
2653 set_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002654 spin_lock_init(&sbi->stat_lock);
Jaegeuk Kim971767c2013-11-18 17:16:17 +09002655
Chao Yub0af6d42017-08-02 23:21:48 +08002656 /* init iostat info */
2657 spin_lock_init(&sbi->iostat_lock);
2658 sbi->iostat_enable = false;
2659
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002660 for (i = 0; i < NR_PAGE_TYPE; i++) {
Jaegeuk Kima912b542017-05-10 11:18:25 -07002661 int n = (i == META) ? 1: NR_TEMP_TYPE;
2662 int j;
2663
Chao Yu4e6aad22017-11-30 19:28:19 +08002664 sbi->write_io[i] = f2fs_kmalloc(sbi,
2665 n * sizeof(struct f2fs_bio_info),
2666 GFP_KERNEL);
Christophe JAILLETb63def92017-06-11 09:21:11 +02002667 if (!sbi->write_io[i]) {
2668 err = -ENOMEM;
Jaegeuk Kima912b542017-05-10 11:18:25 -07002669 goto free_options;
Christophe JAILLETb63def92017-06-11 09:21:11 +02002670 }
Jaegeuk Kima912b542017-05-10 11:18:25 -07002671
2672 for (j = HOT; j < n; j++) {
2673 init_rwsem(&sbi->write_io[i][j].io_rwsem);
2674 sbi->write_io[i][j].sbi = sbi;
2675 sbi->write_io[i][j].bio = NULL;
Chao Yufb830fc2017-05-19 23:37:01 +08002676 spin_lock_init(&sbi->write_io[i][j].io_lock);
2677 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
Jaegeuk Kima912b542017-05-10 11:18:25 -07002678 }
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002679 }
Jaegeuk Kim971767c2013-11-18 17:16:17 +09002680
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002681 init_rwsem(&sbi->cp_rwsem);
Changman Leefb51b5e2013-11-07 12:48:25 +09002682 init_waitqueue_head(&sbi->cp_wait);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002683 init_sb_info(sbi);
2684
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002685 err = init_percpu_info(sbi);
2686 if (err)
Chao Yud7997e62018-01-17 16:31:35 +08002687 goto free_bio_info;
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002688
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002689 if (F2FS_IO_SIZE(sbi) > 1) {
2690 sbi->write_io_dummy =
Chao Yua3ebfe42017-02-27 18:43:13 +08002691 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
Chao Yu1727f312017-06-12 09:44:27 +08002692 if (!sbi->write_io_dummy) {
2693 err = -ENOMEM;
Chao Yud7997e62018-01-17 16:31:35 +08002694 goto free_percpu;
Chao Yu1727f312017-06-12 09:44:27 +08002695 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002696 }
2697
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002698 /* get an inode for meta space */
2699 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
2700 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002701 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002702 err = PTR_ERR(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002703 goto free_io_dummy;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002704 }
2705
2706 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002707 if (err) {
2708 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002709 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +09002710 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002711
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002712 /* Initialize device list */
2713 err = f2fs_scan_devices(sbi);
2714 if (err) {
2715 f2fs_msg(sb, KERN_ERR, "Failed to find devices");
2716 goto free_devices;
2717 }
2718
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002719 sbi->total_valid_node_count =
2720 le32_to_cpu(sbi->ckpt->valid_node_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07002721 percpu_counter_set(&sbi->total_valid_inode_count,
2722 le32_to_cpu(sbi->ckpt->valid_inode_count));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002723 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
2724 sbi->total_valid_block_count =
2725 le64_to_cpu(sbi->ckpt->valid_block_count);
2726 sbi->last_valid_block_count = sbi->total_valid_block_count;
Chao Yudaeb4332017-06-26 16:24:41 +08002727 sbi->reserved_blocks = 0;
Yunlong Song80d42142017-10-27 20:45:05 +08002728 sbi->current_reserved_blocks = 0;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08002729 limit_reserve_root(sbi);
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002730
Chao Yuc227f912015-12-16 13:09:20 +08002731 for (i = 0; i < NR_INODE_TYPE; i++) {
2732 INIT_LIST_HEAD(&sbi->inode_list[i]);
2733 spin_lock_init(&sbi->inode_lock[i]);
2734 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002735
Chao Yu1dcc3362015-02-05 17:57:31 +08002736 init_extent_cache_info(sbi);
2737
Jaegeuk Kim6451e042014-07-25 15:47:17 -07002738 init_ino_entry_info(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002739
2740 /* setup f2fs internal modules */
2741 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002742 if (err) {
2743 f2fs_msg(sb, KERN_ERR,
2744 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002745 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +09002746 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002747 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002748 if (err) {
2749 f2fs_msg(sb, KERN_ERR,
2750 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002751 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +09002752 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002753
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002754 /* For write statistics */
2755 if (sb->s_bdev->bd_part)
2756 sbi->sectors_written_start =
2757 (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
2758
2759 /* Read accumulated write IO statistics if exists */
2760 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
2761 if (__exist_node_summaries(sbi))
2762 sbi->kbytes_written =
Shuoran Liub2dde6f2016-03-29 18:00:15 +08002763 le64_to_cpu(seg_i->journal->info.kbytes_written);
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002764
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002765 build_gc_manager(sbi);
2766
2767 /* get an inode for node space */
2768 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
2769 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002770 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002771 err = PTR_ERR(sbi->node_inode);
2772 goto free_nm;
2773 }
2774
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08002775 err = f2fs_build_stats(sbi);
2776 if (err)
Chao Yubae01ed2017-11-30 19:28:20 +08002777 goto free_node_inode;
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08002778
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002779 /* read root inode and dentry */
2780 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
2781 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002782 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002783 err = PTR_ERR(root);
Chao Yubae01ed2017-11-30 19:28:20 +08002784 goto free_stats;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002785 }
Chao Yu8f99a942013-11-28 15:43:43 +08002786 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
Chao Yu9d847952014-07-25 12:55:09 +08002787 iput(root);
Chao Yu8f99a942013-11-28 15:43:43 +08002788 err = -EINVAL;
Chao Yu9d847952014-07-25 12:55:09 +08002789 goto free_node_inode;
Chao Yu8f99a942013-11-28 15:43:43 +08002790 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002791
2792 sb->s_root = d_make_root(root); /* allocate root dentry */
2793 if (!sb->s_root) {
2794 err = -ENOMEM;
2795 goto free_root_inode;
2796 }
2797
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002798 err = f2fs_register_sysfs(sbi);
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09002799 if (err)
Chao Yua3981012017-06-14 17:39:46 +08002800 goto free_root_inode;
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09002801
Jaegeuk Kimea676732017-10-06 09:14:28 -07002802#ifdef CONFIG_QUOTA
2803 /*
2804 * Turn on quotas which were not enabled for read-only mounts if
2805 * filesystem has quota feature, so that they are updated correctly.
2806 */
2807 if (f2fs_sb_has_quota_ino(sb) && !sb_rdonly(sb)) {
2808 err = f2fs_enable_quotas(sb);
2809 if (err) {
2810 f2fs_msg(sb, KERN_ERR,
2811 "Cannot turn on quotas: error %d", err);
2812 goto free_sysfs;
2813 }
2814 }
2815#endif
Chao Yu4b2414d2017-08-08 10:54:31 +08002816 /* if there are nt orphan nodes free them */
2817 err = recover_orphan_inodes(sbi);
2818 if (err)
Jaegeuk Kimea676732017-10-06 09:14:28 -07002819 goto free_meta;
Chao Yu4b2414d2017-08-08 10:54:31 +08002820
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002821 /* recover fsynced data */
2822 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002823 /*
2824 * mount should be failed, when device has readonly mode, and
2825 * previous checkpoint was not done by clean system shutdown.
2826 */
2827 if (bdev_read_only(sb->s_bdev) &&
Chao Yuaaec2b12016-09-20 11:04:18 +08002828 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002829 err = -EROFS;
Chao Yu4b2414d2017-08-08 10:54:31 +08002830 goto free_meta;
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002831 }
Chao Yu2adc3502015-03-16 21:08:44 +08002832
2833 if (need_fsck)
2834 set_sbi_flag(sbi, SBI_NEED_FSCK);
2835
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07002836 if (!retry)
2837 goto skip_recovery;
2838
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002839 err = recover_fsync_data(sbi, false);
2840 if (err < 0) {
Chao Yu2adc3502015-03-16 21:08:44 +08002841 need_fsck = true;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002842 f2fs_msg(sb, KERN_ERR,
Sheng Yong99e3e852016-05-11 17:08:14 +08002843 "Cannot recover all fsync data errno=%d", err);
Chao Yu4b2414d2017-08-08 10:54:31 +08002844 goto free_meta;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002845 }
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002846 } else {
2847 err = recover_fsync_data(sbi, true);
2848
2849 if (!f2fs_readonly(sb) && err > 0) {
2850 err = -EINVAL;
2851 f2fs_msg(sb, KERN_ERR,
2852 "Need to recover fsync data");
Jaegeuk Kimea676732017-10-06 09:14:28 -07002853 goto free_meta;
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002854 }
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002855 }
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07002856skip_recovery:
Jaegeuk Kim315df832015-08-11 12:45:39 -07002857 /* recover_fsync_data() cleared this already */
2858 clear_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002859
2860 /*
2861 * If filesystem is not mounted as read-only then
2862 * do start the gc_thread.
2863 */
Chao Yu6c029932014-11-18 11:16:01 +08002864 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002865 /* After POR, we can run background GC thread.*/
2866 err = start_gc_thread(sbi);
2867 if (err)
Chao Yu4b2414d2017-08-08 10:54:31 +08002868 goto free_meta;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002869 }
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002870 kfree(options);
hujianyangda554e42015-05-21 14:42:53 +08002871
2872 /* recover broken superblock */
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07002873 if (recovery) {
Chao Yu41214b32016-02-22 18:33:20 +08002874 err = f2fs_commit_super(sbi, true);
2875 f2fs_msg(sb, KERN_INFO,
Sheng Yong99e3e852016-05-11 17:08:14 +08002876 "Try to recover %dth superblock, ret: %d",
Chao Yu41214b32016-02-22 18:33:20 +08002877 sbi->valid_super_block ? 1 : 2, err);
hujianyangda554e42015-05-21 14:42:53 +08002878 }
2879
Chao Yubae01ed2017-11-30 19:28:20 +08002880 f2fs_join_shrinker(sbi);
2881
Jaegeuk Kim84b89e52018-02-22 14:09:30 -08002882 f2fs_tuning_parameters(sbi);
2883
Jaegeuk Kim1200abb2017-02-09 13:25:35 -08002884 f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
2885 cur_cp_version(F2FS_CKPT(sbi)));
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08002886 f2fs_update_time(sbi, CP_TIME);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002887 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002888 return 0;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002889
Chao Yu4b2414d2017-08-08 10:54:31 +08002890free_meta:
Jaegeuk Kimea676732017-10-06 09:14:28 -07002891#ifdef CONFIG_QUOTA
2892 if (f2fs_sb_has_quota_ino(sb) && !sb_rdonly(sb))
2893 f2fs_quota_off_umount(sbi->sb);
2894#endif
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07002895 f2fs_sync_inode_meta(sbi);
Chao Yu4b2414d2017-08-08 10:54:31 +08002896 /*
2897 * Some dirty meta pages can be produced by recover_orphan_inodes()
2898 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
2899 * followed by write_checkpoint() through f2fs_write_node_pages(), which
2900 * falls into an infinite loop in sync_meta_pages().
2901 */
2902 truncate_inode_pages_final(META_MAPPING(sbi));
Jaegeuk Kimea676732017-10-06 09:14:28 -07002903#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +08002904free_sysfs:
Jaegeuk Kimea676732017-10-06 09:14:28 -07002905#endif
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002906 f2fs_unregister_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002907free_root_inode:
2908 dput(sb->s_root);
2909 sb->s_root = NULL;
Chao Yubae01ed2017-11-30 19:28:20 +08002910free_stats:
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08002911 f2fs_destroy_stats(sbi);
Chao Yubae01ed2017-11-30 19:28:20 +08002912free_node_inode:
2913 release_ino_entry(sbi, true);
2914 truncate_inode_pages_final(NODE_MAPPING(sbi));
2915 iput(sbi->node_inode);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002916free_nm:
2917 destroy_node_manager(sbi);
2918free_sm:
2919 destroy_segment_manager(sbi);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002920free_devices:
2921 destroy_device_list(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002922 kfree(sbi->ckpt);
2923free_meta_inode:
2924 make_bad_inode(sbi->meta_inode);
2925 iput(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002926free_io_dummy:
2927 mempool_destroy(sbi->write_io_dummy);
Chao Yud7997e62018-01-17 16:31:35 +08002928free_percpu:
2929 destroy_percpu_info(sbi);
2930free_bio_info:
Jaegeuk Kima912b542017-05-10 11:18:25 -07002931 for (i = 0; i < NR_PAGE_TYPE; i++)
2932 kfree(sbi->write_io[i]);
Chao Yud7997e62018-01-17 16:31:35 +08002933free_options:
Chao Yu4b2414d2017-08-08 10:54:31 +08002934#ifdef CONFIG_QUOTA
2935 for (i = 0; i < MAXQUOTAS; i++)
2936 kfree(sbi->s_qf_names[i]);
2937#endif
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002938 kfree(options);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002939free_sb_buf:
Yunlei Heb39f0de2015-12-15 17:17:20 +08002940 kfree(raw_super);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002941free_sbi:
Keith Mok43b65732016-03-02 12:04:24 -08002942 if (sbi->s_chksum_driver)
2943 crypto_free_shash(sbi->s_chksum_driver);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002944 kfree(sbi);
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002945
2946 /* give only one another chance */
2947 if (retry) {
Taehee Yoo9df47ba2015-04-13 21:48:06 +09002948 retry = false;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002949 shrink_dcache_sb(sb);
2950 goto try_onemore;
2951 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002952 return err;
2953}
2954
2955static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
2956 const char *dev_name, void *data)
2957{
2958 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
2959}
2960
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002961static void kill_f2fs_super(struct super_block *sb)
2962{
Chao Yucce13252017-06-29 23:17:45 +08002963 if (sb->s_root) {
Chao Yucaf00472015-01-28 17:48:42 +08002964 set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
Chao Yucce13252017-06-29 23:17:45 +08002965 stop_gc_thread(F2FS_SB(sb));
2966 stop_discard_thread(F2FS_SB(sb));
2967 }
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002968 kill_block_super(sb);
2969}
2970
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002971static struct file_system_type f2fs_fs_type = {
2972 .owner = THIS_MODULE,
2973 .name = "f2fs",
2974 .mount = f2fs_mount,
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002975 .kill_sb = kill_f2fs_super,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002976 .fs_flags = FS_REQUIRES_DEV,
2977};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08002978MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002979
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002980static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002981{
Vladimir Davydov5d097052016-01-14 15:18:21 -08002982 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
2983 sizeof(struct f2fs_inode_info), 0,
2984 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002985 if (!f2fs_inode_cachep)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002986 return -ENOMEM;
2987 return 0;
2988}
2989
2990static void destroy_inodecache(void)
2991{
2992 /*
2993 * Make sure all delayed rcu free inodes are flushed before we
2994 * destroy cache.
2995 */
2996 rcu_barrier();
2997 kmem_cache_destroy(f2fs_inode_cachep);
2998}
2999
3000static int __init init_f2fs_fs(void)
3001{
3002 int err;
3003
Jaegeuk Kimc0508652015-01-07 14:07:36 -08003004 f2fs_build_trace_ios();
3005
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003006 err = init_inodecache();
3007 if (err)
3008 goto fail;
3009 err = create_node_manager_caches();
3010 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08003011 goto free_inodecache;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003012 err = create_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003013 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08003014 goto free_node_manager_caches;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003015 err = create_checkpoint_caches();
3016 if (err)
Chao Yu06292072014-12-29 15:56:18 +08003017 goto free_segment_manager_caches;
Chao Yu1dcc3362015-02-05 17:57:31 +08003018 err = create_extent_cache();
3019 if (err)
3020 goto free_checkpoint_caches;
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07003021 err = f2fs_init_sysfs();
Chao Yua3981012017-06-14 17:39:46 +08003022 if (err)
Chao Yu1dcc3362015-02-05 17:57:31 +08003023 goto free_extent_cache;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07003024 err = register_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimcfc4d972015-05-15 15:37:24 -07003025 if (err)
Chao Yua3981012017-06-14 17:39:46 +08003026 goto free_sysfs;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07003027 err = register_filesystem(&f2fs_fs_type);
3028 if (err)
3029 goto free_shrinker;
Chao Yu787c7b8c2015-10-29 09:13:04 +08003030 err = f2fs_create_root_stats();
3031 if (err)
3032 goto free_filesystem;
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08003033 return 0;
3034
Chao Yu787c7b8c2015-10-29 09:13:04 +08003035free_filesystem:
3036 unregister_filesystem(&f2fs_fs_type);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07003037free_shrinker:
3038 unregister_shrinker(&f2fs_shrinker_info);
Chao Yua3981012017-06-14 17:39:46 +08003039free_sysfs:
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07003040 f2fs_exit_sysfs();
Chao Yu1dcc3362015-02-05 17:57:31 +08003041free_extent_cache:
3042 destroy_extent_cache();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08003043free_checkpoint_caches:
3044 destroy_checkpoint_caches();
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003045free_segment_manager_caches:
3046 destroy_segment_manager_caches();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08003047free_node_manager_caches:
3048 destroy_node_manager_caches();
3049free_inodecache:
3050 destroy_inodecache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003051fail:
3052 return err;
3053}
3054
3055static void __exit exit_f2fs_fs(void)
3056{
Namjae Jeon4589d252013-01-15 19:58:47 +09003057 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003058 unregister_filesystem(&f2fs_fs_type);
Tiezhu Yangb8bef792016-05-18 08:02:25 +08003059 unregister_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07003060 f2fs_exit_sysfs();
Wanpeng Lifdf6c8b2015-03-06 15:00:54 +08003061 destroy_extent_cache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003062 destroy_checkpoint_caches();
Changman Lee5dcd8a72013-12-11 14:32:13 +09003063 destroy_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003064 destroy_node_manager_caches();
3065 destroy_inodecache();
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -08003066 f2fs_destroy_trace_ios();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003067}
3068
3069module_init(init_f2fs_fs)
3070module_exit(exit_f2fs_fs)
3071
3072MODULE_AUTHOR("Samsung Electronics's Praesto Team");
3073MODULE_DESCRIPTION("Flash Friendly File System");
3074MODULE_LICENSE("GPL");
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -07003075