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