]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/f2fs/super.c
f2fs: fix a build failure due to missing the kobject header
[karo-tx-linux.git] / fs / f2fs / super.c
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/proc_fs.h>
22 #include <linux/random.h>
23 #include <linux/exportfs.h>
24 #include <linux/blkdev.h>
25 #include <linux/f2fs_fs.h>
26 #include <linux/sysfs.h>
27
28 #include "f2fs.h"
29 #include "node.h"
30 #include "segment.h"
31 #include "xattr.h"
32 #include "gc.h"
33
34 #define CREATE_TRACE_POINTS
35 #include <trace/events/f2fs.h>
36
37 static struct proc_dir_entry *f2fs_proc_root;
38 static struct kmem_cache *f2fs_inode_cachep;
39 static struct kset *f2fs_kset;
40
41 enum {
42         Opt_gc_background,
43         Opt_disable_roll_forward,
44         Opt_discard,
45         Opt_noheap,
46         Opt_nouser_xattr,
47         Opt_noacl,
48         Opt_active_logs,
49         Opt_disable_ext_identify,
50         Opt_err,
51 };
52
53 static match_table_t f2fs_tokens = {
54         {Opt_gc_background, "background_gc=%s"},
55         {Opt_disable_roll_forward, "disable_roll_forward"},
56         {Opt_discard, "discard"},
57         {Opt_noheap, "no_heap"},
58         {Opt_nouser_xattr, "nouser_xattr"},
59         {Opt_noacl, "noacl"},
60         {Opt_active_logs, "active_logs=%u"},
61         {Opt_disable_ext_identify, "disable_ext_identify"},
62         {Opt_err, NULL},
63 };
64
65 /* Sysfs support for f2fs */
66 struct f2fs_attr {
67         struct attribute attr;
68         ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
69         ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
70                          const char *, size_t);
71         int offset;
72 };
73
74 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
75                         struct f2fs_sb_info *sbi, char *buf)
76 {
77         struct f2fs_gc_kthread *gc_kth = sbi->gc_thread;
78         unsigned int *ui;
79
80         if (!gc_kth)
81                 return -EINVAL;
82
83         ui = (unsigned int *)(((char *)gc_kth) + a->offset);
84
85         return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
86 }
87
88 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
89                         struct f2fs_sb_info *sbi,
90                         const char *buf, size_t count)
91 {
92         struct f2fs_gc_kthread *gc_kth = sbi->gc_thread;
93         unsigned long t;
94         unsigned int *ui;
95         ssize_t ret;
96
97         if (!gc_kth)
98                 return -EINVAL;
99
100         ui = (unsigned int *)(((char *)gc_kth) + a->offset);
101
102         ret = kstrtoul(skip_spaces(buf), 0, &t);
103         if (ret < 0)
104                 return ret;
105         *ui = t;
106         return count;
107 }
108
109 static ssize_t f2fs_attr_show(struct kobject *kobj,
110                                 struct attribute *attr, char *buf)
111 {
112         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
113                                                                 s_kobj);
114         struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
115
116         return a->show ? a->show(a, sbi, buf) : 0;
117 }
118
119 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
120                                                 const char *buf, size_t len)
121 {
122         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
123                                                                         s_kobj);
124         struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
125
126         return a->store ? a->store(a, sbi, buf, len) : 0;
127 }
128
129 static void f2fs_sb_release(struct kobject *kobj)
130 {
131         struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
132                                                                 s_kobj);
133         complete(&sbi->s_kobj_unregister);
134 }
135
136 #define F2FS_ATTR_OFFSET(_name, _mode, _show, _store, _elname) \
137 static struct f2fs_attr f2fs_attr_##_name = {                   \
138         .attr = {.name = __stringify(_name), .mode = _mode },   \
139         .show   = _show,                                        \
140         .store  = _store,                                       \
141         .offset = offsetof(struct f2fs_gc_kthread, _elname),    \
142 }
143
144 #define F2FS_RW_ATTR(name, elname)      \
145         F2FS_ATTR_OFFSET(name, 0644, f2fs_sbi_show, f2fs_sbi_store, elname)
146
147 F2FS_RW_ATTR(gc_min_sleep_time, min_sleep_time);
148 F2FS_RW_ATTR(gc_max_sleep_time, max_sleep_time);
149 F2FS_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time);
150 F2FS_RW_ATTR(gc_idle, gc_idle);
151
152 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
153 static struct attribute *f2fs_attrs[] = {
154         ATTR_LIST(gc_min_sleep_time),
155         ATTR_LIST(gc_max_sleep_time),
156         ATTR_LIST(gc_no_gc_sleep_time),
157         ATTR_LIST(gc_idle),
158         NULL,
159 };
160
161 static const struct sysfs_ops f2fs_attr_ops = {
162         .show   = f2fs_attr_show,
163         .store  = f2fs_attr_store,
164 };
165
166 static struct kobj_type f2fs_ktype = {
167         .default_attrs  = f2fs_attrs,
168         .sysfs_ops      = &f2fs_attr_ops,
169         .release        = f2fs_sb_release,
170 };
171
172 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
173 {
174         struct va_format vaf;
175         va_list args;
176
177         va_start(args, fmt);
178         vaf.fmt = fmt;
179         vaf.va = &args;
180         printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
181         va_end(args);
182 }
183
184 static void init_once(void *foo)
185 {
186         struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
187
188         inode_init_once(&fi->vfs_inode);
189 }
190
191 static int parse_options(struct super_block *sb, char *options)
192 {
193         struct f2fs_sb_info *sbi = F2FS_SB(sb);
194         substring_t args[MAX_OPT_ARGS];
195         char *p, *name;
196         int arg = 0;
197
198         if (!options)
199                 return 0;
200
201         while ((p = strsep(&options, ",")) != NULL) {
202                 int token;
203                 if (!*p)
204                         continue;
205                 /*
206                  * Initialize args struct so we know whether arg was
207                  * found; some options take optional arguments.
208                  */
209                 args[0].to = args[0].from = NULL;
210                 token = match_token(p, f2fs_tokens, args);
211
212                 switch (token) {
213                 case Opt_gc_background:
214                         name = match_strdup(&args[0]);
215
216                         if (!name)
217                                 return -ENOMEM;
218                         if (!strncmp(name, "on", 2))
219                                 set_opt(sbi, BG_GC);
220                         else if (!strncmp(name, "off", 3))
221                                 clear_opt(sbi, BG_GC);
222                         else {
223                                 kfree(name);
224                                 return -EINVAL;
225                         }
226                         kfree(name);
227                         break;
228                 case Opt_disable_roll_forward:
229                         set_opt(sbi, DISABLE_ROLL_FORWARD);
230                         break;
231                 case Opt_discard:
232                         set_opt(sbi, DISCARD);
233                         break;
234                 case Opt_noheap:
235                         set_opt(sbi, NOHEAP);
236                         break;
237 #ifdef CONFIG_F2FS_FS_XATTR
238                 case Opt_nouser_xattr:
239                         clear_opt(sbi, XATTR_USER);
240                         break;
241 #else
242                 case Opt_nouser_xattr:
243                         f2fs_msg(sb, KERN_INFO,
244                                 "nouser_xattr options not supported");
245                         break;
246 #endif
247 #ifdef CONFIG_F2FS_FS_POSIX_ACL
248                 case Opt_noacl:
249                         clear_opt(sbi, POSIX_ACL);
250                         break;
251 #else
252                 case Opt_noacl:
253                         f2fs_msg(sb, KERN_INFO, "noacl options not supported");
254                         break;
255 #endif
256                 case Opt_active_logs:
257                         if (args->from && match_int(args, &arg))
258                                 return -EINVAL;
259                         if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
260                                 return -EINVAL;
261                         sbi->active_logs = arg;
262                         break;
263                 case Opt_disable_ext_identify:
264                         set_opt(sbi, DISABLE_EXT_IDENTIFY);
265                         break;
266                 default:
267                         f2fs_msg(sb, KERN_ERR,
268                                 "Unrecognized mount option \"%s\" or missing value",
269                                 p);
270                         return -EINVAL;
271                 }
272         }
273         return 0;
274 }
275
276 static struct inode *f2fs_alloc_inode(struct super_block *sb)
277 {
278         struct f2fs_inode_info *fi;
279
280         fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
281         if (!fi)
282                 return NULL;
283
284         init_once((void *) fi);
285
286         /* Initialize f2fs-specific inode info */
287         fi->vfs_inode.i_version = 1;
288         atomic_set(&fi->dirty_dents, 0);
289         fi->i_current_depth = 1;
290         fi->i_advise = 0;
291         rwlock_init(&fi->ext.ext_lock);
292
293         set_inode_flag(fi, FI_NEW_INODE);
294
295         return &fi->vfs_inode;
296 }
297
298 static int f2fs_drop_inode(struct inode *inode)
299 {
300         /*
301          * This is to avoid a deadlock condition like below.
302          * writeback_single_inode(inode)
303          *  - f2fs_write_data_page
304          *    - f2fs_gc -> iput -> evict
305          *       - inode_wait_for_writeback(inode)
306          */
307         if (!inode_unhashed(inode) && inode->i_state & I_SYNC)
308                 return 0;
309         return generic_drop_inode(inode);
310 }
311
312 /*
313  * f2fs_dirty_inode() is called from __mark_inode_dirty()
314  *
315  * We should call set_dirty_inode to write the dirty inode through write_inode.
316  */
317 static void f2fs_dirty_inode(struct inode *inode, int flags)
318 {
319         set_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
320         return;
321 }
322
323 static void f2fs_i_callback(struct rcu_head *head)
324 {
325         struct inode *inode = container_of(head, struct inode, i_rcu);
326         kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
327 }
328
329 static void f2fs_destroy_inode(struct inode *inode)
330 {
331         call_rcu(&inode->i_rcu, f2fs_i_callback);
332 }
333
334 static void f2fs_put_super(struct super_block *sb)
335 {
336         struct f2fs_sb_info *sbi = F2FS_SB(sb);
337
338         if (sbi->s_proc) {
339                 remove_proc_entry("segment_info", sbi->s_proc);
340                 remove_proc_entry(sb->s_id, f2fs_proc_root);
341         }
342         kobject_del(&sbi->s_kobj);
343
344         f2fs_destroy_stats(sbi);
345         stop_gc_thread(sbi);
346
347         write_checkpoint(sbi, true);
348
349         iput(sbi->node_inode);
350         iput(sbi->meta_inode);
351
352         /* destroy f2fs internal modules */
353         destroy_node_manager(sbi);
354         destroy_segment_manager(sbi);
355
356         kfree(sbi->ckpt);
357         kobject_put(&sbi->s_kobj);
358         wait_for_completion(&sbi->s_kobj_unregister);
359
360         sb->s_fs_info = NULL;
361         brelse(sbi->raw_super_buf);
362         kfree(sbi);
363 }
364
365 int f2fs_sync_fs(struct super_block *sb, int sync)
366 {
367         struct f2fs_sb_info *sbi = F2FS_SB(sb);
368
369         trace_f2fs_sync_fs(sb, sync);
370
371         if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
372                 return 0;
373
374         if (sync) {
375                 mutex_lock(&sbi->gc_mutex);
376                 write_checkpoint(sbi, false);
377                 mutex_unlock(&sbi->gc_mutex);
378         } else {
379                 f2fs_balance_fs(sbi);
380         }
381
382         return 0;
383 }
384
385 static int f2fs_freeze(struct super_block *sb)
386 {
387         int err;
388
389         if (f2fs_readonly(sb))
390                 return 0;
391
392         err = f2fs_sync_fs(sb, 1);
393         return err;
394 }
395
396 static int f2fs_unfreeze(struct super_block *sb)
397 {
398         return 0;
399 }
400
401 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
402 {
403         struct super_block *sb = dentry->d_sb;
404         struct f2fs_sb_info *sbi = F2FS_SB(sb);
405         u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
406         block_t total_count, user_block_count, start_count, ovp_count;
407
408         total_count = le64_to_cpu(sbi->raw_super->block_count);
409         user_block_count = sbi->user_block_count;
410         start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
411         ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
412         buf->f_type = F2FS_SUPER_MAGIC;
413         buf->f_bsize = sbi->blocksize;
414
415         buf->f_blocks = total_count - start_count;
416         buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
417         buf->f_bavail = user_block_count - valid_user_blocks(sbi);
418
419         buf->f_files = sbi->total_node_count;
420         buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
421
422         buf->f_namelen = F2FS_NAME_LEN;
423         buf->f_fsid.val[0] = (u32)id;
424         buf->f_fsid.val[1] = (u32)(id >> 32);
425
426         return 0;
427 }
428
429 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
430 {
431         struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
432
433         if (!(root->d_sb->s_flags & MS_RDONLY) && test_opt(sbi, BG_GC))
434                 seq_printf(seq, ",background_gc=%s", "on");
435         else
436                 seq_printf(seq, ",background_gc=%s", "off");
437         if (test_opt(sbi, DISABLE_ROLL_FORWARD))
438                 seq_puts(seq, ",disable_roll_forward");
439         if (test_opt(sbi, DISCARD))
440                 seq_puts(seq, ",discard");
441         if (test_opt(sbi, NOHEAP))
442                 seq_puts(seq, ",no_heap_alloc");
443 #ifdef CONFIG_F2FS_FS_XATTR
444         if (test_opt(sbi, XATTR_USER))
445                 seq_puts(seq, ",user_xattr");
446         else
447                 seq_puts(seq, ",nouser_xattr");
448 #endif
449 #ifdef CONFIG_F2FS_FS_POSIX_ACL
450         if (test_opt(sbi, POSIX_ACL))
451                 seq_puts(seq, ",acl");
452         else
453                 seq_puts(seq, ",noacl");
454 #endif
455         if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
456                 seq_puts(seq, ",disable_ext_identify");
457
458         seq_printf(seq, ",active_logs=%u", sbi->active_logs);
459
460         return 0;
461 }
462
463 static int segment_info_seq_show(struct seq_file *seq, void *offset)
464 {
465         struct super_block *sb = seq->private;
466         struct f2fs_sb_info *sbi = F2FS_SB(sb);
467         unsigned int total_segs = le32_to_cpu(sbi->raw_super->segment_count_main);
468         int i;
469
470         for (i = 0; i < total_segs; i++) {
471                 seq_printf(seq, "%u", get_valid_blocks(sbi, i, 1));
472                 if (i != 0 && (i % 10) == 0)
473                         seq_puts(seq, "\n");
474                 else
475                         seq_puts(seq, " ");
476         }
477         return 0;
478 }
479
480 static int segment_info_open_fs(struct inode *inode, struct file *file)
481 {
482         return single_open(file, segment_info_seq_show, PDE_DATA(inode));
483 }
484
485 static const struct file_operations f2fs_seq_segment_info_fops = {
486         .owner = THIS_MODULE,
487         .open = segment_info_open_fs,
488         .read = seq_read,
489         .llseek = seq_lseek,
490         .release = single_release,
491 };
492
493 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
494 {
495         struct f2fs_sb_info *sbi = F2FS_SB(sb);
496         struct f2fs_mount_info org_mount_opt;
497         int err, active_logs;
498
499         /*
500          * Save the old mount options in case we
501          * need to restore them.
502          */
503         org_mount_opt = sbi->mount_opt;
504         active_logs = sbi->active_logs;
505
506         /* parse mount options */
507         err = parse_options(sb, data);
508         if (err)
509                 goto restore_opts;
510
511         /*
512          * Previous and new state of filesystem is RO,
513          * so no point in checking GC conditions.
514          */
515         if ((sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
516                 goto skip;
517
518         /*
519          * We stop the GC thread if FS is mounted as RO
520          * or if background_gc = off is passed in mount
521          * option. Also sync the filesystem.
522          */
523         if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
524                 if (sbi->gc_thread) {
525                         stop_gc_thread(sbi);
526                         f2fs_sync_fs(sb, 1);
527                 }
528         } else if (test_opt(sbi, BG_GC) && !sbi->gc_thread) {
529                 err = start_gc_thread(sbi);
530                 if (err)
531                         goto restore_opts;
532         }
533 skip:
534         /* Update the POSIXACL Flag */
535          sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
536                 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
537         return 0;
538
539 restore_opts:
540         sbi->mount_opt = org_mount_opt;
541         sbi->active_logs = active_logs;
542         return err;
543 }
544
545 static struct super_operations f2fs_sops = {
546         .alloc_inode    = f2fs_alloc_inode,
547         .drop_inode     = f2fs_drop_inode,
548         .destroy_inode  = f2fs_destroy_inode,
549         .write_inode    = f2fs_write_inode,
550         .dirty_inode    = f2fs_dirty_inode,
551         .show_options   = f2fs_show_options,
552         .evict_inode    = f2fs_evict_inode,
553         .put_super      = f2fs_put_super,
554         .sync_fs        = f2fs_sync_fs,
555         .freeze_fs      = f2fs_freeze,
556         .unfreeze_fs    = f2fs_unfreeze,
557         .statfs         = f2fs_statfs,
558         .remount_fs     = f2fs_remount,
559 };
560
561 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
562                 u64 ino, u32 generation)
563 {
564         struct f2fs_sb_info *sbi = F2FS_SB(sb);
565         struct inode *inode;
566
567         if (ino < F2FS_ROOT_INO(sbi))
568                 return ERR_PTR(-ESTALE);
569
570         /*
571          * f2fs_iget isn't quite right if the inode is currently unallocated!
572          * However f2fs_iget currently does appropriate checks to handle stale
573          * inodes so everything is OK.
574          */
575         inode = f2fs_iget(sb, ino);
576         if (IS_ERR(inode))
577                 return ERR_CAST(inode);
578         if (generation && inode->i_generation != generation) {
579                 /* we didn't find the right inode.. */
580                 iput(inode);
581                 return ERR_PTR(-ESTALE);
582         }
583         return inode;
584 }
585
586 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
587                 int fh_len, int fh_type)
588 {
589         return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
590                                     f2fs_nfs_get_inode);
591 }
592
593 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
594                 int fh_len, int fh_type)
595 {
596         return generic_fh_to_parent(sb, fid, fh_len, fh_type,
597                                     f2fs_nfs_get_inode);
598 }
599
600 static const struct export_operations f2fs_export_ops = {
601         .fh_to_dentry = f2fs_fh_to_dentry,
602         .fh_to_parent = f2fs_fh_to_parent,
603         .get_parent = f2fs_get_parent,
604 };
605
606 static loff_t max_file_size(unsigned bits)
607 {
608         loff_t result = ADDRS_PER_INODE;
609         loff_t leaf_count = ADDRS_PER_BLOCK;
610
611         /* two direct node blocks */
612         result += (leaf_count * 2);
613
614         /* two indirect node blocks */
615         leaf_count *= NIDS_PER_BLOCK;
616         result += (leaf_count * 2);
617
618         /* one double indirect node block */
619         leaf_count *= NIDS_PER_BLOCK;
620         result += leaf_count;
621
622         result <<= bits;
623         return result;
624 }
625
626 static int sanity_check_raw_super(struct super_block *sb,
627                         struct f2fs_super_block *raw_super)
628 {
629         unsigned int blocksize;
630
631         if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
632                 f2fs_msg(sb, KERN_INFO,
633                         "Magic Mismatch, valid(0x%x) - read(0x%x)",
634                         F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
635                 return 1;
636         }
637
638         /* Currently, support only 4KB page cache size */
639         if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
640                 f2fs_msg(sb, KERN_INFO,
641                         "Invalid page_cache_size (%lu), supports only 4KB\n",
642                         PAGE_CACHE_SIZE);
643                 return 1;
644         }
645
646         /* Currently, support only 4KB block size */
647         blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
648         if (blocksize != F2FS_BLKSIZE) {
649                 f2fs_msg(sb, KERN_INFO,
650                         "Invalid blocksize (%u), supports only 4KB\n",
651                         blocksize);
652                 return 1;
653         }
654
655         if (le32_to_cpu(raw_super->log_sectorsize) !=
656                                         F2FS_LOG_SECTOR_SIZE) {
657                 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
658                 return 1;
659         }
660         if (le32_to_cpu(raw_super->log_sectors_per_block) !=
661                                         F2FS_LOG_SECTORS_PER_BLOCK) {
662                 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
663                 return 1;
664         }
665         return 0;
666 }
667
668 static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
669 {
670         unsigned int total, fsmeta;
671         struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
672         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
673
674         total = le32_to_cpu(raw_super->segment_count);
675         fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
676         fsmeta += le32_to_cpu(raw_super->segment_count_sit);
677         fsmeta += le32_to_cpu(raw_super->segment_count_nat);
678         fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
679         fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
680
681         if (fsmeta >= total)
682                 return 1;
683
684         if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
685                 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
686                 return 1;
687         }
688         return 0;
689 }
690
691 static void init_sb_info(struct f2fs_sb_info *sbi)
692 {
693         struct f2fs_super_block *raw_super = sbi->raw_super;
694         int i;
695
696         sbi->log_sectors_per_block =
697                 le32_to_cpu(raw_super->log_sectors_per_block);
698         sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
699         sbi->blocksize = 1 << sbi->log_blocksize;
700         sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
701         sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
702         sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
703         sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
704         sbi->total_sections = le32_to_cpu(raw_super->section_count);
705         sbi->total_node_count =
706                 (le32_to_cpu(raw_super->segment_count_nat) / 2)
707                         * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
708         sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
709         sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
710         sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
711         sbi->cur_victim_sec = NULL_SECNO;
712
713         for (i = 0; i < NR_COUNT_TYPE; i++)
714                 atomic_set(&sbi->nr_pages[i], 0);
715 }
716
717 static int validate_superblock(struct super_block *sb,
718                 struct f2fs_super_block **raw_super,
719                 struct buffer_head **raw_super_buf, sector_t block)
720 {
721         const char *super = (block == 0 ? "first" : "second");
722
723         /* read f2fs raw super block */
724         *raw_super_buf = sb_bread(sb, block);
725         if (!*raw_super_buf) {
726                 f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
727                                 super);
728                 return -EIO;
729         }
730
731         *raw_super = (struct f2fs_super_block *)
732                 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
733
734         /* sanity checking of raw super */
735         if (!sanity_check_raw_super(sb, *raw_super))
736                 return 0;
737
738         f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
739                                 "in %s superblock", super);
740         return -EINVAL;
741 }
742
743 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
744 {
745         struct f2fs_sb_info *sbi;
746         struct f2fs_super_block *raw_super;
747         struct buffer_head *raw_super_buf;
748         struct inode *root;
749         long err = -EINVAL;
750         int i;
751
752         /* allocate memory for f2fs-specific super block info */
753         sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
754         if (!sbi)
755                 return -ENOMEM;
756
757         /* set a block size */
758         if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
759                 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
760                 goto free_sbi;
761         }
762
763         err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
764         if (err) {
765                 brelse(raw_super_buf);
766                 /* check secondary superblock when primary failed */
767                 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
768                 if (err)
769                         goto free_sb_buf;
770         }
771         sb->s_fs_info = sbi;
772         /* init some FS parameters */
773         sbi->active_logs = NR_CURSEG_TYPE;
774
775         set_opt(sbi, BG_GC);
776
777 #ifdef CONFIG_F2FS_FS_XATTR
778         set_opt(sbi, XATTR_USER);
779 #endif
780 #ifdef CONFIG_F2FS_FS_POSIX_ACL
781         set_opt(sbi, POSIX_ACL);
782 #endif
783         /* parse mount options */
784         err = parse_options(sb, (char *)data);
785         if (err)
786                 goto free_sb_buf;
787
788         sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
789         sb->s_max_links = F2FS_LINK_MAX;
790         get_random_bytes(&sbi->s_next_generation, sizeof(u32));
791
792         sb->s_op = &f2fs_sops;
793         sb->s_xattr = f2fs_xattr_handlers;
794         sb->s_export_op = &f2fs_export_ops;
795         sb->s_magic = F2FS_SUPER_MAGIC;
796         sb->s_time_gran = 1;
797         sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
798                 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
799         memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
800
801         /* init f2fs-specific super block info */
802         sbi->sb = sb;
803         sbi->raw_super = raw_super;
804         sbi->raw_super_buf = raw_super_buf;
805         mutex_init(&sbi->gc_mutex);
806         mutex_init(&sbi->writepages);
807         mutex_init(&sbi->cp_mutex);
808         for (i = 0; i < NR_GLOBAL_LOCKS; i++)
809                 mutex_init(&sbi->fs_lock[i]);
810         mutex_init(&sbi->node_write);
811         sbi->por_doing = 0;
812         spin_lock_init(&sbi->stat_lock);
813         init_rwsem(&sbi->bio_sem);
814         init_sb_info(sbi);
815
816         /* get an inode for meta space */
817         sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
818         if (IS_ERR(sbi->meta_inode)) {
819                 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
820                 err = PTR_ERR(sbi->meta_inode);
821                 goto free_sb_buf;
822         }
823
824         err = get_valid_checkpoint(sbi);
825         if (err) {
826                 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
827                 goto free_meta_inode;
828         }
829
830         /* sanity checking of checkpoint */
831         err = -EINVAL;
832         if (sanity_check_ckpt(sbi)) {
833                 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
834                 goto free_cp;
835         }
836
837         sbi->total_valid_node_count =
838                                 le32_to_cpu(sbi->ckpt->valid_node_count);
839         sbi->total_valid_inode_count =
840                                 le32_to_cpu(sbi->ckpt->valid_inode_count);
841         sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
842         sbi->total_valid_block_count =
843                                 le64_to_cpu(sbi->ckpt->valid_block_count);
844         sbi->last_valid_block_count = sbi->total_valid_block_count;
845         sbi->alloc_valid_block_count = 0;
846         INIT_LIST_HEAD(&sbi->dir_inode_list);
847         spin_lock_init(&sbi->dir_inode_lock);
848
849         init_orphan_info(sbi);
850
851         /* setup f2fs internal modules */
852         err = build_segment_manager(sbi);
853         if (err) {
854                 f2fs_msg(sb, KERN_ERR,
855                         "Failed to initialize F2FS segment manager");
856                 goto free_sm;
857         }
858         err = build_node_manager(sbi);
859         if (err) {
860                 f2fs_msg(sb, KERN_ERR,
861                         "Failed to initialize F2FS node manager");
862                 goto free_nm;
863         }
864
865         build_gc_manager(sbi);
866
867         /* get an inode for node space */
868         sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
869         if (IS_ERR(sbi->node_inode)) {
870                 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
871                 err = PTR_ERR(sbi->node_inode);
872                 goto free_nm;
873         }
874
875         /* if there are nt orphan nodes free them */
876         err = -EINVAL;
877         if (recover_orphan_inodes(sbi))
878                 goto free_node_inode;
879
880         /* read root inode and dentry */
881         root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
882         if (IS_ERR(root)) {
883                 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
884                 err = PTR_ERR(root);
885                 goto free_node_inode;
886         }
887         if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
888                 goto free_root_inode;
889
890         sb->s_root = d_make_root(root); /* allocate root dentry */
891         if (!sb->s_root) {
892                 err = -ENOMEM;
893                 goto free_root_inode;
894         }
895
896         /* recover fsynced data */
897         if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
898                 err = recover_fsync_data(sbi);
899                 if (err)
900                         f2fs_msg(sb, KERN_ERR,
901                                 "Cannot recover all fsync data errno=%ld", err);
902         }
903
904         /*
905          * If filesystem is not mounted as read-only then
906          * do start the gc_thread.
907          */
908         if (!(sb->s_flags & MS_RDONLY)) {
909                 /* After POR, we can run background GC thread.*/
910                 err = start_gc_thread(sbi);
911                 if (err)
912                         goto fail;
913         }
914
915         err = f2fs_build_stats(sbi);
916         if (err)
917                 goto fail;
918
919         if (f2fs_proc_root)
920                 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
921
922         if (sbi->s_proc)
923                 proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
924                                  &f2fs_seq_segment_info_fops, sb);
925
926         if (test_opt(sbi, DISCARD)) {
927                 struct request_queue *q = bdev_get_queue(sb->s_bdev);
928                 if (!blk_queue_discard(q))
929                         f2fs_msg(sb, KERN_WARNING,
930                                         "mounting with \"discard\" option, but "
931                                         "the device does not support discard");
932         }
933
934         sbi->s_kobj.kset = f2fs_kset;
935         init_completion(&sbi->s_kobj_unregister);
936         err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
937                                                         "%s", sb->s_id);
938         if (err)
939                 goto fail;
940
941         return 0;
942 fail:
943         stop_gc_thread(sbi);
944 free_root_inode:
945         dput(sb->s_root);
946         sb->s_root = NULL;
947 free_node_inode:
948         iput(sbi->node_inode);
949 free_nm:
950         destroy_node_manager(sbi);
951 free_sm:
952         destroy_segment_manager(sbi);
953 free_cp:
954         kfree(sbi->ckpt);
955 free_meta_inode:
956         make_bad_inode(sbi->meta_inode);
957         iput(sbi->meta_inode);
958 free_sb_buf:
959         brelse(raw_super_buf);
960 free_sbi:
961         kfree(sbi);
962         return err;
963 }
964
965 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
966                         const char *dev_name, void *data)
967 {
968         return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
969 }
970
971 static struct file_system_type f2fs_fs_type = {
972         .owner          = THIS_MODULE,
973         .name           = "f2fs",
974         .mount          = f2fs_mount,
975         .kill_sb        = kill_block_super,
976         .fs_flags       = FS_REQUIRES_DEV,
977 };
978 MODULE_ALIAS_FS("f2fs");
979
980 static int __init init_inodecache(void)
981 {
982         f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
983                         sizeof(struct f2fs_inode_info), NULL);
984         if (f2fs_inode_cachep == NULL)
985                 return -ENOMEM;
986         return 0;
987 }
988
989 static void destroy_inodecache(void)
990 {
991         /*
992          * Make sure all delayed rcu free inodes are flushed before we
993          * destroy cache.
994          */
995         rcu_barrier();
996         kmem_cache_destroy(f2fs_inode_cachep);
997 }
998
999 static int __init init_f2fs_fs(void)
1000 {
1001         int err;
1002
1003         err = init_inodecache();
1004         if (err)
1005                 goto fail;
1006         err = create_node_manager_caches();
1007         if (err)
1008                 goto fail;
1009         err = create_gc_caches();
1010         if (err)
1011                 goto fail;
1012         err = create_checkpoint_caches();
1013         if (err)
1014                 goto fail;
1015         f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
1016         if (!f2fs_kset)
1017                 goto fail;
1018         err = register_filesystem(&f2fs_fs_type);
1019         if (err)
1020                 goto fail;
1021         f2fs_create_root_stats();
1022         f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1023 fail:
1024         return err;
1025 }
1026
1027 static void __exit exit_f2fs_fs(void)
1028 {
1029         remove_proc_entry("fs/f2fs", NULL);
1030         f2fs_destroy_root_stats();
1031         unregister_filesystem(&f2fs_fs_type);
1032         destroy_checkpoint_caches();
1033         destroy_gc_caches();
1034         destroy_node_manager_caches();
1035         destroy_inodecache();
1036         kset_unregister(f2fs_kset);
1037 }
1038
1039 module_init(init_f2fs_fs)
1040 module_exit(exit_f2fs_fs)
1041
1042 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
1043 MODULE_DESCRIPTION("Flash Friendly File System");
1044 MODULE_LICENSE("GPL");