f2fs: fix to recover inode's uid/gid during POR
[linux-2.6-block.git] / fs / f2fs / super.c
CommitLineData
7c1a000d 1// SPDX-License-Identifier: GPL-2.0
0a8165d7 2/*
aff063e2
JK
3 * fs/f2fs/super.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
aff063e2
JK
7 */
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/fs.h>
11#include <linux/statfs.h>
aff063e2
JK
12#include <linux/buffer_head.h>
13#include <linux/backing-dev.h>
14#include <linux/kthread.h>
15#include <linux/parser.h>
16#include <linux/mount.h>
17#include <linux/seq_file.h>
5e176d54 18#include <linux/proc_fs.h>
aff063e2
JK
19#include <linux/random.h>
20#include <linux/exportfs.h>
d3ee456d 21#include <linux/blkdev.h>
0abd675e 22#include <linux/quotaops.h>
aff063e2 23#include <linux/f2fs_fs.h>
b59d0bae 24#include <linux/sysfs.h>
4b2414d0 25#include <linux/quota.h>
aff063e2
JK
26
27#include "f2fs.h"
28#include "node.h"
5ec4e49f 29#include "segment.h"
aff063e2 30#include "xattr.h"
b59d0bae 31#include "gc.h"
db9f7c1a 32#include "trace.h"
aff063e2 33
a2a4a7e4
NJ
34#define CREATE_TRACE_POINTS
35#include <trace/events/f2fs.h>
36
aff063e2
JK
37static struct kmem_cache *f2fs_inode_cachep;
38
73faec4d 39#ifdef CONFIG_F2FS_FAULT_INJECTION
2c63fead 40
2d3a5856 41char *f2fs_fault_name[FAULT_MAX] = {
2c63fead 42 [FAULT_KMALLOC] = "kmalloc",
628b3d14 43 [FAULT_KVMALLOC] = "kvmalloc",
c41f3cc3 44 [FAULT_PAGE_ALLOC] = "page alloc",
01eccef7 45 [FAULT_PAGE_GET] = "page get",
d62fe971 46 [FAULT_ALLOC_BIO] = "alloc bio",
cb78942b
JK
47 [FAULT_ALLOC_NID] = "alloc nid",
48 [FAULT_ORPHAN] = "orphan",
49 [FAULT_BLOCK] = "no more block",
50 [FAULT_DIR_DEPTH] = "too big dir depth",
53aa6bbf 51 [FAULT_EVICT_INODE] = "evict_inode fail",
14b44d23 52 [FAULT_TRUNCATE] = "truncate fail",
6f5c2ed0 53 [FAULT_READ_IO] = "read IO error",
0f348028 54 [FAULT_CHECKPOINT] = "checkpoint error",
b83dcfe6 55 [FAULT_DISCARD] = "discard error",
6f5c2ed0 56 [FAULT_WRITE_IO] = "write IO error",
2c63fead 57};
08796897 58
d494500a
CY
59void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
60 unsigned int type)
08796897 61{
63189b78 62 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
1ecc0c5c 63
08796897 64 if (rate) {
1ecc0c5c
CY
65 atomic_set(&ffi->inject_ops, 0);
66 ffi->inject_rate = rate;
08796897 67 }
d494500a
CY
68
69 if (type)
70 ffi->inject_type = type;
71
72 if (!rate && !type)
73 memset(ffi, 0, sizeof(struct f2fs_fault_info));
08796897 74}
73faec4d
JK
75#endif
76
2658e50d
JK
77/* f2fs-wide shrinker description */
78static struct shrinker f2fs_shrinker_info = {
79 .scan_objects = f2fs_shrink_scan,
80 .count_objects = f2fs_shrink_count,
81 .seeks = DEFAULT_SEEKS,
82};
83
aff063e2 84enum {
696c018c 85 Opt_gc_background,
aff063e2 86 Opt_disable_roll_forward,
2d834bf9 87 Opt_norecovery,
aff063e2 88 Opt_discard,
64058be9 89 Opt_nodiscard,
aff063e2 90 Opt_noheap,
7a20b8a6 91 Opt_heap,
4058c511 92 Opt_user_xattr,
aff063e2 93 Opt_nouser_xattr,
4058c511 94 Opt_acl,
aff063e2
JK
95 Opt_noacl,
96 Opt_active_logs,
97 Opt_disable_ext_identify,
444c580f 98 Opt_inline_xattr,
23cf7212 99 Opt_noinline_xattr,
6afc662e 100 Opt_inline_xattr_size,
8274de77 101 Opt_inline_data,
5efd3c6f 102 Opt_inline_dentry,
97c1794a 103 Opt_noinline_dentry,
6b4afdd7 104 Opt_flush_merge,
69e9e427 105 Opt_noflush_merge,
0f7b2abd 106 Opt_nobarrier,
d5053a34 107 Opt_fastboot,
89672159 108 Opt_extent_cache,
7daaea25 109 Opt_noextent_cache,
75342797 110 Opt_noinline_data,
343f40f0 111 Opt_data_flush,
7e65be49 112 Opt_reserve_root,
7c2e5963
JK
113 Opt_resgid,
114 Opt_resuid,
36abef4e 115 Opt_mode,
ec91538d 116 Opt_io_size_bits,
73faec4d 117 Opt_fault_injection,
d494500a 118 Opt_fault_type,
6d94c74a
JK
119 Opt_lazytime,
120 Opt_nolazytime,
4b2414d0
CY
121 Opt_quota,
122 Opt_noquota,
0abd675e
CY
123 Opt_usrquota,
124 Opt_grpquota,
5c57132e 125 Opt_prjquota,
4b2414d0
CY
126 Opt_usrjquota,
127 Opt_grpjquota,
128 Opt_prjjquota,
129 Opt_offusrjquota,
130 Opt_offgrpjquota,
131 Opt_offprjjquota,
132 Opt_jqfmt_vfsold,
133 Opt_jqfmt_vfsv0,
134 Opt_jqfmt_vfsv1,
0cdd3195 135 Opt_whint,
07939627 136 Opt_alloc,
93cf93f1 137 Opt_fsync,
ff62af20 138 Opt_test_dummy_encryption,
aff063e2
JK
139 Opt_err,
140};
141
142static match_table_t f2fs_tokens = {
696c018c 143 {Opt_gc_background, "background_gc=%s"},
aff063e2 144 {Opt_disable_roll_forward, "disable_roll_forward"},
2d834bf9 145 {Opt_norecovery, "norecovery"},
aff063e2 146 {Opt_discard, "discard"},
64058be9 147 {Opt_nodiscard, "nodiscard"},
aff063e2 148 {Opt_noheap, "no_heap"},
7a20b8a6 149 {Opt_heap, "heap"},
4058c511 150 {Opt_user_xattr, "user_xattr"},
aff063e2 151 {Opt_nouser_xattr, "nouser_xattr"},
4058c511 152 {Opt_acl, "acl"},
aff063e2
JK
153 {Opt_noacl, "noacl"},
154 {Opt_active_logs, "active_logs=%u"},
155 {Opt_disable_ext_identify, "disable_ext_identify"},
444c580f 156 {Opt_inline_xattr, "inline_xattr"},
23cf7212 157 {Opt_noinline_xattr, "noinline_xattr"},
6afc662e 158 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
8274de77 159 {Opt_inline_data, "inline_data"},
5efd3c6f 160 {Opt_inline_dentry, "inline_dentry"},
97c1794a 161 {Opt_noinline_dentry, "noinline_dentry"},
6b4afdd7 162 {Opt_flush_merge, "flush_merge"},
69e9e427 163 {Opt_noflush_merge, "noflush_merge"},
0f7b2abd 164 {Opt_nobarrier, "nobarrier"},
d5053a34 165 {Opt_fastboot, "fastboot"},
89672159 166 {Opt_extent_cache, "extent_cache"},
7daaea25 167 {Opt_noextent_cache, "noextent_cache"},
75342797 168 {Opt_noinline_data, "noinline_data"},
343f40f0 169 {Opt_data_flush, "data_flush"},
7e65be49 170 {Opt_reserve_root, "reserve_root=%u"},
7c2e5963
JK
171 {Opt_resgid, "resgid=%u"},
172 {Opt_resuid, "resuid=%u"},
36abef4e 173 {Opt_mode, "mode=%s"},
ec91538d 174 {Opt_io_size_bits, "io_bits=%u"},
73faec4d 175 {Opt_fault_injection, "fault_injection=%u"},
d494500a 176 {Opt_fault_type, "fault_type=%u"},
6d94c74a
JK
177 {Opt_lazytime, "lazytime"},
178 {Opt_nolazytime, "nolazytime"},
4b2414d0
CY
179 {Opt_quota, "quota"},
180 {Opt_noquota, "noquota"},
0abd675e
CY
181 {Opt_usrquota, "usrquota"},
182 {Opt_grpquota, "grpquota"},
5c57132e 183 {Opt_prjquota, "prjquota"},
4b2414d0
CY
184 {Opt_usrjquota, "usrjquota=%s"},
185 {Opt_grpjquota, "grpjquota=%s"},
186 {Opt_prjjquota, "prjjquota=%s"},
187 {Opt_offusrjquota, "usrjquota="},
188 {Opt_offgrpjquota, "grpjquota="},
189 {Opt_offprjjquota, "prjjquota="},
190 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
191 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
192 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
0cdd3195 193 {Opt_whint, "whint_mode=%s"},
07939627 194 {Opt_alloc, "alloc_mode=%s"},
93cf93f1 195 {Opt_fsync, "fsync_mode=%s"},
ff62af20 196 {Opt_test_dummy_encryption, "test_dummy_encryption"},
aff063e2
JK
197 {Opt_err, NULL},
198};
199
a07ef784
NJ
200void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
201{
202 struct va_format vaf;
203 va_list args;
204
205 va_start(args, fmt);
206 vaf.fmt = fmt;
207 vaf.va = &args;
22d7ea13 208 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
a07ef784
NJ
209 va_end(args);
210}
211
7e65be49
JK
212static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
213{
214 block_t limit = (sbi->user_block_count << 1) / 1000;
215
216 /* limit is 0.2% */
63189b78
CY
217 if (test_opt(sbi, RESERVE_ROOT) &&
218 F2FS_OPTION(sbi).root_reserved_blocks > limit) {
219 F2FS_OPTION(sbi).root_reserved_blocks = limit;
7e65be49
JK
220 f2fs_msg(sbi->sb, KERN_INFO,
221 "Reduce reserved blocks for root = %u",
63189b78 222 F2FS_OPTION(sbi).root_reserved_blocks);
7e65be49 223 }
7c2e5963 224 if (!test_opt(sbi, RESERVE_ROOT) &&
63189b78 225 (!uid_eq(F2FS_OPTION(sbi).s_resuid,
7c2e5963 226 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
63189b78 227 !gid_eq(F2FS_OPTION(sbi).s_resgid,
7c2e5963
JK
228 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
229 f2fs_msg(sbi->sb, KERN_INFO,
230 "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
63189b78
CY
231 from_kuid_munged(&init_user_ns,
232 F2FS_OPTION(sbi).s_resuid),
233 from_kgid_munged(&init_user_ns,
234 F2FS_OPTION(sbi).s_resgid));
7e65be49
JK
235}
236
aff063e2
JK
237static void init_once(void *foo)
238{
239 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
240
aff063e2
JK
241 inode_init_once(&fi->vfs_inode);
242}
243
4b2414d0
CY
244#ifdef CONFIG_QUOTA
245static const char * const quotatypes[] = INITQFNAMES;
246#define QTYPE2NAME(t) (quotatypes[t])
247static int f2fs_set_qf_name(struct super_block *sb, int qtype,
248 substring_t *args)
249{
250 struct f2fs_sb_info *sbi = F2FS_SB(sb);
251 char *qname;
252 int ret = -EINVAL;
253
63189b78 254 if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
4b2414d0
CY
255 f2fs_msg(sb, KERN_ERR,
256 "Cannot change journaled "
257 "quota options when quota turned on");
258 return -EINVAL;
259 }
ea676733
JK
260 if (f2fs_sb_has_quota_ino(sb)) {
261 f2fs_msg(sb, KERN_INFO,
262 "QUOTA feature is enabled, so ignore qf_name");
263 return 0;
264 }
265
4b2414d0
CY
266 qname = match_strdup(args);
267 if (!qname) {
268 f2fs_msg(sb, KERN_ERR,
269 "Not enough memory for storing quotafile name");
270 return -EINVAL;
271 }
63189b78
CY
272 if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
273 if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
4b2414d0
CY
274 ret = 0;
275 else
276 f2fs_msg(sb, KERN_ERR,
277 "%s quota file already specified",
278 QTYPE2NAME(qtype));
279 goto errout;
280 }
281 if (strchr(qname, '/')) {
282 f2fs_msg(sb, KERN_ERR,
283 "quotafile must be on filesystem root");
284 goto errout;
285 }
63189b78 286 F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
4b2414d0
CY
287 set_opt(sbi, QUOTA);
288 return 0;
289errout:
290 kfree(qname);
291 return ret;
292}
293
294static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
295{
296 struct f2fs_sb_info *sbi = F2FS_SB(sb);
297
63189b78 298 if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
4b2414d0
CY
299 f2fs_msg(sb, KERN_ERR, "Cannot change journaled quota options"
300 " when quota turned on");
301 return -EINVAL;
302 }
63189b78
CY
303 kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
304 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
4b2414d0
CY
305 return 0;
306}
307
308static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
309{
310 /*
311 * We do the test below only for project quotas. 'usrquota' and
312 * 'grpquota' mount options are allowed even without quota feature
313 * to support legacy quotas in quota files.
314 */
315 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi->sb)) {
316 f2fs_msg(sbi->sb, KERN_ERR, "Project quota feature not enabled. "
317 "Cannot enable project quota enforcement.");
318 return -1;
319 }
63189b78
CY
320 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
321 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
322 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
323 if (test_opt(sbi, USRQUOTA) &&
324 F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
4b2414d0
CY
325 clear_opt(sbi, USRQUOTA);
326
63189b78
CY
327 if (test_opt(sbi, GRPQUOTA) &&
328 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
4b2414d0
CY
329 clear_opt(sbi, GRPQUOTA);
330
63189b78
CY
331 if (test_opt(sbi, PRJQUOTA) &&
332 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4b2414d0
CY
333 clear_opt(sbi, PRJQUOTA);
334
335 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
336 test_opt(sbi, PRJQUOTA)) {
337 f2fs_msg(sbi->sb, KERN_ERR, "old and new quota "
338 "format mixing");
339 return -1;
340 }
341
63189b78 342 if (!F2FS_OPTION(sbi).s_jquota_fmt) {
4b2414d0
CY
343 f2fs_msg(sbi->sb, KERN_ERR, "journaled quota format "
344 "not specified");
345 return -1;
346 }
347 }
ea676733 348
63189b78 349 if (f2fs_sb_has_quota_ino(sbi->sb) && F2FS_OPTION(sbi).s_jquota_fmt) {
ea676733
JK
350 f2fs_msg(sbi->sb, KERN_INFO,
351 "QUOTA feature is enabled, so ignore jquota_fmt");
63189b78 352 F2FS_OPTION(sbi).s_jquota_fmt = 0;
ea676733 353 }
4b2414d0
CY
354 return 0;
355}
356#endif
357
696c018c
NJ
358static int parse_options(struct super_block *sb, char *options)
359{
360 struct f2fs_sb_info *sbi = F2FS_SB(sb);
361 substring_t args[MAX_OPT_ARGS];
362 char *p, *name;
363 int arg = 0;
7c2e5963
JK
364 kuid_t uid;
365 kgid_t gid;
4b2414d0
CY
366#ifdef CONFIG_QUOTA
367 int ret;
368#endif
696c018c
NJ
369
370 if (!options)
371 return 0;
372
373 while ((p = strsep(&options, ",")) != NULL) {
374 int token;
375 if (!*p)
376 continue;
377 /*
378 * Initialize args struct so we know whether arg was
379 * found; some options take optional arguments.
380 */
381 args[0].to = args[0].from = NULL;
382 token = match_token(p, f2fs_tokens, args);
383
384 switch (token) {
385 case Opt_gc_background:
386 name = match_strdup(&args[0]);
387
388 if (!name)
389 return -ENOMEM;
6aefd93b 390 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
696c018c 391 set_opt(sbi, BG_GC);
6aefd93b
JK
392 clear_opt(sbi, FORCE_FG_GC);
393 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
696c018c 394 clear_opt(sbi, BG_GC);
6aefd93b
JK
395 clear_opt(sbi, FORCE_FG_GC);
396 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
397 set_opt(sbi, BG_GC);
398 set_opt(sbi, FORCE_FG_GC);
399 } else {
696c018c
NJ
400 kfree(name);
401 return -EINVAL;
402 }
403 kfree(name);
404 break;
405 case Opt_disable_roll_forward:
406 set_opt(sbi, DISABLE_ROLL_FORWARD);
407 break;
2d834bf9
JK
408 case Opt_norecovery:
409 /* this option mounts f2fs with ro */
410 set_opt(sbi, DISABLE_ROLL_FORWARD);
411 if (!f2fs_readonly(sb))
412 return -EINVAL;
413 break;
696c018c 414 case Opt_discard:
7d20c8ab 415 set_opt(sbi, DISCARD);
696c018c 416 break;
64058be9 417 case Opt_nodiscard:
ccd31cb2 418 if (f2fs_sb_has_blkzoned(sb)) {
96ba2dec
DLM
419 f2fs_msg(sb, KERN_WARNING,
420 "discard is required for zoned block devices");
421 return -EINVAL;
422 }
64058be9 423 clear_opt(sbi, DISCARD);
487df616 424 break;
696c018c
NJ
425 case Opt_noheap:
426 set_opt(sbi, NOHEAP);
427 break;
7a20b8a6
JK
428 case Opt_heap:
429 clear_opt(sbi, NOHEAP);
430 break;
696c018c 431#ifdef CONFIG_F2FS_FS_XATTR
4058c511
KA
432 case Opt_user_xattr:
433 set_opt(sbi, XATTR_USER);
434 break;
696c018c
NJ
435 case Opt_nouser_xattr:
436 clear_opt(sbi, XATTR_USER);
437 break;
444c580f
JK
438 case Opt_inline_xattr:
439 set_opt(sbi, INLINE_XATTR);
440 break;
23cf7212
CY
441 case Opt_noinline_xattr:
442 clear_opt(sbi, INLINE_XATTR);
443 break;
6afc662e
CY
444 case Opt_inline_xattr_size:
445 if (args->from && match_int(args, &arg))
446 return -EINVAL;
447 set_opt(sbi, INLINE_XATTR_SIZE);
63189b78 448 F2FS_OPTION(sbi).inline_xattr_size = arg;
6afc662e 449 break;
696c018c 450#else
4058c511
KA
451 case Opt_user_xattr:
452 f2fs_msg(sb, KERN_INFO,
453 "user_xattr options not supported");
454 break;
696c018c
NJ
455 case Opt_nouser_xattr:
456 f2fs_msg(sb, KERN_INFO,
457 "nouser_xattr options not supported");
458 break;
444c580f
JK
459 case Opt_inline_xattr:
460 f2fs_msg(sb, KERN_INFO,
461 "inline_xattr options not supported");
462 break;
23cf7212
CY
463 case Opt_noinline_xattr:
464 f2fs_msg(sb, KERN_INFO,
465 "noinline_xattr options not supported");
466 break;
696c018c
NJ
467#endif
468#ifdef CONFIG_F2FS_FS_POSIX_ACL
4058c511
KA
469 case Opt_acl:
470 set_opt(sbi, POSIX_ACL);
471 break;
696c018c
NJ
472 case Opt_noacl:
473 clear_opt(sbi, POSIX_ACL);
474 break;
475#else
4058c511
KA
476 case Opt_acl:
477 f2fs_msg(sb, KERN_INFO, "acl options not supported");
478 break;
696c018c
NJ
479 case Opt_noacl:
480 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
481 break;
482#endif
483 case Opt_active_logs:
484 if (args->from && match_int(args, &arg))
485 return -EINVAL;
486 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
487 return -EINVAL;
63189b78 488 F2FS_OPTION(sbi).active_logs = arg;
696c018c
NJ
489 break;
490 case Opt_disable_ext_identify:
491 set_opt(sbi, DISABLE_EXT_IDENTIFY);
492 break;
8274de77
HL
493 case Opt_inline_data:
494 set_opt(sbi, INLINE_DATA);
495 break;
5efd3c6f
CY
496 case Opt_inline_dentry:
497 set_opt(sbi, INLINE_DENTRY);
498 break;
97c1794a
CY
499 case Opt_noinline_dentry:
500 clear_opt(sbi, INLINE_DENTRY);
501 break;
6b4afdd7
JK
502 case Opt_flush_merge:
503 set_opt(sbi, FLUSH_MERGE);
504 break;
69e9e427
JK
505 case Opt_noflush_merge:
506 clear_opt(sbi, FLUSH_MERGE);
507 break;
0f7b2abd
JK
508 case Opt_nobarrier:
509 set_opt(sbi, NOBARRIER);
510 break;
d5053a34
JK
511 case Opt_fastboot:
512 set_opt(sbi, FASTBOOT);
513 break;
89672159
CY
514 case Opt_extent_cache:
515 set_opt(sbi, EXTENT_CACHE);
516 break;
7daaea25
JK
517 case Opt_noextent_cache:
518 clear_opt(sbi, EXTENT_CACHE);
519 break;
75342797
WL
520 case Opt_noinline_data:
521 clear_opt(sbi, INLINE_DATA);
522 break;
343f40f0
CY
523 case Opt_data_flush:
524 set_opt(sbi, DATA_FLUSH);
525 break;
7e65be49
JK
526 case Opt_reserve_root:
527 if (args->from && match_int(args, &arg))
528 return -EINVAL;
529 if (test_opt(sbi, RESERVE_ROOT)) {
530 f2fs_msg(sb, KERN_INFO,
531 "Preserve previous reserve_root=%u",
63189b78 532 F2FS_OPTION(sbi).root_reserved_blocks);
7e65be49 533 } else {
63189b78 534 F2FS_OPTION(sbi).root_reserved_blocks = arg;
7e65be49
JK
535 set_opt(sbi, RESERVE_ROOT);
536 }
537 break;
7c2e5963
JK
538 case Opt_resuid:
539 if (args->from && match_int(args, &arg))
540 return -EINVAL;
541 uid = make_kuid(current_user_ns(), arg);
542 if (!uid_valid(uid)) {
543 f2fs_msg(sb, KERN_ERR,
544 "Invalid uid value %d", arg);
545 return -EINVAL;
546 }
63189b78 547 F2FS_OPTION(sbi).s_resuid = uid;
7c2e5963
JK
548 break;
549 case Opt_resgid:
550 if (args->from && match_int(args, &arg))
551 return -EINVAL;
552 gid = make_kgid(current_user_ns(), arg);
553 if (!gid_valid(gid)) {
554 f2fs_msg(sb, KERN_ERR,
555 "Invalid gid value %d", arg);
556 return -EINVAL;
557 }
63189b78 558 F2FS_OPTION(sbi).s_resgid = gid;
7c2e5963 559 break;
36abef4e
JK
560 case Opt_mode:
561 name = match_strdup(&args[0]);
562
563 if (!name)
564 return -ENOMEM;
565 if (strlen(name) == 8 &&
566 !strncmp(name, "adaptive", 8)) {
ccd31cb2 567 if (f2fs_sb_has_blkzoned(sb)) {
3adc57e9
DLM
568 f2fs_msg(sb, KERN_WARNING,
569 "adaptive mode is not allowed with "
570 "zoned block device feature");
571 kfree(name);
572 return -EINVAL;
573 }
52763a4b 574 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
36abef4e
JK
575 } else if (strlen(name) == 3 &&
576 !strncmp(name, "lfs", 3)) {
52763a4b 577 set_opt_mode(sbi, F2FS_MOUNT_LFS);
36abef4e
JK
578 } else {
579 kfree(name);
580 return -EINVAL;
581 }
582 kfree(name);
583 break;
ec91538d
JK
584 case Opt_io_size_bits:
585 if (args->from && match_int(args, &arg))
586 return -EINVAL;
587 if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
588 f2fs_msg(sb, KERN_WARNING,
589 "Not support %d, larger than %d",
590 1 << arg, BIO_MAX_PAGES);
591 return -EINVAL;
592 }
63189b78 593 F2FS_OPTION(sbi).write_io_size_bits = arg;
ec91538d 594 break;
4cb037ec 595#ifdef CONFIG_F2FS_FAULT_INJECTION
73faec4d
JK
596 case Opt_fault_injection:
597 if (args->from && match_int(args, &arg))
598 return -EINVAL;
d494500a
CY
599 f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
600 set_opt(sbi, FAULT_INJECTION);
d494500a 601 break;
4cb037ec 602
d494500a
CY
603 case Opt_fault_type:
604 if (args->from && match_int(args, &arg))
605 return -EINVAL;
d494500a 606 f2fs_build_fault_attr(sbi, 0, arg);
0cc0dec2 607 set_opt(sbi, FAULT_INJECTION);
4cb037ec 608 break;
73faec4d 609#else
4cb037ec 610 case Opt_fault_injection:
73faec4d 611 f2fs_msg(sb, KERN_INFO,
4cb037ec 612 "fault_injection options not supported");
73faec4d 613 break;
4cb037ec
CX
614
615 case Opt_fault_type:
616 f2fs_msg(sb, KERN_INFO,
617 "fault_type options not supported");
618 break;
619#endif
6d94c74a 620 case Opt_lazytime:
1751e8a6 621 sb->s_flags |= SB_LAZYTIME;
6d94c74a
JK
622 break;
623 case Opt_nolazytime:
1751e8a6 624 sb->s_flags &= ~SB_LAZYTIME;
6d94c74a 625 break;
0abd675e 626#ifdef CONFIG_QUOTA
4b2414d0 627 case Opt_quota:
0abd675e
CY
628 case Opt_usrquota:
629 set_opt(sbi, USRQUOTA);
630 break;
631 case Opt_grpquota:
632 set_opt(sbi, GRPQUOTA);
633 break;
5c57132e
CY
634 case Opt_prjquota:
635 set_opt(sbi, PRJQUOTA);
636 break;
4b2414d0
CY
637 case Opt_usrjquota:
638 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
639 if (ret)
640 return ret;
641 break;
642 case Opt_grpjquota:
643 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
644 if (ret)
645 return ret;
646 break;
647 case Opt_prjjquota:
648 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
649 if (ret)
650 return ret;
651 break;
652 case Opt_offusrjquota:
653 ret = f2fs_clear_qf_name(sb, USRQUOTA);
654 if (ret)
655 return ret;
656 break;
657 case Opt_offgrpjquota:
658 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
659 if (ret)
660 return ret;
661 break;
662 case Opt_offprjjquota:
663 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
664 if (ret)
665 return ret;
666 break;
667 case Opt_jqfmt_vfsold:
63189b78 668 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
4b2414d0
CY
669 break;
670 case Opt_jqfmt_vfsv0:
63189b78 671 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
4b2414d0
CY
672 break;
673 case Opt_jqfmt_vfsv1:
63189b78 674 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
4b2414d0
CY
675 break;
676 case Opt_noquota:
677 clear_opt(sbi, QUOTA);
678 clear_opt(sbi, USRQUOTA);
679 clear_opt(sbi, GRPQUOTA);
680 clear_opt(sbi, PRJQUOTA);
681 break;
0abd675e 682#else
4b2414d0 683 case Opt_quota:
0abd675e
CY
684 case Opt_usrquota:
685 case Opt_grpquota:
5c57132e 686 case Opt_prjquota:
4b2414d0
CY
687 case Opt_usrjquota:
688 case Opt_grpjquota:
689 case Opt_prjjquota:
690 case Opt_offusrjquota:
691 case Opt_offgrpjquota:
692 case Opt_offprjjquota:
693 case Opt_jqfmt_vfsold:
694 case Opt_jqfmt_vfsv0:
695 case Opt_jqfmt_vfsv1:
696 case Opt_noquota:
0abd675e
CY
697 f2fs_msg(sb, KERN_INFO,
698 "quota operations not supported");
699 break;
700#endif
0cdd3195
HL
701 case Opt_whint:
702 name = match_strdup(&args[0]);
703 if (!name)
704 return -ENOMEM;
705 if (strlen(name) == 10 &&
706 !strncmp(name, "user-based", 10)) {
63189b78 707 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_USER;
0cdd3195
HL
708 } else if (strlen(name) == 3 &&
709 !strncmp(name, "off", 3)) {
63189b78 710 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
f2e703f9
HL
711 } else if (strlen(name) == 8 &&
712 !strncmp(name, "fs-based", 8)) {
63189b78 713 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_FS;
0cdd3195
HL
714 } else {
715 kfree(name);
716 return -EINVAL;
717 }
718 kfree(name);
719 break;
07939627
JK
720 case Opt_alloc:
721 name = match_strdup(&args[0]);
722 if (!name)
723 return -ENOMEM;
724
725 if (strlen(name) == 7 &&
726 !strncmp(name, "default", 7)) {
63189b78 727 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
07939627
JK
728 } else if (strlen(name) == 5 &&
729 !strncmp(name, "reuse", 5)) {
63189b78 730 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
07939627
JK
731 } else {
732 kfree(name);
733 return -EINVAL;
734 }
735 kfree(name);
736 break;
93cf93f1
JZ
737 case Opt_fsync:
738 name = match_strdup(&args[0]);
739 if (!name)
740 return -ENOMEM;
741 if (strlen(name) == 5 &&
742 !strncmp(name, "posix", 5)) {
63189b78 743 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
93cf93f1
JZ
744 } else if (strlen(name) == 6 &&
745 !strncmp(name, "strict", 6)) {
63189b78 746 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
d6290814
JK
747 } else if (strlen(name) == 9 &&
748 !strncmp(name, "nobarrier", 9)) {
749 F2FS_OPTION(sbi).fsync_mode =
750 FSYNC_MODE_NOBARRIER;
93cf93f1
JZ
751 } else {
752 kfree(name);
753 return -EINVAL;
754 }
755 kfree(name);
756 break;
ff62af20
SY
757 case Opt_test_dummy_encryption:
758#ifdef CONFIG_F2FS_FS_ENCRYPTION
759 if (!f2fs_sb_has_encrypt(sb)) {
760 f2fs_msg(sb, KERN_ERR, "Encrypt feature is off");
761 return -EINVAL;
762 }
763
764 F2FS_OPTION(sbi).test_dummy_encryption = true;
765 f2fs_msg(sb, KERN_INFO,
766 "Test dummy encryption mode enabled");
767#else
768 f2fs_msg(sb, KERN_INFO,
769 "Test dummy encryption mount option ignored");
770#endif
771 break;
696c018c
NJ
772 default:
773 f2fs_msg(sb, KERN_ERR,
774 "Unrecognized mount option \"%s\" or missing value",
775 p);
776 return -EINVAL;
777 }
778 }
4b2414d0
CY
779#ifdef CONFIG_QUOTA
780 if (f2fs_check_quota_options(sbi))
781 return -EINVAL;
00960c2c
SY
782#else
783 if (f2fs_sb_has_quota_ino(sbi->sb) && !f2fs_readonly(sbi->sb)) {
784 f2fs_msg(sbi->sb, KERN_INFO,
785 "Filesystem with quota feature cannot be mounted RDWR "
786 "without CONFIG_QUOTA");
787 return -EINVAL;
788 }
4ddc1b28
CY
789 if (f2fs_sb_has_project_quota(sbi->sb) && !f2fs_readonly(sbi->sb)) {
790 f2fs_msg(sb, KERN_ERR,
791 "Filesystem with project quota feature cannot be "
792 "mounted RDWR without CONFIG_QUOTA");
793 return -EINVAL;
794 }
4b2414d0 795#endif
ec91538d
JK
796
797 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
798 f2fs_msg(sb, KERN_ERR,
799 "Should set mode=lfs with %uKB-sized IO",
800 F2FS_IO_SIZE_KB(sbi));
801 return -EINVAL;
802 }
6afc662e
CY
803
804 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
4d817ae0
CY
805 if (!f2fs_sb_has_extra_attr(sb) ||
806 !f2fs_sb_has_flexible_inline_xattr(sb)) {
807 f2fs_msg(sb, KERN_ERR,
808 "extra_attr or flexible_inline_xattr "
809 "feature is off");
810 return -EINVAL;
811 }
6afc662e
CY
812 if (!test_opt(sbi, INLINE_XATTR)) {
813 f2fs_msg(sb, KERN_ERR,
814 "inline_xattr_size option should be "
815 "set with inline_xattr option");
816 return -EINVAL;
817 }
63189b78
CY
818 if (!F2FS_OPTION(sbi).inline_xattr_size ||
819 F2FS_OPTION(sbi).inline_xattr_size >=
820 DEF_ADDRS_PER_INODE -
6afc662e
CY
821 F2FS_TOTAL_EXTRA_ATTR_SIZE -
822 DEF_INLINE_RESERVED_SIZE -
823 DEF_MIN_INLINE_SIZE) {
824 f2fs_msg(sb, KERN_ERR,
825 "inline xattr size is out of range");
826 return -EINVAL;
827 }
828 }
0cdd3195
HL
829
830 /* Not pass down write hints if the number of active logs is lesser
831 * than NR_CURSEG_TYPE.
832 */
63189b78
CY
833 if (F2FS_OPTION(sbi).active_logs != NR_CURSEG_TYPE)
834 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
696c018c
NJ
835 return 0;
836}
837
aff063e2
JK
838static struct inode *f2fs_alloc_inode(struct super_block *sb)
839{
840 struct f2fs_inode_info *fi;
841
a0acdfe0 842 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
aff063e2
JK
843 if (!fi)
844 return NULL;
845
846 init_once((void *) fi);
847
434720fa 848 /* Initialize f2fs-specific inode info */
204706c7 849 atomic_set(&fi->dirty_pages, 0);
d928bfbf 850 init_rwsem(&fi->i_sem);
2710fd7e 851 INIT_LIST_HEAD(&fi->dirty_list);
0f18b462 852 INIT_LIST_HEAD(&fi->gdirty_list);
57864ae5 853 INIT_LIST_HEAD(&fi->inmem_ilist);
88b88a66
JK
854 INIT_LIST_HEAD(&fi->inmem_pages);
855 mutex_init(&fi->inmem_lock);
b2532c69
CY
856 init_rwsem(&fi->i_gc_rwsem[READ]);
857 init_rwsem(&fi->i_gc_rwsem[WRITE]);
5a3a2d83 858 init_rwsem(&fi->i_mmap_sem);
27161f13 859 init_rwsem(&fi->i_xattr_sem);
aff063e2 860
ab9fa662
JK
861 /* Will be used by directory only */
862 fi->i_dir_level = F2FS_SB(sb)->dir_level;
f2470371 863
aff063e2
JK
864 return &fi->vfs_inode;
865}
866
531ad7d5
JK
867static int f2fs_drop_inode(struct inode *inode)
868{
b8d96a30 869 int ret;
531ad7d5
JK
870 /*
871 * This is to avoid a deadlock condition like below.
872 * writeback_single_inode(inode)
873 * - f2fs_write_data_page
874 * - f2fs_gc -> iput -> evict
875 * - inode_wait_for_writeback(inode)
876 */
0f18b462 877 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
06e1bc05 878 if (!inode->i_nlink && !is_bad_inode(inode)) {
3e72f721
JK
879 /* to avoid evict_inode call simultaneously */
880 atomic_inc(&inode->i_count);
06e1bc05
JK
881 spin_unlock(&inode->i_lock);
882
883 /* some remained atomic pages should discarded */
884 if (f2fs_is_atomic_file(inode))
4d57b86d 885 f2fs_drop_inmem_pages(inode);
06e1bc05 886
3e72f721
JK
887 /* should remain fi->extent_tree for writepage */
888 f2fs_destroy_extent_node(inode);
889
06e1bc05 890 sb_start_intwrite(inode->i_sb);
fc9581c8 891 f2fs_i_size_write(inode, 0);
06e1bc05
JK
892
893 if (F2FS_HAS_BLOCKS(inode))
9a449e9c 894 f2fs_truncate(inode);
06e1bc05
JK
895
896 sb_end_intwrite(inode->i_sb);
897
06e1bc05 898 spin_lock(&inode->i_lock);
3e72f721 899 atomic_dec(&inode->i_count);
06e1bc05 900 }
b8d96a30 901 trace_f2fs_drop_inode(inode, 0);
531ad7d5 902 return 0;
06e1bc05 903 }
b8d96a30
HP
904 ret = generic_drop_inode(inode);
905 trace_f2fs_drop_inode(inode, ret);
906 return ret;
531ad7d5
JK
907}
908
7c45729a 909int f2fs_inode_dirtied(struct inode *inode, bool sync)
b3783873 910{
0f18b462 911 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
7c45729a 912 int ret = 0;
0f18b462 913
0f18b462
JK
914 spin_lock(&sbi->inode_lock[DIRTY_META]);
915 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
7c45729a
JK
916 ret = 1;
917 } else {
918 set_inode_flag(inode, FI_DIRTY_INODE);
919 stat_inc_dirty_inode(sbi, DIRTY_META);
0f18b462 920 }
7c45729a
JK
921 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
922 list_add_tail(&F2FS_I(inode)->gdirty_list,
0f18b462 923 &sbi->inode_list[DIRTY_META]);
7c45729a
JK
924 inc_page_count(sbi, F2FS_DIRTY_IMETA);
925 }
338bbfa0 926 spin_unlock(&sbi->inode_lock[DIRTY_META]);
7c45729a 927 return ret;
0f18b462
JK
928}
929
930void f2fs_inode_synced(struct inode *inode)
931{
932 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
933
934 spin_lock(&sbi->inode_lock[DIRTY_META]);
935 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
936 spin_unlock(&sbi->inode_lock[DIRTY_META]);
937 return;
938 }
7c45729a
JK
939 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
940 list_del_init(&F2FS_I(inode)->gdirty_list);
941 dec_page_count(sbi, F2FS_DIRTY_IMETA);
942 }
0f18b462 943 clear_inode_flag(inode, FI_DIRTY_INODE);
26de9b11 944 clear_inode_flag(inode, FI_AUTO_RECOVER);
0f18b462 945 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
338bbfa0 946 spin_unlock(&sbi->inode_lock[DIRTY_META]);
b3783873
JK
947}
948
b56ab837
JK
949/*
950 * f2fs_dirty_inode() is called from __mark_inode_dirty()
951 *
952 * We should call set_dirty_inode to write the dirty inode through write_inode.
953 */
954static void f2fs_dirty_inode(struct inode *inode, int flags)
955{
956 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
957
958 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
959 inode->i_ino == F2FS_META_INO(sbi))
960 return;
961
962 if (flags == I_DIRTY_TIME)
963 return;
964
965 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
966 clear_inode_flag(inode, FI_AUTO_RECOVER);
967
7c45729a 968 f2fs_inode_dirtied(inode, false);
b56ab837
JK
969}
970
aff063e2
JK
971static void f2fs_i_callback(struct rcu_head *head)
972{
973 struct inode *inode = container_of(head, struct inode, i_rcu);
974 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
975}
976
25ca923b 977static void f2fs_destroy_inode(struct inode *inode)
aff063e2
JK
978{
979 call_rcu(&inode->i_rcu, f2fs_i_callback);
980}
981
523be8a6
JK
982static void destroy_percpu_info(struct f2fs_sb_info *sbi)
983{
41382ec4 984 percpu_counter_destroy(&sbi->alloc_valid_block_count);
513c5f37 985 percpu_counter_destroy(&sbi->total_valid_inode_count);
523be8a6
JK
986}
987
3c62be17
JK
988static void destroy_device_list(struct f2fs_sb_info *sbi)
989{
990 int i;
991
992 for (i = 0; i < sbi->s_ndevs; i++) {
993 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
994#ifdef CONFIG_BLK_DEV_ZONED
995 kfree(FDEV(i).blkz_type);
996#endif
997 }
998 kfree(sbi->devs);
999}
1000
aff063e2
JK
1001static void f2fs_put_super(struct super_block *sb)
1002{
1003 struct f2fs_sb_info *sbi = F2FS_SB(sb);
a398101a 1004 int i;
cf5c759f 1005 bool dropped;
aff063e2 1006
0abd675e 1007 f2fs_quota_off_umount(sb);
aff063e2 1008
2658e50d
JK
1009 /* prevent remaining shrinker jobs */
1010 mutex_lock(&sbi->umount_mutex);
1011
85dc2f2c
JK
1012 /*
1013 * We don't need to do checkpoint when superblock is clean.
1014 * But, the previous checkpoint was not done by umount, it needs to do
1015 * clean checkpoint again.
1016 */
caf0047e 1017 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
aaec2b1d 1018 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
75ab4cb8
JK
1019 struct cp_control cpc = {
1020 .reason = CP_UMOUNT,
1021 };
4d57b86d 1022 f2fs_write_checkpoint(sbi, &cpc);
75ab4cb8 1023 }
aff063e2 1024
4e6a8d9b 1025 /* be sure to wait for any on-going discard commands */
cf5c759f 1026 dropped = f2fs_wait_discard_bios(sbi);
4e6a8d9b 1027
7d20c8ab
CY
1028 if ((f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) &&
1029 !sbi->discard_blks && !dropped) {
1f43e2ad
CY
1030 struct cp_control cpc = {
1031 .reason = CP_UMOUNT | CP_TRIMMED,
1032 };
4d57b86d 1033 f2fs_write_checkpoint(sbi, &cpc);
1f43e2ad
CY
1034 }
1035
4d57b86d 1036 /* f2fs_write_checkpoint can update stat informaion */
eca616f8
JK
1037 f2fs_destroy_stats(sbi);
1038
cf779cab
JK
1039 /*
1040 * normally superblock is clean, so we need to release this.
1041 * In addition, EIO will skip do checkpoint, we need this as well.
1042 */
4d57b86d 1043 f2fs_release_ino_entry(sbi, true);
6f12ac25 1044
2658e50d
JK
1045 f2fs_leave_shrinker(sbi);
1046 mutex_unlock(&sbi->umount_mutex);
1047
17c19120 1048 /* our cp_error case, we can wait for any writeback page */
b9109b0e 1049 f2fs_flush_merged_writes(sbi);
17c19120 1050
50fa53ec
CY
1051 f2fs_wait_on_all_pages_writeback(sbi);
1052
1053 f2fs_bug_on(sbi, sbi->fsync_node_num);
1054
aff063e2
JK
1055 iput(sbi->node_inode);
1056 iput(sbi->meta_inode);
1057
1058 /* destroy f2fs internal modules */
4d57b86d
CY
1059 f2fs_destroy_node_manager(sbi);
1060 f2fs_destroy_segment_manager(sbi);
aff063e2
JK
1061
1062 kfree(sbi->ckpt);
a398101a 1063
dc6b2055 1064 f2fs_unregister_sysfs(sbi);
aff063e2
JK
1065
1066 sb->s_fs_info = NULL;
43b6573b
KM
1067 if (sbi->s_chksum_driver)
1068 crypto_free_shash(sbi->s_chksum_driver);
b39f0de2 1069 kfree(sbi->raw_super);
523be8a6 1070
3c62be17 1071 destroy_device_list(sbi);
b6895e8f 1072 mempool_destroy(sbi->write_io_dummy);
4b2414d0
CY
1073#ifdef CONFIG_QUOTA
1074 for (i = 0; i < MAXQUOTAS; i++)
63189b78 1075 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
4b2414d0 1076#endif
523be8a6 1077 destroy_percpu_info(sbi);
a912b54d
JK
1078 for (i = 0; i < NR_PAGE_TYPE; i++)
1079 kfree(sbi->write_io[i]);
aff063e2
JK
1080 kfree(sbi);
1081}
1082
1083int f2fs_sync_fs(struct super_block *sb, int sync)
1084{
1085 struct f2fs_sb_info *sbi = F2FS_SB(sb);
c34f42e2 1086 int err = 0;
aff063e2 1087
1f227a3e
JK
1088 if (unlikely(f2fs_cp_error(sbi)))
1089 return 0;
1090
a2a4a7e4
NJ
1091 trace_f2fs_sync_fs(sb, sync);
1092
4b2414d0
CY
1093 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1094 return -EAGAIN;
1095
b7473754 1096 if (sync) {
d5053a34
JK
1097 struct cp_control cpc;
1098
119ee914
JK
1099 cpc.reason = __get_cp_reason(sbi);
1100
b7473754 1101 mutex_lock(&sbi->gc_mutex);
4d57b86d 1102 err = f2fs_write_checkpoint(sbi, &cpc);
b7473754 1103 mutex_unlock(&sbi->gc_mutex);
b7473754 1104 }
05ca3632 1105 f2fs_trace_ios(NULL, 1);
aff063e2 1106
c34f42e2 1107 return err;
aff063e2
JK
1108}
1109
d6212a5f
CL
1110static int f2fs_freeze(struct super_block *sb)
1111{
77888c1e 1112 if (f2fs_readonly(sb))
d6212a5f
CL
1113 return 0;
1114
b4b9d34c
JK
1115 /* IO error happened before */
1116 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1117 return -EIO;
1118
1119 /* must be clean, since sync_filesystem() was already called */
1120 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1121 return -EINVAL;
1122 return 0;
d6212a5f
CL
1123}
1124
1125static int f2fs_unfreeze(struct super_block *sb)
1126{
1127 return 0;
1128}
1129
ddc34e32
CY
1130#ifdef CONFIG_QUOTA
1131static int f2fs_statfs_project(struct super_block *sb,
1132 kprojid_t projid, struct kstatfs *buf)
1133{
1134 struct kqid qid;
1135 struct dquot *dquot;
1136 u64 limit;
1137 u64 curblock;
1138
1139 qid = make_kqid_projid(projid);
1140 dquot = dqget(sb, qid);
1141 if (IS_ERR(dquot))
1142 return PTR_ERR(dquot);
955ac6e5 1143 spin_lock(&dquot->dq_dqb_lock);
ddc34e32
CY
1144
1145 limit = (dquot->dq_dqb.dqb_bsoftlimit ?
1146 dquot->dq_dqb.dqb_bsoftlimit :
1147 dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
1148 if (limit && buf->f_blocks > limit) {
1149 curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits;
1150 buf->f_blocks = limit;
1151 buf->f_bfree = buf->f_bavail =
1152 (buf->f_blocks > curblock) ?
1153 (buf->f_blocks - curblock) : 0;
1154 }
1155
1156 limit = dquot->dq_dqb.dqb_isoftlimit ?
1157 dquot->dq_dqb.dqb_isoftlimit :
1158 dquot->dq_dqb.dqb_ihardlimit;
1159 if (limit && buf->f_files > limit) {
1160 buf->f_files = limit;
1161 buf->f_ffree =
1162 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1163 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1164 }
1165
955ac6e5 1166 spin_unlock(&dquot->dq_dqb_lock);
ddc34e32
CY
1167 dqput(dquot);
1168 return 0;
1169}
1170#endif
1171
aff063e2
JK
1172static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1173{
1174 struct super_block *sb = dentry->d_sb;
1175 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1176 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
f66c027e 1177 block_t total_count, user_block_count, start_count;
0cc091d0 1178 u64 avail_node_count;
aff063e2
JK
1179
1180 total_count = le64_to_cpu(sbi->raw_super->block_count);
1181 user_block_count = sbi->user_block_count;
1182 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
aff063e2
JK
1183 buf->f_type = F2FS_SUPER_MAGIC;
1184 buf->f_bsize = sbi->blocksize;
1185
1186 buf->f_blocks = total_count - start_count;
f66c027e 1187 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
80d42145 1188 sbi->current_reserved_blocks;
63189b78
CY
1189 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1190 buf->f_bavail = buf->f_bfree -
1191 F2FS_OPTION(sbi).root_reserved_blocks;
7e65be49
JK
1192 else
1193 buf->f_bavail = 0;
aff063e2 1194
292c196a
CY
1195 avail_node_count = sbi->total_node_count - sbi->nquota_files -
1196 F2FS_RESERVED_NODE_NUM;
0cc091d0
JK
1197
1198 if (avail_node_count > user_block_count) {
1199 buf->f_files = user_block_count;
1200 buf->f_ffree = buf->f_bavail;
1201 } else {
1202 buf->f_files = avail_node_count;
1203 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
1204 buf->f_bavail);
1205 }
aff063e2 1206
5a20d339 1207 buf->f_namelen = F2FS_NAME_LEN;
aff063e2
JK
1208 buf->f_fsid.val[0] = (u32)id;
1209 buf->f_fsid.val[1] = (u32)(id >> 32);
1210
ddc34e32
CY
1211#ifdef CONFIG_QUOTA
1212 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1213 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1214 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1215 }
1216#endif
aff063e2
JK
1217 return 0;
1218}
1219
4b2414d0
CY
1220static inline void f2fs_show_quota_options(struct seq_file *seq,
1221 struct super_block *sb)
1222{
1223#ifdef CONFIG_QUOTA
1224 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1225
63189b78 1226 if (F2FS_OPTION(sbi).s_jquota_fmt) {
4b2414d0
CY
1227 char *fmtname = "";
1228
63189b78 1229 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
4b2414d0
CY
1230 case QFMT_VFS_OLD:
1231 fmtname = "vfsold";
1232 break;
1233 case QFMT_VFS_V0:
1234 fmtname = "vfsv0";
1235 break;
1236 case QFMT_VFS_V1:
1237 fmtname = "vfsv1";
1238 break;
1239 }
1240 seq_printf(seq, ",jqfmt=%s", fmtname);
1241 }
1242
63189b78
CY
1243 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1244 seq_show_option(seq, "usrjquota",
1245 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
4b2414d0 1246
63189b78
CY
1247 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1248 seq_show_option(seq, "grpjquota",
1249 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
4b2414d0 1250
63189b78
CY
1251 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1252 seq_show_option(seq, "prjjquota",
1253 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
4b2414d0
CY
1254#endif
1255}
1256
aff063e2
JK
1257static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1258{
1259 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1260
6aefd93b
JK
1261 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
1262 if (test_opt(sbi, FORCE_FG_GC))
1263 seq_printf(seq, ",background_gc=%s", "sync");
1264 else
1265 seq_printf(seq, ",background_gc=%s", "on");
1266 } else {
696c018c 1267 seq_printf(seq, ",background_gc=%s", "off");
6aefd93b 1268 }
aff063e2
JK
1269 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1270 seq_puts(seq, ",disable_roll_forward");
1271 if (test_opt(sbi, DISCARD))
1272 seq_puts(seq, ",discard");
1273 if (test_opt(sbi, NOHEAP))
7a20b8a6
JK
1274 seq_puts(seq, ",no_heap");
1275 else
1276 seq_puts(seq, ",heap");
aff063e2
JK
1277#ifdef CONFIG_F2FS_FS_XATTR
1278 if (test_opt(sbi, XATTR_USER))
1279 seq_puts(seq, ",user_xattr");
1280 else
1281 seq_puts(seq, ",nouser_xattr");
444c580f
JK
1282 if (test_opt(sbi, INLINE_XATTR))
1283 seq_puts(seq, ",inline_xattr");
23cf7212
CY
1284 else
1285 seq_puts(seq, ",noinline_xattr");
6afc662e
CY
1286 if (test_opt(sbi, INLINE_XATTR_SIZE))
1287 seq_printf(seq, ",inline_xattr_size=%u",
63189b78 1288 F2FS_OPTION(sbi).inline_xattr_size);
aff063e2
JK
1289#endif
1290#ifdef CONFIG_F2FS_FS_POSIX_ACL
1291 if (test_opt(sbi, POSIX_ACL))
1292 seq_puts(seq, ",acl");
1293 else
1294 seq_puts(seq, ",noacl");
1295#endif
1296 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
aa43507f 1297 seq_puts(seq, ",disable_ext_identify");
8274de77
HL
1298 if (test_opt(sbi, INLINE_DATA))
1299 seq_puts(seq, ",inline_data");
75342797
WL
1300 else
1301 seq_puts(seq, ",noinline_data");
5efd3c6f
CY
1302 if (test_opt(sbi, INLINE_DENTRY))
1303 seq_puts(seq, ",inline_dentry");
97c1794a
CY
1304 else
1305 seq_puts(seq, ",noinline_dentry");
b270ad6f 1306 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
6b4afdd7 1307 seq_puts(seq, ",flush_merge");
0f7b2abd
JK
1308 if (test_opt(sbi, NOBARRIER))
1309 seq_puts(seq, ",nobarrier");
d5053a34
JK
1310 if (test_opt(sbi, FASTBOOT))
1311 seq_puts(seq, ",fastboot");
89672159
CY
1312 if (test_opt(sbi, EXTENT_CACHE))
1313 seq_puts(seq, ",extent_cache");
7daaea25
JK
1314 else
1315 seq_puts(seq, ",noextent_cache");
343f40f0
CY
1316 if (test_opt(sbi, DATA_FLUSH))
1317 seq_puts(seq, ",data_flush");
36abef4e
JK
1318
1319 seq_puts(seq, ",mode=");
1320 if (test_opt(sbi, ADAPTIVE))
1321 seq_puts(seq, "adaptive");
1322 else if (test_opt(sbi, LFS))
1323 seq_puts(seq, "lfs");
63189b78 1324 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
7e65be49 1325 if (test_opt(sbi, RESERVE_ROOT))
7c2e5963 1326 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
63189b78
CY
1327 F2FS_OPTION(sbi).root_reserved_blocks,
1328 from_kuid_munged(&init_user_ns,
1329 F2FS_OPTION(sbi).s_resuid),
1330 from_kgid_munged(&init_user_ns,
1331 F2FS_OPTION(sbi).s_resgid));
ec91538d
JK
1332 if (F2FS_IO_SIZE_BITS(sbi))
1333 seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
0cc0dec2 1334#ifdef CONFIG_F2FS_FAULT_INJECTION
d494500a 1335 if (test_opt(sbi, FAULT_INJECTION)) {
44529f89 1336 seq_printf(seq, ",fault_injection=%u",
63189b78 1337 F2FS_OPTION(sbi).fault_info.inject_rate);
d494500a
CY
1338 seq_printf(seq, ",fault_type=%u",
1339 F2FS_OPTION(sbi).fault_info.inject_type);
1340 }
0cc0dec2 1341#endif
0abd675e 1342#ifdef CONFIG_QUOTA
4b2414d0
CY
1343 if (test_opt(sbi, QUOTA))
1344 seq_puts(seq, ",quota");
0abd675e
CY
1345 if (test_opt(sbi, USRQUOTA))
1346 seq_puts(seq, ",usrquota");
1347 if (test_opt(sbi, GRPQUOTA))
1348 seq_puts(seq, ",grpquota");
5c57132e
CY
1349 if (test_opt(sbi, PRJQUOTA))
1350 seq_puts(seq, ",prjquota");
0cc0dec2 1351#endif
4b2414d0 1352 f2fs_show_quota_options(seq, sbi->sb);
63189b78 1353 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER)
0cdd3195 1354 seq_printf(seq, ",whint_mode=%s", "user-based");
63189b78 1355 else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS)
f2e703f9 1356 seq_printf(seq, ",whint_mode=%s", "fs-based");
ff62af20
SY
1357#ifdef CONFIG_F2FS_FS_ENCRYPTION
1358 if (F2FS_OPTION(sbi).test_dummy_encryption)
1359 seq_puts(seq, ",test_dummy_encryption");
1360#endif
aff063e2 1361
63189b78 1362 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
07939627 1363 seq_printf(seq, ",alloc_mode=%s", "default");
63189b78 1364 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
07939627 1365 seq_printf(seq, ",alloc_mode=%s", "reuse");
93cf93f1 1366
63189b78 1367 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
93cf93f1 1368 seq_printf(seq, ",fsync_mode=%s", "posix");
63189b78 1369 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
93cf93f1 1370 seq_printf(seq, ",fsync_mode=%s", "strict");
dc132802
ST
1371 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
1372 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
aff063e2
JK
1373 return 0;
1374}
1375
498c5e9f
YH
1376static void default_options(struct f2fs_sb_info *sbi)
1377{
1378 /* init some FS parameters */
63189b78
CY
1379 F2FS_OPTION(sbi).active_logs = NR_CURSEG_TYPE;
1380 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
1381 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1382 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
1383 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
ff62af20 1384 F2FS_OPTION(sbi).test_dummy_encryption = false;
0aa7e0f8
CY
1385 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
1386 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
498c5e9f
YH
1387
1388 set_opt(sbi, BG_GC);
39133a50 1389 set_opt(sbi, INLINE_XATTR);
498c5e9f 1390 set_opt(sbi, INLINE_DATA);
97c1794a 1391 set_opt(sbi, INLINE_DENTRY);
3e72f721 1392 set_opt(sbi, EXTENT_CACHE);
7a20b8a6 1393 set_opt(sbi, NOHEAP);
1751e8a6 1394 sbi->sb->s_flags |= SB_LAZYTIME;
69e9e427 1395 set_opt(sbi, FLUSH_MERGE);
7d20c8ab 1396 set_opt(sbi, DISCARD);
a39e5365
CY
1397 if (f2fs_sb_has_blkzoned(sbi->sb))
1398 set_opt_mode(sbi, F2FS_MOUNT_LFS);
1399 else
52763a4b 1400 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
498c5e9f
YH
1401
1402#ifdef CONFIG_F2FS_FS_XATTR
1403 set_opt(sbi, XATTR_USER);
1404#endif
1405#ifdef CONFIG_F2FS_FS_POSIX_ACL
1406 set_opt(sbi, POSIX_ACL);
1407#endif
36dbd328 1408
d494500a 1409 f2fs_build_fault_attr(sbi, 0, 0);
498c5e9f
YH
1410}
1411
ea676733
JK
1412#ifdef CONFIG_QUOTA
1413static int f2fs_enable_quotas(struct super_block *sb);
1414#endif
696c018c
NJ
1415static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1416{
1417 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1418 struct f2fs_mount_info org_mount_opt;
0abd675e 1419 unsigned long old_sb_flags;
63189b78 1420 int err;
876dc59e
GZ
1421 bool need_restart_gc = false;
1422 bool need_stop_gc = false;
9cd81ce3 1423 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
4b2414d0 1424#ifdef CONFIG_QUOTA
4b2414d0
CY
1425 int i, j;
1426#endif
696c018c
NJ
1427
1428 /*
1429 * Save the old mount options in case we
1430 * need to restore them.
1431 */
1432 org_mount_opt = sbi->mount_opt;
0abd675e 1433 old_sb_flags = sb->s_flags;
696c018c 1434
4b2414d0 1435#ifdef CONFIG_QUOTA
63189b78 1436 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
4b2414d0 1437 for (i = 0; i < MAXQUOTAS; i++) {
63189b78
CY
1438 if (F2FS_OPTION(sbi).s_qf_names[i]) {
1439 org_mount_opt.s_qf_names[i] =
1440 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
1441 GFP_KERNEL);
1442 if (!org_mount_opt.s_qf_names[i]) {
4b2414d0 1443 for (j = 0; j < i; j++)
63189b78 1444 kfree(org_mount_opt.s_qf_names[j]);
4b2414d0
CY
1445 return -ENOMEM;
1446 }
1447 } else {
63189b78 1448 org_mount_opt.s_qf_names[i] = NULL;
4b2414d0
CY
1449 }
1450 }
1451#endif
1452
df728b0f 1453 /* recover superblocks we couldn't write due to previous RO mount */
1751e8a6 1454 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
df728b0f
JK
1455 err = f2fs_commit_super(sbi, false);
1456 f2fs_msg(sb, KERN_INFO,
1457 "Try to recover all the superblocks, ret: %d", err);
1458 if (!err)
1459 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1460 }
1461
498c5e9f 1462 default_options(sbi);
26666c8a 1463
696c018c
NJ
1464 /* parse mount options */
1465 err = parse_options(sb, data);
1466 if (err)
1467 goto restore_opts;
1468
1469 /*
1470 * Previous and new state of filesystem is RO,
876dc59e 1471 * so skip checking GC and FLUSH_MERGE conditions.
696c018c 1472 */
1751e8a6 1473 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
696c018c
NJ
1474 goto skip;
1475
ea676733 1476#ifdef CONFIG_QUOTA
1751e8a6 1477 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
0abd675e
CY
1478 err = dquot_suspend(sb, -1);
1479 if (err < 0)
1480 goto restore_opts;
6279398d 1481 } else if (f2fs_readonly(sb) && !(*flags & MS_RDONLY)) {
0abd675e 1482 /* dquot_resume needs RW */
1751e8a6 1483 sb->s_flags &= ~SB_RDONLY;
ea676733
JK
1484 if (sb_any_quota_suspended(sb)) {
1485 dquot_resume(sb, -1);
1486 } else if (f2fs_sb_has_quota_ino(sb)) {
1487 err = f2fs_enable_quotas(sb);
1488 if (err)
1489 goto restore_opts;
1490 }
0abd675e 1491 }
ea676733 1492#endif
9cd81ce3
CY
1493 /* disallow enable/disable extent_cache dynamically */
1494 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
1495 err = -EINVAL;
1496 f2fs_msg(sbi->sb, KERN_WARNING,
1497 "switch extent_cache option is not allowed");
1498 goto restore_opts;
1499 }
1500
696c018c
NJ
1501 /*
1502 * We stop the GC thread if FS is mounted as RO
1503 * or if background_gc = off is passed in mount
1504 * option. Also sync the filesystem.
1505 */
1751e8a6 1506 if ((*flags & SB_RDONLY) || !test_opt(sbi, BG_GC)) {
696c018c 1507 if (sbi->gc_thread) {
4d57b86d 1508 f2fs_stop_gc_thread(sbi);
876dc59e 1509 need_restart_gc = true;
696c018c 1510 }
aba291b3 1511 } else if (!sbi->gc_thread) {
4d57b86d 1512 err = f2fs_start_gc_thread(sbi);
696c018c
NJ
1513 if (err)
1514 goto restore_opts;
876dc59e
GZ
1515 need_stop_gc = true;
1516 }
1517
63189b78
CY
1518 if (*flags & SB_RDONLY ||
1519 F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
faa0e55b
JK
1520 writeback_inodes_sb(sb, WB_REASON_SYNC);
1521 sync_inodes_sb(sb);
1522
1523 set_sbi_flag(sbi, SBI_IS_DIRTY);
1524 set_sbi_flag(sbi, SBI_IS_CLOSE);
1525 f2fs_sync_fs(sb, 1);
1526 clear_sbi_flag(sbi, SBI_IS_CLOSE);
1527 }
1528
876dc59e
GZ
1529 /*
1530 * We stop issue flush thread if FS is mounted as RO
1531 * or if flush_merge is not passed in mount option.
1532 */
1751e8a6 1533 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
5eba8c5d 1534 clear_opt(sbi, FLUSH_MERGE);
4d57b86d 1535 f2fs_destroy_flush_cmd_control(sbi, false);
5eba8c5d 1536 } else {
4d57b86d 1537 err = f2fs_create_flush_cmd_control(sbi);
2163d198 1538 if (err)
a688b9d9 1539 goto restore_gc;
696c018c
NJ
1540 }
1541skip:
4b2414d0
CY
1542#ifdef CONFIG_QUOTA
1543 /* Release old quota file names */
1544 for (i = 0; i < MAXQUOTAS; i++)
63189b78 1545 kfree(org_mount_opt.s_qf_names[i]);
4b2414d0 1546#endif
696c018c 1547 /* Update the POSIXACL Flag */
1751e8a6
LT
1548 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
1549 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
df728b0f 1550
7e65be49 1551 limit_reserve_root(sbi);
696c018c 1552 return 0;
876dc59e
GZ
1553restore_gc:
1554 if (need_restart_gc) {
4d57b86d 1555 if (f2fs_start_gc_thread(sbi))
876dc59e 1556 f2fs_msg(sbi->sb, KERN_WARNING,
e1c42045 1557 "background gc thread has stopped");
876dc59e 1558 } else if (need_stop_gc) {
4d57b86d 1559 f2fs_stop_gc_thread(sbi);
876dc59e 1560 }
696c018c 1561restore_opts:
4b2414d0 1562#ifdef CONFIG_QUOTA
63189b78 1563 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
4b2414d0 1564 for (i = 0; i < MAXQUOTAS; i++) {
63189b78
CY
1565 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1566 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
4b2414d0
CY
1567 }
1568#endif
696c018c 1569 sbi->mount_opt = org_mount_opt;
0abd675e 1570 sb->s_flags = old_sb_flags;
696c018c
NJ
1571 return err;
1572}
1573
0abd675e
CY
1574#ifdef CONFIG_QUOTA
1575/* Read data from quotafile */
1576static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
1577 size_t len, loff_t off)
1578{
1579 struct inode *inode = sb_dqopt(sb)->files[type];
1580 struct address_space *mapping = inode->i_mapping;
1581 block_t blkidx = F2FS_BYTES_TO_BLK(off);
1582 int offset = off & (sb->s_blocksize - 1);
1583 int tocopy;
1584 size_t toread;
1585 loff_t i_size = i_size_read(inode);
1586 struct page *page;
1587 char *kaddr;
1588
1589 if (off > i_size)
1590 return 0;
1591
1592 if (off + len > i_size)
1593 len = i_size - off;
1594 toread = len;
1595 while (toread > 0) {
1596 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
1597repeat:
02117b8a 1598 page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
4e46a023
JK
1599 if (IS_ERR(page)) {
1600 if (PTR_ERR(page) == -ENOMEM) {
1601 congestion_wait(BLK_RW_ASYNC, HZ/50);
1602 goto repeat;
1603 }
0abd675e 1604 return PTR_ERR(page);
4e46a023 1605 }
0abd675e
CY
1606
1607 lock_page(page);
1608
1609 if (unlikely(page->mapping != mapping)) {
1610 f2fs_put_page(page, 1);
1611 goto repeat;
1612 }
1613 if (unlikely(!PageUptodate(page))) {
1614 f2fs_put_page(page, 1);
1615 return -EIO;
1616 }
1617
1618 kaddr = kmap_atomic(page);
1619 memcpy(data, kaddr + offset, tocopy);
1620 kunmap_atomic(kaddr);
1621 f2fs_put_page(page, 1);
1622
1623 offset = 0;
1624 toread -= tocopy;
1625 data += tocopy;
1626 blkidx++;
1627 }
1628 return len;
1629}
1630
1631/* Write to quotafile */
1632static ssize_t f2fs_quota_write(struct super_block *sb, int type,
1633 const char *data, size_t len, loff_t off)
1634{
1635 struct inode *inode = sb_dqopt(sb)->files[type];
1636 struct address_space *mapping = inode->i_mapping;
1637 const struct address_space_operations *a_ops = mapping->a_ops;
1638 int offset = off & (sb->s_blocksize - 1);
1639 size_t towrite = len;
1640 struct page *page;
1641 char *kaddr;
1642 int err = 0;
1643 int tocopy;
1644
1645 while (towrite > 0) {
1646 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
1647 towrite);
4e46a023 1648retry:
0abd675e
CY
1649 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
1650 &page, NULL);
4e46a023
JK
1651 if (unlikely(err)) {
1652 if (err == -ENOMEM) {
1653 congestion_wait(BLK_RW_ASYNC, HZ/50);
1654 goto retry;
1655 }
0abd675e 1656 break;
4e46a023 1657 }
0abd675e
CY
1658
1659 kaddr = kmap_atomic(page);
1660 memcpy(kaddr + offset, data, tocopy);
1661 kunmap_atomic(kaddr);
1662 flush_dcache_page(page);
1663
1664 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
1665 page, NULL);
1666 offset = 0;
1667 towrite -= tocopy;
1668 off += tocopy;
1669 data += tocopy;
1670 cond_resched();
1671 }
1672
1673 if (len == towrite)
6e5b5d41 1674 return err;
0abd675e
CY
1675 inode->i_mtime = inode->i_ctime = current_time(inode);
1676 f2fs_mark_inode_dirty_sync(inode, false);
1677 return len - towrite;
1678}
1679
1680static struct dquot **f2fs_get_dquots(struct inode *inode)
1681{
1682 return F2FS_I(inode)->i_dquot;
1683}
1684
1685static qsize_t *f2fs_get_reserved_space(struct inode *inode)
1686{
1687 return &F2FS_I(inode)->i_reserved_quota;
1688}
1689
4b2414d0
CY
1690static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
1691{
63189b78
CY
1692 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
1693 F2FS_OPTION(sbi).s_jquota_fmt, type);
4b2414d0
CY
1694}
1695
ea676733 1696int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
4b2414d0 1697{
ea676733
JK
1698 int enabled = 0;
1699 int i, err;
1700
1701 if (f2fs_sb_has_quota_ino(sbi->sb) && rdonly) {
1702 err = f2fs_enable_quotas(sbi->sb);
1703 if (err) {
1704 f2fs_msg(sbi->sb, KERN_ERR,
1705 "Cannot turn on quota_ino: %d", err);
1706 return 0;
1707 }
1708 return 1;
1709 }
4b2414d0
CY
1710
1711 for (i = 0; i < MAXQUOTAS; i++) {
63189b78 1712 if (F2FS_OPTION(sbi).s_qf_names[i]) {
ea676733
JK
1713 err = f2fs_quota_on_mount(sbi, i);
1714 if (!err) {
1715 enabled = 1;
1716 continue;
1717 }
1718 f2fs_msg(sbi->sb, KERN_ERR,
1719 "Cannot turn on quotas: %d on %d", err, i);
4b2414d0
CY
1720 }
1721 }
ea676733
JK
1722 return enabled;
1723}
1724
1725static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
1726 unsigned int flags)
1727{
1728 struct inode *qf_inode;
1729 unsigned long qf_inum;
1730 int err;
1731
1732 BUG_ON(!f2fs_sb_has_quota_ino(sb));
1733
1734 qf_inum = f2fs_qf_ino(sb, type);
1735 if (!qf_inum)
1736 return -EPERM;
1737
1738 qf_inode = f2fs_iget(sb, qf_inum);
1739 if (IS_ERR(qf_inode)) {
1740 f2fs_msg(sb, KERN_ERR,
1741 "Bad quota inode %u:%lu", type, qf_inum);
1742 return PTR_ERR(qf_inode);
1743 }
1744
1745 /* Don't account quota for quota files to avoid recursion */
1746 qf_inode->i_flags |= S_NOQUOTA;
1747 err = dquot_enable(qf_inode, type, format_id, flags);
1748 iput(qf_inode);
1749 return err;
1750}
1751
1752static int f2fs_enable_quotas(struct super_block *sb)
1753{
1754 int type, err = 0;
1755 unsigned long qf_inum;
1756 bool quota_mopt[MAXQUOTAS] = {
1757 test_opt(F2FS_SB(sb), USRQUOTA),
1758 test_opt(F2FS_SB(sb), GRPQUOTA),
1759 test_opt(F2FS_SB(sb), PRJQUOTA),
1760 };
1761
1762 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
1763 for (type = 0; type < MAXQUOTAS; type++) {
1764 qf_inum = f2fs_qf_ino(sb, type);
1765 if (qf_inum) {
1766 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
1767 DQUOT_USAGE_ENABLED |
1768 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
1769 if (err) {
1770 f2fs_msg(sb, KERN_ERR,
1771 "Failed to enable quota tracking "
1772 "(type=%d, err=%d). Please run "
1773 "fsck to fix.", type, err);
1774 for (type--; type >= 0; type--)
1775 dquot_quota_off(sb, type);
1776 return err;
1777 }
4b2414d0
CY
1778 }
1779 }
ea676733 1780 return 0;
4b2414d0
CY
1781}
1782
0abd675e
CY
1783static int f2fs_quota_sync(struct super_block *sb, int type)
1784{
1785 struct quota_info *dqopt = sb_dqopt(sb);
1786 int cnt;
1787 int ret;
1788
1789 ret = dquot_writeback_dquots(sb, type);
1790 if (ret)
1791 return ret;
1792
1793 /*
1794 * Now when everything is written we can discard the pagecache so
1795 * that userspace sees the changes.
1796 */
1797 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1798 if (type != -1 && cnt != type)
1799 continue;
1800 if (!sb_has_quota_active(sb, cnt))
1801 continue;
1802
1803 ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping);
1804 if (ret)
1805 return ret;
1806
1807 inode_lock(dqopt->files[cnt]);
1808 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
1809 inode_unlock(dqopt->files[cnt]);
1810 }
1811 return 0;
1812}
1813
1814static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
1815 const struct path *path)
1816{
1817 struct inode *inode;
1818 int err;
1819
9a20d391 1820 err = f2fs_quota_sync(sb, type);
0abd675e
CY
1821 if (err)
1822 return err;
1823
1824 err = dquot_quota_on(sb, type, format_id, path);
1825 if (err)
1826 return err;
1827
1828 inode = d_inode(path->dentry);
1829
1830 inode_lock(inode);
59c84408 1831 F2FS_I(inode)->i_flags |= F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
0abd675e
CY
1832 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
1833 S_NOATIME | S_IMMUTABLE);
1834 inode_unlock(inode);
1835 f2fs_mark_inode_dirty_sync(inode, false);
1836
1837 return 0;
1838}
1839
1840static int f2fs_quota_off(struct super_block *sb, int type)
1841{
1842 struct inode *inode = sb_dqopt(sb)->files[type];
1843 int err;
1844
1845 if (!inode || !igrab(inode))
1846 return dquot_quota_off(sb, type);
1847
cda9cc59
YH
1848 err = f2fs_quota_sync(sb, type);
1849 if (err)
1850 goto out_put;
0abd675e
CY
1851
1852 err = dquot_quota_off(sb, type);
ea676733 1853 if (err || f2fs_sb_has_quota_ino(sb))
0abd675e
CY
1854 goto out_put;
1855
1856 inode_lock(inode);
59c84408 1857 F2FS_I(inode)->i_flags &= ~(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL);
0abd675e
CY
1858 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
1859 inode_unlock(inode);
1860 f2fs_mark_inode_dirty_sync(inode, false);
1861out_put:
1862 iput(inode);
1863 return err;
1864}
1865
4b2414d0 1866void f2fs_quota_off_umount(struct super_block *sb)
0abd675e
CY
1867{
1868 int type;
cda9cc59
YH
1869 int err;
1870
1871 for (type = 0; type < MAXQUOTAS; type++) {
1872 err = f2fs_quota_off(sb, type);
1873 if (err) {
1874 int ret = dquot_quota_off(sb, type);
0abd675e 1875
cda9cc59
YH
1876 f2fs_msg(sb, KERN_ERR,
1877 "Fail to turn off disk quota "
1878 "(type: %d, err: %d, ret:%d), Please "
1879 "run fsck to fix it.", type, err, ret);
1880 set_sbi_flag(F2FS_SB(sb), SBI_NEED_FSCK);
1881 }
1882 }
0abd675e
CY
1883}
1884
94b1e10e 1885static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
5c57132e
CY
1886{
1887 *projid = F2FS_I(inode)->i_projid;
1888 return 0;
1889}
1890
0abd675e
CY
1891static const struct dquot_operations f2fs_quota_operations = {
1892 .get_reserved_space = f2fs_get_reserved_space,
1893 .write_dquot = dquot_commit,
1894 .acquire_dquot = dquot_acquire,
1895 .release_dquot = dquot_release,
1896 .mark_dirty = dquot_mark_dquot_dirty,
1897 .write_info = dquot_commit_info,
1898 .alloc_dquot = dquot_alloc,
1899 .destroy_dquot = dquot_destroy,
5c57132e 1900 .get_projid = f2fs_get_projid,
0abd675e
CY
1901 .get_next_id = dquot_get_next_id,
1902};
1903
1904static const struct quotactl_ops f2fs_quotactl_ops = {
1905 .quota_on = f2fs_quota_on,
1906 .quota_off = f2fs_quota_off,
1907 .quota_sync = f2fs_quota_sync,
1908 .get_state = dquot_get_state,
1909 .set_info = dquot_set_dqinfo,
1910 .get_dqblk = dquot_get_dqblk,
1911 .set_dqblk = dquot_set_dqblk,
1912 .get_nextdqblk = dquot_get_next_dqblk,
1913};
1914#else
4b2414d0 1915void f2fs_quota_off_umount(struct super_block *sb)
0abd675e
CY
1916{
1917}
1918#endif
1919
f62fc9f9 1920static const struct super_operations f2fs_sops = {
aff063e2 1921 .alloc_inode = f2fs_alloc_inode,
531ad7d5 1922 .drop_inode = f2fs_drop_inode,
aff063e2
JK
1923 .destroy_inode = f2fs_destroy_inode,
1924 .write_inode = f2fs_write_inode,
b3783873 1925 .dirty_inode = f2fs_dirty_inode,
aff063e2 1926 .show_options = f2fs_show_options,
0abd675e
CY
1927#ifdef CONFIG_QUOTA
1928 .quota_read = f2fs_quota_read,
1929 .quota_write = f2fs_quota_write,
1930 .get_dquots = f2fs_get_dquots,
1931#endif
aff063e2
JK
1932 .evict_inode = f2fs_evict_inode,
1933 .put_super = f2fs_put_super,
1934 .sync_fs = f2fs_sync_fs,
d6212a5f
CL
1935 .freeze_fs = f2fs_freeze,
1936 .unfreeze_fs = f2fs_unfreeze,
aff063e2 1937 .statfs = f2fs_statfs,
696c018c 1938 .remount_fs = f2fs_remount,
aff063e2
JK
1939};
1940
0b81d077
JK
1941#ifdef CONFIG_F2FS_FS_ENCRYPTION
1942static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
1943{
1944 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1945 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1946 ctx, len, NULL);
1947}
1948
1949static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
1950 void *fs_data)
1951{
b7c409de
SY
1952 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1953
1954 /*
1955 * Encrypting the root directory is not allowed because fsck
1956 * expects lost+found directory to exist and remain unencrypted
1957 * if LOST_FOUND feature is enabled.
1958 *
1959 */
1960 if (f2fs_sb_has_lost_found(sbi->sb) &&
1961 inode->i_ino == F2FS_ROOT_INO(sbi))
1962 return -EPERM;
1963
0b81d077
JK
1964 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1965 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1966 ctx, len, fs_data, XATTR_CREATE);
1967}
1968
ff62af20
SY
1969static bool f2fs_dummy_context(struct inode *inode)
1970{
1971 return DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(inode));
1972}
1973
6f69f0ed 1974static const struct fscrypt_operations f2fs_cryptops = {
a5d431ef 1975 .key_prefix = "f2fs:",
0b81d077
JK
1976 .get_context = f2fs_get_context,
1977 .set_context = f2fs_set_context,
ff62af20 1978 .dummy_context = f2fs_dummy_context,
0b81d077 1979 .empty_dir = f2fs_empty_dir,
e12ee683 1980 .max_namelen = F2FS_NAME_LEN,
0b81d077 1981};
0b81d077
JK
1982#endif
1983
aff063e2
JK
1984static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
1985 u64 ino, u32 generation)
1986{
1987 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1988 struct inode *inode;
1989
4d57b86d 1990 if (f2fs_check_nid_range(sbi, ino))
910bb12d 1991 return ERR_PTR(-ESTALE);
aff063e2
JK
1992
1993 /*
1994 * f2fs_iget isn't quite right if the inode is currently unallocated!
1995 * However f2fs_iget currently does appropriate checks to handle stale
1996 * inodes so everything is OK.
1997 */
1998 inode = f2fs_iget(sb, ino);
1999 if (IS_ERR(inode))
2000 return ERR_CAST(inode);
6bacf52f 2001 if (unlikely(generation && inode->i_generation != generation)) {
aff063e2
JK
2002 /* we didn't find the right inode.. */
2003 iput(inode);
2004 return ERR_PTR(-ESTALE);
2005 }
2006 return inode;
2007}
2008
2009static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
2010 int fh_len, int fh_type)
2011{
2012 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2013 f2fs_nfs_get_inode);
2014}
2015
2016static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
2017 int fh_len, int fh_type)
2018{
2019 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2020 f2fs_nfs_get_inode);
2021}
2022
2023static const struct export_operations f2fs_export_ops = {
2024 .fh_to_dentry = f2fs_fh_to_dentry,
2025 .fh_to_parent = f2fs_fh_to_parent,
2026 .get_parent = f2fs_get_parent,
2027};
2028
e0afc4d6 2029static loff_t max_file_blocks(void)
aff063e2 2030{
7a2af766 2031 loff_t result = 0;
aff063e2
JK
2032 loff_t leaf_count = ADDRS_PER_BLOCK;
2033
7a2af766
CY
2034 /*
2035 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
6afc662e 2036 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
7a2af766
CY
2037 * space in inode.i_addr, it will be more safe to reassign
2038 * result as zero.
2039 */
2040
aff063e2
JK
2041 /* two direct node blocks */
2042 result += (leaf_count * 2);
2043
2044 /* two indirect node blocks */
2045 leaf_count *= NIDS_PER_BLOCK;
2046 result += (leaf_count * 2);
2047
2048 /* one double indirect node block */
2049 leaf_count *= NIDS_PER_BLOCK;
2050 result += leaf_count;
2051
aff063e2
JK
2052 return result;
2053}
2054
fd694733
JK
2055static int __f2fs_commit_super(struct buffer_head *bh,
2056 struct f2fs_super_block *super)
2057{
2058 lock_buffer(bh);
2059 if (super)
2060 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
fd694733
JK
2061 set_buffer_dirty(bh);
2062 unlock_buffer(bh);
2063
2064 /* it's rare case, we can do fua all the time */
3adc5fcb 2065 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
fd694733
JK
2066}
2067
df728b0f 2068static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
fd694733 2069 struct buffer_head *bh)
9a59b62f 2070{
fd694733
JK
2071 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2072 (bh->b_data + F2FS_SUPER_OFFSET);
df728b0f 2073 struct super_block *sb = sbi->sb;
9a59b62f
CY
2074 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2075 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
2076 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
2077 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
2078 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2079 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2080 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
2081 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
2082 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
2083 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
2084 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2085 u32 segment_count = le32_to_cpu(raw_super->segment_count);
2086 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
fd694733
JK
2087 u64 main_end_blkaddr = main_blkaddr +
2088 (segment_count_main << log_blocks_per_seg);
2089 u64 seg_end_blkaddr = segment0_blkaddr +
2090 (segment_count << log_blocks_per_seg);
9a59b62f
CY
2091
2092 if (segment0_blkaddr != cp_blkaddr) {
2093 f2fs_msg(sb, KERN_INFO,
2094 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
2095 segment0_blkaddr, cp_blkaddr);
2096 return true;
2097 }
2098
2099 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
2100 sit_blkaddr) {
2101 f2fs_msg(sb, KERN_INFO,
2102 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
2103 cp_blkaddr, sit_blkaddr,
2104 segment_count_ckpt << log_blocks_per_seg);
2105 return true;
2106 }
2107
2108 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
2109 nat_blkaddr) {
2110 f2fs_msg(sb, KERN_INFO,
2111 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
2112 sit_blkaddr, nat_blkaddr,
2113 segment_count_sit << log_blocks_per_seg);
2114 return true;
2115 }
2116
2117 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
2118 ssa_blkaddr) {
2119 f2fs_msg(sb, KERN_INFO,
2120 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
2121 nat_blkaddr, ssa_blkaddr,
2122 segment_count_nat << log_blocks_per_seg);
2123 return true;
2124 }
2125
2126 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
2127 main_blkaddr) {
2128 f2fs_msg(sb, KERN_INFO,
2129 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
2130 ssa_blkaddr, main_blkaddr,
2131 segment_count_ssa << log_blocks_per_seg);
2132 return true;
2133 }
2134
fd694733 2135 if (main_end_blkaddr > seg_end_blkaddr) {
9a59b62f 2136 f2fs_msg(sb, KERN_INFO,
fd694733 2137 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
9a59b62f 2138 main_blkaddr,
fd694733
JK
2139 segment0_blkaddr +
2140 (segment_count << log_blocks_per_seg),
9a59b62f
CY
2141 segment_count_main << log_blocks_per_seg);
2142 return true;
fd694733
JK
2143 } else if (main_end_blkaddr < seg_end_blkaddr) {
2144 int err = 0;
2145 char *res;
2146
2147 /* fix in-memory information all the time */
2148 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
2149 segment0_blkaddr) >> log_blocks_per_seg);
2150
2151 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
df728b0f 2152 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
fd694733
JK
2153 res = "internally";
2154 } else {
2155 err = __f2fs_commit_super(bh, NULL);
2156 res = err ? "failed" : "done";
2157 }
2158 f2fs_msg(sb, KERN_INFO,
2159 "Fix alignment : %s, start(%u) end(%u) block(%u)",
2160 res, main_blkaddr,
2161 segment0_blkaddr +
2162 (segment_count << log_blocks_per_seg),
2163 segment_count_main << log_blocks_per_seg);
2164 if (err)
2165 return true;
9a59b62f 2166 }
9a59b62f
CY
2167 return false;
2168}
2169
df728b0f 2170static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
fd694733 2171 struct buffer_head *bh)
aff063e2 2172{
0cfe75c5
JK
2173 block_t segment_count, segs_per_sec, secs_per_zone;
2174 block_t total_sections, blocks_per_seg;
fd694733
JK
2175 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2176 (bh->b_data + F2FS_SUPER_OFFSET);
df728b0f 2177 struct super_block *sb = sbi->sb;
aff063e2
JK
2178 unsigned int blocksize;
2179
a07ef784
NJ
2180 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
2181 f2fs_msg(sb, KERN_INFO,
2182 "Magic Mismatch, valid(0x%x) - read(0x%x)",
2183 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
aff063e2 2184 return 1;
a07ef784 2185 }
aff063e2 2186
5c9b4692 2187 /* Currently, support only 4KB page cache size */
09cbfeaf 2188 if (F2FS_BLKSIZE != PAGE_SIZE) {
5c9b4692 2189 f2fs_msg(sb, KERN_INFO,
14d7e9de 2190 "Invalid page_cache_size (%lu), supports only 4KB\n",
09cbfeaf 2191 PAGE_SIZE);
5c9b4692 2192 return 1;
2193 }
2194
aff063e2
JK
2195 /* Currently, support only 4KB block size */
2196 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
5c9b4692 2197 if (blocksize != F2FS_BLKSIZE) {
a07ef784
NJ
2198 f2fs_msg(sb, KERN_INFO,
2199 "Invalid blocksize (%u), supports only 4KB\n",
2200 blocksize);
aff063e2 2201 return 1;
a07ef784 2202 }
5c9b4692 2203
9a59b62f
CY
2204 /* check log blocks per segment */
2205 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
2206 f2fs_msg(sb, KERN_INFO,
2207 "Invalid log blocks per segment (%u)\n",
2208 le32_to_cpu(raw_super->log_blocks_per_seg));
2209 return 1;
2210 }
2211
55cf9cb6
CY
2212 /* Currently, support 512/1024/2048/4096 bytes sector size */
2213 if (le32_to_cpu(raw_super->log_sectorsize) >
2214 F2FS_MAX_LOG_SECTOR_SIZE ||
2215 le32_to_cpu(raw_super->log_sectorsize) <
2216 F2FS_MIN_LOG_SECTOR_SIZE) {
2217 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
2218 le32_to_cpu(raw_super->log_sectorsize));
aff063e2 2219 return 1;
a07ef784 2220 }
55cf9cb6
CY
2221 if (le32_to_cpu(raw_super->log_sectors_per_block) +
2222 le32_to_cpu(raw_super->log_sectorsize) !=
2223 F2FS_MAX_LOG_SECTOR_SIZE) {
2224 f2fs_msg(sb, KERN_INFO,
2225 "Invalid log sectors per block(%u) log sectorsize(%u)",
2226 le32_to_cpu(raw_super->log_sectors_per_block),
2227 le32_to_cpu(raw_super->log_sectorsize));
aff063e2 2228 return 1;
a07ef784 2229 }
9a59b62f 2230
0cfe75c5
JK
2231 segment_count = le32_to_cpu(raw_super->segment_count);
2232 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2233 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2234 total_sections = le32_to_cpu(raw_super->section_count);
2235
2236 /* blocks_per_seg should be 512, given the above check */
2237 blocks_per_seg = 1 << le32_to_cpu(raw_super->log_blocks_per_seg);
2238
2239 if (segment_count > F2FS_MAX_SEGMENT ||
2240 segment_count < F2FS_MIN_SEGMENTS) {
2241 f2fs_msg(sb, KERN_INFO,
2242 "Invalid segment count (%u)",
2243 segment_count);
2244 return 1;
2245 }
2246
2247 if (total_sections > segment_count ||
2248 total_sections < F2FS_MIN_SEGMENTS ||
2249 segs_per_sec > segment_count || !segs_per_sec) {
2250 f2fs_msg(sb, KERN_INFO,
2251 "Invalid segment/section count (%u, %u x %u)",
2252 segment_count, total_sections, segs_per_sec);
2253 return 1;
2254 }
2255
2256 if ((segment_count / segs_per_sec) < total_sections) {
2257 f2fs_msg(sb, KERN_INFO,
2258 "Small segment_count (%u < %u * %u)",
2259 segment_count, segs_per_sec, total_sections);
2260 return 1;
2261 }
2262
2263 if (segment_count > (le32_to_cpu(raw_super->block_count) >> 9)) {
2264 f2fs_msg(sb, KERN_INFO,
2265 "Wrong segment_count / block_count (%u > %u)",
2266 segment_count, le32_to_cpu(raw_super->block_count));
2267 return 1;
2268 }
2269
42bf546c 2270 if (secs_per_zone > total_sections || !secs_per_zone) {
0cfe75c5 2271 f2fs_msg(sb, KERN_INFO,
42bf546c 2272 "Wrong secs_per_zone / total_sections (%u, %u)",
0cfe75c5
JK
2273 secs_per_zone, total_sections);
2274 return 1;
2275 }
2276 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
2277 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
2278 (le32_to_cpu(raw_super->extension_count) +
2279 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
2280 f2fs_msg(sb, KERN_INFO,
2281 "Corrupted extension count (%u + %u > %u)",
2282 le32_to_cpu(raw_super->extension_count),
2283 raw_super->hot_ext_count,
2284 F2FS_MAX_EXTENSION);
2285 return 1;
2286 }
2287
2288 if (le32_to_cpu(raw_super->cp_payload) >
2289 (blocks_per_seg - F2FS_CP_PACKS)) {
2290 f2fs_msg(sb, KERN_INFO,
2291 "Insane cp_payload (%u > %u)",
2292 le32_to_cpu(raw_super->cp_payload),
2293 blocks_per_seg - F2FS_CP_PACKS);
2294 return 1;
2295 }
2296
9a59b62f
CY
2297 /* check reserved ino info */
2298 if (le32_to_cpu(raw_super->node_ino) != 1 ||
2299 le32_to_cpu(raw_super->meta_ino) != 2 ||
2300 le32_to_cpu(raw_super->root_ino) != 3) {
2301 f2fs_msg(sb, KERN_INFO,
2302 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
2303 le32_to_cpu(raw_super->node_ino),
2304 le32_to_cpu(raw_super->meta_ino),
2305 le32_to_cpu(raw_super->root_ino));
2306 return 1;
2307 }
2308
2309 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
df728b0f 2310 if (sanity_check_area_boundary(sbi, bh))
9a59b62f
CY
2311 return 1;
2312
aff063e2
JK
2313 return 0;
2314}
2315
4d57b86d 2316int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
aff063e2
JK
2317{
2318 unsigned int total, fsmeta;
577e3495
JK
2319 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2320 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2040fce8 2321 unsigned int ovp_segments, reserved_segments;
15d3042a 2322 unsigned int main_segs, blocks_per_seg;
c77ec61c
CY
2323 unsigned int sit_segs, nat_segs;
2324 unsigned int sit_bitmap_size, nat_bitmap_size;
2325 unsigned int log_blocks_per_seg;
9dc956b2 2326 unsigned int segment_count_main;
e494c2f9 2327 unsigned int cp_pack_start_sum, cp_payload;
9dc956b2 2328 block_t user_block_count;
042be0f8 2329 int i, j;
aff063e2
JK
2330
2331 total = le32_to_cpu(raw_super->segment_count);
2332 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
c77ec61c
CY
2333 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
2334 fsmeta += sit_segs;
2335 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
2336 fsmeta += nat_segs;
aff063e2
JK
2337 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
2338 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
2339
6bacf52f 2340 if (unlikely(fsmeta >= total))
aff063e2 2341 return 1;
577e3495 2342
2040fce8
JK
2343 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2344 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2345
2346 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
2347 ovp_segments == 0 || reserved_segments == 0)) {
2348 f2fs_msg(sbi->sb, KERN_ERR,
2349 "Wrong layout: check mkfs.f2fs version");
2350 return 1;
2351 }
2352
9dc956b2
CY
2353 user_block_count = le64_to_cpu(ckpt->user_block_count);
2354 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2355 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2356 if (!user_block_count || user_block_count >=
2357 segment_count_main << log_blocks_per_seg) {
2358 f2fs_msg(sbi->sb, KERN_ERR,
2359 "Wrong user_block_count: %u", user_block_count);
2360 return 1;
2361 }
2362
15d3042a
JQ
2363 main_segs = le32_to_cpu(raw_super->segment_count_main);
2364 blocks_per_seg = sbi->blocks_per_seg;
2365
2366 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2367 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
2368 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
2369 return 1;
042be0f8
CY
2370 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
2371 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
2372 le32_to_cpu(ckpt->cur_node_segno[j])) {
2373 f2fs_msg(sbi->sb, KERN_ERR,
2374 "Node segment (%u, %u) has the same "
2375 "segno: %u", i, j,
2376 le32_to_cpu(ckpt->cur_node_segno[i]));
2377 return 1;
2378 }
2379 }
15d3042a
JQ
2380 }
2381 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
2382 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
2383 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
2384 return 1;
042be0f8
CY
2385 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
2386 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
2387 le32_to_cpu(ckpt->cur_data_segno[j])) {
2388 f2fs_msg(sbi->sb, KERN_ERR,
2389 "Data segment (%u, %u) has the same "
2390 "segno: %u", i, j,
2391 le32_to_cpu(ckpt->cur_data_segno[i]));
2392 return 1;
2393 }
2394 }
2395 }
2396 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2397 for (j = i; j < NR_CURSEG_DATA_TYPE; j++) {
2398 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
2399 le32_to_cpu(ckpt->cur_data_segno[j])) {
2400 f2fs_msg(sbi->sb, KERN_ERR,
2401 "Data segment (%u) and Data segment (%u)"
2402 " has the same segno: %u", i, j,
2403 le32_to_cpu(ckpt->cur_node_segno[i]));
2404 return 1;
2405 }
2406 }
15d3042a
JQ
2407 }
2408
c77ec61c
CY
2409 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
2410 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
c77ec61c
CY
2411
2412 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
2413 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
2414 f2fs_msg(sbi->sb, KERN_ERR,
2415 "Wrong bitmap size: sit: %u, nat:%u",
2416 sit_bitmap_size, nat_bitmap_size);
2417 return 1;
2418 }
e494c2f9
CY
2419
2420 cp_pack_start_sum = __start_sum_addr(sbi);
2421 cp_payload = __cp_payload(sbi);
2422 if (cp_pack_start_sum < cp_payload + 1 ||
2423 cp_pack_start_sum > blocks_per_seg - 1 -
2424 NR_CURSEG_TYPE) {
2425 f2fs_msg(sbi->sb, KERN_ERR,
2426 "Wrong cp_pack_start_sum: %u",
2427 cp_pack_start_sum);
2428 return 1;
2429 }
c77ec61c 2430
1e968fdf 2431 if (unlikely(f2fs_cp_error(sbi))) {
577e3495
JK
2432 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
2433 return 1;
2434 }
aff063e2
JK
2435 return 0;
2436}
2437
2438static void init_sb_info(struct f2fs_sb_info *sbi)
2439{
2440 struct f2fs_super_block *raw_super = sbi->raw_super;
e41e6d75 2441 int i, j;
aff063e2
JK
2442
2443 sbi->log_sectors_per_block =
2444 le32_to_cpu(raw_super->log_sectors_per_block);
2445 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
2446 sbi->blocksize = 1 << sbi->log_blocksize;
2447 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2448 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
2449 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2450 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2451 sbi->total_sections = le32_to_cpu(raw_super->section_count);
2452 sbi->total_node_count =
2453 (le32_to_cpu(raw_super->segment_count_nat) / 2)
2454 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
2455 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
2456 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
2457 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
5ec4e49f 2458 sbi->cur_victim_sec = NULL_SECNO;
b1c57c1c 2459 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
aff063e2 2460
ab9fa662 2461 sbi->dir_level = DEF_DIR_LEVEL;
6beceb54 2462 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
d0239e1b 2463 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
a7d10cf3
ST
2464 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
2465 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
caf0047e 2466 clear_sbi_flag(sbi, SBI_NEED_FSCK);
2658e50d 2467
35782b23
JK
2468 for (i = 0; i < NR_COUNT_TYPE; i++)
2469 atomic_set(&sbi->nr_pages[i], 0);
2470
c29fd0c0
CY
2471 for (i = 0; i < META; i++)
2472 atomic_set(&sbi->wb_sync_req[i], 0);
687de7f1 2473
2658e50d
JK
2474 INIT_LIST_HEAD(&sbi->s_list);
2475 mutex_init(&sbi->umount_mutex);
e41e6d75
CY
2476 for (i = 0; i < NR_PAGE_TYPE - 1; i++)
2477 for (j = HOT; j < NR_TEMP_TYPE; j++)
2478 mutex_init(&sbi->wio_mutex[i][j]);
107a805d 2479 init_rwsem(&sbi->io_order_lock);
aaec2b1d 2480 spin_lock_init(&sbi->cp_lock);
1228b482
CY
2481
2482 sbi->dirty_device = 0;
2483 spin_lock_init(&sbi->dev_lock);
d0d3f1b3 2484
846ae671 2485 init_rwsem(&sbi->sb_lock);
aff063e2
JK
2486}
2487
523be8a6
JK
2488static int init_percpu_info(struct f2fs_sb_info *sbi)
2489{
35782b23 2490 int err;
41382ec4 2491
513c5f37
JK
2492 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
2493 if (err)
2494 return err;
2495
4a70e255 2496 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
41382ec4 2497 GFP_KERNEL);
4a70e255
CY
2498 if (err)
2499 percpu_counter_destroy(&sbi->alloc_valid_block_count);
2500
2501 return err;
523be8a6
JK
2502}
2503
178053e2 2504#ifdef CONFIG_BLK_DEV_ZONED
3c62be17 2505static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
178053e2 2506{
3c62be17 2507 struct block_device *bdev = FDEV(devi).bdev;
178053e2
DLM
2508 sector_t nr_sectors = bdev->bd_part->nr_sects;
2509 sector_t sector = 0;
2510 struct blk_zone *zones;
2511 unsigned int i, nr_zones;
2512 unsigned int n = 0;
2513 int err = -EIO;
2514
ccd31cb2 2515 if (!f2fs_sb_has_blkzoned(sbi->sb))
178053e2
DLM
2516 return 0;
2517
3c62be17 2518 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
f99e8648 2519 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
3c62be17 2520 return -EINVAL;
f99e8648 2521 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
3c62be17
JK
2522 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
2523 __ilog2_u32(sbi->blocks_per_blkz))
2524 return -EINVAL;
178053e2 2525 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
3c62be17
JK
2526 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
2527 sbi->log_blocks_per_blkz;
f99e8648 2528 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
3c62be17 2529 FDEV(devi).nr_blkz++;
178053e2 2530
4e6aad29
CY
2531 FDEV(devi).blkz_type = f2fs_kmalloc(sbi, FDEV(devi).nr_blkz,
2532 GFP_KERNEL);
3c62be17 2533 if (!FDEV(devi).blkz_type)
178053e2
DLM
2534 return -ENOMEM;
2535
2536#define F2FS_REPORT_NR_ZONES 4096
2537
026f0507
KC
2538 zones = f2fs_kzalloc(sbi,
2539 array_size(F2FS_REPORT_NR_ZONES,
2540 sizeof(struct blk_zone)),
2541 GFP_KERNEL);
178053e2
DLM
2542 if (!zones)
2543 return -ENOMEM;
2544
2545 /* Get block zones type */
2546 while (zones && sector < nr_sectors) {
2547
2548 nr_zones = F2FS_REPORT_NR_ZONES;
2549 err = blkdev_report_zones(bdev, sector,
2550 zones, &nr_zones,
2551 GFP_KERNEL);
2552 if (err)
2553 break;
2554 if (!nr_zones) {
2555 err = -EIO;
2556 break;
2557 }
2558
2559 for (i = 0; i < nr_zones; i++) {
3c62be17 2560 FDEV(devi).blkz_type[n] = zones[i].type;
178053e2
DLM
2561 sector += zones[i].len;
2562 n++;
2563 }
2564 }
2565
2566 kfree(zones);
2567
2568 return err;
2569}
2570#endif
2571
9076a75f
GZ
2572/*
2573 * Read f2fs raw super block.
2b39e907
SL
2574 * Because we have two copies of super block, so read both of them
2575 * to get the first valid one. If any one of them is broken, we pass
2576 * them recovery flag back to the caller.
9076a75f 2577 */
df728b0f 2578static int read_raw_super_block(struct f2fs_sb_info *sbi,
9076a75f 2579 struct f2fs_super_block **raw_super,
e8240f65 2580 int *valid_super_block, int *recovery)
14d7e9de 2581{
df728b0f 2582 struct super_block *sb = sbi->sb;
2b39e907 2583 int block;
e8240f65 2584 struct buffer_head *bh;
fd694733 2585 struct f2fs_super_block *super;
da554e48 2586 int err = 0;
14d7e9de 2587
b39f0de2
YH
2588 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
2589 if (!super)
2590 return -ENOMEM;
2b39e907
SL
2591
2592 for (block = 0; block < 2; block++) {
2593 bh = sb_bread(sb, block);
2594 if (!bh) {
2595 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
9076a75f 2596 block + 1);
2b39e907
SL
2597 err = -EIO;
2598 continue;
2599 }
14d7e9de 2600
2b39e907 2601 /* sanity checking of raw super */
df728b0f 2602 if (sanity_check_raw_super(sbi, bh)) {
2b39e907
SL
2603 f2fs_msg(sb, KERN_ERR,
2604 "Can't find valid F2FS filesystem in %dth superblock",
2605 block + 1);
2606 err = -EINVAL;
2607 brelse(bh);
2608 continue;
2609 }
14d7e9de 2610
2b39e907 2611 if (!*raw_super) {
fd694733
JK
2612 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
2613 sizeof(*super));
2b39e907
SL
2614 *valid_super_block = block;
2615 *raw_super = super;
2616 }
2617 brelse(bh);
da554e48 2618 }
2619
2b39e907
SL
2620 /* Fail to read any one of the superblocks*/
2621 if (err < 0)
2622 *recovery = 1;
da554e48 2623
da554e48 2624 /* No valid superblock */
2b39e907 2625 if (!*raw_super)
b39f0de2 2626 kfree(super);
2b39e907
SL
2627 else
2628 err = 0;
da554e48 2629
2b39e907 2630 return err;
14d7e9de 2631}
2632
fd694733 2633int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
26d815ad 2634{
5d909cdb 2635 struct buffer_head *bh;
26d815ad
JK
2636 int err;
2637
df728b0f
JK
2638 if ((recover && f2fs_readonly(sbi->sb)) ||
2639 bdev_read_only(sbi->sb->s_bdev)) {
2640 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
f2353d7b 2641 return -EROFS;
df728b0f 2642 }
f2353d7b 2643
fd694733 2644 /* write back-up superblock first */
0964fc1a 2645 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
5d909cdb
JK
2646 if (!bh)
2647 return -EIO;
fd694733 2648 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
5d909cdb 2649 brelse(bh);
c5bda1c8
CY
2650
2651 /* if we are in recovery path, skip writing valid superblock */
2652 if (recover || err)
5d909cdb 2653 return err;
26d815ad
JK
2654
2655 /* write current valid superblock */
0964fc1a 2656 bh = sb_bread(sbi->sb, sbi->valid_super_block);
fd694733
JK
2657 if (!bh)
2658 return -EIO;
2659 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
2660 brelse(bh);
2661 return err;
26d815ad
JK
2662}
2663
3c62be17
JK
2664static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
2665{
2666 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
7bb3a371 2667 unsigned int max_devices = MAX_DEVICES;
3c62be17
JK
2668 int i;
2669
7bb3a371
MS
2670 /* Initialize single device information */
2671 if (!RDEV(0).path[0]) {
2672 if (!bdev_is_zoned(sbi->sb->s_bdev))
3c62be17 2673 return 0;
7bb3a371
MS
2674 max_devices = 1;
2675 }
3c62be17 2676
7bb3a371
MS
2677 /*
2678 * Initialize multiple devices information, or single
2679 * zoned block device information.
2680 */
026f0507
KC
2681 sbi->devs = f2fs_kzalloc(sbi,
2682 array_size(max_devices,
2683 sizeof(struct f2fs_dev_info)),
2684 GFP_KERNEL);
7bb3a371
MS
2685 if (!sbi->devs)
2686 return -ENOMEM;
3c62be17 2687
7bb3a371 2688 for (i = 0; i < max_devices; i++) {
3c62be17 2689
7bb3a371
MS
2690 if (i > 0 && !RDEV(i).path[0])
2691 break;
2692
2693 if (max_devices == 1) {
2694 /* Single zoned block device mount */
2695 FDEV(0).bdev =
2696 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
3c62be17 2697 sbi->sb->s_mode, sbi->sb->s_type);
7bb3a371
MS
2698 } else {
2699 /* Multi-device mount */
2700 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
2701 FDEV(i).total_segments =
2702 le32_to_cpu(RDEV(i).total_segments);
2703 if (i == 0) {
2704 FDEV(i).start_blk = 0;
2705 FDEV(i).end_blk = FDEV(i).start_blk +
2706 (FDEV(i).total_segments <<
2707 sbi->log_blocks_per_seg) - 1 +
2708 le32_to_cpu(raw_super->segment0_blkaddr);
2709 } else {
2710 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
2711 FDEV(i).end_blk = FDEV(i).start_blk +
2712 (FDEV(i).total_segments <<
2713 sbi->log_blocks_per_seg) - 1;
2714 }
2715 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
3c62be17 2716 sbi->sb->s_mode, sbi->sb->s_type);
7bb3a371 2717 }
3c62be17
JK
2718 if (IS_ERR(FDEV(i).bdev))
2719 return PTR_ERR(FDEV(i).bdev);
2720
2721 /* to release errored devices */
2722 sbi->s_ndevs = i + 1;
2723
2724#ifdef CONFIG_BLK_DEV_ZONED
2725 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
ccd31cb2 2726 !f2fs_sb_has_blkzoned(sbi->sb)) {
3c62be17
JK
2727 f2fs_msg(sbi->sb, KERN_ERR,
2728 "Zoned block device feature not enabled\n");
2729 return -EINVAL;
2730 }
2731 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
2732 if (init_blkz_info(sbi, i)) {
2733 f2fs_msg(sbi->sb, KERN_ERR,
2734 "Failed to initialize F2FS blkzone information");
2735 return -EINVAL;
2736 }
7bb3a371
MS
2737 if (max_devices == 1)
2738 break;
3c62be17
JK
2739 f2fs_msg(sbi->sb, KERN_INFO,
2740 "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
2741 i, FDEV(i).path,
2742 FDEV(i).total_segments,
2743 FDEV(i).start_blk, FDEV(i).end_blk,
2744 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
2745 "Host-aware" : "Host-managed");
2746 continue;
2747 }
2748#endif
2749 f2fs_msg(sbi->sb, KERN_INFO,
2750 "Mount Device [%2d]: %20s, %8u, %8x - %8x",
2751 i, FDEV(i).path,
2752 FDEV(i).total_segments,
2753 FDEV(i).start_blk, FDEV(i).end_blk);
2754 }
0a595eba
JK
2755 f2fs_msg(sbi->sb, KERN_INFO,
2756 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
3c62be17
JK
2757 return 0;
2758}
2759
84b89e5d
JK
2760static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
2761{
2762 struct f2fs_sm_info *sm_i = SM_I(sbi);
2763
2764 /* adjust parameters according to the volume size */
2765 if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
63189b78 2766 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
84b89e5d
JK
2767 sm_i->dcc_info->discard_granularity = 1;
2768 sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
2769 }
4cac90d5
CY
2770
2771 sbi->readdir_ra = 1;
84b89e5d
JK
2772}
2773
aff063e2
JK
2774static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
2775{
2776 struct f2fs_sb_info *sbi;
da554e48 2777 struct f2fs_super_block *raw_super;
aff063e2 2778 struct inode *root;
99e3e858 2779 int err;
2adc3505 2780 bool retry = true, need_fsck = false;
dabc4a5c 2781 char *options = NULL;
e8240f65 2782 int recovery, i, valid_super_block;
8f1dbbbb 2783 struct curseg_info *seg_i;
aff063e2 2784
ed2e621a 2785try_onemore:
da554e48 2786 err = -EINVAL;
2787 raw_super = NULL;
e8240f65 2788 valid_super_block = -1;
da554e48 2789 recovery = 0;
2790
aff063e2
JK
2791 /* allocate memory for f2fs-specific super block info */
2792 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
2793 if (!sbi)
2794 return -ENOMEM;
2795
df728b0f
JK
2796 sbi->sb = sb;
2797
43b6573b
KM
2798 /* Load the checksum driver */
2799 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
2800 if (IS_ERR(sbi->s_chksum_driver)) {
2801 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
2802 err = PTR_ERR(sbi->s_chksum_driver);
2803 sbi->s_chksum_driver = NULL;
2804 goto free_sbi;
2805 }
2806
ff9234ad 2807 /* set a block size */
6bacf52f 2808 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
a07ef784 2809 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
aff063e2 2810 goto free_sbi;
a07ef784 2811 }
aff063e2 2812
df728b0f 2813 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
e8240f65 2814 &recovery);
9076a75f
GZ
2815 if (err)
2816 goto free_sbi;
2817
5fb08372 2818 sb->s_fs_info = sbi;
52763a4b
JK
2819 sbi->raw_super = raw_super;
2820
704956ec
CY
2821 /* precompute checksum seed for metadata */
2822 if (f2fs_sb_has_inode_chksum(sb))
2823 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
2824 sizeof(raw_super->uuid));
2825
d1b959c8
DLM
2826 /*
2827 * The BLKZONED feature indicates that the drive was formatted with
2828 * zone alignment optimization. This is optional for host-aware
2829 * devices, but mandatory for host-managed zoned block devices.
2830 */
2831#ifndef CONFIG_BLK_DEV_ZONED
ccd31cb2 2832 if (f2fs_sb_has_blkzoned(sb)) {
d1b959c8
DLM
2833 f2fs_msg(sb, KERN_ERR,
2834 "Zoned block device support is not enabled\n");
1727f317 2835 err = -EOPNOTSUPP;
d1b959c8
DLM
2836 goto free_sb_buf;
2837 }
d1b959c8 2838#endif
498c5e9f 2839 default_options(sbi);
aff063e2 2840 /* parse mount options */
dabc4a5c
JK
2841 options = kstrdup((const char *)data, GFP_KERNEL);
2842 if (data && !options) {
2843 err = -ENOMEM;
aff063e2 2844 goto free_sb_buf;
dabc4a5c
JK
2845 }
2846
2847 err = parse_options(sb, options);
2848 if (err)
2849 goto free_options;
aff063e2 2850
e0afc4d6
CY
2851 sbi->max_file_blocks = max_file_blocks();
2852 sb->s_maxbytes = sbi->max_file_blocks <<
2853 le32_to_cpu(raw_super->log_blocksize);
aff063e2
JK
2854 sb->s_max_links = F2FS_LINK_MAX;
2855 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2856
0abd675e
CY
2857#ifdef CONFIG_QUOTA
2858 sb->dq_op = &f2fs_quota_operations;
ea676733
JK
2859 if (f2fs_sb_has_quota_ino(sb))
2860 sb->s_qcop = &dquot_quotactl_sysfile_ops;
2861 else
2862 sb->s_qcop = &f2fs_quotactl_ops;
5c57132e 2863 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
292c196a
CY
2864
2865 if (f2fs_sb_has_quota_ino(sbi->sb)) {
2866 for (i = 0; i < MAXQUOTAS; i++) {
2867 if (f2fs_qf_ino(sbi->sb, i))
2868 sbi->nquota_files++;
2869 }
2870 }
0abd675e
CY
2871#endif
2872
aff063e2 2873 sb->s_op = &f2fs_sops;
ffcc4182 2874#ifdef CONFIG_F2FS_FS_ENCRYPTION
0b81d077 2875 sb->s_cop = &f2fs_cryptops;
ffcc4182 2876#endif
aff063e2
JK
2877 sb->s_xattr = f2fs_xattr_handlers;
2878 sb->s_export_op = &f2fs_export_ops;
2879 sb->s_magic = F2FS_SUPER_MAGIC;
aff063e2 2880 sb->s_time_gran = 1;
1751e8a6
LT
2881 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2882 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
85787090 2883 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
578c6478 2884 sb->s_iflags |= SB_I_CGROUPWB;
aff063e2
JK
2885
2886 /* init f2fs-specific super block info */
e8240f65 2887 sbi->valid_super_block = valid_super_block;
aff063e2 2888 mutex_init(&sbi->gc_mutex);
853137ce 2889 mutex_init(&sbi->writepages);
aff063e2 2890 mutex_init(&sbi->cp_mutex);
b3582c68 2891 init_rwsem(&sbi->node_write);
59c9081b 2892 init_rwsem(&sbi->node_change);
315df839
JK
2893
2894 /* disallow all the data/node/meta page writes */
2895 set_sbi_flag(sbi, SBI_POR_DOING);
aff063e2 2896 spin_lock_init(&sbi->stat_lock);
971767ca 2897
b0af6d49
CY
2898 /* init iostat info */
2899 spin_lock_init(&sbi->iostat_lock);
2900 sbi->iostat_enable = false;
2901
458e6197 2902 for (i = 0; i < NR_PAGE_TYPE; i++) {
a912b54d
JK
2903 int n = (i == META) ? 1: NR_TEMP_TYPE;
2904 int j;
2905
c8606593
KC
2906 sbi->write_io[i] =
2907 f2fs_kmalloc(sbi,
2908 array_size(n,
2909 sizeof(struct f2fs_bio_info)),
2910 GFP_KERNEL);
b63def91
CJ
2911 if (!sbi->write_io[i]) {
2912 err = -ENOMEM;
0b2103e8 2913 goto free_bio_info;
b63def91 2914 }
a912b54d
JK
2915
2916 for (j = HOT; j < n; j++) {
2917 init_rwsem(&sbi->write_io[i][j].io_rwsem);
2918 sbi->write_io[i][j].sbi = sbi;
2919 sbi->write_io[i][j].bio = NULL;
fb830fc5
CY
2920 spin_lock_init(&sbi->write_io[i][j].io_lock);
2921 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
a912b54d 2922 }
458e6197 2923 }
971767ca 2924
b873b798 2925 init_rwsem(&sbi->cp_rwsem);
fb51b5ef 2926 init_waitqueue_head(&sbi->cp_wait);
aff063e2
JK
2927 init_sb_info(sbi);
2928
523be8a6
JK
2929 err = init_percpu_info(sbi);
2930 if (err)
d7997e63 2931 goto free_bio_info;
523be8a6 2932
0a595eba
JK
2933 if (F2FS_IO_SIZE(sbi) > 1) {
2934 sbi->write_io_dummy =
a3ebfe4f 2935 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
1727f317
CY
2936 if (!sbi->write_io_dummy) {
2937 err = -ENOMEM;
d7997e63 2938 goto free_percpu;
1727f317 2939 }
0a595eba
JK
2940 }
2941
aff063e2
JK
2942 /* get an inode for meta space */
2943 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
2944 if (IS_ERR(sbi->meta_inode)) {
a07ef784 2945 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
aff063e2 2946 err = PTR_ERR(sbi->meta_inode);
0a595eba 2947 goto free_io_dummy;
aff063e2
JK
2948 }
2949
4d57b86d 2950 err = f2fs_get_valid_checkpoint(sbi);
a07ef784
NJ
2951 if (err) {
2952 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
aff063e2 2953 goto free_meta_inode;
a07ef784 2954 }
aff063e2 2955
3c62be17
JK
2956 /* Initialize device list */
2957 err = f2fs_scan_devices(sbi);
2958 if (err) {
2959 f2fs_msg(sb, KERN_ERR, "Failed to find devices");
2960 goto free_devices;
2961 }
2962
aff063e2
JK
2963 sbi->total_valid_node_count =
2964 le32_to_cpu(sbi->ckpt->valid_node_count);
513c5f37
JK
2965 percpu_counter_set(&sbi->total_valid_inode_count,
2966 le32_to_cpu(sbi->ckpt->valid_inode_count));
aff063e2
JK
2967 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
2968 sbi->total_valid_block_count =
2969 le64_to_cpu(sbi->ckpt->valid_block_count);
2970 sbi->last_valid_block_count = sbi->total_valid_block_count;
daeb433e 2971 sbi->reserved_blocks = 0;
80d42145 2972 sbi->current_reserved_blocks = 0;
7e65be49 2973 limit_reserve_root(sbi);
41382ec4 2974
c227f912
CY
2975 for (i = 0; i < NR_INODE_TYPE; i++) {
2976 INIT_LIST_HEAD(&sbi->inode_list[i]);
2977 spin_lock_init(&sbi->inode_lock[i]);
2978 }
aff063e2 2979
4d57b86d 2980 f2fs_init_extent_cache_info(sbi);
1dcc336b 2981
4d57b86d 2982 f2fs_init_ino_entry_info(sbi);
aff063e2 2983
50fa53ec
CY
2984 f2fs_init_fsync_node_info(sbi);
2985
aff063e2 2986 /* setup f2fs internal modules */
4d57b86d 2987 err = f2fs_build_segment_manager(sbi);
a07ef784
NJ
2988 if (err) {
2989 f2fs_msg(sb, KERN_ERR,
2990 "Failed to initialize F2FS segment manager");
aff063e2 2991 goto free_sm;
a07ef784 2992 }
4d57b86d 2993 err = f2fs_build_node_manager(sbi);
a07ef784
NJ
2994 if (err) {
2995 f2fs_msg(sb, KERN_ERR,
2996 "Failed to initialize F2FS node manager");
aff063e2 2997 goto free_nm;
a07ef784 2998 }
aff063e2 2999
8f1dbbbb
SL
3000 /* For write statistics */
3001 if (sb->s_bdev->bd_part)
3002 sbi->sectors_written_start =
dbae2c55
MC
3003 (u64)part_stat_read(sb->s_bdev->bd_part,
3004 sectors[STAT_WRITE]);
8f1dbbbb
SL
3005
3006 /* Read accumulated write IO statistics if exists */
3007 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
3008 if (__exist_node_summaries(sbi))
3009 sbi->kbytes_written =
b2dde6fc 3010 le64_to_cpu(seg_i->journal->info.kbytes_written);
8f1dbbbb 3011
4d57b86d 3012 f2fs_build_gc_manager(sbi);
aff063e2
JK
3013
3014 /* get an inode for node space */
3015 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
3016 if (IS_ERR(sbi->node_inode)) {
a07ef784 3017 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
aff063e2
JK
3018 err = PTR_ERR(sbi->node_inode);
3019 goto free_nm;
3020 }
3021
aa51d08a
JK
3022 err = f2fs_build_stats(sbi);
3023 if (err)
bae01eda 3024 goto free_node_inode;
aa51d08a 3025
aff063e2
JK
3026 /* read root inode and dentry */
3027 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
3028 if (IS_ERR(root)) {
a07ef784 3029 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
aff063e2 3030 err = PTR_ERR(root);
bae01eda 3031 goto free_stats;
aff063e2 3032 }
bcbfbd60
CY
3033 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
3034 !root->i_size || !root->i_nlink) {
9d847950 3035 iput(root);
8f99a946 3036 err = -EINVAL;
4e423832 3037 goto free_stats;
8f99a946 3038 }
aff063e2
JK
3039
3040 sb->s_root = d_make_root(root); /* allocate root dentry */
3041 if (!sb->s_root) {
3042 err = -ENOMEM;
3043 goto free_root_inode;
3044 }
3045
dc6b2055 3046 err = f2fs_register_sysfs(sbi);
b59d0bae 3047 if (err)
a398101a 3048 goto free_root_inode;
b59d0bae 3049
ea676733 3050#ifdef CONFIG_QUOTA
76cf05d7 3051 /* Enable quota usage during mount */
deeedd71 3052 if (f2fs_sb_has_quota_ino(sb) && !f2fs_readonly(sb)) {
ea676733
JK
3053 err = f2fs_enable_quotas(sb);
3054 if (err) {
3055 f2fs_msg(sb, KERN_ERR,
3056 "Cannot turn on quotas: error %d", err);
3057 goto free_sysfs;
3058 }
3059 }
3060#endif
4b2414d0 3061 /* if there are nt orphan nodes free them */
4d57b86d 3062 err = f2fs_recover_orphan_inodes(sbi);
4b2414d0 3063 if (err)
ea676733 3064 goto free_meta;
4b2414d0 3065
6437d1b0
JK
3066 /* recover fsynced data */
3067 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
081d78c2
JK
3068 /*
3069 * mount should be failed, when device has readonly mode, and
3070 * previous checkpoint was not done by clean system shutdown.
3071 */
3072 if (bdev_read_only(sb->s_bdev) &&
aaec2b1d 3073 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
081d78c2 3074 err = -EROFS;
4b2414d0 3075 goto free_meta;
081d78c2 3076 }
2adc3505
CY
3077
3078 if (need_fsck)
3079 set_sbi_flag(sbi, SBI_NEED_FSCK);
3080
a468f0ef
JK
3081 if (!retry)
3082 goto skip_recovery;
3083
4d57b86d 3084 err = f2fs_recover_fsync_data(sbi, false);
6781eabb 3085 if (err < 0) {
2adc3505 3086 need_fsck = true;
6437d1b0 3087 f2fs_msg(sb, KERN_ERR,
99e3e858 3088 "Cannot recover all fsync data errno=%d", err);
4b2414d0 3089 goto free_meta;
ed2e621a 3090 }
6781eabb 3091 } else {
4d57b86d 3092 err = f2fs_recover_fsync_data(sbi, true);
6781eabb
JK
3093
3094 if (!f2fs_readonly(sb) && err > 0) {
3095 err = -EINVAL;
3096 f2fs_msg(sb, KERN_ERR,
3097 "Need to recover fsync data");
ea676733 3098 goto free_meta;
6781eabb 3099 }
6437d1b0 3100 }
a468f0ef 3101skip_recovery:
4d57b86d 3102 /* f2fs_recover_fsync_data() cleared this already */
315df839 3103 clear_sbi_flag(sbi, SBI_POR_DOING);
b59d0bae 3104
6437d1b0
JK
3105 /*
3106 * If filesystem is not mounted as read-only then
3107 * do start the gc_thread.
3108 */
6c029932 3109 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
6437d1b0 3110 /* After POR, we can run background GC thread.*/
4d57b86d 3111 err = f2fs_start_gc_thread(sbi);
6437d1b0 3112 if (err)
4b2414d0 3113 goto free_meta;
6437d1b0 3114 }
dabc4a5c 3115 kfree(options);
da554e48 3116
3117 /* recover broken superblock */
f2353d7b 3118 if (recovery) {
41214b3c
CY
3119 err = f2fs_commit_super(sbi, true);
3120 f2fs_msg(sb, KERN_INFO,
99e3e858 3121 "Try to recover %dth superblock, ret: %d",
41214b3c 3122 sbi->valid_super_block ? 1 : 2, err);
da554e48 3123 }
3124
bae01eda
CY
3125 f2fs_join_shrinker(sbi);
3126
84b89e5d
JK
3127 f2fs_tuning_parameters(sbi);
3128
1200abb2
JK
3129 f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
3130 cur_cp_version(F2FS_CKPT(sbi)));
6beceb54 3131 f2fs_update_time(sbi, CP_TIME);
d0239e1b 3132 f2fs_update_time(sbi, REQ_TIME);
aff063e2 3133 return 0;
6437d1b0 3134
4b2414d0 3135free_meta:
ea676733 3136#ifdef CONFIG_QUOTA
deeedd71 3137 if (f2fs_sb_has_quota_ino(sb) && !f2fs_readonly(sb))
ea676733
JK
3138 f2fs_quota_off_umount(sbi->sb);
3139#endif
0f18b462 3140 f2fs_sync_inode_meta(sbi);
4b2414d0 3141 /*
4d57b86d 3142 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
4b2414d0 3143 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
4d57b86d
CY
3144 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
3145 * falls into an infinite loop in f2fs_sync_meta_pages().
4b2414d0
CY
3146 */
3147 truncate_inode_pages_final(META_MAPPING(sbi));
ea676733 3148#ifdef CONFIG_QUOTA
4b2414d0 3149free_sysfs:
ea676733 3150#endif
dc6b2055 3151 f2fs_unregister_sysfs(sbi);
aff063e2
JK
3152free_root_inode:
3153 dput(sb->s_root);
3154 sb->s_root = NULL;
bae01eda
CY
3155free_stats:
3156 f2fs_destroy_stats(sbi);
aff063e2 3157free_node_inode:
4d57b86d 3158 f2fs_release_ino_entry(sbi, true);
bae01eda 3159 truncate_inode_pages_final(NODE_MAPPING(sbi));
aff063e2
JK
3160 iput(sbi->node_inode);
3161free_nm:
4d57b86d 3162 f2fs_destroy_node_manager(sbi);
aff063e2 3163free_sm:
4d57b86d 3164 f2fs_destroy_segment_manager(sbi);
3c62be17
JK
3165free_devices:
3166 destroy_device_list(sbi);
aff063e2
JK
3167 kfree(sbi->ckpt);
3168free_meta_inode:
3169 make_bad_inode(sbi->meta_inode);
3170 iput(sbi->meta_inode);
0a595eba
JK
3171free_io_dummy:
3172 mempool_destroy(sbi->write_io_dummy);
d7997e63
CY
3173free_percpu:
3174 destroy_percpu_info(sbi);
3175free_bio_info:
a912b54d
JK
3176 for (i = 0; i < NR_PAGE_TYPE; i++)
3177 kfree(sbi->write_io[i]);
d7997e63 3178free_options:
4b2414d0
CY
3179#ifdef CONFIG_QUOTA
3180 for (i = 0; i < MAXQUOTAS; i++)
63189b78 3181 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
4b2414d0 3182#endif
dabc4a5c 3183 kfree(options);
aff063e2 3184free_sb_buf:
b39f0de2 3185 kfree(raw_super);
aff063e2 3186free_sbi:
43b6573b
KM
3187 if (sbi->s_chksum_driver)
3188 crypto_free_shash(sbi->s_chksum_driver);
aff063e2 3189 kfree(sbi);
ed2e621a
JK
3190
3191 /* give only one another chance */
3192 if (retry) {
9df47ba7 3193 retry = false;
ed2e621a
JK
3194 shrink_dcache_sb(sb);
3195 goto try_onemore;
3196 }
aff063e2
JK
3197 return err;
3198}
3199
3200static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
3201 const char *dev_name, void *data)
3202{
3203 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
3204}
3205
30a5537f
JK
3206static void kill_f2fs_super(struct super_block *sb)
3207{
cce13252 3208 if (sb->s_root) {
1cb50f87
JK
3209 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3210
3211 set_sbi_flag(sbi, SBI_IS_CLOSE);
3212 f2fs_stop_gc_thread(sbi);
3213 f2fs_stop_discard_thread(sbi);
3214
3215 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
3216 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3217 struct cp_control cpc = {
3218 .reason = CP_UMOUNT,
3219 };
3220 f2fs_write_checkpoint(sbi, &cpc);
3221 }
1378752b
CY
3222
3223 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
3224 sb->s_flags &= ~SB_RDONLY;
cce13252 3225 }
30a5537f
JK
3226 kill_block_super(sb);
3227}
3228
aff063e2
JK
3229static struct file_system_type f2fs_fs_type = {
3230 .owner = THIS_MODULE,
3231 .name = "f2fs",
3232 .mount = f2fs_mount,
30a5537f 3233 .kill_sb = kill_f2fs_super,
aff063e2
JK
3234 .fs_flags = FS_REQUIRES_DEV,
3235};
7f78e035 3236MODULE_ALIAS_FS("f2fs");
aff063e2 3237
6e6093a8 3238static int __init init_inodecache(void)
aff063e2 3239{
5d097056
VD
3240 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
3241 sizeof(struct f2fs_inode_info), 0,
3242 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
6bacf52f 3243 if (!f2fs_inode_cachep)
aff063e2
JK
3244 return -ENOMEM;
3245 return 0;
3246}
3247
3248static void destroy_inodecache(void)
3249{
3250 /*
3251 * Make sure all delayed rcu free inodes are flushed before we
3252 * destroy cache.
3253 */
3254 rcu_barrier();
3255 kmem_cache_destroy(f2fs_inode_cachep);
3256}
3257
3258static int __init init_f2fs_fs(void)
3259{
3260 int err;
3261
4071e67c
AP
3262 if (PAGE_SIZE != F2FS_BLKSIZE) {
3263 printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
3264 PAGE_SIZE, F2FS_BLKSIZE);
3265 return -EINVAL;
3266 }
3267
c0508650
JK
3268 f2fs_build_trace_ios();
3269
aff063e2
JK
3270 err = init_inodecache();
3271 if (err)
3272 goto fail;
4d57b86d 3273 err = f2fs_create_node_manager_caches();
aff063e2 3274 if (err)
9890ff3f 3275 goto free_inodecache;
4d57b86d 3276 err = f2fs_create_segment_manager_caches();
aff063e2 3277 if (err)
9890ff3f 3278 goto free_node_manager_caches;
4d57b86d 3279 err = f2fs_create_checkpoint_caches();
aff063e2 3280 if (err)
06292073 3281 goto free_segment_manager_caches;
4d57b86d 3282 err = f2fs_create_extent_cache();
1dcc336b
CY
3283 if (err)
3284 goto free_checkpoint_caches;
dc6b2055 3285 err = f2fs_init_sysfs();
a398101a 3286 if (err)
1dcc336b 3287 goto free_extent_cache;
2658e50d 3288 err = register_shrinker(&f2fs_shrinker_info);
cfc4d971 3289 if (err)
a398101a 3290 goto free_sysfs;
2658e50d
JK
3291 err = register_filesystem(&f2fs_fs_type);
3292 if (err)
3293 goto free_shrinker;
787c7b8c
CY
3294 err = f2fs_create_root_stats();
3295 if (err)
3296 goto free_filesystem;
6dbb1796
EB
3297 err = f2fs_init_post_read_processing();
3298 if (err)
3299 goto free_root_stats;
9890ff3f
ZH
3300 return 0;
3301
6dbb1796
EB
3302free_root_stats:
3303 f2fs_destroy_root_stats();
787c7b8c
CY
3304free_filesystem:
3305 unregister_filesystem(&f2fs_fs_type);
2658e50d
JK
3306free_shrinker:
3307 unregister_shrinker(&f2fs_shrinker_info);
a398101a 3308free_sysfs:
dc6b2055 3309 f2fs_exit_sysfs();
1dcc336b 3310free_extent_cache:
4d57b86d 3311 f2fs_destroy_extent_cache();
9890ff3f 3312free_checkpoint_caches:
4d57b86d 3313 f2fs_destroy_checkpoint_caches();
7fd9e544 3314free_segment_manager_caches:
4d57b86d 3315 f2fs_destroy_segment_manager_caches();
9890ff3f 3316free_node_manager_caches:
4d57b86d 3317 f2fs_destroy_node_manager_caches();
9890ff3f
ZH
3318free_inodecache:
3319 destroy_inodecache();
aff063e2
JK
3320fail:
3321 return err;
3322}
3323
3324static void __exit exit_f2fs_fs(void)
3325{
6dbb1796 3326 f2fs_destroy_post_read_processing();
4589d25d 3327 f2fs_destroy_root_stats();
aff063e2 3328 unregister_filesystem(&f2fs_fs_type);
b8bef79d 3329 unregister_shrinker(&f2fs_shrinker_info);
dc6b2055 3330 f2fs_exit_sysfs();
4d57b86d
CY
3331 f2fs_destroy_extent_cache();
3332 f2fs_destroy_checkpoint_caches();
3333 f2fs_destroy_segment_manager_caches();
3334 f2fs_destroy_node_manager_caches();
aff063e2 3335 destroy_inodecache();
351f4fba 3336 f2fs_destroy_trace_ios();
aff063e2
JK
3337}
3338
3339module_init(init_f2fs_fs)
3340module_exit(exit_f2fs_fs)
3341
3342MODULE_AUTHOR("Samsung Electronics's Praesto Team");
3343MODULE_DESCRIPTION("Flash Friendly File System");
3344MODULE_LICENSE("GPL");
b4b9d34c 3345