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