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