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