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