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