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