]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/f2fs/super.c
f2fs: require key for truncate(2) of encrypted file
[mirror_ubuntu-hirsute-kernel.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>
aff063e2 25#include <linux/f2fs_fs.h>
b59d0bae 26#include <linux/sysfs.h>
aff063e2
JK
27
28#include "f2fs.h"
29#include "node.h"
5ec4e49f 30#include "segment.h"
aff063e2 31#include "xattr.h"
b59d0bae 32#include "gc.h"
db9f7c1a 33#include "trace.h"
aff063e2 34
a2a4a7e4
NJ
35#define CREATE_TRACE_POINTS
36#include <trace/events/f2fs.h>
37
aff063e2
JK
38static struct kmem_cache *f2fs_inode_cachep;
39
73faec4d 40#ifdef CONFIG_F2FS_FAULT_INJECTION
2c63fead
JK
41
42char *fault_name[FAULT_MAX] = {
43 [FAULT_KMALLOC] = "kmalloc",
c41f3cc3 44 [FAULT_PAGE_ALLOC] = "page alloc",
cb78942b
JK
45 [FAULT_ALLOC_NID] = "alloc nid",
46 [FAULT_ORPHAN] = "orphan",
47 [FAULT_BLOCK] = "no more block",
48 [FAULT_DIR_DEPTH] = "too big dir depth",
53aa6bbf 49 [FAULT_EVICT_INODE] = "evict_inode fail",
14b44d23 50 [FAULT_TRUNCATE] = "truncate fail",
8b038c70 51 [FAULT_IO] = "IO error",
0f348028 52 [FAULT_CHECKPOINT] = "checkpoint error",
2c63fead 53};
08796897 54
1ecc0c5c
CY
55static void f2fs_build_fault_attr(struct f2fs_sb_info *sbi,
56 unsigned int rate)
08796897 57{
1ecc0c5c
CY
58 struct f2fs_fault_info *ffi = &sbi->fault_info;
59
08796897 60 if (rate) {
1ecc0c5c
CY
61 atomic_set(&ffi->inject_ops, 0);
62 ffi->inject_rate = rate;
63 ffi->inject_type = (1 << FAULT_MAX) - 1;
08796897 64 } else {
1ecc0c5c 65 memset(ffi, 0, sizeof(struct f2fs_fault_info));
08796897
SY
66 }
67}
73faec4d
JK
68#endif
69
2658e50d
JK
70/* f2fs-wide shrinker description */
71static struct shrinker f2fs_shrinker_info = {
72 .scan_objects = f2fs_shrink_scan,
73 .count_objects = f2fs_shrink_count,
74 .seeks = DEFAULT_SEEKS,
75};
76
aff063e2 77enum {
696c018c 78 Opt_gc_background,
aff063e2 79 Opt_disable_roll_forward,
2d834bf9 80 Opt_norecovery,
aff063e2 81 Opt_discard,
64058be9 82 Opt_nodiscard,
aff063e2 83 Opt_noheap,
7a20b8a6 84 Opt_heap,
4058c511 85 Opt_user_xattr,
aff063e2 86 Opt_nouser_xattr,
4058c511 87 Opt_acl,
aff063e2
JK
88 Opt_noacl,
89 Opt_active_logs,
90 Opt_disable_ext_identify,
444c580f 91 Opt_inline_xattr,
23cf7212 92 Opt_noinline_xattr,
8274de77 93 Opt_inline_data,
5efd3c6f 94 Opt_inline_dentry,
97c1794a 95 Opt_noinline_dentry,
6b4afdd7 96 Opt_flush_merge,
69e9e427 97 Opt_noflush_merge,
0f7b2abd 98 Opt_nobarrier,
d5053a34 99 Opt_fastboot,
89672159 100 Opt_extent_cache,
7daaea25 101 Opt_noextent_cache,
75342797 102 Opt_noinline_data,
343f40f0 103 Opt_data_flush,
36abef4e 104 Opt_mode,
ec91538d 105 Opt_io_size_bits,
73faec4d 106 Opt_fault_injection,
6d94c74a
JK
107 Opt_lazytime,
108 Opt_nolazytime,
aff063e2
JK
109 Opt_err,
110};
111
112static match_table_t f2fs_tokens = {
696c018c 113 {Opt_gc_background, "background_gc=%s"},
aff063e2 114 {Opt_disable_roll_forward, "disable_roll_forward"},
2d834bf9 115 {Opt_norecovery, "norecovery"},
aff063e2 116 {Opt_discard, "discard"},
64058be9 117 {Opt_nodiscard, "nodiscard"},
aff063e2 118 {Opt_noheap, "no_heap"},
7a20b8a6 119 {Opt_heap, "heap"},
4058c511 120 {Opt_user_xattr, "user_xattr"},
aff063e2 121 {Opt_nouser_xattr, "nouser_xattr"},
4058c511 122 {Opt_acl, "acl"},
aff063e2
JK
123 {Opt_noacl, "noacl"},
124 {Opt_active_logs, "active_logs=%u"},
125 {Opt_disable_ext_identify, "disable_ext_identify"},
444c580f 126 {Opt_inline_xattr, "inline_xattr"},
23cf7212 127 {Opt_noinline_xattr, "noinline_xattr"},
8274de77 128 {Opt_inline_data, "inline_data"},
5efd3c6f 129 {Opt_inline_dentry, "inline_dentry"},
97c1794a 130 {Opt_noinline_dentry, "noinline_dentry"},
6b4afdd7 131 {Opt_flush_merge, "flush_merge"},
69e9e427 132 {Opt_noflush_merge, "noflush_merge"},
0f7b2abd 133 {Opt_nobarrier, "nobarrier"},
d5053a34 134 {Opt_fastboot, "fastboot"},
89672159 135 {Opt_extent_cache, "extent_cache"},
7daaea25 136 {Opt_noextent_cache, "noextent_cache"},
75342797 137 {Opt_noinline_data, "noinline_data"},
343f40f0 138 {Opt_data_flush, "data_flush"},
36abef4e 139 {Opt_mode, "mode=%s"},
ec91538d 140 {Opt_io_size_bits, "io_bits=%u"},
73faec4d 141 {Opt_fault_injection, "fault_injection=%u"},
6d94c74a
JK
142 {Opt_lazytime, "lazytime"},
143 {Opt_nolazytime, "nolazytime"},
aff063e2
JK
144 {Opt_err, NULL},
145};
146
a07ef784
NJ
147void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
148{
149 struct va_format vaf;
150 va_list args;
151
152 va_start(args, fmt);
153 vaf.fmt = fmt;
154 vaf.va = &args;
155 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
156 va_end(args);
157}
158
aff063e2
JK
159static void init_once(void *foo)
160{
161 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
162
aff063e2
JK
163 inode_init_once(&fi->vfs_inode);
164}
165
696c018c
NJ
166static int parse_options(struct super_block *sb, char *options)
167{
168 struct f2fs_sb_info *sbi = F2FS_SB(sb);
09d54cdd 169 struct request_queue *q;
696c018c
NJ
170 substring_t args[MAX_OPT_ARGS];
171 char *p, *name;
172 int arg = 0;
173
174 if (!options)
175 return 0;
176
177 while ((p = strsep(&options, ",")) != NULL) {
178 int token;
179 if (!*p)
180 continue;
181 /*
182 * Initialize args struct so we know whether arg was
183 * found; some options take optional arguments.
184 */
185 args[0].to = args[0].from = NULL;
186 token = match_token(p, f2fs_tokens, args);
187
188 switch (token) {
189 case Opt_gc_background:
190 name = match_strdup(&args[0]);
191
192 if (!name)
193 return -ENOMEM;
6aefd93b 194 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
696c018c 195 set_opt(sbi, BG_GC);
6aefd93b
JK
196 clear_opt(sbi, FORCE_FG_GC);
197 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
696c018c 198 clear_opt(sbi, BG_GC);
6aefd93b
JK
199 clear_opt(sbi, FORCE_FG_GC);
200 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
201 set_opt(sbi, BG_GC);
202 set_opt(sbi, FORCE_FG_GC);
203 } else {
696c018c
NJ
204 kfree(name);
205 return -EINVAL;
206 }
207 kfree(name);
208 break;
209 case Opt_disable_roll_forward:
210 set_opt(sbi, DISABLE_ROLL_FORWARD);
211 break;
2d834bf9
JK
212 case Opt_norecovery:
213 /* this option mounts f2fs with ro */
214 set_opt(sbi, DISABLE_ROLL_FORWARD);
215 if (!f2fs_readonly(sb))
216 return -EINVAL;
217 break;
696c018c 218 case Opt_discard:
09d54cdd
CY
219 q = bdev_get_queue(sb->s_bdev);
220 if (blk_queue_discard(q)) {
221 set_opt(sbi, DISCARD);
0ab02998 222 } else if (!f2fs_sb_mounted_blkzoned(sb)) {
09d54cdd
CY
223 f2fs_msg(sb, KERN_WARNING,
224 "mounting with \"discard\" option, but "
225 "the device does not support discard");
226 }
696c018c 227 break;
64058be9 228 case Opt_nodiscard:
96ba2dec
DLM
229 if (f2fs_sb_mounted_blkzoned(sb)) {
230 f2fs_msg(sb, KERN_WARNING,
231 "discard is required for zoned block devices");
232 return -EINVAL;
233 }
64058be9 234 clear_opt(sbi, DISCARD);
487df616 235 break;
696c018c
NJ
236 case Opt_noheap:
237 set_opt(sbi, NOHEAP);
238 break;
7a20b8a6
JK
239 case Opt_heap:
240 clear_opt(sbi, NOHEAP);
241 break;
696c018c 242#ifdef CONFIG_F2FS_FS_XATTR
4058c511
KA
243 case Opt_user_xattr:
244 set_opt(sbi, XATTR_USER);
245 break;
696c018c
NJ
246 case Opt_nouser_xattr:
247 clear_opt(sbi, XATTR_USER);
248 break;
444c580f
JK
249 case Opt_inline_xattr:
250 set_opt(sbi, INLINE_XATTR);
251 break;
23cf7212
CY
252 case Opt_noinline_xattr:
253 clear_opt(sbi, INLINE_XATTR);
254 break;
696c018c 255#else
4058c511
KA
256 case Opt_user_xattr:
257 f2fs_msg(sb, KERN_INFO,
258 "user_xattr options not supported");
259 break;
696c018c
NJ
260 case Opt_nouser_xattr:
261 f2fs_msg(sb, KERN_INFO,
262 "nouser_xattr options not supported");
263 break;
444c580f
JK
264 case Opt_inline_xattr:
265 f2fs_msg(sb, KERN_INFO,
266 "inline_xattr options not supported");
267 break;
23cf7212
CY
268 case Opt_noinline_xattr:
269 f2fs_msg(sb, KERN_INFO,
270 "noinline_xattr options not supported");
271 break;
696c018c
NJ
272#endif
273#ifdef CONFIG_F2FS_FS_POSIX_ACL
4058c511
KA
274 case Opt_acl:
275 set_opt(sbi, POSIX_ACL);
276 break;
696c018c
NJ
277 case Opt_noacl:
278 clear_opt(sbi, POSIX_ACL);
279 break;
280#else
4058c511
KA
281 case Opt_acl:
282 f2fs_msg(sb, KERN_INFO, "acl options not supported");
283 break;
696c018c
NJ
284 case Opt_noacl:
285 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
286 break;
287#endif
288 case Opt_active_logs:
289 if (args->from && match_int(args, &arg))
290 return -EINVAL;
291 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
292 return -EINVAL;
293 sbi->active_logs = arg;
294 break;
295 case Opt_disable_ext_identify:
296 set_opt(sbi, DISABLE_EXT_IDENTIFY);
297 break;
8274de77
HL
298 case Opt_inline_data:
299 set_opt(sbi, INLINE_DATA);
300 break;
5efd3c6f
CY
301 case Opt_inline_dentry:
302 set_opt(sbi, INLINE_DENTRY);
303 break;
97c1794a
CY
304 case Opt_noinline_dentry:
305 clear_opt(sbi, INLINE_DENTRY);
306 break;
6b4afdd7
JK
307 case Opt_flush_merge:
308 set_opt(sbi, FLUSH_MERGE);
309 break;
69e9e427
JK
310 case Opt_noflush_merge:
311 clear_opt(sbi, FLUSH_MERGE);
312 break;
0f7b2abd
JK
313 case Opt_nobarrier:
314 set_opt(sbi, NOBARRIER);
315 break;
d5053a34
JK
316 case Opt_fastboot:
317 set_opt(sbi, FASTBOOT);
318 break;
89672159
CY
319 case Opt_extent_cache:
320 set_opt(sbi, EXTENT_CACHE);
321 break;
7daaea25
JK
322 case Opt_noextent_cache:
323 clear_opt(sbi, EXTENT_CACHE);
324 break;
75342797
WL
325 case Opt_noinline_data:
326 clear_opt(sbi, INLINE_DATA);
327 break;
343f40f0
CY
328 case Opt_data_flush:
329 set_opt(sbi, DATA_FLUSH);
330 break;
36abef4e
JK
331 case Opt_mode:
332 name = match_strdup(&args[0]);
333
334 if (!name)
335 return -ENOMEM;
336 if (strlen(name) == 8 &&
337 !strncmp(name, "adaptive", 8)) {
3adc57e9
DLM
338 if (f2fs_sb_mounted_blkzoned(sb)) {
339 f2fs_msg(sb, KERN_WARNING,
340 "adaptive mode is not allowed with "
341 "zoned block device feature");
342 kfree(name);
343 return -EINVAL;
344 }
52763a4b 345 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
36abef4e
JK
346 } else if (strlen(name) == 3 &&
347 !strncmp(name, "lfs", 3)) {
52763a4b 348 set_opt_mode(sbi, F2FS_MOUNT_LFS);
36abef4e
JK
349 } else {
350 kfree(name);
351 return -EINVAL;
352 }
353 kfree(name);
354 break;
ec91538d
JK
355 case Opt_io_size_bits:
356 if (args->from && match_int(args, &arg))
357 return -EINVAL;
358 if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
359 f2fs_msg(sb, KERN_WARNING,
360 "Not support %d, larger than %d",
361 1 << arg, BIO_MAX_PAGES);
362 return -EINVAL;
363 }
364 sbi->write_io_size_bits = arg;
365 break;
73faec4d
JK
366 case Opt_fault_injection:
367 if (args->from && match_int(args, &arg))
368 return -EINVAL;
369#ifdef CONFIG_F2FS_FAULT_INJECTION
1ecc0c5c 370 f2fs_build_fault_attr(sbi, arg);
0cc0dec2 371 set_opt(sbi, FAULT_INJECTION);
73faec4d
JK
372#else
373 f2fs_msg(sb, KERN_INFO,
374 "FAULT_INJECTION was not selected");
375#endif
376 break;
6d94c74a
JK
377 case Opt_lazytime:
378 sb->s_flags |= MS_LAZYTIME;
379 break;
380 case Opt_nolazytime:
381 sb->s_flags &= ~MS_LAZYTIME;
382 break;
696c018c
NJ
383 default:
384 f2fs_msg(sb, KERN_ERR,
385 "Unrecognized mount option \"%s\" or missing value",
386 p);
387 return -EINVAL;
388 }
389 }
ec91538d
JK
390
391 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
392 f2fs_msg(sb, KERN_ERR,
393 "Should set mode=lfs with %uKB-sized IO",
394 F2FS_IO_SIZE_KB(sbi));
395 return -EINVAL;
396 }
696c018c
NJ
397 return 0;
398}
399
aff063e2
JK
400static struct inode *f2fs_alloc_inode(struct super_block *sb)
401{
402 struct f2fs_inode_info *fi;
403
a0acdfe0 404 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
aff063e2
JK
405 if (!fi)
406 return NULL;
407
408 init_once((void *) fi);
409
434720fa 410 /* Initialize f2fs-specific inode info */
aff063e2 411 fi->vfs_inode.i_version = 1;
204706c7 412 atomic_set(&fi->dirty_pages, 0);
aff063e2
JK
413 fi->i_current_depth = 1;
414 fi->i_advise = 0;
d928bfbf 415 init_rwsem(&fi->i_sem);
2710fd7e 416 INIT_LIST_HEAD(&fi->dirty_list);
0f18b462 417 INIT_LIST_HEAD(&fi->gdirty_list);
88b88a66
JK
418 INIT_LIST_HEAD(&fi->inmem_pages);
419 mutex_init(&fi->inmem_lock);
82e0a5aa
CY
420 init_rwsem(&fi->dio_rwsem[READ]);
421 init_rwsem(&fi->dio_rwsem[WRITE]);
5a3a2d83 422 init_rwsem(&fi->i_mmap_sem);
aff063e2 423
ab9fa662
JK
424 /* Will be used by directory only */
425 fi->i_dir_level = F2FS_SB(sb)->dir_level;
aff063e2
JK
426 return &fi->vfs_inode;
427}
428
531ad7d5
JK
429static int f2fs_drop_inode(struct inode *inode)
430{
b8d96a30 431 int ret;
531ad7d5
JK
432 /*
433 * This is to avoid a deadlock condition like below.
434 * writeback_single_inode(inode)
435 * - f2fs_write_data_page
436 * - f2fs_gc -> iput -> evict
437 * - inode_wait_for_writeback(inode)
438 */
0f18b462 439 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
06e1bc05 440 if (!inode->i_nlink && !is_bad_inode(inode)) {
3e72f721
JK
441 /* to avoid evict_inode call simultaneously */
442 atomic_inc(&inode->i_count);
06e1bc05
JK
443 spin_unlock(&inode->i_lock);
444
445 /* some remained atomic pages should discarded */
446 if (f2fs_is_atomic_file(inode))
29b96b54 447 drop_inmem_pages(inode);
06e1bc05 448
3e72f721
JK
449 /* should remain fi->extent_tree for writepage */
450 f2fs_destroy_extent_node(inode);
451
06e1bc05 452 sb_start_intwrite(inode->i_sb);
fc9581c8 453 f2fs_i_size_write(inode, 0);
06e1bc05
JK
454
455 if (F2FS_HAS_BLOCKS(inode))
9a449e9c 456 f2fs_truncate(inode);
06e1bc05
JK
457
458 sb_end_intwrite(inode->i_sb);
459
0b81d077 460 fscrypt_put_encryption_info(inode, NULL);
06e1bc05 461 spin_lock(&inode->i_lock);
3e72f721 462 atomic_dec(&inode->i_count);
06e1bc05 463 }
b8d96a30 464 trace_f2fs_drop_inode(inode, 0);
531ad7d5 465 return 0;
06e1bc05 466 }
b8d96a30
HP
467 ret = generic_drop_inode(inode);
468 trace_f2fs_drop_inode(inode, ret);
469 return ret;
531ad7d5
JK
470}
471
7c45729a 472int f2fs_inode_dirtied(struct inode *inode, bool sync)
b3783873 473{
0f18b462 474 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
7c45729a 475 int ret = 0;
0f18b462 476
0f18b462
JK
477 spin_lock(&sbi->inode_lock[DIRTY_META]);
478 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
7c45729a
JK
479 ret = 1;
480 } else {
481 set_inode_flag(inode, FI_DIRTY_INODE);
482 stat_inc_dirty_inode(sbi, DIRTY_META);
0f18b462 483 }
7c45729a
JK
484 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
485 list_add_tail(&F2FS_I(inode)->gdirty_list,
0f18b462 486 &sbi->inode_list[DIRTY_META]);
7c45729a
JK
487 inc_page_count(sbi, F2FS_DIRTY_IMETA);
488 }
338bbfa0 489 spin_unlock(&sbi->inode_lock[DIRTY_META]);
7c45729a 490 return ret;
0f18b462
JK
491}
492
493void f2fs_inode_synced(struct inode *inode)
494{
495 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
496
497 spin_lock(&sbi->inode_lock[DIRTY_META]);
498 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
499 spin_unlock(&sbi->inode_lock[DIRTY_META]);
500 return;
501 }
7c45729a
JK
502 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
503 list_del_init(&F2FS_I(inode)->gdirty_list);
504 dec_page_count(sbi, F2FS_DIRTY_IMETA);
505 }
0f18b462 506 clear_inode_flag(inode, FI_DIRTY_INODE);
26de9b11 507 clear_inode_flag(inode, FI_AUTO_RECOVER);
0f18b462 508 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
338bbfa0 509 spin_unlock(&sbi->inode_lock[DIRTY_META]);
b3783873
JK
510}
511
b56ab837
JK
512/*
513 * f2fs_dirty_inode() is called from __mark_inode_dirty()
514 *
515 * We should call set_dirty_inode to write the dirty inode through write_inode.
516 */
517static void f2fs_dirty_inode(struct inode *inode, int flags)
518{
519 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
520
521 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
522 inode->i_ino == F2FS_META_INO(sbi))
523 return;
524
525 if (flags == I_DIRTY_TIME)
526 return;
527
528 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
529 clear_inode_flag(inode, FI_AUTO_RECOVER);
530
7c45729a 531 f2fs_inode_dirtied(inode, false);
b56ab837
JK
532}
533
aff063e2
JK
534static void f2fs_i_callback(struct rcu_head *head)
535{
536 struct inode *inode = container_of(head, struct inode, i_rcu);
537 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
538}
539
25ca923b 540static void f2fs_destroy_inode(struct inode *inode)
aff063e2
JK
541{
542 call_rcu(&inode->i_rcu, f2fs_i_callback);
543}
544
523be8a6
JK
545static void destroy_percpu_info(struct f2fs_sb_info *sbi)
546{
41382ec4 547 percpu_counter_destroy(&sbi->alloc_valid_block_count);
513c5f37 548 percpu_counter_destroy(&sbi->total_valid_inode_count);
523be8a6
JK
549}
550
3c62be17
JK
551static void destroy_device_list(struct f2fs_sb_info *sbi)
552{
553 int i;
554
555 for (i = 0; i < sbi->s_ndevs; i++) {
556 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
557#ifdef CONFIG_BLK_DEV_ZONED
558 kfree(FDEV(i).blkz_type);
559#endif
560 }
561 kfree(sbi->devs);
562}
563
a398101a
CY
564static void f2fs_put_super(struct super_block *sb)
565{
566 struct f2fs_sb_info *sbi = F2FS_SB(sb);
567 int i;
5e176d54 568
aff063e2
JK
569 stop_gc_thread(sbi);
570
2658e50d
JK
571 /* prevent remaining shrinker jobs */
572 mutex_lock(&sbi->umount_mutex);
573
85dc2f2c
JK
574 /*
575 * We don't need to do checkpoint when superblock is clean.
576 * But, the previous checkpoint was not done by umount, it needs to do
577 * clean checkpoint again.
578 */
caf0047e 579 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
aaec2b1d 580 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
75ab4cb8
JK
581 struct cp_control cpc = {
582 .reason = CP_UMOUNT,
583 };
584 write_checkpoint(sbi, &cpc);
585 }
aff063e2 586
4e6a8d9b 587 /* be sure to wait for any on-going discard commands */
d431413f 588 f2fs_wait_discard_bios(sbi);
4e6a8d9b 589
1f43e2ad
CY
590 if (!sbi->discard_blks) {
591 struct cp_control cpc = {
592 .reason = CP_UMOUNT | CP_TRIMMED,
593 };
594 write_checkpoint(sbi, &cpc);
595 }
596
eca616f8
JK
597 /* write_checkpoint can update stat informaion */
598 f2fs_destroy_stats(sbi);
599
cf779cab
JK
600 /*
601 * normally superblock is clean, so we need to release this.
602 * In addition, EIO will skip do checkpoint, we need this as well.
603 */
74ef9241 604 release_ino_entry(sbi, true);
6f12ac25 605
2658e50d
JK
606 f2fs_leave_shrinker(sbi);
607 mutex_unlock(&sbi->umount_mutex);
608
17c19120 609 /* our cp_error case, we can wait for any writeback page */
b9109b0e 610 f2fs_flush_merged_writes(sbi);
17c19120 611
aff063e2
JK
612 iput(sbi->node_inode);
613 iput(sbi->meta_inode);
614
615 /* destroy f2fs internal modules */
616 destroy_node_manager(sbi);
617 destroy_segment_manager(sbi);
618
619 kfree(sbi->ckpt);
a398101a
CY
620
621 f2fs_exit_sysfs(sbi);
aff063e2
JK
622
623 sb->s_fs_info = NULL;
43b6573b
KM
624 if (sbi->s_chksum_driver)
625 crypto_free_shash(sbi->s_chksum_driver);
b39f0de2 626 kfree(sbi->raw_super);
523be8a6 627
3c62be17 628 destroy_device_list(sbi);
b6895e8f 629 mempool_destroy(sbi->write_io_dummy);
523be8a6 630 destroy_percpu_info(sbi);
a912b54d
JK
631 for (i = 0; i < NR_PAGE_TYPE; i++)
632 kfree(sbi->write_io[i]);
aff063e2
JK
633 kfree(sbi);
634}
635
636int f2fs_sync_fs(struct super_block *sb, int sync)
637{
638 struct f2fs_sb_info *sbi = F2FS_SB(sb);
c34f42e2 639 int err = 0;
aff063e2 640
a2a4a7e4
NJ
641 trace_f2fs_sync_fs(sb, sync);
642
b7473754 643 if (sync) {
d5053a34
JK
644 struct cp_control cpc;
645
119ee914
JK
646 cpc.reason = __get_cp_reason(sbi);
647
b7473754 648 mutex_lock(&sbi->gc_mutex);
c34f42e2 649 err = write_checkpoint(sbi, &cpc);
b7473754 650 mutex_unlock(&sbi->gc_mutex);
b7473754 651 }
05ca3632 652 f2fs_trace_ios(NULL, 1);
aff063e2 653
c34f42e2 654 return err;
aff063e2
JK
655}
656
d6212a5f
CL
657static int f2fs_freeze(struct super_block *sb)
658{
77888c1e 659 if (f2fs_readonly(sb))
d6212a5f
CL
660 return 0;
661
b4b9d34c
JK
662 /* IO error happened before */
663 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
664 return -EIO;
665
666 /* must be clean, since sync_filesystem() was already called */
667 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
668 return -EINVAL;
669 return 0;
d6212a5f
CL
670}
671
672static int f2fs_unfreeze(struct super_block *sb)
673{
674 return 0;
675}
676
aff063e2
JK
677static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
678{
679 struct super_block *sb = dentry->d_sb;
680 struct f2fs_sb_info *sbi = F2FS_SB(sb);
681 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
682 block_t total_count, user_block_count, start_count, ovp_count;
683
684 total_count = le64_to_cpu(sbi->raw_super->block_count);
685 user_block_count = sbi->user_block_count;
686 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
687 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
688 buf->f_type = F2FS_SUPER_MAGIC;
689 buf->f_bsize = sbi->blocksize;
690
691 buf->f_blocks = total_count - start_count;
3e6d0b4d 692 buf->f_bfree = user_block_count - valid_user_blocks(sbi) + ovp_count;
aff063e2
JK
693 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
694
c200b1aa 695 buf->f_files = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
b08b12d2
CY
696 buf->f_ffree = min(buf->f_files - valid_node_count(sbi),
697 buf->f_bavail);
aff063e2 698
5a20d339 699 buf->f_namelen = F2FS_NAME_LEN;
aff063e2
JK
700 buf->f_fsid.val[0] = (u32)id;
701 buf->f_fsid.val[1] = (u32)(id >> 32);
702
703 return 0;
704}
705
706static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
707{
708 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
709
6aefd93b
JK
710 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
711 if (test_opt(sbi, FORCE_FG_GC))
712 seq_printf(seq, ",background_gc=%s", "sync");
713 else
714 seq_printf(seq, ",background_gc=%s", "on");
715 } else {
696c018c 716 seq_printf(seq, ",background_gc=%s", "off");
6aefd93b 717 }
aff063e2
JK
718 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
719 seq_puts(seq, ",disable_roll_forward");
720 if (test_opt(sbi, DISCARD))
721 seq_puts(seq, ",discard");
722 if (test_opt(sbi, NOHEAP))
7a20b8a6
JK
723 seq_puts(seq, ",no_heap");
724 else
725 seq_puts(seq, ",heap");
aff063e2
JK
726#ifdef CONFIG_F2FS_FS_XATTR
727 if (test_opt(sbi, XATTR_USER))
728 seq_puts(seq, ",user_xattr");
729 else
730 seq_puts(seq, ",nouser_xattr");
444c580f
JK
731 if (test_opt(sbi, INLINE_XATTR))
732 seq_puts(seq, ",inline_xattr");
23cf7212
CY
733 else
734 seq_puts(seq, ",noinline_xattr");
aff063e2
JK
735#endif
736#ifdef CONFIG_F2FS_FS_POSIX_ACL
737 if (test_opt(sbi, POSIX_ACL))
738 seq_puts(seq, ",acl");
739 else
740 seq_puts(seq, ",noacl");
741#endif
742 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
aa43507f 743 seq_puts(seq, ",disable_ext_identify");
8274de77
HL
744 if (test_opt(sbi, INLINE_DATA))
745 seq_puts(seq, ",inline_data");
75342797
WL
746 else
747 seq_puts(seq, ",noinline_data");
5efd3c6f
CY
748 if (test_opt(sbi, INLINE_DENTRY))
749 seq_puts(seq, ",inline_dentry");
97c1794a
CY
750 else
751 seq_puts(seq, ",noinline_dentry");
b270ad6f 752 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
6b4afdd7 753 seq_puts(seq, ",flush_merge");
0f7b2abd
JK
754 if (test_opt(sbi, NOBARRIER))
755 seq_puts(seq, ",nobarrier");
d5053a34
JK
756 if (test_opt(sbi, FASTBOOT))
757 seq_puts(seq, ",fastboot");
89672159
CY
758 if (test_opt(sbi, EXTENT_CACHE))
759 seq_puts(seq, ",extent_cache");
7daaea25
JK
760 else
761 seq_puts(seq, ",noextent_cache");
343f40f0
CY
762 if (test_opt(sbi, DATA_FLUSH))
763 seq_puts(seq, ",data_flush");
36abef4e
JK
764
765 seq_puts(seq, ",mode=");
766 if (test_opt(sbi, ADAPTIVE))
767 seq_puts(seq, "adaptive");
768 else if (test_opt(sbi, LFS))
769 seq_puts(seq, "lfs");
aff063e2 770 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
ec91538d
JK
771 if (F2FS_IO_SIZE_BITS(sbi))
772 seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
0cc0dec2
KX
773#ifdef CONFIG_F2FS_FAULT_INJECTION
774 if (test_opt(sbi, FAULT_INJECTION))
44529f89
CY
775 seq_printf(seq, ",fault_injection=%u",
776 sbi->fault_info.inject_rate);
0cc0dec2 777#endif
aff063e2
JK
778
779 return 0;
780}
781
498c5e9f
YH
782static void default_options(struct f2fs_sb_info *sbi)
783{
784 /* init some FS parameters */
785 sbi->active_logs = NR_CURSEG_TYPE;
786
787 set_opt(sbi, BG_GC);
39133a50 788 set_opt(sbi, INLINE_XATTR);
498c5e9f 789 set_opt(sbi, INLINE_DATA);
97c1794a 790 set_opt(sbi, INLINE_DENTRY);
3e72f721 791 set_opt(sbi, EXTENT_CACHE);
7a20b8a6 792 set_opt(sbi, NOHEAP);
6d94c74a 793 sbi->sb->s_flags |= MS_LAZYTIME;
69e9e427 794 set_opt(sbi, FLUSH_MERGE);
0bfd7a09 795 if (f2fs_sb_mounted_blkzoned(sbi->sb)) {
52763a4b
JK
796 set_opt_mode(sbi, F2FS_MOUNT_LFS);
797 set_opt(sbi, DISCARD);
798 } else {
799 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
800 }
498c5e9f
YH
801
802#ifdef CONFIG_F2FS_FS_XATTR
803 set_opt(sbi, XATTR_USER);
804#endif
805#ifdef CONFIG_F2FS_FS_POSIX_ACL
806 set_opt(sbi, POSIX_ACL);
807#endif
36dbd328
CY
808
809#ifdef CONFIG_F2FS_FAULT_INJECTION
810 f2fs_build_fault_attr(sbi, 0);
811#endif
498c5e9f
YH
812}
813
696c018c
NJ
814static int f2fs_remount(struct super_block *sb, int *flags, char *data)
815{
816 struct f2fs_sb_info *sbi = F2FS_SB(sb);
817 struct f2fs_mount_info org_mount_opt;
818 int err, active_logs;
876dc59e
GZ
819 bool need_restart_gc = false;
820 bool need_stop_gc = false;
9cd81ce3 821 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
2443b8b3
CY
822#ifdef CONFIG_F2FS_FAULT_INJECTION
823 struct f2fs_fault_info ffi = sbi->fault_info;
824#endif
696c018c
NJ
825
826 /*
827 * Save the old mount options in case we
828 * need to restore them.
829 */
830 org_mount_opt = sbi->mount_opt;
831 active_logs = sbi->active_logs;
832
df728b0f
JK
833 /* recover superblocks we couldn't write due to previous RO mount */
834 if (!(*flags & MS_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
835 err = f2fs_commit_super(sbi, false);
836 f2fs_msg(sb, KERN_INFO,
837 "Try to recover all the superblocks, ret: %d", err);
838 if (!err)
839 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
840 }
841
26666c8a 842 sbi->mount_opt.opt = 0;
498c5e9f 843 default_options(sbi);
26666c8a 844
696c018c
NJ
845 /* parse mount options */
846 err = parse_options(sb, data);
847 if (err)
848 goto restore_opts;
849
850 /*
851 * Previous and new state of filesystem is RO,
876dc59e 852 * so skip checking GC and FLUSH_MERGE conditions.
696c018c 853 */
6b2920a5 854 if (f2fs_readonly(sb) && (*flags & MS_RDONLY))
696c018c
NJ
855 goto skip;
856
9cd81ce3
CY
857 /* disallow enable/disable extent_cache dynamically */
858 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
859 err = -EINVAL;
860 f2fs_msg(sbi->sb, KERN_WARNING,
861 "switch extent_cache option is not allowed");
862 goto restore_opts;
863 }
864
696c018c
NJ
865 /*
866 * We stop the GC thread if FS is mounted as RO
867 * or if background_gc = off is passed in mount
868 * option. Also sync the filesystem.
869 */
870 if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
871 if (sbi->gc_thread) {
872 stop_gc_thread(sbi);
876dc59e 873 need_restart_gc = true;
696c018c 874 }
aba291b3 875 } else if (!sbi->gc_thread) {
696c018c
NJ
876 err = start_gc_thread(sbi);
877 if (err)
878 goto restore_opts;
876dc59e
GZ
879 need_stop_gc = true;
880 }
881
faa0e55b
JK
882 if (*flags & MS_RDONLY) {
883 writeback_inodes_sb(sb, WB_REASON_SYNC);
884 sync_inodes_sb(sb);
885
886 set_sbi_flag(sbi, SBI_IS_DIRTY);
887 set_sbi_flag(sbi, SBI_IS_CLOSE);
888 f2fs_sync_fs(sb, 1);
889 clear_sbi_flag(sbi, SBI_IS_CLOSE);
890 }
891
876dc59e
GZ
892 /*
893 * We stop issue flush thread if FS is mounted as RO
894 * or if flush_merge is not passed in mount option.
895 */
896 if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
5eba8c5d
JK
897 clear_opt(sbi, FLUSH_MERGE);
898 destroy_flush_cmd_control(sbi, false);
899 } else {
2163d198
GZ
900 err = create_flush_cmd_control(sbi);
901 if (err)
a688b9d9 902 goto restore_gc;
696c018c
NJ
903 }
904skip:
905 /* Update the POSIXACL Flag */
df728b0f 906 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
696c018c 907 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
df728b0f 908
696c018c 909 return 0;
876dc59e
GZ
910restore_gc:
911 if (need_restart_gc) {
912 if (start_gc_thread(sbi))
913 f2fs_msg(sbi->sb, KERN_WARNING,
e1c42045 914 "background gc thread has stopped");
876dc59e
GZ
915 } else if (need_stop_gc) {
916 stop_gc_thread(sbi);
917 }
696c018c
NJ
918restore_opts:
919 sbi->mount_opt = org_mount_opt;
920 sbi->active_logs = active_logs;
2443b8b3
CY
921#ifdef CONFIG_F2FS_FAULT_INJECTION
922 sbi->fault_info = ffi;
923#endif
696c018c
NJ
924 return err;
925}
926
aff063e2
JK
927static struct super_operations f2fs_sops = {
928 .alloc_inode = f2fs_alloc_inode,
531ad7d5 929 .drop_inode = f2fs_drop_inode,
aff063e2
JK
930 .destroy_inode = f2fs_destroy_inode,
931 .write_inode = f2fs_write_inode,
b3783873 932 .dirty_inode = f2fs_dirty_inode,
aff063e2
JK
933 .show_options = f2fs_show_options,
934 .evict_inode = f2fs_evict_inode,
935 .put_super = f2fs_put_super,
936 .sync_fs = f2fs_sync_fs,
d6212a5f
CL
937 .freeze_fs = f2fs_freeze,
938 .unfreeze_fs = f2fs_unfreeze,
aff063e2 939 .statfs = f2fs_statfs,
696c018c 940 .remount_fs = f2fs_remount,
aff063e2
JK
941};
942
0b81d077
JK
943#ifdef CONFIG_F2FS_FS_ENCRYPTION
944static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
945{
946 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
947 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
948 ctx, len, NULL);
949}
950
951static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
952 void *fs_data)
953{
954 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
955 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
956 ctx, len, fs_data, XATTR_CREATE);
957}
958
959static unsigned f2fs_max_namelen(struct inode *inode)
960{
961 return S_ISLNK(inode->i_mode) ?
962 inode->i_sb->s_blocksize : F2FS_NAME_LEN;
963}
964
6f69f0ed 965static const struct fscrypt_operations f2fs_cryptops = {
a5d431ef 966 .key_prefix = "f2fs:",
0b81d077
JK
967 .get_context = f2fs_get_context,
968 .set_context = f2fs_set_context,
969 .is_encrypted = f2fs_encrypted_inode,
970 .empty_dir = f2fs_empty_dir,
971 .max_namelen = f2fs_max_namelen,
972};
973#else
6f69f0ed 974static const struct fscrypt_operations f2fs_cryptops = {
0b81d077
JK
975 .is_encrypted = f2fs_encrypted_inode,
976};
977#endif
978
aff063e2
JK
979static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
980 u64 ino, u32 generation)
981{
982 struct f2fs_sb_info *sbi = F2FS_SB(sb);
983 struct inode *inode;
984
d6b7d4b3 985 if (check_nid_range(sbi, ino))
910bb12d 986 return ERR_PTR(-ESTALE);
aff063e2
JK
987
988 /*
989 * f2fs_iget isn't quite right if the inode is currently unallocated!
990 * However f2fs_iget currently does appropriate checks to handle stale
991 * inodes so everything is OK.
992 */
993 inode = f2fs_iget(sb, ino);
994 if (IS_ERR(inode))
995 return ERR_CAST(inode);
6bacf52f 996 if (unlikely(generation && inode->i_generation != generation)) {
aff063e2
JK
997 /* we didn't find the right inode.. */
998 iput(inode);
999 return ERR_PTR(-ESTALE);
1000 }
1001 return inode;
1002}
1003
1004static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1005 int fh_len, int fh_type)
1006{
1007 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1008 f2fs_nfs_get_inode);
1009}
1010
1011static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
1012 int fh_len, int fh_type)
1013{
1014 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1015 f2fs_nfs_get_inode);
1016}
1017
1018static const struct export_operations f2fs_export_ops = {
1019 .fh_to_dentry = f2fs_fh_to_dentry,
1020 .fh_to_parent = f2fs_fh_to_parent,
1021 .get_parent = f2fs_get_parent,
1022};
1023
e0afc4d6 1024static loff_t max_file_blocks(void)
aff063e2 1025{
de93653f 1026 loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
aff063e2
JK
1027 loff_t leaf_count = ADDRS_PER_BLOCK;
1028
1029 /* two direct node blocks */
1030 result += (leaf_count * 2);
1031
1032 /* two indirect node blocks */
1033 leaf_count *= NIDS_PER_BLOCK;
1034 result += (leaf_count * 2);
1035
1036 /* one double indirect node block */
1037 leaf_count *= NIDS_PER_BLOCK;
1038 result += leaf_count;
1039
aff063e2
JK
1040 return result;
1041}
1042
fd694733
JK
1043static int __f2fs_commit_super(struct buffer_head *bh,
1044 struct f2fs_super_block *super)
1045{
1046 lock_buffer(bh);
1047 if (super)
1048 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
1049 set_buffer_uptodate(bh);
1050 set_buffer_dirty(bh);
1051 unlock_buffer(bh);
1052
1053 /* it's rare case, we can do fua all the time */
3adc5fcb 1054 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
fd694733
JK
1055}
1056
df728b0f 1057static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
fd694733 1058 struct buffer_head *bh)
9a59b62f 1059{
fd694733
JK
1060 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1061 (bh->b_data + F2FS_SUPER_OFFSET);
df728b0f 1062 struct super_block *sb = sbi->sb;
9a59b62f
CY
1063 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1064 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
1065 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
1066 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
1067 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1068 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1069 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
1070 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
1071 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
1072 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
1073 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
1074 u32 segment_count = le32_to_cpu(raw_super->segment_count);
1075 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
fd694733
JK
1076 u64 main_end_blkaddr = main_blkaddr +
1077 (segment_count_main << log_blocks_per_seg);
1078 u64 seg_end_blkaddr = segment0_blkaddr +
1079 (segment_count << log_blocks_per_seg);
9a59b62f
CY
1080
1081 if (segment0_blkaddr != cp_blkaddr) {
1082 f2fs_msg(sb, KERN_INFO,
1083 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
1084 segment0_blkaddr, cp_blkaddr);
1085 return true;
1086 }
1087
1088 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
1089 sit_blkaddr) {
1090 f2fs_msg(sb, KERN_INFO,
1091 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
1092 cp_blkaddr, sit_blkaddr,
1093 segment_count_ckpt << log_blocks_per_seg);
1094 return true;
1095 }
1096
1097 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
1098 nat_blkaddr) {
1099 f2fs_msg(sb, KERN_INFO,
1100 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
1101 sit_blkaddr, nat_blkaddr,
1102 segment_count_sit << log_blocks_per_seg);
1103 return true;
1104 }
1105
1106 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
1107 ssa_blkaddr) {
1108 f2fs_msg(sb, KERN_INFO,
1109 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
1110 nat_blkaddr, ssa_blkaddr,
1111 segment_count_nat << log_blocks_per_seg);
1112 return true;
1113 }
1114
1115 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
1116 main_blkaddr) {
1117 f2fs_msg(sb, KERN_INFO,
1118 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
1119 ssa_blkaddr, main_blkaddr,
1120 segment_count_ssa << log_blocks_per_seg);
1121 return true;
1122 }
1123
fd694733 1124 if (main_end_blkaddr > seg_end_blkaddr) {
9a59b62f 1125 f2fs_msg(sb, KERN_INFO,
fd694733 1126 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
9a59b62f 1127 main_blkaddr,
fd694733
JK
1128 segment0_blkaddr +
1129 (segment_count << log_blocks_per_seg),
9a59b62f
CY
1130 segment_count_main << log_blocks_per_seg);
1131 return true;
fd694733
JK
1132 } else if (main_end_blkaddr < seg_end_blkaddr) {
1133 int err = 0;
1134 char *res;
1135
1136 /* fix in-memory information all the time */
1137 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
1138 segment0_blkaddr) >> log_blocks_per_seg);
1139
1140 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
df728b0f 1141 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
fd694733
JK
1142 res = "internally";
1143 } else {
1144 err = __f2fs_commit_super(bh, NULL);
1145 res = err ? "failed" : "done";
1146 }
1147 f2fs_msg(sb, KERN_INFO,
1148 "Fix alignment : %s, start(%u) end(%u) block(%u)",
1149 res, main_blkaddr,
1150 segment0_blkaddr +
1151 (segment_count << log_blocks_per_seg),
1152 segment_count_main << log_blocks_per_seg);
1153 if (err)
1154 return true;
9a59b62f 1155 }
9a59b62f
CY
1156 return false;
1157}
1158
df728b0f 1159static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
fd694733 1160 struct buffer_head *bh)
aff063e2 1161{
fd694733
JK
1162 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1163 (bh->b_data + F2FS_SUPER_OFFSET);
df728b0f 1164 struct super_block *sb = sbi->sb;
aff063e2
JK
1165 unsigned int blocksize;
1166
a07ef784
NJ
1167 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
1168 f2fs_msg(sb, KERN_INFO,
1169 "Magic Mismatch, valid(0x%x) - read(0x%x)",
1170 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
aff063e2 1171 return 1;
a07ef784 1172 }
aff063e2 1173
5c9b4692 1174 /* Currently, support only 4KB page cache size */
09cbfeaf 1175 if (F2FS_BLKSIZE != PAGE_SIZE) {
5c9b4692 1176 f2fs_msg(sb, KERN_INFO,
14d7e9de 1177 "Invalid page_cache_size (%lu), supports only 4KB\n",
09cbfeaf 1178 PAGE_SIZE);
5c9b4692 1179 return 1;
1180 }
1181
aff063e2
JK
1182 /* Currently, support only 4KB block size */
1183 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
5c9b4692 1184 if (blocksize != F2FS_BLKSIZE) {
a07ef784
NJ
1185 f2fs_msg(sb, KERN_INFO,
1186 "Invalid blocksize (%u), supports only 4KB\n",
1187 blocksize);
aff063e2 1188 return 1;
a07ef784 1189 }
5c9b4692 1190
9a59b62f
CY
1191 /* check log blocks per segment */
1192 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
1193 f2fs_msg(sb, KERN_INFO,
1194 "Invalid log blocks per segment (%u)\n",
1195 le32_to_cpu(raw_super->log_blocks_per_seg));
1196 return 1;
1197 }
1198
55cf9cb6
CY
1199 /* Currently, support 512/1024/2048/4096 bytes sector size */
1200 if (le32_to_cpu(raw_super->log_sectorsize) >
1201 F2FS_MAX_LOG_SECTOR_SIZE ||
1202 le32_to_cpu(raw_super->log_sectorsize) <
1203 F2FS_MIN_LOG_SECTOR_SIZE) {
1204 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
1205 le32_to_cpu(raw_super->log_sectorsize));
aff063e2 1206 return 1;
a07ef784 1207 }
55cf9cb6
CY
1208 if (le32_to_cpu(raw_super->log_sectors_per_block) +
1209 le32_to_cpu(raw_super->log_sectorsize) !=
1210 F2FS_MAX_LOG_SECTOR_SIZE) {
1211 f2fs_msg(sb, KERN_INFO,
1212 "Invalid log sectors per block(%u) log sectorsize(%u)",
1213 le32_to_cpu(raw_super->log_sectors_per_block),
1214 le32_to_cpu(raw_super->log_sectorsize));
aff063e2 1215 return 1;
a07ef784 1216 }
9a59b62f
CY
1217
1218 /* check reserved ino info */
1219 if (le32_to_cpu(raw_super->node_ino) != 1 ||
1220 le32_to_cpu(raw_super->meta_ino) != 2 ||
1221 le32_to_cpu(raw_super->root_ino) != 3) {
1222 f2fs_msg(sb, KERN_INFO,
1223 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
1224 le32_to_cpu(raw_super->node_ino),
1225 le32_to_cpu(raw_super->meta_ino),
1226 le32_to_cpu(raw_super->root_ino));
1227 return 1;
1228 }
1229
b9dd4618
JQ
1230 if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) {
1231 f2fs_msg(sb, KERN_INFO,
1232 "Invalid segment count (%u)",
1233 le32_to_cpu(raw_super->segment_count));
1234 return 1;
1235 }
1236
9a59b62f 1237 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
df728b0f 1238 if (sanity_check_area_boundary(sbi, bh))
9a59b62f
CY
1239 return 1;
1240
aff063e2
JK
1241 return 0;
1242}
1243
984ec63c 1244int sanity_check_ckpt(struct f2fs_sb_info *sbi)
aff063e2
JK
1245{
1246 unsigned int total, fsmeta;
577e3495
JK
1247 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1248 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2040fce8 1249 unsigned int ovp_segments, reserved_segments;
15d3042a
JQ
1250 unsigned int main_segs, blocks_per_seg;
1251 int i;
aff063e2
JK
1252
1253 total = le32_to_cpu(raw_super->segment_count);
1254 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
1255 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
1256 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
1257 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
1258 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
1259
6bacf52f 1260 if (unlikely(fsmeta >= total))
aff063e2 1261 return 1;
577e3495 1262
2040fce8
JK
1263 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1264 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1265
1266 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
1267 ovp_segments == 0 || reserved_segments == 0)) {
1268 f2fs_msg(sbi->sb, KERN_ERR,
1269 "Wrong layout: check mkfs.f2fs version");
1270 return 1;
1271 }
1272
15d3042a
JQ
1273 main_segs = le32_to_cpu(raw_super->segment_count_main);
1274 blocks_per_seg = sbi->blocks_per_seg;
1275
1276 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1277 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
1278 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
1279 return 1;
1280 }
1281 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1282 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
1283 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
1284 return 1;
1285 }
1286
1e968fdf 1287 if (unlikely(f2fs_cp_error(sbi))) {
577e3495
JK
1288 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
1289 return 1;
1290 }
aff063e2
JK
1291 return 0;
1292}
1293
1294static void init_sb_info(struct f2fs_sb_info *sbi)
1295{
1296 struct f2fs_super_block *raw_super = sbi->raw_super;
e41e6d75 1297 int i, j;
aff063e2
JK
1298
1299 sbi->log_sectors_per_block =
1300 le32_to_cpu(raw_super->log_sectors_per_block);
1301 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
1302 sbi->blocksize = 1 << sbi->log_blocksize;
1303 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
1304 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
1305 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
1306 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
1307 sbi->total_sections = le32_to_cpu(raw_super->section_count);
1308 sbi->total_node_count =
1309 (le32_to_cpu(raw_super->segment_count_nat) / 2)
1310 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
1311 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
1312 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
1313 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
5ec4e49f 1314 sbi->cur_victim_sec = NULL_SECNO;
b1c57c1c 1315 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
aff063e2 1316
ab9fa662 1317 sbi->dir_level = DEF_DIR_LEVEL;
6beceb54 1318 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
d0239e1b 1319 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
caf0047e 1320 clear_sbi_flag(sbi, SBI_NEED_FSCK);
2658e50d 1321
35782b23
JK
1322 for (i = 0; i < NR_COUNT_TYPE; i++)
1323 atomic_set(&sbi->nr_pages[i], 0);
1324
687de7f1
JK
1325 atomic_set(&sbi->wb_sync_req, 0);
1326
2658e50d
JK
1327 INIT_LIST_HEAD(&sbi->s_list);
1328 mutex_init(&sbi->umount_mutex);
e41e6d75
CY
1329 for (i = 0; i < NR_PAGE_TYPE - 1; i++)
1330 for (j = HOT; j < NR_TEMP_TYPE; j++)
1331 mutex_init(&sbi->wio_mutex[i][j]);
aaec2b1d 1332 spin_lock_init(&sbi->cp_lock);
aff063e2
JK
1333}
1334
523be8a6
JK
1335static int init_percpu_info(struct f2fs_sb_info *sbi)
1336{
35782b23 1337 int err;
41382ec4 1338
513c5f37
JK
1339 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
1340 if (err)
1341 return err;
1342
1343 return percpu_counter_init(&sbi->total_valid_inode_count, 0,
41382ec4 1344 GFP_KERNEL);
523be8a6
JK
1345}
1346
178053e2 1347#ifdef CONFIG_BLK_DEV_ZONED
3c62be17 1348static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
178053e2 1349{
3c62be17 1350 struct block_device *bdev = FDEV(devi).bdev;
178053e2
DLM
1351 sector_t nr_sectors = bdev->bd_part->nr_sects;
1352 sector_t sector = 0;
1353 struct blk_zone *zones;
1354 unsigned int i, nr_zones;
1355 unsigned int n = 0;
1356 int err = -EIO;
1357
1358 if (!f2fs_sb_mounted_blkzoned(sbi->sb))
1359 return 0;
1360
3c62be17 1361 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
f99e8648 1362 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
3c62be17 1363 return -EINVAL;
f99e8648 1364 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
3c62be17
JK
1365 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
1366 __ilog2_u32(sbi->blocks_per_blkz))
1367 return -EINVAL;
178053e2 1368 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
3c62be17
JK
1369 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
1370 sbi->log_blocks_per_blkz;
f99e8648 1371 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
3c62be17 1372 FDEV(devi).nr_blkz++;
178053e2 1373
3c62be17
JK
1374 FDEV(devi).blkz_type = kmalloc(FDEV(devi).nr_blkz, GFP_KERNEL);
1375 if (!FDEV(devi).blkz_type)
178053e2
DLM
1376 return -ENOMEM;
1377
1378#define F2FS_REPORT_NR_ZONES 4096
1379
1380 zones = kcalloc(F2FS_REPORT_NR_ZONES, sizeof(struct blk_zone),
1381 GFP_KERNEL);
1382 if (!zones)
1383 return -ENOMEM;
1384
1385 /* Get block zones type */
1386 while (zones && sector < nr_sectors) {
1387
1388 nr_zones = F2FS_REPORT_NR_ZONES;
1389 err = blkdev_report_zones(bdev, sector,
1390 zones, &nr_zones,
1391 GFP_KERNEL);
1392 if (err)
1393 break;
1394 if (!nr_zones) {
1395 err = -EIO;
1396 break;
1397 }
1398
1399 for (i = 0; i < nr_zones; i++) {
3c62be17 1400 FDEV(devi).blkz_type[n] = zones[i].type;
178053e2
DLM
1401 sector += zones[i].len;
1402 n++;
1403 }
1404 }
1405
1406 kfree(zones);
1407
1408 return err;
1409}
1410#endif
1411
9076a75f
GZ
1412/*
1413 * Read f2fs raw super block.
2b39e907
SL
1414 * Because we have two copies of super block, so read both of them
1415 * to get the first valid one. If any one of them is broken, we pass
1416 * them recovery flag back to the caller.
9076a75f 1417 */
df728b0f 1418static int read_raw_super_block(struct f2fs_sb_info *sbi,
9076a75f 1419 struct f2fs_super_block **raw_super,
e8240f65 1420 int *valid_super_block, int *recovery)
14d7e9de 1421{
df728b0f 1422 struct super_block *sb = sbi->sb;
2b39e907 1423 int block;
e8240f65 1424 struct buffer_head *bh;
fd694733 1425 struct f2fs_super_block *super;
da554e48 1426 int err = 0;
14d7e9de 1427
b39f0de2
YH
1428 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
1429 if (!super)
1430 return -ENOMEM;
2b39e907
SL
1431
1432 for (block = 0; block < 2; block++) {
1433 bh = sb_bread(sb, block);
1434 if (!bh) {
1435 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
9076a75f 1436 block + 1);
2b39e907
SL
1437 err = -EIO;
1438 continue;
1439 }
14d7e9de 1440
2b39e907 1441 /* sanity checking of raw super */
df728b0f 1442 if (sanity_check_raw_super(sbi, bh)) {
2b39e907
SL
1443 f2fs_msg(sb, KERN_ERR,
1444 "Can't find valid F2FS filesystem in %dth superblock",
1445 block + 1);
1446 err = -EINVAL;
1447 brelse(bh);
1448 continue;
1449 }
14d7e9de 1450
2b39e907 1451 if (!*raw_super) {
fd694733
JK
1452 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
1453 sizeof(*super));
2b39e907
SL
1454 *valid_super_block = block;
1455 *raw_super = super;
1456 }
1457 brelse(bh);
da554e48 1458 }
1459
2b39e907
SL
1460 /* Fail to read any one of the superblocks*/
1461 if (err < 0)
1462 *recovery = 1;
da554e48 1463
da554e48 1464 /* No valid superblock */
2b39e907 1465 if (!*raw_super)
b39f0de2 1466 kfree(super);
2b39e907
SL
1467 else
1468 err = 0;
da554e48 1469
2b39e907 1470 return err;
14d7e9de 1471}
1472
fd694733 1473int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
26d815ad 1474{
5d909cdb 1475 struct buffer_head *bh;
26d815ad
JK
1476 int err;
1477
df728b0f
JK
1478 if ((recover && f2fs_readonly(sbi->sb)) ||
1479 bdev_read_only(sbi->sb->s_bdev)) {
1480 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
f2353d7b 1481 return -EROFS;
df728b0f 1482 }
f2353d7b 1483
fd694733
JK
1484 /* write back-up superblock first */
1485 bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
5d909cdb
JK
1486 if (!bh)
1487 return -EIO;
fd694733 1488 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
5d909cdb 1489 brelse(bh);
c5bda1c8
CY
1490
1491 /* if we are in recovery path, skip writing valid superblock */
1492 if (recover || err)
5d909cdb 1493 return err;
26d815ad
JK
1494
1495 /* write current valid superblock */
fd694733
JK
1496 bh = sb_getblk(sbi->sb, sbi->valid_super_block);
1497 if (!bh)
1498 return -EIO;
1499 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
1500 brelse(bh);
1501 return err;
26d815ad
JK
1502}
1503
3c62be17
JK
1504static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
1505{
1506 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
7bb3a371 1507 unsigned int max_devices = MAX_DEVICES;
3c62be17
JK
1508 int i;
1509
7bb3a371
MS
1510 /* Initialize single device information */
1511 if (!RDEV(0).path[0]) {
1512 if (!bdev_is_zoned(sbi->sb->s_bdev))
3c62be17 1513 return 0;
7bb3a371
MS
1514 max_devices = 1;
1515 }
3c62be17 1516
7bb3a371
MS
1517 /*
1518 * Initialize multiple devices information, or single
1519 * zoned block device information.
1520 */
1521 sbi->devs = kcalloc(max_devices, sizeof(struct f2fs_dev_info),
1522 GFP_KERNEL);
1523 if (!sbi->devs)
1524 return -ENOMEM;
3c62be17 1525
7bb3a371 1526 for (i = 0; i < max_devices; i++) {
3c62be17 1527
7bb3a371
MS
1528 if (i > 0 && !RDEV(i).path[0])
1529 break;
1530
1531 if (max_devices == 1) {
1532 /* Single zoned block device mount */
1533 FDEV(0).bdev =
1534 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
3c62be17 1535 sbi->sb->s_mode, sbi->sb->s_type);
7bb3a371
MS
1536 } else {
1537 /* Multi-device mount */
1538 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
1539 FDEV(i).total_segments =
1540 le32_to_cpu(RDEV(i).total_segments);
1541 if (i == 0) {
1542 FDEV(i).start_blk = 0;
1543 FDEV(i).end_blk = FDEV(i).start_blk +
1544 (FDEV(i).total_segments <<
1545 sbi->log_blocks_per_seg) - 1 +
1546 le32_to_cpu(raw_super->segment0_blkaddr);
1547 } else {
1548 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
1549 FDEV(i).end_blk = FDEV(i).start_blk +
1550 (FDEV(i).total_segments <<
1551 sbi->log_blocks_per_seg) - 1;
1552 }
1553 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
3c62be17 1554 sbi->sb->s_mode, sbi->sb->s_type);
7bb3a371 1555 }
3c62be17
JK
1556 if (IS_ERR(FDEV(i).bdev))
1557 return PTR_ERR(FDEV(i).bdev);
1558
1559 /* to release errored devices */
1560 sbi->s_ndevs = i + 1;
1561
1562#ifdef CONFIG_BLK_DEV_ZONED
1563 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
1564 !f2fs_sb_mounted_blkzoned(sbi->sb)) {
1565 f2fs_msg(sbi->sb, KERN_ERR,
1566 "Zoned block device feature not enabled\n");
1567 return -EINVAL;
1568 }
1569 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
1570 if (init_blkz_info(sbi, i)) {
1571 f2fs_msg(sbi->sb, KERN_ERR,
1572 "Failed to initialize F2FS blkzone information");
1573 return -EINVAL;
1574 }
7bb3a371
MS
1575 if (max_devices == 1)
1576 break;
3c62be17
JK
1577 f2fs_msg(sbi->sb, KERN_INFO,
1578 "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
1579 i, FDEV(i).path,
1580 FDEV(i).total_segments,
1581 FDEV(i).start_blk, FDEV(i).end_blk,
1582 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
1583 "Host-aware" : "Host-managed");
1584 continue;
1585 }
1586#endif
1587 f2fs_msg(sbi->sb, KERN_INFO,
1588 "Mount Device [%2d]: %20s, %8u, %8x - %8x",
1589 i, FDEV(i).path,
1590 FDEV(i).total_segments,
1591 FDEV(i).start_blk, FDEV(i).end_blk);
1592 }
0a595eba
JK
1593 f2fs_msg(sbi->sb, KERN_INFO,
1594 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
3c62be17
JK
1595 return 0;
1596}
1597
aff063e2
JK
1598static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
1599{
1600 struct f2fs_sb_info *sbi;
da554e48 1601 struct f2fs_super_block *raw_super;
aff063e2 1602 struct inode *root;
99e3e858 1603 int err;
2adc3505 1604 bool retry = true, need_fsck = false;
dabc4a5c 1605 char *options = NULL;
e8240f65 1606 int recovery, i, valid_super_block;
8f1dbbbb 1607 struct curseg_info *seg_i;
aff063e2 1608
ed2e621a 1609try_onemore:
da554e48 1610 err = -EINVAL;
1611 raw_super = NULL;
e8240f65 1612 valid_super_block = -1;
da554e48 1613 recovery = 0;
1614
aff063e2
JK
1615 /* allocate memory for f2fs-specific super block info */
1616 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
1617 if (!sbi)
1618 return -ENOMEM;
1619
df728b0f
JK
1620 sbi->sb = sb;
1621
43b6573b
KM
1622 /* Load the checksum driver */
1623 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
1624 if (IS_ERR(sbi->s_chksum_driver)) {
1625 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
1626 err = PTR_ERR(sbi->s_chksum_driver);
1627 sbi->s_chksum_driver = NULL;
1628 goto free_sbi;
1629 }
1630
ff9234ad 1631 /* set a block size */
6bacf52f 1632 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
a07ef784 1633 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
aff063e2 1634 goto free_sbi;
a07ef784 1635 }
aff063e2 1636
df728b0f 1637 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
e8240f65 1638 &recovery);
9076a75f
GZ
1639 if (err)
1640 goto free_sbi;
1641
5fb08372 1642 sb->s_fs_info = sbi;
52763a4b
JK
1643 sbi->raw_super = raw_super;
1644
d1b959c8
DLM
1645 /*
1646 * The BLKZONED feature indicates that the drive was formatted with
1647 * zone alignment optimization. This is optional for host-aware
1648 * devices, but mandatory for host-managed zoned block devices.
1649 */
1650#ifndef CONFIG_BLK_DEV_ZONED
1651 if (f2fs_sb_mounted_blkzoned(sb)) {
1652 f2fs_msg(sb, KERN_ERR,
1653 "Zoned block device support is not enabled\n");
1727f317 1654 err = -EOPNOTSUPP;
d1b959c8
DLM
1655 goto free_sb_buf;
1656 }
d1b959c8 1657#endif
498c5e9f 1658 default_options(sbi);
aff063e2 1659 /* parse mount options */
dabc4a5c
JK
1660 options = kstrdup((const char *)data, GFP_KERNEL);
1661 if (data && !options) {
1662 err = -ENOMEM;
aff063e2 1663 goto free_sb_buf;
dabc4a5c
JK
1664 }
1665
1666 err = parse_options(sb, options);
1667 if (err)
1668 goto free_options;
aff063e2 1669
e0afc4d6
CY
1670 sbi->max_file_blocks = max_file_blocks();
1671 sb->s_maxbytes = sbi->max_file_blocks <<
1672 le32_to_cpu(raw_super->log_blocksize);
aff063e2
JK
1673 sb->s_max_links = F2FS_LINK_MAX;
1674 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1675
1676 sb->s_op = &f2fs_sops;
0b81d077 1677 sb->s_cop = &f2fs_cryptops;
aff063e2
JK
1678 sb->s_xattr = f2fs_xattr_handlers;
1679 sb->s_export_op = &f2fs_export_ops;
1680 sb->s_magic = F2FS_SUPER_MAGIC;
aff063e2
JK
1681 sb->s_time_gran = 1;
1682 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1683 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
1684 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
1685
1686 /* init f2fs-specific super block info */
e8240f65 1687 sbi->valid_super_block = valid_super_block;
aff063e2 1688 mutex_init(&sbi->gc_mutex);
aff063e2 1689 mutex_init(&sbi->cp_mutex);
b3582c68 1690 init_rwsem(&sbi->node_write);
59c9081b 1691 init_rwsem(&sbi->node_change);
315df839
JK
1692
1693 /* disallow all the data/node/meta page writes */
1694 set_sbi_flag(sbi, SBI_POR_DOING);
aff063e2 1695 spin_lock_init(&sbi->stat_lock);
971767ca 1696
458e6197 1697 for (i = 0; i < NR_PAGE_TYPE; i++) {
a912b54d
JK
1698 int n = (i == META) ? 1: NR_TEMP_TYPE;
1699 int j;
1700
1701 sbi->write_io[i] = kmalloc(n * sizeof(struct f2fs_bio_info),
1702 GFP_KERNEL);
b63def91
CJ
1703 if (!sbi->write_io[i]) {
1704 err = -ENOMEM;
a912b54d 1705 goto free_options;
b63def91 1706 }
a912b54d
JK
1707
1708 for (j = HOT; j < n; j++) {
1709 init_rwsem(&sbi->write_io[i][j].io_rwsem);
1710 sbi->write_io[i][j].sbi = sbi;
1711 sbi->write_io[i][j].bio = NULL;
fb830fc5
CY
1712 spin_lock_init(&sbi->write_io[i][j].io_lock);
1713 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
a912b54d 1714 }
458e6197 1715 }
971767ca 1716
b873b798 1717 init_rwsem(&sbi->cp_rwsem);
fb51b5ef 1718 init_waitqueue_head(&sbi->cp_wait);
aff063e2
JK
1719 init_sb_info(sbi);
1720
523be8a6
JK
1721 err = init_percpu_info(sbi);
1722 if (err)
1723 goto free_options;
1724
0a595eba
JK
1725 if (F2FS_IO_SIZE(sbi) > 1) {
1726 sbi->write_io_dummy =
a3ebfe4f 1727 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
1727f317
CY
1728 if (!sbi->write_io_dummy) {
1729 err = -ENOMEM;
0a595eba 1730 goto free_options;
1727f317 1731 }
0a595eba
JK
1732 }
1733
aff063e2
JK
1734 /* get an inode for meta space */
1735 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
1736 if (IS_ERR(sbi->meta_inode)) {
a07ef784 1737 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
aff063e2 1738 err = PTR_ERR(sbi->meta_inode);
0a595eba 1739 goto free_io_dummy;
aff063e2
JK
1740 }
1741
1742 err = get_valid_checkpoint(sbi);
a07ef784
NJ
1743 if (err) {
1744 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
aff063e2 1745 goto free_meta_inode;
a07ef784 1746 }
aff063e2 1747
3c62be17
JK
1748 /* Initialize device list */
1749 err = f2fs_scan_devices(sbi);
1750 if (err) {
1751 f2fs_msg(sb, KERN_ERR, "Failed to find devices");
1752 goto free_devices;
1753 }
1754
aff063e2
JK
1755 sbi->total_valid_node_count =
1756 le32_to_cpu(sbi->ckpt->valid_node_count);
513c5f37
JK
1757 percpu_counter_set(&sbi->total_valid_inode_count,
1758 le32_to_cpu(sbi->ckpt->valid_inode_count));
aff063e2
JK
1759 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
1760 sbi->total_valid_block_count =
1761 le64_to_cpu(sbi->ckpt->valid_block_count);
1762 sbi->last_valid_block_count = sbi->total_valid_block_count;
41382ec4 1763
c227f912
CY
1764 for (i = 0; i < NR_INODE_TYPE; i++) {
1765 INIT_LIST_HEAD(&sbi->inode_list[i]);
1766 spin_lock_init(&sbi->inode_lock[i]);
1767 }
aff063e2 1768
1dcc336b
CY
1769 init_extent_cache_info(sbi);
1770
6451e041 1771 init_ino_entry_info(sbi);
aff063e2
JK
1772
1773 /* setup f2fs internal modules */
1774 err = build_segment_manager(sbi);
a07ef784
NJ
1775 if (err) {
1776 f2fs_msg(sb, KERN_ERR,
1777 "Failed to initialize F2FS segment manager");
aff063e2 1778 goto free_sm;
a07ef784 1779 }
aff063e2 1780 err = build_node_manager(sbi);
a07ef784
NJ
1781 if (err) {
1782 f2fs_msg(sb, KERN_ERR,
1783 "Failed to initialize F2FS node manager");
aff063e2 1784 goto free_nm;
a07ef784 1785 }
aff063e2 1786
8f1dbbbb
SL
1787 /* For write statistics */
1788 if (sb->s_bdev->bd_part)
1789 sbi->sectors_written_start =
1790 (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
1791
1792 /* Read accumulated write IO statistics if exists */
1793 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1794 if (__exist_node_summaries(sbi))
1795 sbi->kbytes_written =
b2dde6fc 1796 le64_to_cpu(seg_i->journal->info.kbytes_written);
8f1dbbbb 1797
aff063e2
JK
1798 build_gc_manager(sbi);
1799
1800 /* get an inode for node space */
1801 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
1802 if (IS_ERR(sbi->node_inode)) {
a07ef784 1803 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
aff063e2
JK
1804 err = PTR_ERR(sbi->node_inode);
1805 goto free_nm;
1806 }
1807
2658e50d
JK
1808 f2fs_join_shrinker(sbi);
1809
aa51d08a
JK
1810 err = f2fs_build_stats(sbi);
1811 if (err)
1812 goto free_nm;
1813
aff063e2 1814 /* if there are nt orphan nodes free them */
8c14bfad
CY
1815 err = recover_orphan_inodes(sbi);
1816 if (err)
1817 goto free_node_inode;
aff063e2
JK
1818
1819 /* read root inode and dentry */
1820 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
1821 if (IS_ERR(root)) {
a07ef784 1822 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
aff063e2
JK
1823 err = PTR_ERR(root);
1824 goto free_node_inode;
1825 }
8f99a946 1826 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
9d847950 1827 iput(root);
8f99a946 1828 err = -EINVAL;
9d847950 1829 goto free_node_inode;
8f99a946 1830 }
aff063e2
JK
1831
1832 sb->s_root = d_make_root(root); /* allocate root dentry */
1833 if (!sb->s_root) {
1834 err = -ENOMEM;
1835 goto free_root_inode;
1836 }
1837
a398101a 1838 err = f2fs_init_sysfs(sbi);
b59d0bae 1839 if (err)
a398101a 1840 goto free_root_inode;
b59d0bae 1841
6437d1b0
JK
1842 /* recover fsynced data */
1843 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
081d78c2
JK
1844 /*
1845 * mount should be failed, when device has readonly mode, and
1846 * previous checkpoint was not done by clean system shutdown.
1847 */
1848 if (bdev_read_only(sb->s_bdev) &&
aaec2b1d 1849 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
081d78c2 1850 err = -EROFS;
a398101a 1851 goto free_sysfs;
081d78c2 1852 }
2adc3505
CY
1853
1854 if (need_fsck)
1855 set_sbi_flag(sbi, SBI_NEED_FSCK);
1856
a468f0ef
JK
1857 if (!retry)
1858 goto skip_recovery;
1859
6781eabb
JK
1860 err = recover_fsync_data(sbi, false);
1861 if (err < 0) {
2adc3505 1862 need_fsck = true;
6437d1b0 1863 f2fs_msg(sb, KERN_ERR,
99e3e858 1864 "Cannot recover all fsync data errno=%d", err);
a398101a 1865 goto free_sysfs;
ed2e621a 1866 }
6781eabb
JK
1867 } else {
1868 err = recover_fsync_data(sbi, true);
1869
1870 if (!f2fs_readonly(sb) && err > 0) {
1871 err = -EINVAL;
1872 f2fs_msg(sb, KERN_ERR,
1873 "Need to recover fsync data");
a398101a 1874 goto free_sysfs;
6781eabb 1875 }
6437d1b0 1876 }
a468f0ef 1877skip_recovery:
315df839
JK
1878 /* recover_fsync_data() cleared this already */
1879 clear_sbi_flag(sbi, SBI_POR_DOING);
b59d0bae 1880
6437d1b0
JK
1881 /*
1882 * If filesystem is not mounted as read-only then
1883 * do start the gc_thread.
1884 */
6c029932 1885 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
6437d1b0
JK
1886 /* After POR, we can run background GC thread.*/
1887 err = start_gc_thread(sbi);
1888 if (err)
a398101a 1889 goto free_sysfs;
6437d1b0 1890 }
dabc4a5c 1891 kfree(options);
da554e48 1892
1893 /* recover broken superblock */
f2353d7b 1894 if (recovery) {
41214b3c
CY
1895 err = f2fs_commit_super(sbi, true);
1896 f2fs_msg(sb, KERN_INFO,
99e3e858 1897 "Try to recover %dth superblock, ret: %d",
41214b3c 1898 sbi->valid_super_block ? 1 : 2, err);
da554e48 1899 }
1900
1200abb2
JK
1901 f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
1902 cur_cp_version(F2FS_CKPT(sbi)));
6beceb54 1903 f2fs_update_time(sbi, CP_TIME);
d0239e1b 1904 f2fs_update_time(sbi, REQ_TIME);
aff063e2 1905 return 0;
6437d1b0 1906
a398101a 1907free_sysfs:
0f18b462 1908 f2fs_sync_inode_meta(sbi);
a398101a 1909 f2fs_exit_sysfs(sbi);
aff063e2
JK
1910free_root_inode:
1911 dput(sb->s_root);
1912 sb->s_root = NULL;
1913free_node_inode:
bb5dada7 1914 truncate_inode_pages_final(NODE_MAPPING(sbi));
2658e50d 1915 mutex_lock(&sbi->umount_mutex);
d41065e2 1916 release_ino_entry(sbi, true);
2658e50d 1917 f2fs_leave_shrinker(sbi);
09922800
JK
1918 /*
1919 * Some dirty meta pages can be produced by recover_orphan_inodes()
1920 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
1921 * followed by write_checkpoint() through f2fs_write_node_pages(), which
1922 * falls into an infinite loop in sync_meta_pages().
1923 */
1924 truncate_inode_pages_final(META_MAPPING(sbi));
aff063e2 1925 iput(sbi->node_inode);
2658e50d 1926 mutex_unlock(&sbi->umount_mutex);
aa51d08a 1927 f2fs_destroy_stats(sbi);
aff063e2
JK
1928free_nm:
1929 destroy_node_manager(sbi);
1930free_sm:
1931 destroy_segment_manager(sbi);
3c62be17
JK
1932free_devices:
1933 destroy_device_list(sbi);
aff063e2
JK
1934 kfree(sbi->ckpt);
1935free_meta_inode:
1936 make_bad_inode(sbi->meta_inode);
1937 iput(sbi->meta_inode);
0a595eba
JK
1938free_io_dummy:
1939 mempool_destroy(sbi->write_io_dummy);
dabc4a5c 1940free_options:
a912b54d
JK
1941 for (i = 0; i < NR_PAGE_TYPE; i++)
1942 kfree(sbi->write_io[i]);
523be8a6 1943 destroy_percpu_info(sbi);
dabc4a5c 1944 kfree(options);
aff063e2 1945free_sb_buf:
b39f0de2 1946 kfree(raw_super);
aff063e2 1947free_sbi:
43b6573b
KM
1948 if (sbi->s_chksum_driver)
1949 crypto_free_shash(sbi->s_chksum_driver);
aff063e2 1950 kfree(sbi);
ed2e621a
JK
1951
1952 /* give only one another chance */
1953 if (retry) {
9df47ba7 1954 retry = false;
ed2e621a
JK
1955 shrink_dcache_sb(sb);
1956 goto try_onemore;
1957 }
aff063e2
JK
1958 return err;
1959}
1960
1961static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
1962 const char *dev_name, void *data)
1963{
1964 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
1965}
1966
30a5537f
JK
1967static void kill_f2fs_super(struct super_block *sb)
1968{
1969 if (sb->s_root)
caf0047e 1970 set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
30a5537f
JK
1971 kill_block_super(sb);
1972}
1973
aff063e2
JK
1974static struct file_system_type f2fs_fs_type = {
1975 .owner = THIS_MODULE,
1976 .name = "f2fs",
1977 .mount = f2fs_mount,
30a5537f 1978 .kill_sb = kill_f2fs_super,
aff063e2
JK
1979 .fs_flags = FS_REQUIRES_DEV,
1980};
7f78e035 1981MODULE_ALIAS_FS("f2fs");
aff063e2 1982
6e6093a8 1983static int __init init_inodecache(void)
aff063e2 1984{
5d097056
VD
1985 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
1986 sizeof(struct f2fs_inode_info), 0,
1987 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
6bacf52f 1988 if (!f2fs_inode_cachep)
aff063e2
JK
1989 return -ENOMEM;
1990 return 0;
1991}
1992
1993static void destroy_inodecache(void)
1994{
1995 /*
1996 * Make sure all delayed rcu free inodes are flushed before we
1997 * destroy cache.
1998 */
1999 rcu_barrier();
2000 kmem_cache_destroy(f2fs_inode_cachep);
2001}
2002
2003static int __init init_f2fs_fs(void)
2004{
2005 int err;
2006
c0508650
JK
2007 f2fs_build_trace_ios();
2008
aff063e2
JK
2009 err = init_inodecache();
2010 if (err)
2011 goto fail;
2012 err = create_node_manager_caches();
2013 if (err)
9890ff3f 2014 goto free_inodecache;
7fd9e544 2015 err = create_segment_manager_caches();
aff063e2 2016 if (err)
9890ff3f 2017 goto free_node_manager_caches;
aff063e2
JK
2018 err = create_checkpoint_caches();
2019 if (err)
06292073 2020 goto free_segment_manager_caches;
1dcc336b
CY
2021 err = create_extent_cache();
2022 if (err)
2023 goto free_checkpoint_caches;
a398101a
CY
2024 err = f2fs_register_sysfs();
2025 if (err)
1dcc336b 2026 goto free_extent_cache;
2658e50d 2027 err = register_shrinker(&f2fs_shrinker_info);
cfc4d971 2028 if (err)
a398101a 2029 goto free_sysfs;
2658e50d
JK
2030 err = register_filesystem(&f2fs_fs_type);
2031 if (err)
2032 goto free_shrinker;
787c7b8c
CY
2033 err = f2fs_create_root_stats();
2034 if (err)
2035 goto free_filesystem;
9890ff3f
ZH
2036 return 0;
2037
787c7b8c
CY
2038free_filesystem:
2039 unregister_filesystem(&f2fs_fs_type);
2658e50d
JK
2040free_shrinker:
2041 unregister_shrinker(&f2fs_shrinker_info);
a398101a
CY
2042free_sysfs:
2043 f2fs_unregister_sysfs();
1dcc336b
CY
2044free_extent_cache:
2045 destroy_extent_cache();
9890ff3f
ZH
2046free_checkpoint_caches:
2047 destroy_checkpoint_caches();
7fd9e544
JK
2048free_segment_manager_caches:
2049 destroy_segment_manager_caches();
9890ff3f
ZH
2050free_node_manager_caches:
2051 destroy_node_manager_caches();
2052free_inodecache:
2053 destroy_inodecache();
aff063e2
JK
2054fail:
2055 return err;
2056}
2057
2058static void __exit exit_f2fs_fs(void)
2059{
4589d25d 2060 f2fs_destroy_root_stats();
aff063e2 2061 unregister_filesystem(&f2fs_fs_type);
b8bef79d 2062 unregister_shrinker(&f2fs_shrinker_info);
a398101a 2063 f2fs_unregister_sysfs();
fdf6c8be 2064 destroy_extent_cache();
aff063e2 2065 destroy_checkpoint_caches();
5dcd8a71 2066 destroy_segment_manager_caches();
aff063e2
JK
2067 destroy_node_manager_caches();
2068 destroy_inodecache();
351f4fba 2069 f2fs_destroy_trace_ios();
aff063e2
JK
2070}
2071
2072module_init(init_f2fs_fs)
2073module_exit(exit_f2fs_fs)
2074
2075MODULE_AUTHOR("Samsung Electronics's Praesto Team");
2076MODULE_DESCRIPTION("Flash Friendly File System");
2077MODULE_LICENSE("GPL");
b4b9d34c 2078