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