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