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