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