blob: d2833a2325287d68506bf2318e0a875b717e586a [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 Kimaff063e2012-11-02 17:07:47 +0900133 Opt_err,
134};
135
136static match_table_t f2fs_tokens = {
Namjae Jeon696c0182013-06-16 09:48:48 +0900137 {Opt_gc_background, "background_gc=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900138 {Opt_disable_roll_forward, "disable_roll_forward"},
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800139 {Opt_norecovery, "norecovery"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900140 {Opt_discard, "discard"},
Chao Yu64058be2016-07-03 22:05:14 +0800141 {Opt_nodiscard, "nodiscard"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900142 {Opt_noheap, "no_heap"},
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400143 {Opt_heap, "heap"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900144 {Opt_user_xattr, "user_xattr"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900145 {Opt_nouser_xattr, "nouser_xattr"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900146 {Opt_acl, "acl"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900147 {Opt_noacl, "noacl"},
148 {Opt_active_logs, "active_logs=%u"},
149 {Opt_disable_ext_identify, "disable_ext_identify"},
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900150 {Opt_inline_xattr, "inline_xattr"},
Chao Yu23cf7212017-02-15 10:34:45 +0800151 {Opt_noinline_xattr, "noinline_xattr"},
Chao Yu6afc6622017-09-06 21:59:50 +0800152 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
Huajun Li8274de72013-11-10 23:13:17 +0800153 {Opt_inline_data, "inline_data"},
Chao Yu5efd3c62014-09-24 18:16:13 +0800154 {Opt_inline_dentry, "inline_dentry"},
Chao Yu97c17942016-05-09 19:56:34 +0800155 {Opt_noinline_dentry, "noinline_dentry"},
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900156 {Opt_flush_merge, "flush_merge"},
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700157 {Opt_noflush_merge, "noflush_merge"},
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700158 {Opt_nobarrier, "nobarrier"},
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700159 {Opt_fastboot, "fastboot"},
Chao Yu89672152015-02-05 17:55:51 +0800160 {Opt_extent_cache, "extent_cache"},
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700161 {Opt_noextent_cache, "noextent_cache"},
Wanpeng Li75342792015-03-24 10:20:27 +0800162 {Opt_noinline_data, "noinline_data"},
Chao Yu343f40f2015-12-16 13:12:16 +0800163 {Opt_data_flush, "data_flush"},
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800164 {Opt_reserve_root, "reserve_root=%u"},
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800165 {Opt_resgid, "resgid=%u"},
166 {Opt_resuid, "resuid=%u"},
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700167 {Opt_mode, "mode=%s"},
Jaegeuk Kimec915382016-12-21 17:09:19 -0800168 {Opt_io_size_bits, "io_bits=%u"},
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700169 {Opt_fault_injection, "fault_injection=%u"},
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700170 {Opt_lazytime, "lazytime"},
171 {Opt_nolazytime, "nolazytime"},
Chao Yu4b2414d2017-08-08 10:54:31 +0800172 {Opt_quota, "quota"},
173 {Opt_noquota, "noquota"},
Chao Yu0abd6752017-07-09 00:13:07 +0800174 {Opt_usrquota, "usrquota"},
175 {Opt_grpquota, "grpquota"},
Chao Yu5c571322017-07-26 00:01:41 +0800176 {Opt_prjquota, "prjquota"},
Chao Yu4b2414d2017-08-08 10:54:31 +0800177 {Opt_usrjquota, "usrjquota=%s"},
178 {Opt_grpjquota, "grpjquota=%s"},
179 {Opt_prjjquota, "prjjquota=%s"},
180 {Opt_offusrjquota, "usrjquota="},
181 {Opt_offgrpjquota, "grpjquota="},
182 {Opt_offprjjquota, "prjjquota="},
183 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
184 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
185 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900186 {Opt_whint, "whint_mode=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900187 {Opt_err, NULL},
188};
189
Namjae Jeona07ef782012-12-30 14:52:05 +0900190void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
191{
192 struct va_format vaf;
193 va_list args;
194
195 va_start(args, fmt);
196 vaf.fmt = fmt;
197 vaf.va = &args;
Jaegeuk Kima36c1062017-08-02 20:58:29 -0700198 printk_ratelimited("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
Namjae Jeona07ef782012-12-30 14:52:05 +0900199 va_end(args);
200}
201
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800202static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
203{
204 block_t limit = (sbi->user_block_count << 1) / 1000;
205
206 /* limit is 0.2% */
207 if (test_opt(sbi, RESERVE_ROOT) && sbi->root_reserved_blocks > limit) {
208 sbi->root_reserved_blocks = limit;
209 f2fs_msg(sbi->sb, KERN_INFO,
210 "Reduce reserved blocks for root = %u",
211 sbi->root_reserved_blocks);
212 }
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800213 if (!test_opt(sbi, RESERVE_ROOT) &&
214 (!uid_eq(sbi->s_resuid,
215 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
216 !gid_eq(sbi->s_resgid,
217 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
218 f2fs_msg(sbi->sb, KERN_INFO,
219 "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
220 from_kuid_munged(&init_user_ns, sbi->s_resuid),
221 from_kgid_munged(&init_user_ns, sbi->s_resgid));
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800222}
223
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900224static void init_once(void *foo)
225{
226 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
227
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900228 inode_init_once(&fi->vfs_inode);
229}
230
Chao Yu4b2414d2017-08-08 10:54:31 +0800231#ifdef CONFIG_QUOTA
232static const char * const quotatypes[] = INITQFNAMES;
233#define QTYPE2NAME(t) (quotatypes[t])
234static int f2fs_set_qf_name(struct super_block *sb, int qtype,
235 substring_t *args)
236{
237 struct f2fs_sb_info *sbi = F2FS_SB(sb);
238 char *qname;
239 int ret = -EINVAL;
240
241 if (sb_any_quota_loaded(sb) && !sbi->s_qf_names[qtype]) {
242 f2fs_msg(sb, KERN_ERR,
243 "Cannot change journaled "
244 "quota options when quota turned on");
245 return -EINVAL;
246 }
Jaegeuk Kimea676732017-10-06 09:14:28 -0700247 if (f2fs_sb_has_quota_ino(sb)) {
248 f2fs_msg(sb, KERN_INFO,
249 "QUOTA feature is enabled, so ignore qf_name");
250 return 0;
251 }
252
Chao Yu4b2414d2017-08-08 10:54:31 +0800253 qname = match_strdup(args);
254 if (!qname) {
255 f2fs_msg(sb, KERN_ERR,
256 "Not enough memory for storing quotafile name");
257 return -EINVAL;
258 }
259 if (sbi->s_qf_names[qtype]) {
260 if (strcmp(sbi->s_qf_names[qtype], qname) == 0)
261 ret = 0;
262 else
263 f2fs_msg(sb, KERN_ERR,
264 "%s quota file already specified",
265 QTYPE2NAME(qtype));
266 goto errout;
267 }
268 if (strchr(qname, '/')) {
269 f2fs_msg(sb, KERN_ERR,
270 "quotafile must be on filesystem root");
271 goto errout;
272 }
273 sbi->s_qf_names[qtype] = qname;
274 set_opt(sbi, QUOTA);
275 return 0;
276errout:
277 kfree(qname);
278 return ret;
279}
280
281static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
282{
283 struct f2fs_sb_info *sbi = F2FS_SB(sb);
284
285 if (sb_any_quota_loaded(sb) && sbi->s_qf_names[qtype]) {
286 f2fs_msg(sb, KERN_ERR, "Cannot change journaled quota options"
287 " when quota turned on");
288 return -EINVAL;
289 }
290 kfree(sbi->s_qf_names[qtype]);
291 sbi->s_qf_names[qtype] = NULL;
292 return 0;
293}
294
295static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
296{
297 /*
298 * We do the test below only for project quotas. 'usrquota' and
299 * 'grpquota' mount options are allowed even without quota feature
300 * to support legacy quotas in quota files.
301 */
302 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi->sb)) {
303 f2fs_msg(sbi->sb, KERN_ERR, "Project quota feature not enabled. "
304 "Cannot enable project quota enforcement.");
305 return -1;
306 }
307 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA] ||
308 sbi->s_qf_names[PRJQUOTA]) {
309 if (test_opt(sbi, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
310 clear_opt(sbi, USRQUOTA);
311
312 if (test_opt(sbi, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
313 clear_opt(sbi, GRPQUOTA);
314
315 if (test_opt(sbi, PRJQUOTA) && sbi->s_qf_names[PRJQUOTA])
316 clear_opt(sbi, PRJQUOTA);
317
318 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
319 test_opt(sbi, PRJQUOTA)) {
320 f2fs_msg(sbi->sb, KERN_ERR, "old and new quota "
321 "format mixing");
322 return -1;
323 }
324
325 if (!sbi->s_jquota_fmt) {
326 f2fs_msg(sbi->sb, KERN_ERR, "journaled quota format "
327 "not specified");
328 return -1;
329 }
330 }
Jaegeuk Kimea676732017-10-06 09:14:28 -0700331
332 if (f2fs_sb_has_quota_ino(sbi->sb) && sbi->s_jquota_fmt) {
333 f2fs_msg(sbi->sb, KERN_INFO,
334 "QUOTA feature is enabled, so ignore jquota_fmt");
335 sbi->s_jquota_fmt = 0;
336 }
337 if (f2fs_sb_has_quota_ino(sbi->sb) && sb_rdonly(sbi->sb)) {
338 f2fs_msg(sbi->sb, KERN_INFO,
339 "Filesystem with quota feature cannot be mounted RDWR "
340 "without CONFIG_QUOTA");
341 return -1;
342 }
Chao Yu4b2414d2017-08-08 10:54:31 +0800343 return 0;
344}
345#endif
346
Namjae Jeon696c0182013-06-16 09:48:48 +0900347static int parse_options(struct super_block *sb, char *options)
348{
349 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yu09d54cd2015-06-08 13:20:10 +0800350 struct request_queue *q;
Namjae Jeon696c0182013-06-16 09:48:48 +0900351 substring_t args[MAX_OPT_ARGS];
352 char *p, *name;
353 int arg = 0;
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800354 kuid_t uid;
355 kgid_t gid;
Chao Yu4b2414d2017-08-08 10:54:31 +0800356#ifdef CONFIG_QUOTA
357 int ret;
358#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900359
360 if (!options)
361 return 0;
362
363 while ((p = strsep(&options, ",")) != NULL) {
364 int token;
365 if (!*p)
366 continue;
367 /*
368 * Initialize args struct so we know whether arg was
369 * found; some options take optional arguments.
370 */
371 args[0].to = args[0].from = NULL;
372 token = match_token(p, f2fs_tokens, args);
373
374 switch (token) {
375 case Opt_gc_background:
376 name = match_strdup(&args[0]);
377
378 if (!name)
379 return -ENOMEM;
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700380 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900381 set_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700382 clear_opt(sbi, FORCE_FG_GC);
383 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900384 clear_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700385 clear_opt(sbi, FORCE_FG_GC);
386 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
387 set_opt(sbi, BG_GC);
388 set_opt(sbi, FORCE_FG_GC);
389 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +0900390 kfree(name);
391 return -EINVAL;
392 }
393 kfree(name);
394 break;
395 case Opt_disable_roll_forward:
396 set_opt(sbi, DISABLE_ROLL_FORWARD);
397 break;
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800398 case Opt_norecovery:
399 /* this option mounts f2fs with ro */
400 set_opt(sbi, DISABLE_ROLL_FORWARD);
401 if (!f2fs_readonly(sb))
402 return -EINVAL;
403 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900404 case Opt_discard:
Chao Yu09d54cd2015-06-08 13:20:10 +0800405 q = bdev_get_queue(sb->s_bdev);
406 if (blk_queue_discard(q)) {
407 set_opt(sbi, DISCARD);
Sheng Yongccd31cb2018-02-06 12:31:17 +0800408 } else if (!f2fs_sb_has_blkzoned(sb)) {
Chao Yu09d54cd2015-06-08 13:20:10 +0800409 f2fs_msg(sb, KERN_WARNING,
410 "mounting with \"discard\" option, but "
411 "the device does not support discard");
412 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900413 break;
Chao Yu64058be2016-07-03 22:05:14 +0800414 case Opt_nodiscard:
Sheng Yongccd31cb2018-02-06 12:31:17 +0800415 if (f2fs_sb_has_blkzoned(sb)) {
Damien Le Moal96ba2de2016-10-28 17:45:03 +0900416 f2fs_msg(sb, KERN_WARNING,
417 "discard is required for zoned block devices");
418 return -EINVAL;
419 }
Chao Yu64058be2016-07-03 22:05:14 +0800420 clear_opt(sbi, DISCARD);
Damien Le Moal487df612016-10-28 17:44:59 +0900421 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900422 case Opt_noheap:
423 set_opt(sbi, NOHEAP);
424 break;
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400425 case Opt_heap:
426 clear_opt(sbi, NOHEAP);
427 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900428#ifdef CONFIG_F2FS_FS_XATTR
Kelly Anderson4058c512013-10-07 11:36:20 +0900429 case Opt_user_xattr:
430 set_opt(sbi, XATTR_USER);
431 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900432 case Opt_nouser_xattr:
433 clear_opt(sbi, XATTR_USER);
434 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900435 case Opt_inline_xattr:
436 set_opt(sbi, INLINE_XATTR);
437 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800438 case Opt_noinline_xattr:
439 clear_opt(sbi, INLINE_XATTR);
440 break;
Chao Yu6afc6622017-09-06 21:59:50 +0800441 case Opt_inline_xattr_size:
442 if (args->from && match_int(args, &arg))
443 return -EINVAL;
444 set_opt(sbi, INLINE_XATTR_SIZE);
445 sbi->inline_xattr_size = arg;
446 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900447#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900448 case Opt_user_xattr:
449 f2fs_msg(sb, KERN_INFO,
450 "user_xattr options not supported");
451 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900452 case Opt_nouser_xattr:
453 f2fs_msg(sb, KERN_INFO,
454 "nouser_xattr options not supported");
455 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900456 case Opt_inline_xattr:
457 f2fs_msg(sb, KERN_INFO,
458 "inline_xattr options not supported");
459 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800460 case Opt_noinline_xattr:
461 f2fs_msg(sb, KERN_INFO,
462 "noinline_xattr options not supported");
463 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900464#endif
465#ifdef CONFIG_F2FS_FS_POSIX_ACL
Kelly Anderson4058c512013-10-07 11:36:20 +0900466 case Opt_acl:
467 set_opt(sbi, POSIX_ACL);
468 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900469 case Opt_noacl:
470 clear_opt(sbi, POSIX_ACL);
471 break;
472#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900473 case Opt_acl:
474 f2fs_msg(sb, KERN_INFO, "acl options not supported");
475 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900476 case Opt_noacl:
477 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
478 break;
479#endif
480 case Opt_active_logs:
481 if (args->from && match_int(args, &arg))
482 return -EINVAL;
483 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
484 return -EINVAL;
485 sbi->active_logs = arg;
486 break;
487 case Opt_disable_ext_identify:
488 set_opt(sbi, DISABLE_EXT_IDENTIFY);
489 break;
Huajun Li8274de72013-11-10 23:13:17 +0800490 case Opt_inline_data:
491 set_opt(sbi, INLINE_DATA);
492 break;
Chao Yu5efd3c62014-09-24 18:16:13 +0800493 case Opt_inline_dentry:
494 set_opt(sbi, INLINE_DENTRY);
495 break;
Chao Yu97c17942016-05-09 19:56:34 +0800496 case Opt_noinline_dentry:
497 clear_opt(sbi, INLINE_DENTRY);
498 break;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900499 case Opt_flush_merge:
500 set_opt(sbi, FLUSH_MERGE);
501 break;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700502 case Opt_noflush_merge:
503 clear_opt(sbi, FLUSH_MERGE);
504 break;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700505 case Opt_nobarrier:
506 set_opt(sbi, NOBARRIER);
507 break;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700508 case Opt_fastboot:
509 set_opt(sbi, FASTBOOT);
510 break;
Chao Yu89672152015-02-05 17:55:51 +0800511 case Opt_extent_cache:
512 set_opt(sbi, EXTENT_CACHE);
513 break;
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700514 case Opt_noextent_cache:
515 clear_opt(sbi, EXTENT_CACHE);
516 break;
Wanpeng Li75342792015-03-24 10:20:27 +0800517 case Opt_noinline_data:
518 clear_opt(sbi, INLINE_DATA);
519 break;
Chao Yu343f40f2015-12-16 13:12:16 +0800520 case Opt_data_flush:
521 set_opt(sbi, DATA_FLUSH);
522 break;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800523 case Opt_reserve_root:
524 if (args->from && match_int(args, &arg))
525 return -EINVAL;
526 if (test_opt(sbi, RESERVE_ROOT)) {
527 f2fs_msg(sb, KERN_INFO,
528 "Preserve previous reserve_root=%u",
529 sbi->root_reserved_blocks);
530 } else {
531 sbi->root_reserved_blocks = arg;
532 set_opt(sbi, RESERVE_ROOT);
533 }
534 break;
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800535 case Opt_resuid:
536 if (args->from && match_int(args, &arg))
537 return -EINVAL;
538 uid = make_kuid(current_user_ns(), arg);
539 if (!uid_valid(uid)) {
540 f2fs_msg(sb, KERN_ERR,
541 "Invalid uid value %d", arg);
542 return -EINVAL;
543 }
544 sbi->s_resuid = uid;
545 break;
546 case Opt_resgid:
547 if (args->from && match_int(args, &arg))
548 return -EINVAL;
549 gid = make_kgid(current_user_ns(), arg);
550 if (!gid_valid(gid)) {
551 f2fs_msg(sb, KERN_ERR,
552 "Invalid gid value %d", arg);
553 return -EINVAL;
554 }
555 sbi->s_resgid = gid;
556 break;
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700557 case Opt_mode:
558 name = match_strdup(&args[0]);
559
560 if (!name)
561 return -ENOMEM;
562 if (strlen(name) == 8 &&
563 !strncmp(name, "adaptive", 8)) {
Sheng Yongccd31cb2018-02-06 12:31:17 +0800564 if (f2fs_sb_has_blkzoned(sb)) {
Damien Le Moal3adc57e2016-10-28 17:45:04 +0900565 f2fs_msg(sb, KERN_WARNING,
566 "adaptive mode is not allowed with "
567 "zoned block device feature");
568 kfree(name);
569 return -EINVAL;
570 }
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700571 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700572 } else if (strlen(name) == 3 &&
573 !strncmp(name, "lfs", 3)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700574 set_opt_mode(sbi, F2FS_MOUNT_LFS);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700575 } else {
576 kfree(name);
577 return -EINVAL;
578 }
579 kfree(name);
580 break;
Jaegeuk Kimec915382016-12-21 17:09:19 -0800581 case Opt_io_size_bits:
582 if (args->from && match_int(args, &arg))
583 return -EINVAL;
584 if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
585 f2fs_msg(sb, KERN_WARNING,
586 "Not support %d, larger than %d",
587 1 << arg, BIO_MAX_PAGES);
588 return -EINVAL;
589 }
590 sbi->write_io_size_bits = arg;
591 break;
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700592 case Opt_fault_injection:
593 if (args->from && match_int(args, &arg))
594 return -EINVAL;
595#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu1ecc0c52016-09-23 21:30:09 +0800596 f2fs_build_fault_attr(sbi, arg);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800597 set_opt(sbi, FAULT_INJECTION);
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700598#else
599 f2fs_msg(sb, KERN_INFO,
600 "FAULT_INJECTION was not selected");
601#endif
602 break;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700603 case Opt_lazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800604 sb->s_flags |= SB_LAZYTIME;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700605 break;
606 case Opt_nolazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800607 sb->s_flags &= ~SB_LAZYTIME;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700608 break;
Chao Yu0abd6752017-07-09 00:13:07 +0800609#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +0800610 case Opt_quota:
Chao Yu0abd6752017-07-09 00:13:07 +0800611 case Opt_usrquota:
612 set_opt(sbi, USRQUOTA);
613 break;
614 case Opt_grpquota:
615 set_opt(sbi, GRPQUOTA);
616 break;
Chao Yu5c571322017-07-26 00:01:41 +0800617 case Opt_prjquota:
618 set_opt(sbi, PRJQUOTA);
619 break;
Chao Yu4b2414d2017-08-08 10:54:31 +0800620 case Opt_usrjquota:
621 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
622 if (ret)
623 return ret;
624 break;
625 case Opt_grpjquota:
626 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
627 if (ret)
628 return ret;
629 break;
630 case Opt_prjjquota:
631 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
632 if (ret)
633 return ret;
634 break;
635 case Opt_offusrjquota:
636 ret = f2fs_clear_qf_name(sb, USRQUOTA);
637 if (ret)
638 return ret;
639 break;
640 case Opt_offgrpjquota:
641 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
642 if (ret)
643 return ret;
644 break;
645 case Opt_offprjjquota:
646 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
647 if (ret)
648 return ret;
649 break;
650 case Opt_jqfmt_vfsold:
651 sbi->s_jquota_fmt = QFMT_VFS_OLD;
652 break;
653 case Opt_jqfmt_vfsv0:
654 sbi->s_jquota_fmt = QFMT_VFS_V0;
655 break;
656 case Opt_jqfmt_vfsv1:
657 sbi->s_jquota_fmt = QFMT_VFS_V1;
658 break;
659 case Opt_noquota:
660 clear_opt(sbi, QUOTA);
661 clear_opt(sbi, USRQUOTA);
662 clear_opt(sbi, GRPQUOTA);
663 clear_opt(sbi, PRJQUOTA);
664 break;
Chao Yu0abd6752017-07-09 00:13:07 +0800665#else
Chao Yu4b2414d2017-08-08 10:54:31 +0800666 case Opt_quota:
Chao Yu0abd6752017-07-09 00:13:07 +0800667 case Opt_usrquota:
668 case Opt_grpquota:
Chao Yu5c571322017-07-26 00:01:41 +0800669 case Opt_prjquota:
Chao Yu4b2414d2017-08-08 10:54:31 +0800670 case Opt_usrjquota:
671 case Opt_grpjquota:
672 case Opt_prjjquota:
673 case Opt_offusrjquota:
674 case Opt_offgrpjquota:
675 case Opt_offprjjquota:
676 case Opt_jqfmt_vfsold:
677 case Opt_jqfmt_vfsv0:
678 case Opt_jqfmt_vfsv1:
679 case Opt_noquota:
Chao Yu0abd6752017-07-09 00:13:07 +0800680 f2fs_msg(sb, KERN_INFO,
681 "quota operations not supported");
682 break;
683#endif
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900684 case Opt_whint:
685 name = match_strdup(&args[0]);
686 if (!name)
687 return -ENOMEM;
688 if (strlen(name) == 10 &&
689 !strncmp(name, "user-based", 10)) {
690 sbi->whint_mode = WHINT_MODE_USER;
691 } else if (strlen(name) == 3 &&
692 !strncmp(name, "off", 3)) {
693 sbi->whint_mode = WHINT_MODE_OFF;
Hyunchul Leef2e703f2018-01-31 11:36:58 +0900694 } else if (strlen(name) == 8 &&
695 !strncmp(name, "fs-based", 8)) {
696 sbi->whint_mode = WHINT_MODE_FS;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900697 } else {
698 kfree(name);
699 return -EINVAL;
700 }
701 kfree(name);
702 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900703 default:
704 f2fs_msg(sb, KERN_ERR,
705 "Unrecognized mount option \"%s\" or missing value",
706 p);
707 return -EINVAL;
708 }
709 }
Chao Yu4b2414d2017-08-08 10:54:31 +0800710#ifdef CONFIG_QUOTA
711 if (f2fs_check_quota_options(sbi))
712 return -EINVAL;
713#endif
Jaegeuk Kimec915382016-12-21 17:09:19 -0800714
715 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
716 f2fs_msg(sb, KERN_ERR,
717 "Should set mode=lfs with %uKB-sized IO",
718 F2FS_IO_SIZE_KB(sbi));
719 return -EINVAL;
720 }
Chao Yu6afc6622017-09-06 21:59:50 +0800721
722 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
Chao Yu4d817ae2018-01-27 17:29:48 +0800723 if (!f2fs_sb_has_extra_attr(sb) ||
724 !f2fs_sb_has_flexible_inline_xattr(sb)) {
725 f2fs_msg(sb, KERN_ERR,
726 "extra_attr or flexible_inline_xattr "
727 "feature is off");
728 return -EINVAL;
729 }
Chao Yu6afc6622017-09-06 21:59:50 +0800730 if (!test_opt(sbi, INLINE_XATTR)) {
731 f2fs_msg(sb, KERN_ERR,
732 "inline_xattr_size option should be "
733 "set with inline_xattr option");
734 return -EINVAL;
735 }
736 if (!sbi->inline_xattr_size ||
737 sbi->inline_xattr_size >= DEF_ADDRS_PER_INODE -
738 F2FS_TOTAL_EXTRA_ATTR_SIZE -
739 DEF_INLINE_RESERVED_SIZE -
740 DEF_MIN_INLINE_SIZE) {
741 f2fs_msg(sb, KERN_ERR,
742 "inline xattr size is out of range");
743 return -EINVAL;
744 }
745 }
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900746
747 /* Not pass down write hints if the number of active logs is lesser
748 * than NR_CURSEG_TYPE.
749 */
750 if (sbi->active_logs != NR_CURSEG_TYPE)
751 sbi->whint_mode = WHINT_MODE_OFF;
Namjae Jeon696c0182013-06-16 09:48:48 +0900752 return 0;
753}
754
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900755static struct inode *f2fs_alloc_inode(struct super_block *sb)
756{
757 struct f2fs_inode_info *fi;
758
Chao Yua0acdfe2013-12-05 09:54:00 +0800759 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900760 if (!fi)
761 return NULL;
762
763 init_once((void *) fi);
764
Masanari Iida434720f2013-03-19 08:03:35 +0900765 /* Initialize f2fs-specific inode info */
Jaegeuk Kim204706c2016-12-02 15:11:32 -0800766 atomic_set(&fi->dirty_pages, 0);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900767 fi->i_current_depth = 1;
768 fi->i_advise = 0;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900769 init_rwsem(&fi->i_sem);
Chao Yu2710fd72015-12-15 13:30:45 +0800770 INIT_LIST_HEAD(&fi->dirty_list);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700771 INIT_LIST_HEAD(&fi->gdirty_list);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700772 INIT_LIST_HEAD(&fi->inmem_ilist);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700773 INIT_LIST_HEAD(&fi->inmem_pages);
774 mutex_init(&fi->inmem_lock);
Chao Yu82e0a5a2016-07-13 09:18:29 +0800775 init_rwsem(&fi->dio_rwsem[READ]);
776 init_rwsem(&fi->dio_rwsem[WRITE]);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800777 init_rwsem(&fi->i_mmap_sem);
Yunlei He27161f12017-09-07 10:40:54 +0800778 init_rwsem(&fi->i_xattr_sem);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900779
Chao Yu0abd6752017-07-09 00:13:07 +0800780#ifdef CONFIG_QUOTA
781 memset(&fi->i_dquot, 0, sizeof(fi->i_dquot));
782 fi->i_reserved_quota = 0;
783#endif
Jaegeuk Kimab9fa662014-02-27 20:09:05 +0900784 /* Will be used by directory only */
785 fi->i_dir_level = F2FS_SB(sb)->dir_level;
Chao Yuf2470372017-07-19 00:19:05 +0800786
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900787 return &fi->vfs_inode;
788}
789
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900790static int f2fs_drop_inode(struct inode *inode)
791{
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000792 int ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900793 /*
794 * This is to avoid a deadlock condition like below.
795 * writeback_single_inode(inode)
796 * - f2fs_write_data_page
797 * - f2fs_gc -> iput -> evict
798 * - inode_wait_for_writeback(inode)
799 */
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700800 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700801 if (!inode->i_nlink && !is_bad_inode(inode)) {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700802 /* to avoid evict_inode call simultaneously */
803 atomic_inc(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700804 spin_unlock(&inode->i_lock);
805
806 /* some remained atomic pages should discarded */
807 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +0800808 drop_inmem_pages(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700809
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700810 /* should remain fi->extent_tree for writepage */
811 f2fs_destroy_extent_node(inode);
812
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700813 sb_start_intwrite(inode->i_sb);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700814 f2fs_i_size_write(inode, 0);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700815
816 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700817 f2fs_truncate(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700818
819 sb_end_intwrite(inode->i_sb);
820
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700821 spin_lock(&inode->i_lock);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700822 atomic_dec(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700823 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000824 trace_f2fs_drop_inode(inode, 0);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900825 return 0;
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700826 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000827 ret = generic_drop_inode(inode);
828 trace_f2fs_drop_inode(inode, ret);
829 return ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900830}
831
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700832int f2fs_inode_dirtied(struct inode *inode, bool sync)
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700833{
834 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700835 int ret = 0;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700836
837 spin_lock(&sbi->inode_lock[DIRTY_META]);
838 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700839 ret = 1;
840 } else {
841 set_inode_flag(inode, FI_DIRTY_INODE);
842 stat_inc_dirty_inode(sbi, DIRTY_META);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700843 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700844 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
845 list_add_tail(&F2FS_I(inode)->gdirty_list,
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700846 &sbi->inode_list[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700847 inc_page_count(sbi, F2FS_DIRTY_IMETA);
848 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700849 spin_unlock(&sbi->inode_lock[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700850 return ret;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700851}
852
853void f2fs_inode_synced(struct inode *inode)
854{
855 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
856
857 spin_lock(&sbi->inode_lock[DIRTY_META]);
858 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
859 spin_unlock(&sbi->inode_lock[DIRTY_META]);
860 return;
861 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700862 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
863 list_del_init(&F2FS_I(inode)->gdirty_list);
864 dec_page_count(sbi, F2FS_DIRTY_IMETA);
865 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700866 clear_inode_flag(inode, FI_DIRTY_INODE);
867 clear_inode_flag(inode, FI_AUTO_RECOVER);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700868 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
869 spin_unlock(&sbi->inode_lock[DIRTY_META]);
870}
871
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900872/*
873 * f2fs_dirty_inode() is called from __mark_inode_dirty()
874 *
875 * We should call set_dirty_inode to write the dirty inode through write_inode.
876 */
877static void f2fs_dirty_inode(struct inode *inode, int flags)
878{
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700879 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
880
881 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
882 inode->i_ino == F2FS_META_INO(sbi))
883 return;
884
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700885 if (flags == I_DIRTY_TIME)
886 return;
887
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700888 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
889 clear_inode_flag(inode, FI_AUTO_RECOVER);
890
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700891 f2fs_inode_dirtied(inode, false);
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900892}
893
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900894static void f2fs_i_callback(struct rcu_head *head)
895{
896 struct inode *inode = container_of(head, struct inode, i_rcu);
897 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
898}
899
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900900static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900901{
902 call_rcu(&inode->i_rcu, f2fs_i_callback);
903}
904
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700905static void destroy_percpu_info(struct f2fs_sb_info *sbi)
906{
Jaegeuk Kim41382ec2016-05-16 11:06:50 -0700907 percpu_counter_destroy(&sbi->alloc_valid_block_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -0700908 percpu_counter_destroy(&sbi->total_valid_inode_count);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700909}
910
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700911static void destroy_device_list(struct f2fs_sb_info *sbi)
912{
913 int i;
914
915 for (i = 0; i < sbi->s_ndevs; i++) {
916 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
917#ifdef CONFIG_BLK_DEV_ZONED
918 kfree(FDEV(i).blkz_type);
919#endif
920 }
921 kfree(sbi->devs);
922}
923
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900924static void f2fs_put_super(struct super_block *sb)
925{
926 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yua3981012017-06-14 17:39:46 +0800927 int i;
Chao Yucf5c7592017-10-04 09:08:37 +0800928 bool dropped;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900929
Chao Yu0abd6752017-07-09 00:13:07 +0800930 f2fs_quota_off_umount(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900931
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700932 /* prevent remaining shrinker jobs */
933 mutex_lock(&sbi->umount_mutex);
934
Jaegeuk Kim85dc2f22015-01-14 17:41:41 -0800935 /*
936 * We don't need to do checkpoint when superblock is clean.
937 * But, the previous checkpoint was not done by umount, it needs to do
938 * clean checkpoint again.
939 */
Chao Yucaf00472015-01-28 17:48:42 +0800940 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
Chao Yuaaec2b12016-09-20 11:04:18 +0800941 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700942 struct cp_control cpc = {
943 .reason = CP_UMOUNT,
944 };
945 write_checkpoint(sbi, &cpc);
946 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900947
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -0800948 /* be sure to wait for any on-going discard commands */
Chao Yucf5c7592017-10-04 09:08:37 +0800949 dropped = f2fs_wait_discard_bios(sbi);
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -0800950
Chao Yucf5c7592017-10-04 09:08:37 +0800951 if (f2fs_discard_en(sbi) && !sbi->discard_blks && !dropped) {
Chao Yu1f43e2a2017-04-28 13:56:08 +0800952 struct cp_control cpc = {
953 .reason = CP_UMOUNT | CP_TRIMMED,
954 };
955 write_checkpoint(sbi, &cpc);
956 }
957
Jaegeuk Kimeca616f2015-06-15 14:52:29 -0700958 /* write_checkpoint can update stat informaion */
959 f2fs_destroy_stats(sbi);
960
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700961 /*
962 * normally superblock is clean, so we need to release this.
963 * In addition, EIO will skip do checkpoint, we need this as well.
964 */
Jaegeuk Kim74ef9242016-05-02 22:09:56 -0700965 release_ino_entry(sbi, true);
Jaegeuk Kim6f12ac22014-08-19 09:48:22 -0700966
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700967 f2fs_leave_shrinker(sbi);
968 mutex_unlock(&sbi->umount_mutex);
969
Jaegeuk Kim17c19122016-01-29 08:57:59 -0800970 /* our cp_error case, we can wait for any writeback page */
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700971 f2fs_flush_merged_writes(sbi);
Jaegeuk Kim17c19122016-01-29 08:57:59 -0800972
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900973 iput(sbi->node_inode);
974 iput(sbi->meta_inode);
975
976 /* destroy f2fs internal modules */
977 destroy_node_manager(sbi);
978 destroy_segment_manager(sbi);
979
980 kfree(sbi->ckpt);
Chao Yua3981012017-06-14 17:39:46 +0800981
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700982 f2fs_unregister_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900983
984 sb->s_fs_info = NULL;
Keith Mok43b65732016-03-02 12:04:24 -0800985 if (sbi->s_chksum_driver)
986 crypto_free_shash(sbi->s_chksum_driver);
Yunlei Heb39f0de2015-12-15 17:17:20 +0800987 kfree(sbi->raw_super);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700988
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700989 destroy_device_list(sbi);
Chao Yub6895e82017-02-27 18:43:12 +0800990 mempool_destroy(sbi->write_io_dummy);
Chao Yu4b2414d2017-08-08 10:54:31 +0800991#ifdef CONFIG_QUOTA
992 for (i = 0; i < MAXQUOTAS; i++)
993 kfree(sbi->s_qf_names[i]);
994#endif
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700995 destroy_percpu_info(sbi);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700996 for (i = 0; i < NR_PAGE_TYPE; i++)
997 kfree(sbi->write_io[i]);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900998 kfree(sbi);
999}
1000
1001int f2fs_sync_fs(struct super_block *sb, int sync)
1002{
1003 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yuc34f42e2015-12-23 17:50:30 +08001004 int err = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001005
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001006 if (unlikely(f2fs_cp_error(sbi)))
1007 return 0;
1008
Namjae Jeona2a4a7e2013-04-20 01:28:40 +09001009 trace_f2fs_sync_fs(sb, sync);
1010
Chao Yu4b2414d2017-08-08 10:54:31 +08001011 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1012 return -EAGAIN;
1013
Jaegeuk Kimb7473752013-04-01 08:32:21 +09001014 if (sync) {
Jaegeuk Kimd5053a342014-10-30 22:47:03 -07001015 struct cp_control cpc;
1016
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001017 cpc.reason = __get_cp_reason(sbi);
1018
Jaegeuk Kimb7473752013-04-01 08:32:21 +09001019 mutex_lock(&sbi->gc_mutex);
Chao Yuc34f42e2015-12-23 17:50:30 +08001020 err = write_checkpoint(sbi, &cpc);
Jaegeuk Kimb7473752013-04-01 08:32:21 +09001021 mutex_unlock(&sbi->gc_mutex);
Jaegeuk Kimb7473752013-04-01 08:32:21 +09001022 }
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001023 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001024
Chao Yuc34f42e2015-12-23 17:50:30 +08001025 return err;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001026}
1027
Changman Leed6212a52013-01-29 18:30:07 +09001028static int f2fs_freeze(struct super_block *sb)
1029{
Jaegeuk Kim77888c12013-05-20 20:28:47 +09001030 if (f2fs_readonly(sb))
Changman Leed6212a52013-01-29 18:30:07 +09001031 return 0;
1032
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -07001033 /* IO error happened before */
1034 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1035 return -EIO;
1036
1037 /* must be clean, since sync_filesystem() was already called */
1038 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1039 return -EINVAL;
1040 return 0;
Changman Leed6212a52013-01-29 18:30:07 +09001041}
1042
1043static int f2fs_unfreeze(struct super_block *sb)
1044{
1045 return 0;
1046}
1047
Chao Yuddc34e32017-07-29 00:32:53 +08001048#ifdef CONFIG_QUOTA
1049static int f2fs_statfs_project(struct super_block *sb,
1050 kprojid_t projid, struct kstatfs *buf)
1051{
1052 struct kqid qid;
1053 struct dquot *dquot;
1054 u64 limit;
1055 u64 curblock;
1056
1057 qid = make_kqid_projid(projid);
1058 dquot = dqget(sb, qid);
1059 if (IS_ERR(dquot))
1060 return PTR_ERR(dquot);
1061 spin_lock(&dq_data_lock);
1062
1063 limit = (dquot->dq_dqb.dqb_bsoftlimit ?
1064 dquot->dq_dqb.dqb_bsoftlimit :
1065 dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
1066 if (limit && buf->f_blocks > limit) {
1067 curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits;
1068 buf->f_blocks = limit;
1069 buf->f_bfree = buf->f_bavail =
1070 (buf->f_blocks > curblock) ?
1071 (buf->f_blocks - curblock) : 0;
1072 }
1073
1074 limit = dquot->dq_dqb.dqb_isoftlimit ?
1075 dquot->dq_dqb.dqb_isoftlimit :
1076 dquot->dq_dqb.dqb_ihardlimit;
1077 if (limit && buf->f_files > limit) {
1078 buf->f_files = limit;
1079 buf->f_ffree =
1080 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1081 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1082 }
1083
1084 spin_unlock(&dq_data_lock);
1085 dqput(dquot);
1086 return 0;
1087}
1088#endif
1089
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001090static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1091{
1092 struct super_block *sb = dentry->d_sb;
1093 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1094 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Jaegeuk Kimf66c0272018-01-03 10:55:07 -08001095 block_t total_count, user_block_count, start_count;
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -07001096 u64 avail_node_count;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001097
1098 total_count = le64_to_cpu(sbi->raw_super->block_count);
1099 user_block_count = sbi->user_block_count;
1100 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001101 buf->f_type = F2FS_SUPER_MAGIC;
1102 buf->f_bsize = sbi->blocksize;
1103
1104 buf->f_blocks = total_count - start_count;
Jaegeuk Kimf66c0272018-01-03 10:55:07 -08001105 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
Yunlong Song80d42142017-10-27 20:45:05 +08001106 sbi->current_reserved_blocks;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08001107 if (buf->f_bfree > sbi->root_reserved_blocks)
1108 buf->f_bavail = buf->f_bfree - sbi->root_reserved_blocks;
1109 else
1110 buf->f_bavail = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001111
Chao Yu292c196a2017-11-16 16:59:14 +08001112 avail_node_count = sbi->total_node_count - sbi->nquota_files -
1113 F2FS_RESERVED_NODE_NUM;
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -07001114
1115 if (avail_node_count > user_block_count) {
1116 buf->f_files = user_block_count;
1117 buf->f_ffree = buf->f_bavail;
1118 } else {
1119 buf->f_files = avail_node_count;
1120 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
1121 buf->f_bavail);
1122 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001123
Jaegeuk Kim5a20d332013-03-03 13:58:05 +09001124 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001125 buf->f_fsid.val[0] = (u32)id;
1126 buf->f_fsid.val[1] = (u32)(id >> 32);
1127
Chao Yuddc34e32017-07-29 00:32:53 +08001128#ifdef CONFIG_QUOTA
1129 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1130 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1131 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1132 }
1133#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001134 return 0;
1135}
1136
Chao Yu4b2414d2017-08-08 10:54:31 +08001137static inline void f2fs_show_quota_options(struct seq_file *seq,
1138 struct super_block *sb)
1139{
1140#ifdef CONFIG_QUOTA
1141 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1142
1143 if (sbi->s_jquota_fmt) {
1144 char *fmtname = "";
1145
1146 switch (sbi->s_jquota_fmt) {
1147 case QFMT_VFS_OLD:
1148 fmtname = "vfsold";
1149 break;
1150 case QFMT_VFS_V0:
1151 fmtname = "vfsv0";
1152 break;
1153 case QFMT_VFS_V1:
1154 fmtname = "vfsv1";
1155 break;
1156 }
1157 seq_printf(seq, ",jqfmt=%s", fmtname);
1158 }
1159
1160 if (sbi->s_qf_names[USRQUOTA])
1161 seq_show_option(seq, "usrjquota", sbi->s_qf_names[USRQUOTA]);
1162
1163 if (sbi->s_qf_names[GRPQUOTA])
1164 seq_show_option(seq, "grpjquota", sbi->s_qf_names[GRPQUOTA]);
1165
1166 if (sbi->s_qf_names[PRJQUOTA])
1167 seq_show_option(seq, "prjjquota", sbi->s_qf_names[PRJQUOTA]);
1168#endif
1169}
1170
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001171static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1172{
1173 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1174
Jaegeuk Kim6aefd932015-10-05 11:02:54 -07001175 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
1176 if (test_opt(sbi, FORCE_FG_GC))
1177 seq_printf(seq, ",background_gc=%s", "sync");
1178 else
1179 seq_printf(seq, ",background_gc=%s", "on");
1180 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +09001181 seq_printf(seq, ",background_gc=%s", "off");
Jaegeuk Kim6aefd932015-10-05 11:02:54 -07001182 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001183 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1184 seq_puts(seq, ",disable_roll_forward");
1185 if (test_opt(sbi, DISCARD))
1186 seq_puts(seq, ",discard");
1187 if (test_opt(sbi, NOHEAP))
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001188 seq_puts(seq, ",no_heap");
1189 else
1190 seq_puts(seq, ",heap");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001191#ifdef CONFIG_F2FS_FS_XATTR
1192 if (test_opt(sbi, XATTR_USER))
1193 seq_puts(seq, ",user_xattr");
1194 else
1195 seq_puts(seq, ",nouser_xattr");
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001196 if (test_opt(sbi, INLINE_XATTR))
1197 seq_puts(seq, ",inline_xattr");
Chao Yu23cf7212017-02-15 10:34:45 +08001198 else
1199 seq_puts(seq, ",noinline_xattr");
Chao Yu6afc6622017-09-06 21:59:50 +08001200 if (test_opt(sbi, INLINE_XATTR_SIZE))
1201 seq_printf(seq, ",inline_xattr_size=%u",
1202 sbi->inline_xattr_size);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001203#endif
1204#ifdef CONFIG_F2FS_FS_POSIX_ACL
1205 if (test_opt(sbi, POSIX_ACL))
1206 seq_puts(seq, ",acl");
1207 else
1208 seq_puts(seq, ",noacl");
1209#endif
1210 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +01001211 seq_puts(seq, ",disable_ext_identify");
Huajun Li8274de72013-11-10 23:13:17 +08001212 if (test_opt(sbi, INLINE_DATA))
1213 seq_puts(seq, ",inline_data");
Wanpeng Li75342792015-03-24 10:20:27 +08001214 else
1215 seq_puts(seq, ",noinline_data");
Chao Yu5efd3c62014-09-24 18:16:13 +08001216 if (test_opt(sbi, INLINE_DENTRY))
1217 seq_puts(seq, ",inline_dentry");
Chao Yu97c17942016-05-09 19:56:34 +08001218 else
1219 seq_puts(seq, ",noinline_dentry");
Gu Zhengb270ad62014-04-11 17:49:55 +08001220 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09001221 seq_puts(seq, ",flush_merge");
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -07001222 if (test_opt(sbi, NOBARRIER))
1223 seq_puts(seq, ",nobarrier");
Jaegeuk Kimd5053a342014-10-30 22:47:03 -07001224 if (test_opt(sbi, FASTBOOT))
1225 seq_puts(seq, ",fastboot");
Chao Yu89672152015-02-05 17:55:51 +08001226 if (test_opt(sbi, EXTENT_CACHE))
1227 seq_puts(seq, ",extent_cache");
Jaegeuk Kim7daaea22015-06-25 17:43:04 -07001228 else
1229 seq_puts(seq, ",noextent_cache");
Chao Yu343f40f2015-12-16 13:12:16 +08001230 if (test_opt(sbi, DATA_FLUSH))
1231 seq_puts(seq, ",data_flush");
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001232
1233 seq_puts(seq, ",mode=");
1234 if (test_opt(sbi, ADAPTIVE))
1235 seq_puts(seq, "adaptive");
1236 else if (test_opt(sbi, LFS))
1237 seq_puts(seq, "lfs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001238 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08001239 if (test_opt(sbi, RESERVE_ROOT))
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -08001240 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
1241 sbi->root_reserved_blocks,
1242 from_kuid_munged(&init_user_ns, sbi->s_resuid),
1243 from_kgid_munged(&init_user_ns, sbi->s_resgid));
Jaegeuk Kimec915382016-12-21 17:09:19 -08001244 if (F2FS_IO_SIZE_BITS(sbi))
1245 seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
Kaixu Xia0cc0dec2017-01-27 09:35:37 +08001246#ifdef CONFIG_F2FS_FAULT_INJECTION
1247 if (test_opt(sbi, FAULT_INJECTION))
Chao Yu44529f82017-06-12 09:44:24 +08001248 seq_printf(seq, ",fault_injection=%u",
1249 sbi->fault_info.inject_rate);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +08001250#endif
Chao Yu0abd6752017-07-09 00:13:07 +08001251#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +08001252 if (test_opt(sbi, QUOTA))
1253 seq_puts(seq, ",quota");
Chao Yu0abd6752017-07-09 00:13:07 +08001254 if (test_opt(sbi, USRQUOTA))
1255 seq_puts(seq, ",usrquota");
1256 if (test_opt(sbi, GRPQUOTA))
1257 seq_puts(seq, ",grpquota");
Chao Yu5c571322017-07-26 00:01:41 +08001258 if (test_opt(sbi, PRJQUOTA))
1259 seq_puts(seq, ",prjquota");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001260#endif
Chao Yu4b2414d2017-08-08 10:54:31 +08001261 f2fs_show_quota_options(seq, sbi->sb);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001262 if (sbi->whint_mode == WHINT_MODE_USER)
1263 seq_printf(seq, ",whint_mode=%s", "user-based");
Hyunchul Leef2e703f2018-01-31 11:36:58 +09001264 else if (sbi->whint_mode == WHINT_MODE_FS)
1265 seq_printf(seq, ",whint_mode=%s", "fs-based");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001266
1267 return 0;
1268}
1269
Yunlei He498c5e92015-05-07 18:11:37 +08001270static void default_options(struct f2fs_sb_info *sbi)
1271{
1272 /* init some FS parameters */
1273 sbi->active_logs = NR_CURSEG_TYPE;
Chao Yu6afc6622017-09-06 21:59:50 +08001274 sbi->inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001275 sbi->whint_mode = WHINT_MODE_OFF;
Yunlei He498c5e92015-05-07 18:11:37 +08001276
1277 set_opt(sbi, BG_GC);
Chao Yu39133a52017-02-08 17:39:44 +08001278 set_opt(sbi, INLINE_XATTR);
Yunlei He498c5e92015-05-07 18:11:37 +08001279 set_opt(sbi, INLINE_DATA);
Chao Yu97c17942016-05-09 19:56:34 +08001280 set_opt(sbi, INLINE_DENTRY);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07001281 set_opt(sbi, EXTENT_CACHE);
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001282 set_opt(sbi, NOHEAP);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001283 sbi->sb->s_flags |= SB_LAZYTIME;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -07001284 set_opt(sbi, FLUSH_MERGE);
Sheng Yongccd31cb2018-02-06 12:31:17 +08001285 if (f2fs_sb_has_blkzoned(sbi->sb)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -07001286 set_opt_mode(sbi, F2FS_MOUNT_LFS);
1287 set_opt(sbi, DISCARD);
1288 } else {
1289 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
1290 }
Yunlei He498c5e92015-05-07 18:11:37 +08001291
1292#ifdef CONFIG_F2FS_FS_XATTR
1293 set_opt(sbi, XATTR_USER);
1294#endif
1295#ifdef CONFIG_F2FS_FS_POSIX_ACL
1296 set_opt(sbi, POSIX_ACL);
1297#endif
Chao Yu36dbd322016-09-26 19:45:05 +08001298
1299#ifdef CONFIG_F2FS_FAULT_INJECTION
1300 f2fs_build_fault_attr(sbi, 0);
1301#endif
Yunlei He498c5e92015-05-07 18:11:37 +08001302}
1303
Jaegeuk Kimea676732017-10-06 09:14:28 -07001304#ifdef CONFIG_QUOTA
1305static int f2fs_enable_quotas(struct super_block *sb);
1306#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001307static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1308{
1309 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1310 struct f2fs_mount_info org_mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +08001311 unsigned long old_sb_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +09001312 int err, active_logs;
Gu Zheng876dc592014-04-11 17:50:00 +08001313 bool need_restart_gc = false;
1314 bool need_stop_gc = false;
Chao Yu9cd81ce2015-09-18 16:55:26 +08001315 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001316 int old_whint_mode = sbi->whint_mode;
Chao Yu2443b8b2016-09-26 19:45:06 +08001317#ifdef CONFIG_F2FS_FAULT_INJECTION
1318 struct f2fs_fault_info ffi = sbi->fault_info;
1319#endif
Chao Yu4b2414d2017-08-08 10:54:31 +08001320#ifdef CONFIG_QUOTA
1321 int s_jquota_fmt;
1322 char *s_qf_names[MAXQUOTAS];
1323 int i, j;
1324#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001325
1326 /*
1327 * Save the old mount options in case we
1328 * need to restore them.
1329 */
1330 org_mount_opt = sbi->mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +08001331 old_sb_flags = sb->s_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +09001332 active_logs = sbi->active_logs;
1333
Chao Yu4b2414d2017-08-08 10:54:31 +08001334#ifdef CONFIG_QUOTA
1335 s_jquota_fmt = sbi->s_jquota_fmt;
1336 for (i = 0; i < MAXQUOTAS; i++) {
1337 if (sbi->s_qf_names[i]) {
1338 s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
1339 GFP_KERNEL);
1340 if (!s_qf_names[i]) {
1341 for (j = 0; j < i; j++)
1342 kfree(s_qf_names[j]);
1343 return -ENOMEM;
1344 }
1345 } else {
1346 s_qf_names[i] = NULL;
1347 }
1348 }
1349#endif
1350
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001351 /* recover superblocks we couldn't write due to previous RO mount */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001352 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001353 err = f2fs_commit_super(sbi, false);
1354 f2fs_msg(sb, KERN_INFO,
1355 "Try to recover all the superblocks, ret: %d", err);
1356 if (!err)
1357 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1358 }
1359
Yunlei He498c5e92015-05-07 18:11:37 +08001360 default_options(sbi);
Chao Yu26666c82014-09-15 18:04:44 +08001361
Namjae Jeon696c0182013-06-16 09:48:48 +09001362 /* parse mount options */
1363 err = parse_options(sb, data);
1364 if (err)
1365 goto restore_opts;
1366
1367 /*
1368 * Previous and new state of filesystem is RO,
Gu Zheng876dc592014-04-11 17:50:00 +08001369 * so skip checking GC and FLUSH_MERGE conditions.
Namjae Jeon696c0182013-06-16 09:48:48 +09001370 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001371 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
Namjae Jeon696c0182013-06-16 09:48:48 +09001372 goto skip;
1373
Jaegeuk Kimea676732017-10-06 09:14:28 -07001374#ifdef CONFIG_QUOTA
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001375 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
Chao Yu0abd6752017-07-09 00:13:07 +08001376 err = dquot_suspend(sb, -1);
1377 if (err < 0)
1378 goto restore_opts;
Jaegeuk Kim62793982018-01-02 11:03:19 -08001379 } else if (f2fs_readonly(sb) && !(*flags & MS_RDONLY)) {
Chao Yu0abd6752017-07-09 00:13:07 +08001380 /* dquot_resume needs RW */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001381 sb->s_flags &= ~SB_RDONLY;
Jaegeuk Kimea676732017-10-06 09:14:28 -07001382 if (sb_any_quota_suspended(sb)) {
1383 dquot_resume(sb, -1);
1384 } else if (f2fs_sb_has_quota_ino(sb)) {
1385 err = f2fs_enable_quotas(sb);
1386 if (err)
1387 goto restore_opts;
1388 }
Chao Yu0abd6752017-07-09 00:13:07 +08001389 }
Jaegeuk Kimea676732017-10-06 09:14:28 -07001390#endif
Chao Yu9cd81ce2015-09-18 16:55:26 +08001391 /* disallow enable/disable extent_cache dynamically */
1392 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
1393 err = -EINVAL;
1394 f2fs_msg(sbi->sb, KERN_WARNING,
1395 "switch extent_cache option is not allowed");
1396 goto restore_opts;
1397 }
1398
Namjae Jeon696c0182013-06-16 09:48:48 +09001399 /*
1400 * We stop the GC thread if FS is mounted as RO
1401 * or if background_gc = off is passed in mount
1402 * option. Also sync the filesystem.
1403 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001404 if ((*flags & SB_RDONLY) || !test_opt(sbi, BG_GC)) {
Namjae Jeon696c0182013-06-16 09:48:48 +09001405 if (sbi->gc_thread) {
1406 stop_gc_thread(sbi);
Gu Zheng876dc592014-04-11 17:50:00 +08001407 need_restart_gc = true;
Namjae Jeon696c0182013-06-16 09:48:48 +09001408 }
Chao Yuaba291b2014-11-18 11:17:20 +08001409 } else if (!sbi->gc_thread) {
Namjae Jeon696c0182013-06-16 09:48:48 +09001410 err = start_gc_thread(sbi);
1411 if (err)
1412 goto restore_opts;
Gu Zheng876dc592014-04-11 17:50:00 +08001413 need_stop_gc = true;
1414 }
1415
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001416 if (*flags & SB_RDONLY || sbi->whint_mode != old_whint_mode) {
Jaegeuk Kimfaa0e552016-03-24 10:29:39 -07001417 writeback_inodes_sb(sb, WB_REASON_SYNC);
1418 sync_inodes_sb(sb);
1419
1420 set_sbi_flag(sbi, SBI_IS_DIRTY);
1421 set_sbi_flag(sbi, SBI_IS_CLOSE);
1422 f2fs_sync_fs(sb, 1);
1423 clear_sbi_flag(sbi, SBI_IS_CLOSE);
1424 }
1425
Gu Zheng876dc592014-04-11 17:50:00 +08001426 /*
1427 * We stop issue flush thread if FS is mounted as RO
1428 * or if flush_merge is not passed in mount option.
1429 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001430 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -08001431 clear_opt(sbi, FLUSH_MERGE);
1432 destroy_flush_cmd_control(sbi, false);
1433 } else {
Gu Zheng2163d192014-04-27 14:21:33 +08001434 err = create_flush_cmd_control(sbi);
1435 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08001436 goto restore_gc;
Namjae Jeon696c0182013-06-16 09:48:48 +09001437 }
1438skip:
Chao Yu4b2414d2017-08-08 10:54:31 +08001439#ifdef CONFIG_QUOTA
1440 /* Release old quota file names */
1441 for (i = 0; i < MAXQUOTAS; i++)
1442 kfree(s_qf_names[i]);
1443#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001444 /* Update the POSIXACL Flag */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001445 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
1446 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001447
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08001448 limit_reserve_root(sbi);
Namjae Jeon696c0182013-06-16 09:48:48 +09001449 return 0;
Gu Zheng876dc592014-04-11 17:50:00 +08001450restore_gc:
1451 if (need_restart_gc) {
1452 if (start_gc_thread(sbi))
1453 f2fs_msg(sbi->sb, KERN_WARNING,
arter97e1c42042014-08-06 23:22:50 +09001454 "background gc thread has stopped");
Gu Zheng876dc592014-04-11 17:50:00 +08001455 } else if (need_stop_gc) {
1456 stop_gc_thread(sbi);
1457 }
Namjae Jeon696c0182013-06-16 09:48:48 +09001458restore_opts:
Chao Yu4b2414d2017-08-08 10:54:31 +08001459#ifdef CONFIG_QUOTA
1460 sbi->s_jquota_fmt = s_jquota_fmt;
1461 for (i = 0; i < MAXQUOTAS; i++) {
1462 kfree(sbi->s_qf_names[i]);
1463 sbi->s_qf_names[i] = s_qf_names[i];
1464 }
1465#endif
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001466 sbi->whint_mode = old_whint_mode;
Namjae Jeon696c0182013-06-16 09:48:48 +09001467 sbi->mount_opt = org_mount_opt;
1468 sbi->active_logs = active_logs;
Chao Yu0abd6752017-07-09 00:13:07 +08001469 sb->s_flags = old_sb_flags;
Chao Yu2443b8b2016-09-26 19:45:06 +08001470#ifdef CONFIG_F2FS_FAULT_INJECTION
1471 sbi->fault_info = ffi;
1472#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001473 return err;
1474}
1475
Chao Yu0abd6752017-07-09 00:13:07 +08001476#ifdef CONFIG_QUOTA
1477/* Read data from quotafile */
1478static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
1479 size_t len, loff_t off)
1480{
1481 struct inode *inode = sb_dqopt(sb)->files[type];
1482 struct address_space *mapping = inode->i_mapping;
1483 block_t blkidx = F2FS_BYTES_TO_BLK(off);
1484 int offset = off & (sb->s_blocksize - 1);
1485 int tocopy;
1486 size_t toread;
1487 loff_t i_size = i_size_read(inode);
1488 struct page *page;
1489 char *kaddr;
1490
1491 if (off > i_size)
1492 return 0;
1493
1494 if (off + len > i_size)
1495 len = i_size - off;
1496 toread = len;
1497 while (toread > 0) {
1498 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
1499repeat:
1500 page = read_mapping_page(mapping, blkidx, NULL);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001501 if (IS_ERR(page)) {
1502 if (PTR_ERR(page) == -ENOMEM) {
1503 congestion_wait(BLK_RW_ASYNC, HZ/50);
1504 goto repeat;
1505 }
Chao Yu0abd6752017-07-09 00:13:07 +08001506 return PTR_ERR(page);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001507 }
Chao Yu0abd6752017-07-09 00:13:07 +08001508
1509 lock_page(page);
1510
1511 if (unlikely(page->mapping != mapping)) {
1512 f2fs_put_page(page, 1);
1513 goto repeat;
1514 }
1515 if (unlikely(!PageUptodate(page))) {
1516 f2fs_put_page(page, 1);
1517 return -EIO;
1518 }
1519
1520 kaddr = kmap_atomic(page);
1521 memcpy(data, kaddr + offset, tocopy);
1522 kunmap_atomic(kaddr);
1523 f2fs_put_page(page, 1);
1524
1525 offset = 0;
1526 toread -= tocopy;
1527 data += tocopy;
1528 blkidx++;
1529 }
1530 return len;
1531}
1532
1533/* Write to quotafile */
1534static ssize_t f2fs_quota_write(struct super_block *sb, int type,
1535 const char *data, size_t len, loff_t off)
1536{
1537 struct inode *inode = sb_dqopt(sb)->files[type];
1538 struct address_space *mapping = inode->i_mapping;
1539 const struct address_space_operations *a_ops = mapping->a_ops;
1540 int offset = off & (sb->s_blocksize - 1);
1541 size_t towrite = len;
1542 struct page *page;
1543 char *kaddr;
1544 int err = 0;
1545 int tocopy;
1546
1547 while (towrite > 0) {
1548 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
1549 towrite);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001550retry:
Chao Yu0abd6752017-07-09 00:13:07 +08001551 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
1552 &page, NULL);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001553 if (unlikely(err)) {
1554 if (err == -ENOMEM) {
1555 congestion_wait(BLK_RW_ASYNC, HZ/50);
1556 goto retry;
1557 }
Chao Yu0abd6752017-07-09 00:13:07 +08001558 break;
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001559 }
Chao Yu0abd6752017-07-09 00:13:07 +08001560
1561 kaddr = kmap_atomic(page);
1562 memcpy(kaddr + offset, data, tocopy);
1563 kunmap_atomic(kaddr);
1564 flush_dcache_page(page);
1565
1566 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
1567 page, NULL);
1568 offset = 0;
1569 towrite -= tocopy;
1570 off += tocopy;
1571 data += tocopy;
1572 cond_resched();
1573 }
1574
1575 if (len == towrite)
Jaegeuk Kim6e5b5d42017-10-19 12:07:11 -07001576 return err;
Chao Yu0abd6752017-07-09 00:13:07 +08001577 inode->i_mtime = inode->i_ctime = current_time(inode);
1578 f2fs_mark_inode_dirty_sync(inode, false);
1579 return len - towrite;
1580}
1581
1582static struct dquot **f2fs_get_dquots(struct inode *inode)
1583{
1584 return F2FS_I(inode)->i_dquot;
1585}
1586
1587static qsize_t *f2fs_get_reserved_space(struct inode *inode)
1588{
1589 return &F2FS_I(inode)->i_reserved_quota;
1590}
1591
Chao Yu4b2414d2017-08-08 10:54:31 +08001592static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
1593{
1594 return dquot_quota_on_mount(sbi->sb, sbi->s_qf_names[type],
1595 sbi->s_jquota_fmt, type);
1596}
1597
Jaegeuk Kimea676732017-10-06 09:14:28 -07001598int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
Chao Yu4b2414d2017-08-08 10:54:31 +08001599{
Jaegeuk Kimea676732017-10-06 09:14:28 -07001600 int enabled = 0;
1601 int i, err;
1602
1603 if (f2fs_sb_has_quota_ino(sbi->sb) && rdonly) {
1604 err = f2fs_enable_quotas(sbi->sb);
1605 if (err) {
1606 f2fs_msg(sbi->sb, KERN_ERR,
1607 "Cannot turn on quota_ino: %d", err);
1608 return 0;
1609 }
1610 return 1;
1611 }
Chao Yu4b2414d2017-08-08 10:54:31 +08001612
1613 for (i = 0; i < MAXQUOTAS; i++) {
1614 if (sbi->s_qf_names[i]) {
Jaegeuk Kimea676732017-10-06 09:14:28 -07001615 err = f2fs_quota_on_mount(sbi, i);
1616 if (!err) {
1617 enabled = 1;
1618 continue;
1619 }
1620 f2fs_msg(sbi->sb, KERN_ERR,
1621 "Cannot turn on quotas: %d on %d", err, i);
Chao Yu4b2414d2017-08-08 10:54:31 +08001622 }
1623 }
Jaegeuk Kimea676732017-10-06 09:14:28 -07001624 return enabled;
1625}
1626
1627static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
1628 unsigned int flags)
1629{
1630 struct inode *qf_inode;
1631 unsigned long qf_inum;
1632 int err;
1633
1634 BUG_ON(!f2fs_sb_has_quota_ino(sb));
1635
1636 qf_inum = f2fs_qf_ino(sb, type);
1637 if (!qf_inum)
1638 return -EPERM;
1639
1640 qf_inode = f2fs_iget(sb, qf_inum);
1641 if (IS_ERR(qf_inode)) {
1642 f2fs_msg(sb, KERN_ERR,
1643 "Bad quota inode %u:%lu", type, qf_inum);
1644 return PTR_ERR(qf_inode);
1645 }
1646
1647 /* Don't account quota for quota files to avoid recursion */
1648 qf_inode->i_flags |= S_NOQUOTA;
1649 err = dquot_enable(qf_inode, type, format_id, flags);
1650 iput(qf_inode);
1651 return err;
1652}
1653
1654static int f2fs_enable_quotas(struct super_block *sb)
1655{
1656 int type, err = 0;
1657 unsigned long qf_inum;
1658 bool quota_mopt[MAXQUOTAS] = {
1659 test_opt(F2FS_SB(sb), USRQUOTA),
1660 test_opt(F2FS_SB(sb), GRPQUOTA),
1661 test_opt(F2FS_SB(sb), PRJQUOTA),
1662 };
1663
1664 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
1665 for (type = 0; type < MAXQUOTAS; type++) {
1666 qf_inum = f2fs_qf_ino(sb, type);
1667 if (qf_inum) {
1668 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
1669 DQUOT_USAGE_ENABLED |
1670 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
1671 if (err) {
1672 f2fs_msg(sb, KERN_ERR,
1673 "Failed to enable quota tracking "
1674 "(type=%d, err=%d). Please run "
1675 "fsck to fix.", type, err);
1676 for (type--; type >= 0; type--)
1677 dquot_quota_off(sb, type);
1678 return err;
1679 }
1680 }
1681 }
1682 return 0;
Chao Yu4b2414d2017-08-08 10:54:31 +08001683}
1684
Chao Yu0abd6752017-07-09 00:13:07 +08001685static int f2fs_quota_sync(struct super_block *sb, int type)
1686{
1687 struct quota_info *dqopt = sb_dqopt(sb);
1688 int cnt;
1689 int ret;
1690
1691 ret = dquot_writeback_dquots(sb, type);
1692 if (ret)
1693 return ret;
1694
1695 /*
1696 * Now when everything is written we can discard the pagecache so
1697 * that userspace sees the changes.
1698 */
1699 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1700 if (type != -1 && cnt != type)
1701 continue;
1702 if (!sb_has_quota_active(sb, cnt))
1703 continue;
1704
1705 ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping);
1706 if (ret)
1707 return ret;
1708
1709 inode_lock(dqopt->files[cnt]);
1710 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
1711 inode_unlock(dqopt->files[cnt]);
1712 }
1713 return 0;
1714}
1715
1716static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
1717 const struct path *path)
1718{
1719 struct inode *inode;
1720 int err;
1721
Chao Yu9a20d392017-08-07 16:37:59 +08001722 err = f2fs_quota_sync(sb, type);
Chao Yu0abd6752017-07-09 00:13:07 +08001723 if (err)
1724 return err;
1725
1726 err = dquot_quota_on(sb, type, format_id, path);
1727 if (err)
1728 return err;
1729
1730 inode = d_inode(path->dentry);
1731
1732 inode_lock(inode);
1733 F2FS_I(inode)->i_flags |= FS_NOATIME_FL | FS_IMMUTABLE_FL;
1734 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
1735 S_NOATIME | S_IMMUTABLE);
1736 inode_unlock(inode);
1737 f2fs_mark_inode_dirty_sync(inode, false);
1738
1739 return 0;
1740}
1741
1742static int f2fs_quota_off(struct super_block *sb, int type)
1743{
1744 struct inode *inode = sb_dqopt(sb)->files[type];
1745 int err;
1746
1747 if (!inode || !igrab(inode))
1748 return dquot_quota_off(sb, type);
1749
Chao Yu9a20d392017-08-07 16:37:59 +08001750 f2fs_quota_sync(sb, type);
Chao Yu0abd6752017-07-09 00:13:07 +08001751
1752 err = dquot_quota_off(sb, type);
Jaegeuk Kimea676732017-10-06 09:14:28 -07001753 if (err || f2fs_sb_has_quota_ino(sb))
Chao Yu0abd6752017-07-09 00:13:07 +08001754 goto out_put;
1755
1756 inode_lock(inode);
1757 F2FS_I(inode)->i_flags &= ~(FS_NOATIME_FL | FS_IMMUTABLE_FL);
1758 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
1759 inode_unlock(inode);
1760 f2fs_mark_inode_dirty_sync(inode, false);
1761out_put:
1762 iput(inode);
1763 return err;
1764}
1765
Chao Yu4b2414d2017-08-08 10:54:31 +08001766void f2fs_quota_off_umount(struct super_block *sb)
Chao Yu0abd6752017-07-09 00:13:07 +08001767{
1768 int type;
1769
1770 for (type = 0; type < MAXQUOTAS; type++)
1771 f2fs_quota_off(sb, type);
1772}
1773
Wei Yongjun94b1e102018-01-05 09:41:20 +00001774static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
Chao Yu5c571322017-07-26 00:01:41 +08001775{
1776 *projid = F2FS_I(inode)->i_projid;
1777 return 0;
1778}
1779
Chao Yu0abd6752017-07-09 00:13:07 +08001780static const struct dquot_operations f2fs_quota_operations = {
1781 .get_reserved_space = f2fs_get_reserved_space,
1782 .write_dquot = dquot_commit,
1783 .acquire_dquot = dquot_acquire,
1784 .release_dquot = dquot_release,
1785 .mark_dirty = dquot_mark_dquot_dirty,
1786 .write_info = dquot_commit_info,
1787 .alloc_dquot = dquot_alloc,
1788 .destroy_dquot = dquot_destroy,
Chao Yu5c571322017-07-26 00:01:41 +08001789 .get_projid = f2fs_get_projid,
Chao Yu0abd6752017-07-09 00:13:07 +08001790 .get_next_id = dquot_get_next_id,
1791};
1792
1793static const struct quotactl_ops f2fs_quotactl_ops = {
1794 .quota_on = f2fs_quota_on,
1795 .quota_off = f2fs_quota_off,
1796 .quota_sync = f2fs_quota_sync,
1797 .get_state = dquot_get_state,
1798 .set_info = dquot_set_dqinfo,
1799 .get_dqblk = dquot_get_dqblk,
1800 .set_dqblk = dquot_set_dqblk,
1801 .get_nextdqblk = dquot_get_next_dqblk,
1802};
1803#else
Chao Yu4b2414d2017-08-08 10:54:31 +08001804void f2fs_quota_off_umount(struct super_block *sb)
Chao Yu0abd6752017-07-09 00:13:07 +08001805{
1806}
1807#endif
1808
Arvind Yadavf62fc9f2017-08-31 15:06:24 +05301809static const struct super_operations f2fs_sops = {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001810 .alloc_inode = f2fs_alloc_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001811 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001812 .destroy_inode = f2fs_destroy_inode,
1813 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +09001814 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001815 .show_options = f2fs_show_options,
Chao Yu0abd6752017-07-09 00:13:07 +08001816#ifdef CONFIG_QUOTA
1817 .quota_read = f2fs_quota_read,
1818 .quota_write = f2fs_quota_write,
1819 .get_dquots = f2fs_get_dquots,
1820#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001821 .evict_inode = f2fs_evict_inode,
1822 .put_super = f2fs_put_super,
1823 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +09001824 .freeze_fs = f2fs_freeze,
1825 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001826 .statfs = f2fs_statfs,
Namjae Jeon696c0182013-06-16 09:48:48 +09001827 .remount_fs = f2fs_remount,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001828};
1829
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001830#ifdef CONFIG_F2FS_FS_ENCRYPTION
1831static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
1832{
1833 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1834 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1835 ctx, len, NULL);
1836}
1837
1838static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
1839 void *fs_data)
1840{
1841 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1842 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1843 ctx, len, fs_data, XATTR_CREATE);
1844}
1845
1846static unsigned f2fs_max_namelen(struct inode *inode)
1847{
1848 return S_ISLNK(inode->i_mode) ?
1849 inode->i_sb->s_blocksize : F2FS_NAME_LEN;
1850}
1851
Eric Biggers6f69f0e2017-02-07 12:42:10 -08001852static const struct fscrypt_operations f2fs_cryptops = {
Eric Biggersa5d431e2017-01-05 13:51:18 -08001853 .key_prefix = "f2fs:",
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001854 .get_context = f2fs_get_context,
1855 .set_context = f2fs_set_context,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001856 .empty_dir = f2fs_empty_dir,
1857 .max_namelen = f2fs_max_namelen,
1858};
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001859#endif
1860
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001861static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
1862 u64 ino, u32 generation)
1863{
1864 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1865 struct inode *inode;
1866
Chao Yud6b7d4b2014-06-12 13:23:41 +08001867 if (check_nid_range(sbi, ino))
Chao Yu910bb122014-03-12 17:08:36 +08001868 return ERR_PTR(-ESTALE);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001869
1870 /*
1871 * f2fs_iget isn't quite right if the inode is currently unallocated!
1872 * However f2fs_iget currently does appropriate checks to handle stale
1873 * inodes so everything is OK.
1874 */
1875 inode = f2fs_iget(sb, ino);
1876 if (IS_ERR(inode))
1877 return ERR_CAST(inode);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001878 if (unlikely(generation && inode->i_generation != generation)) {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001879 /* we didn't find the right inode.. */
1880 iput(inode);
1881 return ERR_PTR(-ESTALE);
1882 }
1883 return inode;
1884}
1885
1886static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1887 int fh_len, int fh_type)
1888{
1889 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1890 f2fs_nfs_get_inode);
1891}
1892
1893static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
1894 int fh_len, int fh_type)
1895{
1896 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1897 f2fs_nfs_get_inode);
1898}
1899
1900static const struct export_operations f2fs_export_ops = {
1901 .fh_to_dentry = f2fs_fh_to_dentry,
1902 .fh_to_parent = f2fs_fh_to_parent,
1903 .get_parent = f2fs_get_parent,
1904};
1905
Chao Yue0afc4d2015-12-31 14:35:37 +08001906static loff_t max_file_blocks(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001907{
Chao Yu7a2af762017-07-19 00:19:06 +08001908 loff_t result = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001909 loff_t leaf_count = ADDRS_PER_BLOCK;
1910
Chao Yu7a2af762017-07-19 00:19:06 +08001911 /*
1912 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
Chao Yu6afc6622017-09-06 21:59:50 +08001913 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
Chao Yu7a2af762017-07-19 00:19:06 +08001914 * space in inode.i_addr, it will be more safe to reassign
1915 * result as zero.
1916 */
1917
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001918 /* two direct node blocks */
1919 result += (leaf_count * 2);
1920
1921 /* two indirect node blocks */
1922 leaf_count *= NIDS_PER_BLOCK;
1923 result += (leaf_count * 2);
1924
1925 /* one double indirect node block */
1926 leaf_count *= NIDS_PER_BLOCK;
1927 result += leaf_count;
1928
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001929 return result;
1930}
1931
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001932static int __f2fs_commit_super(struct buffer_head *bh,
1933 struct f2fs_super_block *super)
Chao Yu9a59b622015-12-15 09:58:18 +08001934{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001935 lock_buffer(bh);
1936 if (super)
1937 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001938 set_buffer_dirty(bh);
1939 unlock_buffer(bh);
1940
1941 /* it's rare case, we can do fua all the time */
Jan Kara3adc5fcb2017-05-02 17:03:47 +02001942 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001943}
1944
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001945static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001946 struct buffer_head *bh)
1947{
1948 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1949 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001950 struct super_block *sb = sbi->sb;
Chao Yu9a59b622015-12-15 09:58:18 +08001951 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1952 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
1953 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
1954 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
1955 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1956 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1957 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
1958 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
1959 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
1960 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
1961 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
1962 u32 segment_count = le32_to_cpu(raw_super->segment_count);
1963 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001964 u64 main_end_blkaddr = main_blkaddr +
1965 (segment_count_main << log_blocks_per_seg);
1966 u64 seg_end_blkaddr = segment0_blkaddr +
1967 (segment_count << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08001968
1969 if (segment0_blkaddr != cp_blkaddr) {
1970 f2fs_msg(sb, KERN_INFO,
1971 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
1972 segment0_blkaddr, cp_blkaddr);
1973 return true;
1974 }
1975
1976 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
1977 sit_blkaddr) {
1978 f2fs_msg(sb, KERN_INFO,
1979 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
1980 cp_blkaddr, sit_blkaddr,
1981 segment_count_ckpt << log_blocks_per_seg);
1982 return true;
1983 }
1984
1985 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
1986 nat_blkaddr) {
1987 f2fs_msg(sb, KERN_INFO,
1988 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
1989 sit_blkaddr, nat_blkaddr,
1990 segment_count_sit << log_blocks_per_seg);
1991 return true;
1992 }
1993
1994 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
1995 ssa_blkaddr) {
1996 f2fs_msg(sb, KERN_INFO,
1997 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
1998 nat_blkaddr, ssa_blkaddr,
1999 segment_count_nat << log_blocks_per_seg);
2000 return true;
2001 }
2002
2003 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
2004 main_blkaddr) {
2005 f2fs_msg(sb, KERN_INFO,
2006 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
2007 ssa_blkaddr, main_blkaddr,
2008 segment_count_ssa << log_blocks_per_seg);
2009 return true;
2010 }
2011
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002012 if (main_end_blkaddr > seg_end_blkaddr) {
Chao Yu9a59b622015-12-15 09:58:18 +08002013 f2fs_msg(sb, KERN_INFO,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002014 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
Chao Yu9a59b622015-12-15 09:58:18 +08002015 main_blkaddr,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002016 segment0_blkaddr +
2017 (segment_count << log_blocks_per_seg),
Chao Yu9a59b622015-12-15 09:58:18 +08002018 segment_count_main << log_blocks_per_seg);
2019 return true;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002020 } else if (main_end_blkaddr < seg_end_blkaddr) {
2021 int err = 0;
2022 char *res;
Chao Yu9a59b622015-12-15 09:58:18 +08002023
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002024 /* fix in-memory information all the time */
2025 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
2026 segment0_blkaddr) >> log_blocks_per_seg);
2027
2028 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002029 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002030 res = "internally";
2031 } else {
2032 err = __f2fs_commit_super(bh, NULL);
2033 res = err ? "failed" : "done";
2034 }
2035 f2fs_msg(sb, KERN_INFO,
2036 "Fix alignment : %s, start(%u) end(%u) block(%u)",
2037 res, main_blkaddr,
2038 segment0_blkaddr +
2039 (segment_count << log_blocks_per_seg),
2040 segment_count_main << log_blocks_per_seg);
2041 if (err)
2042 return true;
2043 }
Chao Yu9a59b622015-12-15 09:58:18 +08002044 return false;
2045}
2046
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002047static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002048 struct buffer_head *bh)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002049{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002050 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2051 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002052 struct super_block *sb = sbi->sb;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002053 unsigned int blocksize;
2054
Namjae Jeona07ef782012-12-30 14:52:05 +09002055 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
2056 f2fs_msg(sb, KERN_INFO,
2057 "Magic Mismatch, valid(0x%x) - read(0x%x)",
2058 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002059 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09002060 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002061
majianpeng5c9b4692013-02-01 19:07:57 +08002062 /* Currently, support only 4KB page cache size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002063 if (F2FS_BLKSIZE != PAGE_SIZE) {
majianpeng5c9b4692013-02-01 19:07:57 +08002064 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +08002065 "Invalid page_cache_size (%lu), supports only 4KB\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002066 PAGE_SIZE);
majianpeng5c9b4692013-02-01 19:07:57 +08002067 return 1;
2068 }
2069
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002070 /* Currently, support only 4KB block size */
2071 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +08002072 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002073 f2fs_msg(sb, KERN_INFO,
2074 "Invalid blocksize (%u), supports only 4KB\n",
2075 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002076 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09002077 }
majianpeng5c9b4692013-02-01 19:07:57 +08002078
Chao Yu9a59b622015-12-15 09:58:18 +08002079 /* check log blocks per segment */
2080 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
2081 f2fs_msg(sb, KERN_INFO,
2082 "Invalid log blocks per segment (%u)\n",
2083 le32_to_cpu(raw_super->log_blocks_per_seg));
2084 return 1;
2085 }
2086
Chao Yu55cf9cb2014-09-15 18:01:10 +08002087 /* Currently, support 512/1024/2048/4096 bytes sector size */
2088 if (le32_to_cpu(raw_super->log_sectorsize) >
2089 F2FS_MAX_LOG_SECTOR_SIZE ||
2090 le32_to_cpu(raw_super->log_sectorsize) <
2091 F2FS_MIN_LOG_SECTOR_SIZE) {
2092 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
2093 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002094 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09002095 }
Chao Yu55cf9cb2014-09-15 18:01:10 +08002096 if (le32_to_cpu(raw_super->log_sectors_per_block) +
2097 le32_to_cpu(raw_super->log_sectorsize) !=
2098 F2FS_MAX_LOG_SECTOR_SIZE) {
2099 f2fs_msg(sb, KERN_INFO,
2100 "Invalid log sectors per block(%u) log sectorsize(%u)",
2101 le32_to_cpu(raw_super->log_sectors_per_block),
2102 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002103 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09002104 }
Chao Yu9a59b622015-12-15 09:58:18 +08002105
2106 /* check reserved ino info */
2107 if (le32_to_cpu(raw_super->node_ino) != 1 ||
2108 le32_to_cpu(raw_super->meta_ino) != 2 ||
2109 le32_to_cpu(raw_super->root_ino) != 3) {
2110 f2fs_msg(sb, KERN_INFO,
2111 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
2112 le32_to_cpu(raw_super->node_ino),
2113 le32_to_cpu(raw_super->meta_ino),
2114 le32_to_cpu(raw_super->root_ino));
2115 return 1;
2116 }
2117
Jin Qianb9dd4612017-04-25 16:28:48 -07002118 if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) {
2119 f2fs_msg(sb, KERN_INFO,
2120 "Invalid segment count (%u)",
2121 le32_to_cpu(raw_super->segment_count));
2122 return 1;
2123 }
2124
Chao Yu9a59b622015-12-15 09:58:18 +08002125 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002126 if (sanity_check_area_boundary(sbi, bh))
Chao Yu9a59b622015-12-15 09:58:18 +08002127 return 1;
2128
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002129 return 0;
2130}
2131
Shawn Lin984ec632016-02-17 11:26:32 +08002132int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002133{
2134 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09002135 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2136 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08002137 unsigned int ovp_segments, reserved_segments;
Jin Qian15d30422017-05-15 10:45:08 -07002138 unsigned int main_segs, blocks_per_seg;
2139 int i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002140
2141 total = le32_to_cpu(raw_super->segment_count);
2142 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
2143 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
2144 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
2145 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
2146 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
2147
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002148 if (unlikely(fsmeta >= total))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002149 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09002150
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08002151 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2152 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2153
2154 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
2155 ovp_segments == 0 || reserved_segments == 0)) {
2156 f2fs_msg(sbi->sb, KERN_ERR,
2157 "Wrong layout: check mkfs.f2fs version");
2158 return 1;
2159 }
2160
Jin Qian15d30422017-05-15 10:45:08 -07002161 main_segs = le32_to_cpu(raw_super->segment_count_main);
2162 blocks_per_seg = sbi->blocks_per_seg;
2163
2164 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2165 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
2166 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
2167 return 1;
2168 }
2169 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
2170 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
2171 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
2172 return 1;
2173 }
2174
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07002175 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim577e3492013-01-24 19:56:11 +09002176 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
2177 return 1;
2178 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002179 return 0;
2180}
2181
2182static void init_sb_info(struct f2fs_sb_info *sbi)
2183{
2184 struct f2fs_super_block *raw_super = sbi->raw_super;
Chao Yue41e6d72017-05-19 23:37:00 +08002185 int i, j;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002186
2187 sbi->log_sectors_per_block =
2188 le32_to_cpu(raw_super->log_sectors_per_block);
2189 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
2190 sbi->blocksize = 1 << sbi->log_blocksize;
2191 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2192 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
2193 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2194 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2195 sbi->total_sections = le32_to_cpu(raw_super->section_count);
2196 sbi->total_node_count =
2197 (le32_to_cpu(raw_super->segment_count_nat) / 2)
2198 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
2199 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
2200 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
2201 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002202 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimb1c57c12014-01-08 13:45:08 +09002203 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002204
Jaegeuk Kimab9fa662014-02-27 20:09:05 +09002205 sbi->dir_level = DEF_DIR_LEVEL;
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08002206 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002207 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
Chao Yucaf00472015-01-28 17:48:42 +08002208 clear_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002209
Jaegeuk Kim35782b22016-10-20 19:09:57 -07002210 for (i = 0; i < NR_COUNT_TYPE; i++)
2211 atomic_set(&sbi->nr_pages[i], 0);
2212
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002213 atomic_set(&sbi->wb_sync_req, 0);
2214
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002215 INIT_LIST_HEAD(&sbi->s_list);
2216 mutex_init(&sbi->umount_mutex);
Chao Yue41e6d72017-05-19 23:37:00 +08002217 for (i = 0; i < NR_PAGE_TYPE - 1; i++)
2218 for (j = HOT; j < NR_TEMP_TYPE; j++)
2219 mutex_init(&sbi->wio_mutex[i][j]);
Chao Yuaaec2b12016-09-20 11:04:18 +08002220 spin_lock_init(&sbi->cp_lock);
Chao Yu1228b482017-09-29 13:59:39 +08002221
2222 sbi->dirty_device = 0;
2223 spin_lock_init(&sbi->dev_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08002224
2225 mutex_init(&sbi->sb_lock);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002226}
2227
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002228static int init_percpu_info(struct f2fs_sb_info *sbi)
2229{
Jaegeuk Kim35782b22016-10-20 19:09:57 -07002230 int err;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002231
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07002232 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
2233 if (err)
2234 return err;
2235
2236 return percpu_counter_init(&sbi->total_valid_inode_count, 0,
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002237 GFP_KERNEL);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002238}
2239
Damien Le Moal178053e2016-10-28 17:45:05 +09002240#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002241static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
Damien Le Moal178053e2016-10-28 17:45:05 +09002242{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002243 struct block_device *bdev = FDEV(devi).bdev;
Damien Le Moal178053e2016-10-28 17:45:05 +09002244 sector_t nr_sectors = bdev->bd_part->nr_sects;
2245 sector_t sector = 0;
2246 struct blk_zone *zones;
2247 unsigned int i, nr_zones;
2248 unsigned int n = 0;
2249 int err = -EIO;
2250
Sheng Yongccd31cb2018-02-06 12:31:17 +08002251 if (!f2fs_sb_has_blkzoned(sbi->sb))
Damien Le Moal178053e2016-10-28 17:45:05 +09002252 return 0;
2253
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002254 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
Damien Le Moalf99e8642017-01-12 07:58:32 -07002255 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002256 return -EINVAL;
Damien Le Moalf99e8642017-01-12 07:58:32 -07002257 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002258 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
2259 __ilog2_u32(sbi->blocks_per_blkz))
2260 return -EINVAL;
Damien Le Moal178053e2016-10-28 17:45:05 +09002261 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002262 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
2263 sbi->log_blocks_per_blkz;
Damien Le Moalf99e8642017-01-12 07:58:32 -07002264 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002265 FDEV(devi).nr_blkz++;
Damien Le Moal178053e2016-10-28 17:45:05 +09002266
Chao Yu4e6aad22017-11-30 19:28:19 +08002267 FDEV(devi).blkz_type = f2fs_kmalloc(sbi, FDEV(devi).nr_blkz,
2268 GFP_KERNEL);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002269 if (!FDEV(devi).blkz_type)
Damien Le Moal178053e2016-10-28 17:45:05 +09002270 return -ENOMEM;
2271
2272#define F2FS_REPORT_NR_ZONES 4096
2273
Chao Yu4e6aad22017-11-30 19:28:19 +08002274 zones = f2fs_kzalloc(sbi, sizeof(struct blk_zone) *
2275 F2FS_REPORT_NR_ZONES, GFP_KERNEL);
Damien Le Moal178053e2016-10-28 17:45:05 +09002276 if (!zones)
2277 return -ENOMEM;
2278
2279 /* Get block zones type */
2280 while (zones && sector < nr_sectors) {
2281
2282 nr_zones = F2FS_REPORT_NR_ZONES;
2283 err = blkdev_report_zones(bdev, sector,
2284 zones, &nr_zones,
2285 GFP_KERNEL);
2286 if (err)
2287 break;
2288 if (!nr_zones) {
2289 err = -EIO;
2290 break;
2291 }
2292
2293 for (i = 0; i < nr_zones; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002294 FDEV(devi).blkz_type[n] = zones[i].type;
Damien Le Moal178053e2016-10-28 17:45:05 +09002295 sector += zones[i].len;
2296 n++;
2297 }
2298 }
2299
2300 kfree(zones);
2301
2302 return err;
2303}
2304#endif
2305
Gu Zheng9076a752013-10-14 18:47:11 +08002306/*
2307 * Read f2fs raw super block.
Shawn Lin2b39e902016-02-17 08:59:01 +08002308 * Because we have two copies of super block, so read both of them
2309 * to get the first valid one. If any one of them is broken, we pass
2310 * them recovery flag back to the caller.
Gu Zheng9076a752013-10-14 18:47:11 +08002311 */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002312static int read_raw_super_block(struct f2fs_sb_info *sbi,
Gu Zheng9076a752013-10-14 18:47:11 +08002313 struct f2fs_super_block **raw_super,
Chao Yue8240f62015-12-15 17:19:26 +08002314 int *valid_super_block, int *recovery)
majianpeng14d7e9d2013-02-01 19:07:03 +08002315{
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002316 struct super_block *sb = sbi->sb;
Shawn Lin2b39e902016-02-17 08:59:01 +08002317 int block;
Chao Yue8240f62015-12-15 17:19:26 +08002318 struct buffer_head *bh;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002319 struct f2fs_super_block *super;
hujianyangda554e42015-05-21 14:42:53 +08002320 int err = 0;
majianpeng14d7e9d2013-02-01 19:07:03 +08002321
Yunlei Heb39f0de2015-12-15 17:17:20 +08002322 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
2323 if (!super)
2324 return -ENOMEM;
Shawn Lin2b39e902016-02-17 08:59:01 +08002325
2326 for (block = 0; block < 2; block++) {
2327 bh = sb_bread(sb, block);
2328 if (!bh) {
2329 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
Gu Zheng9076a752013-10-14 18:47:11 +08002330 block + 1);
Shawn Lin2b39e902016-02-17 08:59:01 +08002331 err = -EIO;
2332 continue;
2333 }
majianpeng14d7e9d2013-02-01 19:07:03 +08002334
Shawn Lin2b39e902016-02-17 08:59:01 +08002335 /* sanity checking of raw super */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002336 if (sanity_check_raw_super(sbi, bh)) {
Shawn Lin2b39e902016-02-17 08:59:01 +08002337 f2fs_msg(sb, KERN_ERR,
2338 "Can't find valid F2FS filesystem in %dth superblock",
2339 block + 1);
2340 err = -EINVAL;
2341 brelse(bh);
2342 continue;
2343 }
2344
2345 if (!*raw_super) {
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002346 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
2347 sizeof(*super));
Shawn Lin2b39e902016-02-17 08:59:01 +08002348 *valid_super_block = block;
2349 *raw_super = super;
2350 }
Chao Yue8240f62015-12-15 17:19:26 +08002351 brelse(bh);
Shawn Lin2b39e902016-02-17 08:59:01 +08002352 }
2353
2354 /* Fail to read any one of the superblocks*/
2355 if (err < 0)
hujianyangda554e42015-05-21 14:42:53 +08002356 *recovery = 1;
hujianyangda554e42015-05-21 14:42:53 +08002357
hujianyangda554e42015-05-21 14:42:53 +08002358 /* No valid superblock */
Shawn Lin2b39e902016-02-17 08:59:01 +08002359 if (!*raw_super)
Yunlei Heb39f0de2015-12-15 17:17:20 +08002360 kfree(super);
Shawn Lin2b39e902016-02-17 08:59:01 +08002361 else
2362 err = 0;
hujianyangda554e42015-05-21 14:42:53 +08002363
Shawn Lin2b39e902016-02-17 08:59:01 +08002364 return err;
majianpeng14d7e9d2013-02-01 19:07:03 +08002365}
2366
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002367int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002368{
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002369 struct buffer_head *bh;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002370 int err;
2371
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002372 if ((recover && f2fs_readonly(sbi->sb)) ||
2373 bdev_read_only(sbi->sb->s_bdev)) {
2374 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07002375 return -EROFS;
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002376 }
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07002377
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002378 /* write back-up superblock first */
Sheng Yong0964fc12018-01-29 19:13:15 +08002379 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002380 if (!bh)
2381 return -EIO;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002382 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002383 brelse(bh);
Chao Yuc5bda1c2015-06-08 13:28:03 +08002384
2385 /* if we are in recovery path, skip writing valid superblock */
2386 if (recover || err)
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002387 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002388
Chao Yue8240f62015-12-15 17:19:26 +08002389 /* write current valid superblock */
Sheng Yong0964fc12018-01-29 19:13:15 +08002390 bh = sb_bread(sbi->sb, sbi->valid_super_block);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002391 if (!bh)
2392 return -EIO;
2393 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
2394 brelse(bh);
2395 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002396}
2397
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002398static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
2399{
2400 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002401 unsigned int max_devices = MAX_DEVICES;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002402 int i;
2403
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002404 /* Initialize single device information */
2405 if (!RDEV(0).path[0]) {
2406 if (!bdev_is_zoned(sbi->sb->s_bdev))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002407 return 0;
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002408 max_devices = 1;
2409 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002410
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002411 /*
2412 * Initialize multiple devices information, or single
2413 * zoned block device information.
2414 */
Chao Yu4e6aad22017-11-30 19:28:19 +08002415 sbi->devs = f2fs_kzalloc(sbi, sizeof(struct f2fs_dev_info) *
2416 max_devices, GFP_KERNEL);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002417 if (!sbi->devs)
2418 return -ENOMEM;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002419
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002420 for (i = 0; i < max_devices; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002421
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002422 if (i > 0 && !RDEV(i).path[0])
2423 break;
2424
2425 if (max_devices == 1) {
2426 /* Single zoned block device mount */
2427 FDEV(0).bdev =
2428 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002429 sbi->sb->s_mode, sbi->sb->s_type);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002430 } else {
2431 /* Multi-device mount */
2432 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
2433 FDEV(i).total_segments =
2434 le32_to_cpu(RDEV(i).total_segments);
2435 if (i == 0) {
2436 FDEV(i).start_blk = 0;
2437 FDEV(i).end_blk = FDEV(i).start_blk +
2438 (FDEV(i).total_segments <<
2439 sbi->log_blocks_per_seg) - 1 +
2440 le32_to_cpu(raw_super->segment0_blkaddr);
2441 } else {
2442 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
2443 FDEV(i).end_blk = FDEV(i).start_blk +
2444 (FDEV(i).total_segments <<
2445 sbi->log_blocks_per_seg) - 1;
2446 }
2447 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
2448 sbi->sb->s_mode, sbi->sb->s_type);
2449 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002450 if (IS_ERR(FDEV(i).bdev))
2451 return PTR_ERR(FDEV(i).bdev);
2452
2453 /* to release errored devices */
2454 sbi->s_ndevs = i + 1;
2455
2456#ifdef CONFIG_BLK_DEV_ZONED
2457 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
Sheng Yongccd31cb2018-02-06 12:31:17 +08002458 !f2fs_sb_has_blkzoned(sbi->sb)) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002459 f2fs_msg(sbi->sb, KERN_ERR,
2460 "Zoned block device feature not enabled\n");
2461 return -EINVAL;
2462 }
2463 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
2464 if (init_blkz_info(sbi, i)) {
2465 f2fs_msg(sbi->sb, KERN_ERR,
2466 "Failed to initialize F2FS blkzone information");
2467 return -EINVAL;
2468 }
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002469 if (max_devices == 1)
2470 break;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002471 f2fs_msg(sbi->sb, KERN_INFO,
2472 "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
2473 i, FDEV(i).path,
2474 FDEV(i).total_segments,
2475 FDEV(i).start_blk, FDEV(i).end_blk,
2476 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
2477 "Host-aware" : "Host-managed");
2478 continue;
2479 }
2480#endif
2481 f2fs_msg(sbi->sb, KERN_INFO,
2482 "Mount Device [%2d]: %20s, %8u, %8x - %8x",
2483 i, FDEV(i).path,
2484 FDEV(i).total_segments,
2485 FDEV(i).start_blk, FDEV(i).end_blk);
2486 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002487 f2fs_msg(sbi->sb, KERN_INFO,
2488 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002489 return 0;
2490}
2491
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002492static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
2493{
2494 struct f2fs_sb_info *sbi;
hujianyangda554e42015-05-21 14:42:53 +08002495 struct f2fs_super_block *raw_super;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002496 struct inode *root;
Sheng Yong99e3e852016-05-11 17:08:14 +08002497 int err;
Chao Yu2adc3502015-03-16 21:08:44 +08002498 bool retry = true, need_fsck = false;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002499 char *options = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08002500 int recovery, i, valid_super_block;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002501 struct curseg_info *seg_i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002502
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002503try_onemore:
hujianyangda554e42015-05-21 14:42:53 +08002504 err = -EINVAL;
2505 raw_super = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08002506 valid_super_block = -1;
hujianyangda554e42015-05-21 14:42:53 +08002507 recovery = 0;
2508
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002509 /* allocate memory for f2fs-specific super block info */
2510 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
2511 if (!sbi)
2512 return -ENOMEM;
2513
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002514 sbi->sb = sb;
2515
Keith Mok43b65732016-03-02 12:04:24 -08002516 /* Load the checksum driver */
2517 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
2518 if (IS_ERR(sbi->s_chksum_driver)) {
2519 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
2520 err = PTR_ERR(sbi->s_chksum_driver);
2521 sbi->s_chksum_driver = NULL;
2522 goto free_sbi;
2523 }
2524
Namjae Jeonff9234a2013-01-12 14:41:13 +09002525 /* set a block size */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002526 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002527 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002528 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +09002529 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002530
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002531 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
Chao Yue8240f62015-12-15 17:19:26 +08002532 &recovery);
Gu Zheng9076a752013-10-14 18:47:11 +08002533 if (err)
2534 goto free_sbi;
2535
Gu Zheng5fb08372013-06-07 14:16:53 +08002536 sb->s_fs_info = sbi;
Jaegeuk Kim52763a42016-06-13 09:47:48 -07002537 sbi->raw_super = raw_super;
2538
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -08002539 sbi->s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
2540 sbi->s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
2541
Chao Yu704956e2017-07-31 20:19:09 +08002542 /* precompute checksum seed for metadata */
2543 if (f2fs_sb_has_inode_chksum(sb))
2544 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
2545 sizeof(raw_super->uuid));
2546
Damien Le Moald1b959c2016-10-28 17:45:01 +09002547 /*
2548 * The BLKZONED feature indicates that the drive was formatted with
2549 * zone alignment optimization. This is optional for host-aware
2550 * devices, but mandatory for host-managed zoned block devices.
2551 */
2552#ifndef CONFIG_BLK_DEV_ZONED
Sheng Yongccd31cb2018-02-06 12:31:17 +08002553 if (f2fs_sb_has_blkzoned(sb)) {
Damien Le Moald1b959c2016-10-28 17:45:01 +09002554 f2fs_msg(sb, KERN_ERR,
2555 "Zoned block device support is not enabled\n");
Chao Yu1727f312017-06-12 09:44:27 +08002556 err = -EOPNOTSUPP;
Damien Le Moald1b959c2016-10-28 17:45:01 +09002557 goto free_sb_buf;
2558 }
Damien Le Moald1b959c2016-10-28 17:45:01 +09002559#endif
Yunlei He498c5e92015-05-07 18:11:37 +08002560 default_options(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002561 /* parse mount options */
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002562 options = kstrdup((const char *)data, GFP_KERNEL);
2563 if (data && !options) {
2564 err = -ENOMEM;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002565 goto free_sb_buf;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002566 }
2567
2568 err = parse_options(sb, options);
2569 if (err)
2570 goto free_options;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002571
Chao Yue0afc4d2015-12-31 14:35:37 +08002572 sbi->max_file_blocks = max_file_blocks();
2573 sb->s_maxbytes = sbi->max_file_blocks <<
2574 le32_to_cpu(raw_super->log_blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002575 sb->s_max_links = F2FS_LINK_MAX;
2576 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2577
Chao Yu0abd6752017-07-09 00:13:07 +08002578#ifdef CONFIG_QUOTA
2579 sb->dq_op = &f2fs_quota_operations;
Jaegeuk Kimea676732017-10-06 09:14:28 -07002580 if (f2fs_sb_has_quota_ino(sb))
2581 sb->s_qcop = &dquot_quotactl_sysfile_ops;
2582 else
2583 sb->s_qcop = &f2fs_quotactl_ops;
Chao Yu5c571322017-07-26 00:01:41 +08002584 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
Chao Yu292c196a2017-11-16 16:59:14 +08002585
2586 if (f2fs_sb_has_quota_ino(sbi->sb)) {
2587 for (i = 0; i < MAXQUOTAS; i++) {
2588 if (f2fs_qf_ino(sbi->sb, i))
2589 sbi->nquota_files++;
2590 }
2591 }
Chao Yu0abd6752017-07-09 00:13:07 +08002592#endif
2593
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002594 sb->s_op = &f2fs_sops;
Eric Biggersffcc41822017-10-09 12:15:38 -07002595#ifdef CONFIG_F2FS_FS_ENCRYPTION
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002596 sb->s_cop = &f2fs_cryptops;
Eric Biggersffcc41822017-10-09 12:15:38 -07002597#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002598 sb->s_xattr = f2fs_xattr_handlers;
2599 sb->s_export_op = &f2fs_export_ops;
2600 sb->s_magic = F2FS_SUPER_MAGIC;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002601 sb->s_time_gran = 1;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002602 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2603 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
Christoph Hellwig85787092017-05-10 15:06:33 +02002604 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
Yufen Yu578c6472018-01-09 19:33:39 +08002605 sb->s_iflags |= SB_I_CGROUPWB;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002606
2607 /* init f2fs-specific super block info */
Chao Yue8240f62015-12-15 17:19:26 +08002608 sbi->valid_super_block = valid_super_block;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002609 mutex_init(&sbi->gc_mutex);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002610 mutex_init(&sbi->cp_mutex);
Chao Yub3582c62014-07-03 18:58:39 +08002611 init_rwsem(&sbi->node_write);
Yunlei He59c90812017-03-13 20:22:18 +08002612 init_rwsem(&sbi->node_change);
Jaegeuk Kim315df832015-08-11 12:45:39 -07002613
2614 /* disallow all the data/node/meta page writes */
2615 set_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002616 spin_lock_init(&sbi->stat_lock);
Jaegeuk Kim971767c2013-11-18 17:16:17 +09002617
Chao Yub0af6d42017-08-02 23:21:48 +08002618 /* init iostat info */
2619 spin_lock_init(&sbi->iostat_lock);
2620 sbi->iostat_enable = false;
2621
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002622 for (i = 0; i < NR_PAGE_TYPE; i++) {
Jaegeuk Kima912b542017-05-10 11:18:25 -07002623 int n = (i == META) ? 1: NR_TEMP_TYPE;
2624 int j;
2625
Chao Yu4e6aad22017-11-30 19:28:19 +08002626 sbi->write_io[i] = f2fs_kmalloc(sbi,
2627 n * sizeof(struct f2fs_bio_info),
2628 GFP_KERNEL);
Christophe JAILLETb63def92017-06-11 09:21:11 +02002629 if (!sbi->write_io[i]) {
2630 err = -ENOMEM;
Jaegeuk Kima912b542017-05-10 11:18:25 -07002631 goto free_options;
Christophe JAILLETb63def92017-06-11 09:21:11 +02002632 }
Jaegeuk Kima912b542017-05-10 11:18:25 -07002633
2634 for (j = HOT; j < n; j++) {
2635 init_rwsem(&sbi->write_io[i][j].io_rwsem);
2636 sbi->write_io[i][j].sbi = sbi;
2637 sbi->write_io[i][j].bio = NULL;
Chao Yufb830fc2017-05-19 23:37:01 +08002638 spin_lock_init(&sbi->write_io[i][j].io_lock);
2639 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
Jaegeuk Kima912b542017-05-10 11:18:25 -07002640 }
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002641 }
Jaegeuk Kim971767c2013-11-18 17:16:17 +09002642
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002643 init_rwsem(&sbi->cp_rwsem);
Changman Leefb51b5e2013-11-07 12:48:25 +09002644 init_waitqueue_head(&sbi->cp_wait);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002645 init_sb_info(sbi);
2646
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002647 err = init_percpu_info(sbi);
2648 if (err)
Chao Yud7997e62018-01-17 16:31:35 +08002649 goto free_bio_info;
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002650
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002651 if (F2FS_IO_SIZE(sbi) > 1) {
2652 sbi->write_io_dummy =
Chao Yua3ebfe42017-02-27 18:43:13 +08002653 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
Chao Yu1727f312017-06-12 09:44:27 +08002654 if (!sbi->write_io_dummy) {
2655 err = -ENOMEM;
Chao Yud7997e62018-01-17 16:31:35 +08002656 goto free_percpu;
Chao Yu1727f312017-06-12 09:44:27 +08002657 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002658 }
2659
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002660 /* get an inode for meta space */
2661 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
2662 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002663 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002664 err = PTR_ERR(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002665 goto free_io_dummy;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002666 }
2667
2668 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002669 if (err) {
2670 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002671 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +09002672 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002673
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002674 /* Initialize device list */
2675 err = f2fs_scan_devices(sbi);
2676 if (err) {
2677 f2fs_msg(sb, KERN_ERR, "Failed to find devices");
2678 goto free_devices;
2679 }
2680
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002681 sbi->total_valid_node_count =
2682 le32_to_cpu(sbi->ckpt->valid_node_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07002683 percpu_counter_set(&sbi->total_valid_inode_count,
2684 le32_to_cpu(sbi->ckpt->valid_inode_count));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002685 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
2686 sbi->total_valid_block_count =
2687 le64_to_cpu(sbi->ckpt->valid_block_count);
2688 sbi->last_valid_block_count = sbi->total_valid_block_count;
Chao Yudaeb4332017-06-26 16:24:41 +08002689 sbi->reserved_blocks = 0;
Yunlong Song80d42142017-10-27 20:45:05 +08002690 sbi->current_reserved_blocks = 0;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08002691 limit_reserve_root(sbi);
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002692
Chao Yuc227f912015-12-16 13:09:20 +08002693 for (i = 0; i < NR_INODE_TYPE; i++) {
2694 INIT_LIST_HEAD(&sbi->inode_list[i]);
2695 spin_lock_init(&sbi->inode_lock[i]);
2696 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002697
Chao Yu1dcc3362015-02-05 17:57:31 +08002698 init_extent_cache_info(sbi);
2699
Jaegeuk Kim6451e042014-07-25 15:47:17 -07002700 init_ino_entry_info(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002701
2702 /* setup f2fs internal modules */
2703 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002704 if (err) {
2705 f2fs_msg(sb, KERN_ERR,
2706 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002707 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +09002708 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002709 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002710 if (err) {
2711 f2fs_msg(sb, KERN_ERR,
2712 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002713 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +09002714 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002715
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002716 /* For write statistics */
2717 if (sb->s_bdev->bd_part)
2718 sbi->sectors_written_start =
2719 (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
2720
2721 /* Read accumulated write IO statistics if exists */
2722 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
2723 if (__exist_node_summaries(sbi))
2724 sbi->kbytes_written =
Shuoran Liub2dde6f2016-03-29 18:00:15 +08002725 le64_to_cpu(seg_i->journal->info.kbytes_written);
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002726
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002727 build_gc_manager(sbi);
2728
2729 /* get an inode for node space */
2730 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
2731 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002732 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002733 err = PTR_ERR(sbi->node_inode);
2734 goto free_nm;
2735 }
2736
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08002737 err = f2fs_build_stats(sbi);
2738 if (err)
Chao Yubae01ed2017-11-30 19:28:20 +08002739 goto free_node_inode;
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08002740
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002741 /* read root inode and dentry */
2742 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
2743 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002744 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002745 err = PTR_ERR(root);
Chao Yubae01ed2017-11-30 19:28:20 +08002746 goto free_stats;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002747 }
Chao Yu8f99a942013-11-28 15:43:43 +08002748 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
Chao Yu9d847952014-07-25 12:55:09 +08002749 iput(root);
Chao Yu8f99a942013-11-28 15:43:43 +08002750 err = -EINVAL;
Chao Yu9d847952014-07-25 12:55:09 +08002751 goto free_node_inode;
Chao Yu8f99a942013-11-28 15:43:43 +08002752 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002753
2754 sb->s_root = d_make_root(root); /* allocate root dentry */
2755 if (!sb->s_root) {
2756 err = -ENOMEM;
2757 goto free_root_inode;
2758 }
2759
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002760 err = f2fs_register_sysfs(sbi);
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09002761 if (err)
Chao Yua3981012017-06-14 17:39:46 +08002762 goto free_root_inode;
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09002763
Jaegeuk Kimea676732017-10-06 09:14:28 -07002764#ifdef CONFIG_QUOTA
2765 /*
2766 * Turn on quotas which were not enabled for read-only mounts if
2767 * filesystem has quota feature, so that they are updated correctly.
2768 */
2769 if (f2fs_sb_has_quota_ino(sb) && !sb_rdonly(sb)) {
2770 err = f2fs_enable_quotas(sb);
2771 if (err) {
2772 f2fs_msg(sb, KERN_ERR,
2773 "Cannot turn on quotas: error %d", err);
2774 goto free_sysfs;
2775 }
2776 }
2777#endif
Chao Yu4b2414d2017-08-08 10:54:31 +08002778 /* if there are nt orphan nodes free them */
2779 err = recover_orphan_inodes(sbi);
2780 if (err)
Jaegeuk Kimea676732017-10-06 09:14:28 -07002781 goto free_meta;
Chao Yu4b2414d2017-08-08 10:54:31 +08002782
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002783 /* recover fsynced data */
2784 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002785 /*
2786 * mount should be failed, when device has readonly mode, and
2787 * previous checkpoint was not done by clean system shutdown.
2788 */
2789 if (bdev_read_only(sb->s_bdev) &&
Chao Yuaaec2b12016-09-20 11:04:18 +08002790 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002791 err = -EROFS;
Chao Yu4b2414d2017-08-08 10:54:31 +08002792 goto free_meta;
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002793 }
Chao Yu2adc3502015-03-16 21:08:44 +08002794
2795 if (need_fsck)
2796 set_sbi_flag(sbi, SBI_NEED_FSCK);
2797
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07002798 if (!retry)
2799 goto skip_recovery;
2800
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002801 err = recover_fsync_data(sbi, false);
2802 if (err < 0) {
Chao Yu2adc3502015-03-16 21:08:44 +08002803 need_fsck = true;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002804 f2fs_msg(sb, KERN_ERR,
Sheng Yong99e3e852016-05-11 17:08:14 +08002805 "Cannot recover all fsync data errno=%d", err);
Chao Yu4b2414d2017-08-08 10:54:31 +08002806 goto free_meta;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002807 }
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002808 } else {
2809 err = recover_fsync_data(sbi, true);
2810
2811 if (!f2fs_readonly(sb) && err > 0) {
2812 err = -EINVAL;
2813 f2fs_msg(sb, KERN_ERR,
2814 "Need to recover fsync data");
Jaegeuk Kimea676732017-10-06 09:14:28 -07002815 goto free_meta;
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002816 }
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002817 }
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07002818skip_recovery:
Jaegeuk Kim315df832015-08-11 12:45:39 -07002819 /* recover_fsync_data() cleared this already */
2820 clear_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002821
2822 /*
2823 * If filesystem is not mounted as read-only then
2824 * do start the gc_thread.
2825 */
Chao Yu6c029932014-11-18 11:16:01 +08002826 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002827 /* After POR, we can run background GC thread.*/
2828 err = start_gc_thread(sbi);
2829 if (err)
Chao Yu4b2414d2017-08-08 10:54:31 +08002830 goto free_meta;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002831 }
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002832 kfree(options);
hujianyangda554e42015-05-21 14:42:53 +08002833
2834 /* recover broken superblock */
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07002835 if (recovery) {
Chao Yu41214b32016-02-22 18:33:20 +08002836 err = f2fs_commit_super(sbi, true);
2837 f2fs_msg(sb, KERN_INFO,
Sheng Yong99e3e852016-05-11 17:08:14 +08002838 "Try to recover %dth superblock, ret: %d",
Chao Yu41214b32016-02-22 18:33:20 +08002839 sbi->valid_super_block ? 1 : 2, err);
hujianyangda554e42015-05-21 14:42:53 +08002840 }
2841
Chao Yubae01ed2017-11-30 19:28:20 +08002842 f2fs_join_shrinker(sbi);
2843
Jaegeuk Kim1200abb2017-02-09 13:25:35 -08002844 f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
2845 cur_cp_version(F2FS_CKPT(sbi)));
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08002846 f2fs_update_time(sbi, CP_TIME);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002847 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002848 return 0;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002849
Chao Yu4b2414d2017-08-08 10:54:31 +08002850free_meta:
Jaegeuk Kimea676732017-10-06 09:14:28 -07002851#ifdef CONFIG_QUOTA
2852 if (f2fs_sb_has_quota_ino(sb) && !sb_rdonly(sb))
2853 f2fs_quota_off_umount(sbi->sb);
2854#endif
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07002855 f2fs_sync_inode_meta(sbi);
Chao Yu4b2414d2017-08-08 10:54:31 +08002856 /*
2857 * Some dirty meta pages can be produced by recover_orphan_inodes()
2858 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
2859 * followed by write_checkpoint() through f2fs_write_node_pages(), which
2860 * falls into an infinite loop in sync_meta_pages().
2861 */
2862 truncate_inode_pages_final(META_MAPPING(sbi));
Jaegeuk Kimea676732017-10-06 09:14:28 -07002863#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +08002864free_sysfs:
Jaegeuk Kimea676732017-10-06 09:14:28 -07002865#endif
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002866 f2fs_unregister_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002867free_root_inode:
2868 dput(sb->s_root);
2869 sb->s_root = NULL;
Chao Yubae01ed2017-11-30 19:28:20 +08002870free_stats:
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08002871 f2fs_destroy_stats(sbi);
Chao Yubae01ed2017-11-30 19:28:20 +08002872free_node_inode:
2873 release_ino_entry(sbi, true);
2874 truncate_inode_pages_final(NODE_MAPPING(sbi));
2875 iput(sbi->node_inode);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002876free_nm:
2877 destroy_node_manager(sbi);
2878free_sm:
2879 destroy_segment_manager(sbi);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002880free_devices:
2881 destroy_device_list(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002882 kfree(sbi->ckpt);
2883free_meta_inode:
2884 make_bad_inode(sbi->meta_inode);
2885 iput(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002886free_io_dummy:
2887 mempool_destroy(sbi->write_io_dummy);
Chao Yud7997e62018-01-17 16:31:35 +08002888free_percpu:
2889 destroy_percpu_info(sbi);
2890free_bio_info:
Jaegeuk Kima912b542017-05-10 11:18:25 -07002891 for (i = 0; i < NR_PAGE_TYPE; i++)
2892 kfree(sbi->write_io[i]);
Chao Yud7997e62018-01-17 16:31:35 +08002893free_options:
Chao Yu4b2414d2017-08-08 10:54:31 +08002894#ifdef CONFIG_QUOTA
2895 for (i = 0; i < MAXQUOTAS; i++)
2896 kfree(sbi->s_qf_names[i]);
2897#endif
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002898 kfree(options);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002899free_sb_buf:
Yunlei Heb39f0de2015-12-15 17:17:20 +08002900 kfree(raw_super);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002901free_sbi:
Keith Mok43b65732016-03-02 12:04:24 -08002902 if (sbi->s_chksum_driver)
2903 crypto_free_shash(sbi->s_chksum_driver);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002904 kfree(sbi);
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002905
2906 /* give only one another chance */
2907 if (retry) {
Taehee Yoo9df47ba2015-04-13 21:48:06 +09002908 retry = false;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002909 shrink_dcache_sb(sb);
2910 goto try_onemore;
2911 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002912 return err;
2913}
2914
2915static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
2916 const char *dev_name, void *data)
2917{
2918 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
2919}
2920
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002921static void kill_f2fs_super(struct super_block *sb)
2922{
Chao Yucce13252017-06-29 23:17:45 +08002923 if (sb->s_root) {
Chao Yucaf00472015-01-28 17:48:42 +08002924 set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
Chao Yucce13252017-06-29 23:17:45 +08002925 stop_gc_thread(F2FS_SB(sb));
2926 stop_discard_thread(F2FS_SB(sb));
2927 }
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002928 kill_block_super(sb);
2929}
2930
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002931static struct file_system_type f2fs_fs_type = {
2932 .owner = THIS_MODULE,
2933 .name = "f2fs",
2934 .mount = f2fs_mount,
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002935 .kill_sb = kill_f2fs_super,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002936 .fs_flags = FS_REQUIRES_DEV,
2937};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08002938MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002939
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002940static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002941{
Vladimir Davydov5d097052016-01-14 15:18:21 -08002942 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
2943 sizeof(struct f2fs_inode_info), 0,
2944 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002945 if (!f2fs_inode_cachep)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002946 return -ENOMEM;
2947 return 0;
2948}
2949
2950static void destroy_inodecache(void)
2951{
2952 /*
2953 * Make sure all delayed rcu free inodes are flushed before we
2954 * destroy cache.
2955 */
2956 rcu_barrier();
2957 kmem_cache_destroy(f2fs_inode_cachep);
2958}
2959
2960static int __init init_f2fs_fs(void)
2961{
2962 int err;
2963
Jaegeuk Kimc0508652015-01-07 14:07:36 -08002964 f2fs_build_trace_ios();
2965
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002966 err = init_inodecache();
2967 if (err)
2968 goto fail;
2969 err = create_node_manager_caches();
2970 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002971 goto free_inodecache;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002972 err = create_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002973 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002974 goto free_node_manager_caches;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002975 err = create_checkpoint_caches();
2976 if (err)
Chao Yu06292072014-12-29 15:56:18 +08002977 goto free_segment_manager_caches;
Chao Yu1dcc3362015-02-05 17:57:31 +08002978 err = create_extent_cache();
2979 if (err)
2980 goto free_checkpoint_caches;
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002981 err = f2fs_init_sysfs();
Chao Yua3981012017-06-14 17:39:46 +08002982 if (err)
Chao Yu1dcc3362015-02-05 17:57:31 +08002983 goto free_extent_cache;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002984 err = register_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimcfc4d972015-05-15 15:37:24 -07002985 if (err)
Chao Yua3981012017-06-14 17:39:46 +08002986 goto free_sysfs;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002987 err = register_filesystem(&f2fs_fs_type);
2988 if (err)
2989 goto free_shrinker;
Chao Yu787c7b8c2015-10-29 09:13:04 +08002990 err = f2fs_create_root_stats();
2991 if (err)
2992 goto free_filesystem;
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002993 return 0;
2994
Chao Yu787c7b8c2015-10-29 09:13:04 +08002995free_filesystem:
2996 unregister_filesystem(&f2fs_fs_type);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002997free_shrinker:
2998 unregister_shrinker(&f2fs_shrinker_info);
Chao Yua3981012017-06-14 17:39:46 +08002999free_sysfs:
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07003000 f2fs_exit_sysfs();
Chao Yu1dcc3362015-02-05 17:57:31 +08003001free_extent_cache:
3002 destroy_extent_cache();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08003003free_checkpoint_caches:
3004 destroy_checkpoint_caches();
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09003005free_segment_manager_caches:
3006 destroy_segment_manager_caches();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08003007free_node_manager_caches:
3008 destroy_node_manager_caches();
3009free_inodecache:
3010 destroy_inodecache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003011fail:
3012 return err;
3013}
3014
3015static void __exit exit_f2fs_fs(void)
3016{
Namjae Jeon4589d252013-01-15 19:58:47 +09003017 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003018 unregister_filesystem(&f2fs_fs_type);
Tiezhu Yangb8bef792016-05-18 08:02:25 +08003019 unregister_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07003020 f2fs_exit_sysfs();
Wanpeng Lifdf6c8b2015-03-06 15:00:54 +08003021 destroy_extent_cache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003022 destroy_checkpoint_caches();
Changman Lee5dcd8a72013-12-11 14:32:13 +09003023 destroy_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003024 destroy_node_manager_caches();
3025 destroy_inodecache();
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -08003026 f2fs_destroy_trace_ios();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003027}
3028
3029module_init(init_f2fs_fs)
3030module_exit(exit_f2fs_fs)
3031
3032MODULE_AUTHOR("Samsung Electronics's Praesto Team");
3033MODULE_DESCRIPTION("Flash Friendly File System");
3034MODULE_LICENSE("GPL");
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -07003035