1 /*
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>
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>
21 #include <linux/random.h>
22 #include <linux/exportfs.h>
23 #include <linux/blkdev.h>
24 #include <linux/f2fs_fs.h>
25
26 #include "f2fs.h"
27 #include "node.h"
28 #include "segment.h"
29 #include "xattr.h"
30
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/f2fs.h>
33
34 static struct kmem_cache *f2fs_inode_cachep;
35
36 enum {
37 Opt_gc_background_off,
38 Opt_disable_roll_forward,
39 Opt_discard,
40 Opt_noheap,
41 Opt_nouser_xattr,
42 Opt_noacl,
43 Opt_active_logs,
44 Opt_disable_ext_identify,
45 Opt_err,
46 };
47
48 static match_table_t f2fs_tokens = {
49 {Opt_gc_background_off, "background_gc_off"},
50 {Opt_disable_roll_forward, "disable_roll_forward"},
51 {Opt_discard, "discard"},
52 {Opt_noheap, "no_heap"},
53 {Opt_nouser_xattr, "nouser_xattr"},
54 {Opt_noacl, "noacl"},
55 {Opt_active_logs, "active_logs=%u"},
56 {Opt_disable_ext_identify, "disable_ext_identify"},
57 {Opt_err, NULL},
58 };
59
f2fs_msg(struct super_block * sb,const char * level,const char * fmt,...)60 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
61 {
62 struct va_format vaf;
63 va_list args;
64
65 va_start(args, fmt);
66 vaf.fmt = fmt;
67 vaf.va = &args;
68 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
69 va_end(args);
70 }
71
init_once(void * foo)72 static void init_once(void *foo)
73 {
74 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
75
76 inode_init_once(&fi->vfs_inode);
77 }
78
f2fs_alloc_inode(struct super_block * sb)79 static struct inode *f2fs_alloc_inode(struct super_block *sb)
80 {
81 struct f2fs_inode_info *fi;
82
83 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
84 if (!fi)
85 return NULL;
86
87 init_once((void *) fi);
88
89 /* Initialize f2fs-specific inode info */
90 fi->vfs_inode.i_version = 1;
91 atomic_set(&fi->dirty_dents, 0);
92 fi->i_current_depth = 1;
93 fi->i_advise = 0;
94 rwlock_init(&fi->ext.ext_lock);
95
96 set_inode_flag(fi, FI_NEW_INODE);
97
98 return &fi->vfs_inode;
99 }
100
f2fs_drop_inode(struct inode * inode)101 static int f2fs_drop_inode(struct inode *inode)
102 {
103 /*
104 * This is to avoid a deadlock condition like below.
105 * writeback_single_inode(inode)
106 * - f2fs_write_data_page
107 * - f2fs_gc -> iput -> evict
108 * - inode_wait_for_writeback(inode)
109 */
110 if (!inode_unhashed(inode) && inode->i_state & I_SYNC)
111 return 0;
112 return generic_drop_inode(inode);
113 }
114
f2fs_i_callback(struct rcu_head * head)115 static void f2fs_i_callback(struct rcu_head *head)
116 {
117 struct inode *inode = container_of(head, struct inode, i_rcu);
118 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
119 }
120
f2fs_destroy_inode(struct inode * inode)121 static void f2fs_destroy_inode(struct inode *inode)
122 {
123 call_rcu(&inode->i_rcu, f2fs_i_callback);
124 }
125
f2fs_put_super(struct super_block * sb)126 static void f2fs_put_super(struct super_block *sb)
127 {
128 struct f2fs_sb_info *sbi = F2FS_SB(sb);
129
130 f2fs_destroy_stats(sbi);
131 stop_gc_thread(sbi);
132
133 write_checkpoint(sbi, true);
134
135 iput(sbi->node_inode);
136 iput(sbi->meta_inode);
137
138 /* destroy f2fs internal modules */
139 destroy_node_manager(sbi);
140 destroy_segment_manager(sbi);
141
142 kfree(sbi->ckpt);
143
144 sb->s_fs_info = NULL;
145 brelse(sbi->raw_super_buf);
146 kfree(sbi);
147 }
148
f2fs_sync_fs(struct super_block * sb,int sync)149 int f2fs_sync_fs(struct super_block *sb, int sync)
150 {
151 struct f2fs_sb_info *sbi = F2FS_SB(sb);
152
153 trace_f2fs_sync_fs(sb, sync);
154
155 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
156 return 0;
157
158 if (sync) {
159 mutex_lock(&sbi->gc_mutex);
160 write_checkpoint(sbi, false);
161 mutex_unlock(&sbi->gc_mutex);
162 } else {
163 f2fs_balance_fs(sbi);
164 }
165
166 return 0;
167 }
168
f2fs_freeze(struct super_block * sb)169 static int f2fs_freeze(struct super_block *sb)
170 {
171 int err;
172
173 if (sb->s_flags & MS_RDONLY)
174 return 0;
175
176 err = f2fs_sync_fs(sb, 1);
177 return err;
178 }
179
f2fs_unfreeze(struct super_block * sb)180 static int f2fs_unfreeze(struct super_block *sb)
181 {
182 return 0;
183 }
184
f2fs_statfs(struct dentry * dentry,struct kstatfs * buf)185 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
186 {
187 struct super_block *sb = dentry->d_sb;
188 struct f2fs_sb_info *sbi = F2FS_SB(sb);
189 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
190 block_t total_count, user_block_count, start_count, ovp_count;
191
192 total_count = le64_to_cpu(sbi->raw_super->block_count);
193 user_block_count = sbi->user_block_count;
194 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
195 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
196 buf->f_type = F2FS_SUPER_MAGIC;
197 buf->f_bsize = sbi->blocksize;
198
199 buf->f_blocks = total_count - start_count;
200 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
201 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
202
203 buf->f_files = sbi->total_node_count;
204 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
205
206 buf->f_namelen = F2FS_NAME_LEN;
207 buf->f_fsid.val[0] = (u32)id;
208 buf->f_fsid.val[1] = (u32)(id >> 32);
209
210 return 0;
211 }
212
f2fs_show_options(struct seq_file * seq,struct dentry * root)213 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
214 {
215 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
216
217 if (test_opt(sbi, BG_GC))
218 seq_puts(seq, ",background_gc_on");
219 else
220 seq_puts(seq, ",background_gc_off");
221 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
222 seq_puts(seq, ",disable_roll_forward");
223 if (test_opt(sbi, DISCARD))
224 seq_puts(seq, ",discard");
225 if (test_opt(sbi, NOHEAP))
226 seq_puts(seq, ",no_heap_alloc");
227 #ifdef CONFIG_F2FS_FS_XATTR
228 if (test_opt(sbi, XATTR_USER))
229 seq_puts(seq, ",user_xattr");
230 else
231 seq_puts(seq, ",nouser_xattr");
232 #endif
233 #ifdef CONFIG_F2FS_FS_POSIX_ACL
234 if (test_opt(sbi, POSIX_ACL))
235 seq_puts(seq, ",acl");
236 else
237 seq_puts(seq, ",noacl");
238 #endif
239 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
240 seq_puts(seq, ",disable_ext_identify");
241
242
243 return 0;
244 }
245
246 static struct super_operations f2fs_sops = {
247 .alloc_inode = f2fs_alloc_inode,
248 .drop_inode = f2fs_drop_inode,
249 .destroy_inode = f2fs_destroy_inode,
250 .write_inode = f2fs_write_inode,
251 .show_options = f2fs_show_options,
252 .evict_inode = f2fs_evict_inode,
253 .put_super = f2fs_put_super,
254 .sync_fs = f2fs_sync_fs,
255 .freeze_fs = f2fs_freeze,
256 .unfreeze_fs = f2fs_unfreeze,
257 .statfs = f2fs_statfs,
258 };
259
f2fs_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)260 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
261 u64 ino, u32 generation)
262 {
263 struct f2fs_sb_info *sbi = F2FS_SB(sb);
264 struct inode *inode;
265
266 if (ino < F2FS_ROOT_INO(sbi))
267 return ERR_PTR(-ESTALE);
268
269 /*
270 * f2fs_iget isn't quite right if the inode is currently unallocated!
271 * However f2fs_iget currently does appropriate checks to handle stale
272 * inodes so everything is OK.
273 */
274 inode = f2fs_iget(sb, ino);
275 if (IS_ERR(inode))
276 return ERR_CAST(inode);
277 if (generation && inode->i_generation != generation) {
278 /* we didn't find the right inode.. */
279 iput(inode);
280 return ERR_PTR(-ESTALE);
281 }
282 return inode;
283 }
284
f2fs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)285 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
286 int fh_len, int fh_type)
287 {
288 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
289 f2fs_nfs_get_inode);
290 }
291
f2fs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)292 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
293 int fh_len, int fh_type)
294 {
295 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
296 f2fs_nfs_get_inode);
297 }
298
299 static const struct export_operations f2fs_export_ops = {
300 .fh_to_dentry = f2fs_fh_to_dentry,
301 .fh_to_parent = f2fs_fh_to_parent,
302 .get_parent = f2fs_get_parent,
303 };
304
parse_options(struct super_block * sb,struct f2fs_sb_info * sbi,char * options)305 static int parse_options(struct super_block *sb, struct f2fs_sb_info *sbi,
306 char *options)
307 {
308 substring_t args[MAX_OPT_ARGS];
309 char *p;
310 int arg = 0;
311
312 if (!options)
313 return 0;
314
315 while ((p = strsep(&options, ",")) != NULL) {
316 int token;
317 if (!*p)
318 continue;
319 /*
320 * Initialize args struct so we know whether arg was
321 * found; some options take optional arguments.
322 */
323 args[0].to = args[0].from = NULL;
324 token = match_token(p, f2fs_tokens, args);
325
326 switch (token) {
327 case Opt_gc_background_off:
328 clear_opt(sbi, BG_GC);
329 break;
330 case Opt_disable_roll_forward:
331 set_opt(sbi, DISABLE_ROLL_FORWARD);
332 break;
333 case Opt_discard:
334 set_opt(sbi, DISCARD);
335 break;
336 case Opt_noheap:
337 set_opt(sbi, NOHEAP);
338 break;
339 #ifdef CONFIG_F2FS_FS_XATTR
340 case Opt_nouser_xattr:
341 clear_opt(sbi, XATTR_USER);
342 break;
343 #else
344 case Opt_nouser_xattr:
345 f2fs_msg(sb, KERN_INFO,
346 "nouser_xattr options not supported");
347 break;
348 #endif
349 #ifdef CONFIG_F2FS_FS_POSIX_ACL
350 case Opt_noacl:
351 clear_opt(sbi, POSIX_ACL);
352 break;
353 #else
354 case Opt_noacl:
355 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
356 break;
357 #endif
358 case Opt_active_logs:
359 if (args->from && match_int(args, &arg))
360 return -EINVAL;
361 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
362 return -EINVAL;
363 sbi->active_logs = arg;
364 break;
365 case Opt_disable_ext_identify:
366 set_opt(sbi, DISABLE_EXT_IDENTIFY);
367 break;
368 default:
369 f2fs_msg(sb, KERN_ERR,
370 "Unrecognized mount option \"%s\" or missing value",
371 p);
372 return -EINVAL;
373 }
374 }
375 return 0;
376 }
377
max_file_size(unsigned bits)378 loff_t max_file_size(unsigned bits)
379 {
380 loff_t result = ADDRS_PER_INODE;
381 loff_t leaf_count = ADDRS_PER_BLOCK;
382
383 /* two direct node blocks */
384 result += (leaf_count * 2);
385
386 /* two indirect node blocks */
387 leaf_count *= NIDS_PER_BLOCK;
388 result += (leaf_count * 2);
389
390 /* one double indirect node block */
391 leaf_count *= NIDS_PER_BLOCK;
392 result += leaf_count;
393
394 result <<= bits;
395 return result;
396 }
397
sanity_check_raw_super(struct super_block * sb,struct f2fs_super_block * raw_super)398 static int sanity_check_raw_super(struct super_block *sb,
399 struct f2fs_super_block *raw_super)
400 {
401 unsigned int blocksize;
402
403 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
404 f2fs_msg(sb, KERN_INFO,
405 "Magic Mismatch, valid(0x%x) - read(0x%x)",
406 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
407 return 1;
408 }
409
410 /* Currently, support only 4KB page cache size */
411 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
412 f2fs_msg(sb, KERN_INFO,
413 "Invalid page_cache_size (%lu), supports only 4KB\n",
414 PAGE_CACHE_SIZE);
415 return 1;
416 }
417
418 /* Currently, support only 4KB block size */
419 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
420 if (blocksize != F2FS_BLKSIZE) {
421 f2fs_msg(sb, KERN_INFO,
422 "Invalid blocksize (%u), supports only 4KB\n",
423 blocksize);
424 return 1;
425 }
426
427 /* check log blocks per segment */
428 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
429 f2fs_msg(sb, KERN_INFO,
430 "Invalid log blocks per segment (%u)\n",
431 le32_to_cpu(raw_super->log_blocks_per_seg));
432 return 1;
433 }
434
435 if (le32_to_cpu(raw_super->log_sectorsize) !=
436 F2FS_LOG_SECTOR_SIZE) {
437 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
438 return 1;
439 }
440 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
441 F2FS_LOG_SECTORS_PER_BLOCK) {
442 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
443 return 1;
444 }
445
446 if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) {
447 f2fs_msg(sb, KERN_INFO,
448 "Invalid segment count (%u)",
449 le32_to_cpu(raw_super->segment_count));
450 return 1;
451 }
452
453 return 0;
454 }
455
sanity_check_ckpt(struct f2fs_sb_info * sbi)456 static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
457 {
458 unsigned int total, fsmeta;
459 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
460 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
461
462 total = le32_to_cpu(raw_super->segment_count);
463 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
464 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
465 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
466 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
467 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
468
469 if (fsmeta >= total)
470 return 1;
471
472 if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
473 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
474 return 1;
475 }
476 return 0;
477 }
478
init_sb_info(struct f2fs_sb_info * sbi)479 static void init_sb_info(struct f2fs_sb_info *sbi)
480 {
481 struct f2fs_super_block *raw_super = sbi->raw_super;
482 int i;
483
484 sbi->log_sectors_per_block =
485 le32_to_cpu(raw_super->log_sectors_per_block);
486 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
487 sbi->blocksize = 1 << sbi->log_blocksize;
488 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
489 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
490 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
491 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
492 sbi->total_sections = le32_to_cpu(raw_super->section_count);
493 sbi->total_node_count =
494 (le32_to_cpu(raw_super->segment_count_nat) / 2)
495 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
496 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
497 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
498 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
499 sbi->cur_victim_sec = NULL_SECNO;
500
501 for (i = 0; i < NR_COUNT_TYPE; i++)
502 atomic_set(&sbi->nr_pages[i], 0);
503 }
504
validate_superblock(struct super_block * sb,struct f2fs_super_block ** raw_super,struct buffer_head ** raw_super_buf,sector_t block)505 static int validate_superblock(struct super_block *sb,
506 struct f2fs_super_block **raw_super,
507 struct buffer_head **raw_super_buf, sector_t block)
508 {
509 const char *super = (block == 0 ? "first" : "second");
510
511 /* read f2fs raw super block */
512 *raw_super_buf = sb_bread(sb, block);
513 if (!*raw_super_buf) {
514 f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
515 super);
516 return -EIO;
517 }
518
519 *raw_super = (struct f2fs_super_block *)
520 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
521
522 /* sanity checking of raw super */
523 if (!sanity_check_raw_super(sb, *raw_super))
524 return 0;
525
526 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
527 "in %s superblock", super);
528 return -EINVAL;
529 }
530
f2fs_fill_super(struct super_block * sb,void * data,int silent)531 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
532 {
533 struct f2fs_sb_info *sbi;
534 struct f2fs_super_block *raw_super;
535 struct buffer_head *raw_super_buf;
536 struct inode *root;
537 long err = -EINVAL;
538 int i;
539
540 /* allocate memory for f2fs-specific super block info */
541 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
542 if (!sbi)
543 return -ENOMEM;
544
545 /* set a block size */
546 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
547 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
548 goto free_sbi;
549 }
550
551 err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
552 if (err) {
553 brelse(raw_super_buf);
554 /* check secondary superblock when primary failed */
555 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
556 if (err)
557 goto free_sb_buf;
558 }
559 /* init some FS parameters */
560 sbi->active_logs = NR_CURSEG_TYPE;
561
562 set_opt(sbi, BG_GC);
563
564 #ifdef CONFIG_F2FS_FS_XATTR
565 set_opt(sbi, XATTR_USER);
566 #endif
567 #ifdef CONFIG_F2FS_FS_POSIX_ACL
568 set_opt(sbi, POSIX_ACL);
569 #endif
570 /* parse mount options */
571 err = parse_options(sb, sbi, (char *)data);
572 if (err)
573 goto free_sb_buf;
574
575 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
576 sb->s_max_links = F2FS_LINK_MAX;
577 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
578
579 sb->s_op = &f2fs_sops;
580 sb->s_xattr = f2fs_xattr_handlers;
581 sb->s_export_op = &f2fs_export_ops;
582 sb->s_magic = F2FS_SUPER_MAGIC;
583 sb->s_fs_info = sbi;
584 sb->s_time_gran = 1;
585 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
586 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
587 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
588
589 /* init f2fs-specific super block info */
590 sbi->sb = sb;
591 sbi->raw_super = raw_super;
592 sbi->raw_super_buf = raw_super_buf;
593 mutex_init(&sbi->gc_mutex);
594 mutex_init(&sbi->writepages);
595 mutex_init(&sbi->cp_mutex);
596 for (i = 0; i < NR_GLOBAL_LOCKS; i++)
597 mutex_init(&sbi->fs_lock[i]);
598 mutex_init(&sbi->node_write);
599 sbi->por_doing = 0;
600 spin_lock_init(&sbi->stat_lock);
601 init_rwsem(&sbi->bio_sem);
602 init_sb_info(sbi);
603
604 /* get an inode for meta space */
605 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
606 if (IS_ERR(sbi->meta_inode)) {
607 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
608 err = PTR_ERR(sbi->meta_inode);
609 goto free_sb_buf;
610 }
611
612 err = get_valid_checkpoint(sbi);
613 if (err) {
614 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
615 goto free_meta_inode;
616 }
617
618 /* sanity checking of checkpoint */
619 err = -EINVAL;
620 if (sanity_check_ckpt(sbi)) {
621 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
622 goto free_cp;
623 }
624
625 sbi->total_valid_node_count =
626 le32_to_cpu(sbi->ckpt->valid_node_count);
627 sbi->total_valid_inode_count =
628 le32_to_cpu(sbi->ckpt->valid_inode_count);
629 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
630 sbi->total_valid_block_count =
631 le64_to_cpu(sbi->ckpt->valid_block_count);
632 sbi->last_valid_block_count = sbi->total_valid_block_count;
633 sbi->alloc_valid_block_count = 0;
634 INIT_LIST_HEAD(&sbi->dir_inode_list);
635 spin_lock_init(&sbi->dir_inode_lock);
636
637 init_orphan_info(sbi);
638
639 /* setup f2fs internal modules */
640 err = build_segment_manager(sbi);
641 if (err) {
642 f2fs_msg(sb, KERN_ERR,
643 "Failed to initialize F2FS segment manager");
644 goto free_sm;
645 }
646 err = build_node_manager(sbi);
647 if (err) {
648 f2fs_msg(sb, KERN_ERR,
649 "Failed to initialize F2FS node manager");
650 goto free_nm;
651 }
652
653 build_gc_manager(sbi);
654
655 /* get an inode for node space */
656 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
657 if (IS_ERR(sbi->node_inode)) {
658 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
659 err = PTR_ERR(sbi->node_inode);
660 goto free_nm;
661 }
662
663 /* if there are nt orphan nodes free them */
664 err = -EINVAL;
665 if (recover_orphan_inodes(sbi))
666 goto free_node_inode;
667
668 /* read root inode and dentry */
669 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
670 if (IS_ERR(root)) {
671 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
672 err = PTR_ERR(root);
673 goto free_node_inode;
674 }
675 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
676 goto free_root_inode;
677
678 sb->s_root = d_make_root(root); /* allocate root dentry */
679 if (!sb->s_root) {
680 err = -ENOMEM;
681 goto free_root_inode;
682 }
683
684 /* recover fsynced data */
685 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
686 err = recover_fsync_data(sbi);
687 if (err)
688 f2fs_msg(sb, KERN_ERR,
689 "Cannot recover all fsync data errno=%ld", err);
690 }
691
692 /* After POR, we can run background GC thread */
693 err = start_gc_thread(sbi);
694 if (err)
695 goto fail;
696
697 err = f2fs_build_stats(sbi);
698 if (err)
699 goto fail;
700
701 if (test_opt(sbi, DISCARD)) {
702 struct request_queue *q = bdev_get_queue(sb->s_bdev);
703 if (!blk_queue_discard(q))
704 f2fs_msg(sb, KERN_WARNING,
705 "mounting with \"discard\" option, but "
706 "the device does not support discard");
707 }
708
709 return 0;
710 fail:
711 stop_gc_thread(sbi);
712 free_root_inode:
713 dput(sb->s_root);
714 sb->s_root = NULL;
715 free_node_inode:
716 iput(sbi->node_inode);
717 free_nm:
718 destroy_node_manager(sbi);
719 free_sm:
720 destroy_segment_manager(sbi);
721 free_cp:
722 kfree(sbi->ckpt);
723 free_meta_inode:
724 make_bad_inode(sbi->meta_inode);
725 iput(sbi->meta_inode);
726 free_sb_buf:
727 brelse(raw_super_buf);
728 free_sbi:
729 kfree(sbi);
730 return err;
731 }
732
f2fs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)733 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
734 const char *dev_name, void *data)
735 {
736 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
737 }
738
739 static struct file_system_type f2fs_fs_type = {
740 .owner = THIS_MODULE,
741 .name = "f2fs",
742 .mount = f2fs_mount,
743 .kill_sb = kill_block_super,
744 .fs_flags = FS_REQUIRES_DEV,
745 };
746 MODULE_ALIAS_FS("f2fs");
747
init_inodecache(void)748 static int __init init_inodecache(void)
749 {
750 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
751 sizeof(struct f2fs_inode_info), NULL);
752 if (f2fs_inode_cachep == NULL)
753 return -ENOMEM;
754 return 0;
755 }
756
destroy_inodecache(void)757 static void destroy_inodecache(void)
758 {
759 /*
760 * Make sure all delayed rcu free inodes are flushed before we
761 * destroy cache.
762 */
763 rcu_barrier();
764 kmem_cache_destroy(f2fs_inode_cachep);
765 }
766
init_f2fs_fs(void)767 static int __init init_f2fs_fs(void)
768 {
769 int err;
770
771 err = init_inodecache();
772 if (err)
773 goto fail;
774 err = create_node_manager_caches();
775 if (err)
776 goto fail;
777 err = create_gc_caches();
778 if (err)
779 goto fail;
780 err = create_checkpoint_caches();
781 if (err)
782 goto fail;
783 err = register_filesystem(&f2fs_fs_type);
784 if (err)
785 goto fail;
786 f2fs_create_root_stats();
787 fail:
788 return err;
789 }
790
exit_f2fs_fs(void)791 static void __exit exit_f2fs_fs(void)
792 {
793 f2fs_destroy_root_stats();
794 unregister_filesystem(&f2fs_fs_type);
795 destroy_checkpoint_caches();
796 destroy_gc_caches();
797 destroy_node_manager_caches();
798 destroy_inodecache();
799 }
800
801 module_init(init_f2fs_fs)
802 module_exit(exit_f2fs_fs)
803
804 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
805 MODULE_DESCRIPTION("Flash Friendly File System");
806 MODULE_LICENSE("GPL");
807