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