blob: f44a09375e641b4ec09556401886ff2cef42be72 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002 * 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>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090015#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>
Jaegeuk Kim5e176d52013-06-28 12:47:01 +090021#include <linux/proc_fs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090022#include <linux/random.h>
23#include <linux/exportfs.h>
Namjae Jeond3ee4562013-03-17 17:26:14 +090024#include <linux/blkdev.h>
Chao Yu0abd6752017-07-09 00:13:07 +080025#include <linux/quotaops.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090026#include <linux/f2fs_fs.h>
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090027#include <linux/sysfs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090028
29#include "f2fs.h"
30#include "node.h"
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +090031#include "segment.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090032#include "xattr.h"
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090033#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080034#include "trace.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090035
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090036#define CREATE_TRACE_POINTS
37#include <trace/events/f2fs.h>
38
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090039static struct kmem_cache *f2fs_inode_cachep;
40
Jaegeuk Kim73faec42016-04-29 15:34:32 -070041#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070042
43char *fault_name[FAULT_MAX] = {
44 [FAULT_KMALLOC] = "kmalloc",
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -070045 [FAULT_PAGE_ALLOC] = "page alloc",
Jaegeuk Kimcb789422016-04-29 16:29:22 -070046 [FAULT_ALLOC_NID] = "alloc nid",
47 [FAULT_ORPHAN] = "orphan",
48 [FAULT_BLOCK] = "no more block",
49 [FAULT_DIR_DEPTH] = "too big dir depth",
Jaegeuk Kim53aa6bb2016-05-25 15:24:18 -070050 [FAULT_EVICT_INODE] = "evict_inode fail",
Jaegeuk Kim14b44d22017-03-09 15:24:24 -080051 [FAULT_TRUNCATE] = "truncate fail",
Chao Yu8b038c72016-09-18 23:30:07 +080052 [FAULT_IO] = "IO error",
Chao Yu0f348022016-09-26 19:45:55 +080053 [FAULT_CHECKPOINT] = "checkpoint error",
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070054};
Sheng Yong08796892016-05-16 12:38:50 +080055
Chao Yu1ecc0c52016-09-23 21:30:09 +080056static void f2fs_build_fault_attr(struct f2fs_sb_info *sbi,
57 unsigned int rate)
Sheng Yong08796892016-05-16 12:38:50 +080058{
Chao Yu1ecc0c52016-09-23 21:30:09 +080059 struct f2fs_fault_info *ffi = &sbi->fault_info;
60
Sheng Yong08796892016-05-16 12:38:50 +080061 if (rate) {
Chao Yu1ecc0c52016-09-23 21:30:09 +080062 atomic_set(&ffi->inject_ops, 0);
63 ffi->inject_rate = rate;
64 ffi->inject_type = (1 << FAULT_MAX) - 1;
Sheng Yong08796892016-05-16 12:38:50 +080065 } else {
Chao Yu1ecc0c52016-09-23 21:30:09 +080066 memset(ffi, 0, sizeof(struct f2fs_fault_info));
Sheng Yong08796892016-05-16 12:38:50 +080067 }
68}
Jaegeuk Kim73faec42016-04-29 15:34:32 -070069#endif
70
Jaegeuk Kim2658e502015-06-19 12:01:21 -070071/* f2fs-wide shrinker description */
72static struct shrinker f2fs_shrinker_info = {
73 .scan_objects = f2fs_shrink_scan,
74 .count_objects = f2fs_shrink_count,
75 .seeks = DEFAULT_SEEKS,
76};
77
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090078enum {
Namjae Jeon696c0182013-06-16 09:48:48 +090079 Opt_gc_background,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090080 Opt_disable_roll_forward,
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -080081 Opt_norecovery,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090082 Opt_discard,
Chao Yu64058be2016-07-03 22:05:14 +080083 Opt_nodiscard,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090084 Opt_noheap,
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -040085 Opt_heap,
Kelly Anderson4058c512013-10-07 11:36:20 +090086 Opt_user_xattr,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090087 Opt_nouser_xattr,
Kelly Anderson4058c512013-10-07 11:36:20 +090088 Opt_acl,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090089 Opt_noacl,
90 Opt_active_logs,
91 Opt_disable_ext_identify,
Jaegeuk Kim444c5802013-08-08 15:16:22 +090092 Opt_inline_xattr,
Chao Yu23cf7212017-02-15 10:34:45 +080093 Opt_noinline_xattr,
Huajun Li8274de72013-11-10 23:13:17 +080094 Opt_inline_data,
Chao Yu5efd3c62014-09-24 18:16:13 +080095 Opt_inline_dentry,
Chao Yu97c17942016-05-09 19:56:34 +080096 Opt_noinline_dentry,
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +090097 Opt_flush_merge,
Jaegeuk Kim69e9e422016-05-20 22:39:20 -070098 Opt_noflush_merge,
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -070099 Opt_nobarrier,
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700100 Opt_fastboot,
Chao Yu89672152015-02-05 17:55:51 +0800101 Opt_extent_cache,
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700102 Opt_noextent_cache,
Wanpeng Li75342792015-03-24 10:20:27 +0800103 Opt_noinline_data,
Chao Yu343f40f2015-12-16 13:12:16 +0800104 Opt_data_flush,
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700105 Opt_mode,
Jaegeuk Kimec915382016-12-21 17:09:19 -0800106 Opt_io_size_bits,
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700107 Opt_fault_injection,
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700108 Opt_lazytime,
109 Opt_nolazytime,
Chao Yu0abd6752017-07-09 00:13:07 +0800110 Opt_usrquota,
111 Opt_grpquota,
Chao Yu5c571322017-07-26 00:01:41 +0800112 Opt_prjquota,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900113 Opt_err,
114};
115
116static match_table_t f2fs_tokens = {
Namjae Jeon696c0182013-06-16 09:48:48 +0900117 {Opt_gc_background, "background_gc=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900118 {Opt_disable_roll_forward, "disable_roll_forward"},
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800119 {Opt_norecovery, "norecovery"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900120 {Opt_discard, "discard"},
Chao Yu64058be2016-07-03 22:05:14 +0800121 {Opt_nodiscard, "nodiscard"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900122 {Opt_noheap, "no_heap"},
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400123 {Opt_heap, "heap"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900124 {Opt_user_xattr, "user_xattr"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900125 {Opt_nouser_xattr, "nouser_xattr"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900126 {Opt_acl, "acl"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900127 {Opt_noacl, "noacl"},
128 {Opt_active_logs, "active_logs=%u"},
129 {Opt_disable_ext_identify, "disable_ext_identify"},
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900130 {Opt_inline_xattr, "inline_xattr"},
Chao Yu23cf7212017-02-15 10:34:45 +0800131 {Opt_noinline_xattr, "noinline_xattr"},
Huajun Li8274de72013-11-10 23:13:17 +0800132 {Opt_inline_data, "inline_data"},
Chao Yu5efd3c62014-09-24 18:16:13 +0800133 {Opt_inline_dentry, "inline_dentry"},
Chao Yu97c17942016-05-09 19:56:34 +0800134 {Opt_noinline_dentry, "noinline_dentry"},
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900135 {Opt_flush_merge, "flush_merge"},
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700136 {Opt_noflush_merge, "noflush_merge"},
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700137 {Opt_nobarrier, "nobarrier"},
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700138 {Opt_fastboot, "fastboot"},
Chao Yu89672152015-02-05 17:55:51 +0800139 {Opt_extent_cache, "extent_cache"},
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700140 {Opt_noextent_cache, "noextent_cache"},
Wanpeng Li75342792015-03-24 10:20:27 +0800141 {Opt_noinline_data, "noinline_data"},
Chao Yu343f40f2015-12-16 13:12:16 +0800142 {Opt_data_flush, "data_flush"},
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700143 {Opt_mode, "mode=%s"},
Jaegeuk Kimec915382016-12-21 17:09:19 -0800144 {Opt_io_size_bits, "io_bits=%u"},
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700145 {Opt_fault_injection, "fault_injection=%u"},
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700146 {Opt_lazytime, "lazytime"},
147 {Opt_nolazytime, "nolazytime"},
Chao Yu0abd6752017-07-09 00:13:07 +0800148 {Opt_usrquota, "usrquota"},
149 {Opt_grpquota, "grpquota"},
Chao Yu5c571322017-07-26 00:01:41 +0800150 {Opt_prjquota, "prjquota"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900151 {Opt_err, NULL},
152};
153
Namjae Jeona07ef782012-12-30 14:52:05 +0900154void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
155{
156 struct va_format vaf;
157 va_list args;
158
159 va_start(args, fmt);
160 vaf.fmt = fmt;
161 vaf.va = &args;
162 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
163 va_end(args);
164}
165
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900166static void init_once(void *foo)
167{
168 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
169
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900170 inode_init_once(&fi->vfs_inode);
171}
172
Namjae Jeon696c0182013-06-16 09:48:48 +0900173static int parse_options(struct super_block *sb, char *options)
174{
175 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yu09d54cd2015-06-08 13:20:10 +0800176 struct request_queue *q;
Namjae Jeon696c0182013-06-16 09:48:48 +0900177 substring_t args[MAX_OPT_ARGS];
178 char *p, *name;
179 int arg = 0;
180
181 if (!options)
182 return 0;
183
184 while ((p = strsep(&options, ",")) != NULL) {
185 int token;
186 if (!*p)
187 continue;
188 /*
189 * Initialize args struct so we know whether arg was
190 * found; some options take optional arguments.
191 */
192 args[0].to = args[0].from = NULL;
193 token = match_token(p, f2fs_tokens, args);
194
195 switch (token) {
196 case Opt_gc_background:
197 name = match_strdup(&args[0]);
198
199 if (!name)
200 return -ENOMEM;
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700201 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900202 set_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700203 clear_opt(sbi, FORCE_FG_GC);
204 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900205 clear_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700206 clear_opt(sbi, FORCE_FG_GC);
207 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
208 set_opt(sbi, BG_GC);
209 set_opt(sbi, FORCE_FG_GC);
210 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +0900211 kfree(name);
212 return -EINVAL;
213 }
214 kfree(name);
215 break;
216 case Opt_disable_roll_forward:
217 set_opt(sbi, DISABLE_ROLL_FORWARD);
218 break;
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800219 case Opt_norecovery:
220 /* this option mounts f2fs with ro */
221 set_opt(sbi, DISABLE_ROLL_FORWARD);
222 if (!f2fs_readonly(sb))
223 return -EINVAL;
224 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900225 case Opt_discard:
Chao Yu09d54cd2015-06-08 13:20:10 +0800226 q = bdev_get_queue(sb->s_bdev);
227 if (blk_queue_discard(q)) {
228 set_opt(sbi, DISCARD);
Damien Le Moal0ab02992016-10-28 17:45:02 +0900229 } else if (!f2fs_sb_mounted_blkzoned(sb)) {
Chao Yu09d54cd2015-06-08 13:20:10 +0800230 f2fs_msg(sb, KERN_WARNING,
231 "mounting with \"discard\" option, but "
232 "the device does not support discard");
233 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900234 break;
Chao Yu64058be2016-07-03 22:05:14 +0800235 case Opt_nodiscard:
Damien Le Moal96ba2de2016-10-28 17:45:03 +0900236 if (f2fs_sb_mounted_blkzoned(sb)) {
237 f2fs_msg(sb, KERN_WARNING,
238 "discard is required for zoned block devices");
239 return -EINVAL;
240 }
Chao Yu64058be2016-07-03 22:05:14 +0800241 clear_opt(sbi, DISCARD);
Damien Le Moal487df612016-10-28 17:44:59 +0900242 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900243 case Opt_noheap:
244 set_opt(sbi, NOHEAP);
245 break;
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400246 case Opt_heap:
247 clear_opt(sbi, NOHEAP);
248 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900249#ifdef CONFIG_F2FS_FS_XATTR
Kelly Anderson4058c512013-10-07 11:36:20 +0900250 case Opt_user_xattr:
251 set_opt(sbi, XATTR_USER);
252 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900253 case Opt_nouser_xattr:
254 clear_opt(sbi, XATTR_USER);
255 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900256 case Opt_inline_xattr:
257 set_opt(sbi, INLINE_XATTR);
258 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800259 case Opt_noinline_xattr:
260 clear_opt(sbi, INLINE_XATTR);
261 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900262#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900263 case Opt_user_xattr:
264 f2fs_msg(sb, KERN_INFO,
265 "user_xattr options not supported");
266 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900267 case Opt_nouser_xattr:
268 f2fs_msg(sb, KERN_INFO,
269 "nouser_xattr options not supported");
270 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900271 case Opt_inline_xattr:
272 f2fs_msg(sb, KERN_INFO,
273 "inline_xattr options not supported");
274 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800275 case Opt_noinline_xattr:
276 f2fs_msg(sb, KERN_INFO,
277 "noinline_xattr options not supported");
278 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900279#endif
280#ifdef CONFIG_F2FS_FS_POSIX_ACL
Kelly Anderson4058c512013-10-07 11:36:20 +0900281 case Opt_acl:
282 set_opt(sbi, POSIX_ACL);
283 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900284 case Opt_noacl:
285 clear_opt(sbi, POSIX_ACL);
286 break;
287#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900288 case Opt_acl:
289 f2fs_msg(sb, KERN_INFO, "acl options not supported");
290 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900291 case Opt_noacl:
292 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
293 break;
294#endif
295 case Opt_active_logs:
296 if (args->from && match_int(args, &arg))
297 return -EINVAL;
298 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
299 return -EINVAL;
300 sbi->active_logs = arg;
301 break;
302 case Opt_disable_ext_identify:
303 set_opt(sbi, DISABLE_EXT_IDENTIFY);
304 break;
Huajun Li8274de72013-11-10 23:13:17 +0800305 case Opt_inline_data:
306 set_opt(sbi, INLINE_DATA);
307 break;
Chao Yu5efd3c62014-09-24 18:16:13 +0800308 case Opt_inline_dentry:
309 set_opt(sbi, INLINE_DENTRY);
310 break;
Chao Yu97c17942016-05-09 19:56:34 +0800311 case Opt_noinline_dentry:
312 clear_opt(sbi, INLINE_DENTRY);
313 break;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900314 case Opt_flush_merge:
315 set_opt(sbi, FLUSH_MERGE);
316 break;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700317 case Opt_noflush_merge:
318 clear_opt(sbi, FLUSH_MERGE);
319 break;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700320 case Opt_nobarrier:
321 set_opt(sbi, NOBARRIER);
322 break;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700323 case Opt_fastboot:
324 set_opt(sbi, FASTBOOT);
325 break;
Chao Yu89672152015-02-05 17:55:51 +0800326 case Opt_extent_cache:
327 set_opt(sbi, EXTENT_CACHE);
328 break;
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700329 case Opt_noextent_cache:
330 clear_opt(sbi, EXTENT_CACHE);
331 break;
Wanpeng Li75342792015-03-24 10:20:27 +0800332 case Opt_noinline_data:
333 clear_opt(sbi, INLINE_DATA);
334 break;
Chao Yu343f40f2015-12-16 13:12:16 +0800335 case Opt_data_flush:
336 set_opt(sbi, DATA_FLUSH);
337 break;
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700338 case Opt_mode:
339 name = match_strdup(&args[0]);
340
341 if (!name)
342 return -ENOMEM;
343 if (strlen(name) == 8 &&
344 !strncmp(name, "adaptive", 8)) {
Damien Le Moal3adc57e2016-10-28 17:45:04 +0900345 if (f2fs_sb_mounted_blkzoned(sb)) {
346 f2fs_msg(sb, KERN_WARNING,
347 "adaptive mode is not allowed with "
348 "zoned block device feature");
349 kfree(name);
350 return -EINVAL;
351 }
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700352 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700353 } else if (strlen(name) == 3 &&
354 !strncmp(name, "lfs", 3)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700355 set_opt_mode(sbi, F2FS_MOUNT_LFS);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700356 } else {
357 kfree(name);
358 return -EINVAL;
359 }
360 kfree(name);
361 break;
Jaegeuk Kimec915382016-12-21 17:09:19 -0800362 case Opt_io_size_bits:
363 if (args->from && match_int(args, &arg))
364 return -EINVAL;
365 if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
366 f2fs_msg(sb, KERN_WARNING,
367 "Not support %d, larger than %d",
368 1 << arg, BIO_MAX_PAGES);
369 return -EINVAL;
370 }
371 sbi->write_io_size_bits = arg;
372 break;
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700373 case Opt_fault_injection:
374 if (args->from && match_int(args, &arg))
375 return -EINVAL;
376#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu1ecc0c52016-09-23 21:30:09 +0800377 f2fs_build_fault_attr(sbi, arg);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800378 set_opt(sbi, FAULT_INJECTION);
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700379#else
380 f2fs_msg(sb, KERN_INFO,
381 "FAULT_INJECTION was not selected");
382#endif
383 break;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700384 case Opt_lazytime:
385 sb->s_flags |= MS_LAZYTIME;
386 break;
387 case Opt_nolazytime:
388 sb->s_flags &= ~MS_LAZYTIME;
389 break;
Chao Yu0abd6752017-07-09 00:13:07 +0800390#ifdef CONFIG_QUOTA
391 case Opt_usrquota:
392 set_opt(sbi, USRQUOTA);
393 break;
394 case Opt_grpquota:
395 set_opt(sbi, GRPQUOTA);
396 break;
Chao Yu5c571322017-07-26 00:01:41 +0800397 case Opt_prjquota:
398 set_opt(sbi, PRJQUOTA);
399 break;
Chao Yu0abd6752017-07-09 00:13:07 +0800400#else
401 case Opt_usrquota:
402 case Opt_grpquota:
Chao Yu5c571322017-07-26 00:01:41 +0800403 case Opt_prjquota:
Chao Yu0abd6752017-07-09 00:13:07 +0800404 f2fs_msg(sb, KERN_INFO,
405 "quota operations not supported");
406 break;
407#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900408 default:
409 f2fs_msg(sb, KERN_ERR,
410 "Unrecognized mount option \"%s\" or missing value",
411 p);
412 return -EINVAL;
413 }
414 }
Jaegeuk Kimec915382016-12-21 17:09:19 -0800415
416 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
417 f2fs_msg(sb, KERN_ERR,
418 "Should set mode=lfs with %uKB-sized IO",
419 F2FS_IO_SIZE_KB(sbi));
420 return -EINVAL;
421 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900422 return 0;
423}
424
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900425static struct inode *f2fs_alloc_inode(struct super_block *sb)
426{
427 struct f2fs_inode_info *fi;
428
Chao Yua0acdfe2013-12-05 09:54:00 +0800429 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900430 if (!fi)
431 return NULL;
432
433 init_once((void *) fi);
434
Masanari Iida434720f2013-03-19 08:03:35 +0900435 /* Initialize f2fs-specific inode info */
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900436 fi->vfs_inode.i_version = 1;
Jaegeuk Kim204706c2016-12-02 15:11:32 -0800437 atomic_set(&fi->dirty_pages, 0);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900438 fi->i_current_depth = 1;
439 fi->i_advise = 0;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900440 init_rwsem(&fi->i_sem);
Chao Yu2710fd72015-12-15 13:30:45 +0800441 INIT_LIST_HEAD(&fi->dirty_list);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700442 INIT_LIST_HEAD(&fi->gdirty_list);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700443 INIT_LIST_HEAD(&fi->inmem_pages);
444 mutex_init(&fi->inmem_lock);
Chao Yu82e0a5a2016-07-13 09:18:29 +0800445 init_rwsem(&fi->dio_rwsem[READ]);
446 init_rwsem(&fi->dio_rwsem[WRITE]);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800447 init_rwsem(&fi->i_mmap_sem);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900448
Chao Yu0abd6752017-07-09 00:13:07 +0800449#ifdef CONFIG_QUOTA
450 memset(&fi->i_dquot, 0, sizeof(fi->i_dquot));
451 fi->i_reserved_quota = 0;
452#endif
Jaegeuk Kimab9fa662014-02-27 20:09:05 +0900453 /* Will be used by directory only */
454 fi->i_dir_level = F2FS_SB(sb)->dir_level;
Chao Yuf2470372017-07-19 00:19:05 +0800455
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900456 return &fi->vfs_inode;
457}
458
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900459static int f2fs_drop_inode(struct inode *inode)
460{
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000461 int ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900462 /*
463 * This is to avoid a deadlock condition like below.
464 * writeback_single_inode(inode)
465 * - f2fs_write_data_page
466 * - f2fs_gc -> iput -> evict
467 * - inode_wait_for_writeback(inode)
468 */
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700469 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700470 if (!inode->i_nlink && !is_bad_inode(inode)) {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700471 /* to avoid evict_inode call simultaneously */
472 atomic_inc(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700473 spin_unlock(&inode->i_lock);
474
475 /* some remained atomic pages should discarded */
476 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +0800477 drop_inmem_pages(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700478
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700479 /* should remain fi->extent_tree for writepage */
480 f2fs_destroy_extent_node(inode);
481
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700482 sb_start_intwrite(inode->i_sb);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700483 f2fs_i_size_write(inode, 0);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700484
485 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700486 f2fs_truncate(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700487
488 sb_end_intwrite(inode->i_sb);
489
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700490 fscrypt_put_encryption_info(inode, NULL);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700491 spin_lock(&inode->i_lock);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700492 atomic_dec(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700493 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000494 trace_f2fs_drop_inode(inode, 0);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900495 return 0;
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700496 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000497 ret = generic_drop_inode(inode);
498 trace_f2fs_drop_inode(inode, ret);
499 return ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900500}
501
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700502int f2fs_inode_dirtied(struct inode *inode, bool sync)
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700503{
504 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700505 int ret = 0;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700506
507 spin_lock(&sbi->inode_lock[DIRTY_META]);
508 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700509 ret = 1;
510 } else {
511 set_inode_flag(inode, FI_DIRTY_INODE);
512 stat_inc_dirty_inode(sbi, DIRTY_META);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700513 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700514 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
515 list_add_tail(&F2FS_I(inode)->gdirty_list,
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700516 &sbi->inode_list[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700517 inc_page_count(sbi, F2FS_DIRTY_IMETA);
518 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700519 spin_unlock(&sbi->inode_lock[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700520 return ret;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700521}
522
523void f2fs_inode_synced(struct inode *inode)
524{
525 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
526
527 spin_lock(&sbi->inode_lock[DIRTY_META]);
528 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
529 spin_unlock(&sbi->inode_lock[DIRTY_META]);
530 return;
531 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700532 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
533 list_del_init(&F2FS_I(inode)->gdirty_list);
534 dec_page_count(sbi, F2FS_DIRTY_IMETA);
535 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700536 clear_inode_flag(inode, FI_DIRTY_INODE);
537 clear_inode_flag(inode, FI_AUTO_RECOVER);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700538 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
539 spin_unlock(&sbi->inode_lock[DIRTY_META]);
540}
541
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900542/*
543 * f2fs_dirty_inode() is called from __mark_inode_dirty()
544 *
545 * We should call set_dirty_inode to write the dirty inode through write_inode.
546 */
547static void f2fs_dirty_inode(struct inode *inode, int flags)
548{
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700549 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
550
551 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
552 inode->i_ino == F2FS_META_INO(sbi))
553 return;
554
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700555 if (flags == I_DIRTY_TIME)
556 return;
557
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700558 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
559 clear_inode_flag(inode, FI_AUTO_RECOVER);
560
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700561 f2fs_inode_dirtied(inode, false);
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900562}
563
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900564static void f2fs_i_callback(struct rcu_head *head)
565{
566 struct inode *inode = container_of(head, struct inode, i_rcu);
567 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
568}
569
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900570static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900571{
572 call_rcu(&inode->i_rcu, f2fs_i_callback);
573}
574
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700575static void destroy_percpu_info(struct f2fs_sb_info *sbi)
576{
Jaegeuk Kim41382ec2016-05-16 11:06:50 -0700577 percpu_counter_destroy(&sbi->alloc_valid_block_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -0700578 percpu_counter_destroy(&sbi->total_valid_inode_count);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700579}
580
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700581static void destroy_device_list(struct f2fs_sb_info *sbi)
582{
583 int i;
584
585 for (i = 0; i < sbi->s_ndevs; i++) {
586 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
587#ifdef CONFIG_BLK_DEV_ZONED
588 kfree(FDEV(i).blkz_type);
589#endif
590 }
591 kfree(sbi->devs);
592}
593
Chao Yu0abd6752017-07-09 00:13:07 +0800594static void f2fs_quota_off_umount(struct super_block *sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900595static void f2fs_put_super(struct super_block *sb)
596{
597 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yua3981012017-06-14 17:39:46 +0800598 int i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900599
Chao Yu0abd6752017-07-09 00:13:07 +0800600 f2fs_quota_off_umount(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900601
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700602 /* prevent remaining shrinker jobs */
603 mutex_lock(&sbi->umount_mutex);
604
Jaegeuk Kim85dc2f22015-01-14 17:41:41 -0800605 /*
606 * We don't need to do checkpoint when superblock is clean.
607 * But, the previous checkpoint was not done by umount, it needs to do
608 * clean checkpoint again.
609 */
Chao Yucaf00472015-01-28 17:48:42 +0800610 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
Chao Yuaaec2b12016-09-20 11:04:18 +0800611 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700612 struct cp_control cpc = {
613 .reason = CP_UMOUNT,
614 };
615 write_checkpoint(sbi, &cpc);
616 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900617
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -0800618 /* be sure to wait for any on-going discard commands */
Chao Yud4314132017-04-05 18:19:49 +0800619 f2fs_wait_discard_bios(sbi);
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -0800620
Chao Yu663f3872017-06-14 23:00:55 +0800621 if (f2fs_discard_en(sbi) && !sbi->discard_blks) {
Chao Yu1f43e2a2017-04-28 13:56:08 +0800622 struct cp_control cpc = {
623 .reason = CP_UMOUNT | CP_TRIMMED,
624 };
625 write_checkpoint(sbi, &cpc);
626 }
627
Jaegeuk Kimeca616f2015-06-15 14:52:29 -0700628 /* write_checkpoint can update stat informaion */
629 f2fs_destroy_stats(sbi);
630
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700631 /*
632 * normally superblock is clean, so we need to release this.
633 * In addition, EIO will skip do checkpoint, we need this as well.
634 */
Jaegeuk Kim74ef9242016-05-02 22:09:56 -0700635 release_ino_entry(sbi, true);
Jaegeuk Kim6f12ac22014-08-19 09:48:22 -0700636
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700637 f2fs_leave_shrinker(sbi);
638 mutex_unlock(&sbi->umount_mutex);
639
Jaegeuk Kim17c19122016-01-29 08:57:59 -0800640 /* our cp_error case, we can wait for any writeback page */
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700641 f2fs_flush_merged_writes(sbi);
Jaegeuk Kim17c19122016-01-29 08:57:59 -0800642
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900643 iput(sbi->node_inode);
644 iput(sbi->meta_inode);
645
646 /* destroy f2fs internal modules */
647 destroy_node_manager(sbi);
648 destroy_segment_manager(sbi);
649
650 kfree(sbi->ckpt);
Chao Yua3981012017-06-14 17:39:46 +0800651
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700652 f2fs_unregister_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900653
654 sb->s_fs_info = NULL;
Keith Mok43b65732016-03-02 12:04:24 -0800655 if (sbi->s_chksum_driver)
656 crypto_free_shash(sbi->s_chksum_driver);
Yunlei Heb39f0de2015-12-15 17:17:20 +0800657 kfree(sbi->raw_super);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700658
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700659 destroy_device_list(sbi);
Chao Yub6895e82017-02-27 18:43:12 +0800660 mempool_destroy(sbi->write_io_dummy);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700661 destroy_percpu_info(sbi);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700662 for (i = 0; i < NR_PAGE_TYPE; i++)
663 kfree(sbi->write_io[i]);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900664 kfree(sbi);
665}
666
667int f2fs_sync_fs(struct super_block *sb, int sync)
668{
669 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yuc34f42e2015-12-23 17:50:30 +0800670 int err = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900671
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900672 trace_f2fs_sync_fs(sb, sync);
673
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900674 if (sync) {
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700675 struct cp_control cpc;
676
Jaegeuk Kim119ee912015-01-29 11:45:33 -0800677 cpc.reason = __get_cp_reason(sbi);
678
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900679 mutex_lock(&sbi->gc_mutex);
Chao Yuc34f42e2015-12-23 17:50:30 +0800680 err = write_checkpoint(sbi, &cpc);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900681 mutex_unlock(&sbi->gc_mutex);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900682 }
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700683 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900684
Chao Yuc34f42e2015-12-23 17:50:30 +0800685 return err;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900686}
687
Changman Leed6212a52013-01-29 18:30:07 +0900688static int f2fs_freeze(struct super_block *sb)
689{
Jaegeuk Kim77888c12013-05-20 20:28:47 +0900690 if (f2fs_readonly(sb))
Changman Leed6212a52013-01-29 18:30:07 +0900691 return 0;
692
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -0700693 /* IO error happened before */
694 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
695 return -EIO;
696
697 /* must be clean, since sync_filesystem() was already called */
698 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
699 return -EINVAL;
700 return 0;
Changman Leed6212a52013-01-29 18:30:07 +0900701}
702
703static int f2fs_unfreeze(struct super_block *sb)
704{
705 return 0;
706}
707
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900708static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
709{
710 struct super_block *sb = dentry->d_sb;
711 struct f2fs_sb_info *sbi = F2FS_SB(sb);
712 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
713 block_t total_count, user_block_count, start_count, ovp_count;
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -0700714 u64 avail_node_count;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900715
716 total_count = le64_to_cpu(sbi->raw_super->block_count);
717 user_block_count = sbi->user_block_count;
718 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
719 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
720 buf->f_type = F2FS_SUPER_MAGIC;
721 buf->f_bsize = sbi->blocksize;
722
723 buf->f_blocks = total_count - start_count;
Chao Yu3e6d0b42016-07-06 14:13:07 +0800724 buf->f_bfree = user_block_count - valid_user_blocks(sbi) + ovp_count;
Chao Yudaeb4332017-06-26 16:24:41 +0800725 buf->f_bavail = user_block_count - valid_user_blocks(sbi) -
726 sbi->reserved_blocks;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900727
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -0700728 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
729
730 if (avail_node_count > user_block_count) {
731 buf->f_files = user_block_count;
732 buf->f_ffree = buf->f_bavail;
733 } else {
734 buf->f_files = avail_node_count;
735 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
736 buf->f_bavail);
737 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900738
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900739 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900740 buf->f_fsid.val[0] = (u32)id;
741 buf->f_fsid.val[1] = (u32)(id >> 32);
742
743 return 0;
744}
745
746static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
747{
748 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
749
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700750 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
751 if (test_opt(sbi, FORCE_FG_GC))
752 seq_printf(seq, ",background_gc=%s", "sync");
753 else
754 seq_printf(seq, ",background_gc=%s", "on");
755 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +0900756 seq_printf(seq, ",background_gc=%s", "off");
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700757 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900758 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
759 seq_puts(seq, ",disable_roll_forward");
760 if (test_opt(sbi, DISCARD))
761 seq_puts(seq, ",discard");
762 if (test_opt(sbi, NOHEAP))
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400763 seq_puts(seq, ",no_heap");
764 else
765 seq_puts(seq, ",heap");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900766#ifdef CONFIG_F2FS_FS_XATTR
767 if (test_opt(sbi, XATTR_USER))
768 seq_puts(seq, ",user_xattr");
769 else
770 seq_puts(seq, ",nouser_xattr");
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900771 if (test_opt(sbi, INLINE_XATTR))
772 seq_puts(seq, ",inline_xattr");
Chao Yu23cf7212017-02-15 10:34:45 +0800773 else
774 seq_puts(seq, ",noinline_xattr");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900775#endif
776#ifdef CONFIG_F2FS_FS_POSIX_ACL
777 if (test_opt(sbi, POSIX_ACL))
778 seq_puts(seq, ",acl");
779 else
780 seq_puts(seq, ",noacl");
781#endif
782 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +0100783 seq_puts(seq, ",disable_ext_identify");
Huajun Li8274de72013-11-10 23:13:17 +0800784 if (test_opt(sbi, INLINE_DATA))
785 seq_puts(seq, ",inline_data");
Wanpeng Li75342792015-03-24 10:20:27 +0800786 else
787 seq_puts(seq, ",noinline_data");
Chao Yu5efd3c62014-09-24 18:16:13 +0800788 if (test_opt(sbi, INLINE_DENTRY))
789 seq_puts(seq, ",inline_dentry");
Chao Yu97c17942016-05-09 19:56:34 +0800790 else
791 seq_puts(seq, ",noinline_dentry");
Gu Zhengb270ad62014-04-11 17:49:55 +0800792 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900793 seq_puts(seq, ",flush_merge");
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700794 if (test_opt(sbi, NOBARRIER))
795 seq_puts(seq, ",nobarrier");
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700796 if (test_opt(sbi, FASTBOOT))
797 seq_puts(seq, ",fastboot");
Chao Yu89672152015-02-05 17:55:51 +0800798 if (test_opt(sbi, EXTENT_CACHE))
799 seq_puts(seq, ",extent_cache");
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700800 else
801 seq_puts(seq, ",noextent_cache");
Chao Yu343f40f2015-12-16 13:12:16 +0800802 if (test_opt(sbi, DATA_FLUSH))
803 seq_puts(seq, ",data_flush");
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700804
805 seq_puts(seq, ",mode=");
806 if (test_opt(sbi, ADAPTIVE))
807 seq_puts(seq, "adaptive");
808 else if (test_opt(sbi, LFS))
809 seq_puts(seq, "lfs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900810 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
Jaegeuk Kimec915382016-12-21 17:09:19 -0800811 if (F2FS_IO_SIZE_BITS(sbi))
812 seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800813#ifdef CONFIG_F2FS_FAULT_INJECTION
814 if (test_opt(sbi, FAULT_INJECTION))
Chao Yu44529f82017-06-12 09:44:24 +0800815 seq_printf(seq, ",fault_injection=%u",
816 sbi->fault_info.inject_rate);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800817#endif
Chao Yu0abd6752017-07-09 00:13:07 +0800818#ifdef CONFIG_QUOTA
819 if (test_opt(sbi, USRQUOTA))
820 seq_puts(seq, ",usrquota");
821 if (test_opt(sbi, GRPQUOTA))
822 seq_puts(seq, ",grpquota");
Chao Yu5c571322017-07-26 00:01:41 +0800823 if (test_opt(sbi, PRJQUOTA))
824 seq_puts(seq, ",prjquota");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900825#endif
826
827 return 0;
828}
829
Yunlei He498c5e92015-05-07 18:11:37 +0800830static void default_options(struct f2fs_sb_info *sbi)
831{
832 /* init some FS parameters */
833 sbi->active_logs = NR_CURSEG_TYPE;
834
835 set_opt(sbi, BG_GC);
Chao Yu39133a52017-02-08 17:39:44 +0800836 set_opt(sbi, INLINE_XATTR);
Yunlei He498c5e92015-05-07 18:11:37 +0800837 set_opt(sbi, INLINE_DATA);
Chao Yu97c17942016-05-09 19:56:34 +0800838 set_opt(sbi, INLINE_DENTRY);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700839 set_opt(sbi, EXTENT_CACHE);
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400840 set_opt(sbi, NOHEAP);
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700841 sbi->sb->s_flags |= MS_LAZYTIME;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700842 set_opt(sbi, FLUSH_MERGE);
Damien Le Moal0bfd7a02016-10-28 17:45:00 +0900843 if (f2fs_sb_mounted_blkzoned(sbi->sb)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700844 set_opt_mode(sbi, F2FS_MOUNT_LFS);
845 set_opt(sbi, DISCARD);
846 } else {
847 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
848 }
Yunlei He498c5e92015-05-07 18:11:37 +0800849
850#ifdef CONFIG_F2FS_FS_XATTR
851 set_opt(sbi, XATTR_USER);
852#endif
853#ifdef CONFIG_F2FS_FS_POSIX_ACL
854 set_opt(sbi, POSIX_ACL);
855#endif
Chao Yu36dbd322016-09-26 19:45:05 +0800856
857#ifdef CONFIG_F2FS_FAULT_INJECTION
858 f2fs_build_fault_attr(sbi, 0);
859#endif
Yunlei He498c5e92015-05-07 18:11:37 +0800860}
861
Namjae Jeon696c0182013-06-16 09:48:48 +0900862static int f2fs_remount(struct super_block *sb, int *flags, char *data)
863{
864 struct f2fs_sb_info *sbi = F2FS_SB(sb);
865 struct f2fs_mount_info org_mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +0800866 unsigned long old_sb_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +0900867 int err, active_logs;
Gu Zheng876dc592014-04-11 17:50:00 +0800868 bool need_restart_gc = false;
869 bool need_stop_gc = false;
Chao Yu9cd81ce2015-09-18 16:55:26 +0800870 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
Chao Yu2443b8b2016-09-26 19:45:06 +0800871#ifdef CONFIG_F2FS_FAULT_INJECTION
872 struct f2fs_fault_info ffi = sbi->fault_info;
873#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900874
875 /*
876 * Save the old mount options in case we
877 * need to restore them.
878 */
879 org_mount_opt = sbi->mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +0800880 old_sb_flags = sb->s_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +0900881 active_logs = sbi->active_logs;
882
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700883 /* recover superblocks we couldn't write due to previous RO mount */
884 if (!(*flags & MS_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
885 err = f2fs_commit_super(sbi, false);
886 f2fs_msg(sb, KERN_INFO,
887 "Try to recover all the superblocks, ret: %d", err);
888 if (!err)
889 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
890 }
891
Yunlei He498c5e92015-05-07 18:11:37 +0800892 default_options(sbi);
Chao Yu26666c82014-09-15 18:04:44 +0800893
Namjae Jeon696c0182013-06-16 09:48:48 +0900894 /* parse mount options */
895 err = parse_options(sb, data);
896 if (err)
897 goto restore_opts;
898
899 /*
900 * Previous and new state of filesystem is RO,
Gu Zheng876dc592014-04-11 17:50:00 +0800901 * so skip checking GC and FLUSH_MERGE conditions.
Namjae Jeon696c0182013-06-16 09:48:48 +0900902 */
Chao Yu6b2920a2014-07-07 11:21:59 +0800903 if (f2fs_readonly(sb) && (*flags & MS_RDONLY))
Namjae Jeon696c0182013-06-16 09:48:48 +0900904 goto skip;
905
Chao Yu0abd6752017-07-09 00:13:07 +0800906 if (!f2fs_readonly(sb) && (*flags & MS_RDONLY)) {
907 err = dquot_suspend(sb, -1);
908 if (err < 0)
909 goto restore_opts;
910 } else {
911 /* dquot_resume needs RW */
912 sb->s_flags &= ~MS_RDONLY;
913 dquot_resume(sb, -1);
914 }
915
Chao Yu9cd81ce2015-09-18 16:55:26 +0800916 /* disallow enable/disable extent_cache dynamically */
917 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
918 err = -EINVAL;
919 f2fs_msg(sbi->sb, KERN_WARNING,
920 "switch extent_cache option is not allowed");
921 goto restore_opts;
922 }
923
Namjae Jeon696c0182013-06-16 09:48:48 +0900924 /*
925 * We stop the GC thread if FS is mounted as RO
926 * or if background_gc = off is passed in mount
927 * option. Also sync the filesystem.
928 */
929 if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
930 if (sbi->gc_thread) {
931 stop_gc_thread(sbi);
Gu Zheng876dc592014-04-11 17:50:00 +0800932 need_restart_gc = true;
Namjae Jeon696c0182013-06-16 09:48:48 +0900933 }
Chao Yuaba291b2014-11-18 11:17:20 +0800934 } else if (!sbi->gc_thread) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900935 err = start_gc_thread(sbi);
936 if (err)
937 goto restore_opts;
Gu Zheng876dc592014-04-11 17:50:00 +0800938 need_stop_gc = true;
939 }
940
Jaegeuk Kimfaa0e552016-03-24 10:29:39 -0700941 if (*flags & MS_RDONLY) {
942 writeback_inodes_sb(sb, WB_REASON_SYNC);
943 sync_inodes_sb(sb);
944
945 set_sbi_flag(sbi, SBI_IS_DIRTY);
946 set_sbi_flag(sbi, SBI_IS_CLOSE);
947 f2fs_sync_fs(sb, 1);
948 clear_sbi_flag(sbi, SBI_IS_CLOSE);
949 }
950
Gu Zheng876dc592014-04-11 17:50:00 +0800951 /*
952 * We stop issue flush thread if FS is mounted as RO
953 * or if flush_merge is not passed in mount option.
954 */
955 if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800956 clear_opt(sbi, FLUSH_MERGE);
957 destroy_flush_cmd_control(sbi, false);
958 } else {
Gu Zheng2163d192014-04-27 14:21:33 +0800959 err = create_flush_cmd_control(sbi);
960 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800961 goto restore_gc;
Namjae Jeon696c0182013-06-16 09:48:48 +0900962 }
963skip:
964 /* Update the POSIXACL Flag */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700965 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Namjae Jeon696c0182013-06-16 09:48:48 +0900966 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700967
Namjae Jeon696c0182013-06-16 09:48:48 +0900968 return 0;
Gu Zheng876dc592014-04-11 17:50:00 +0800969restore_gc:
970 if (need_restart_gc) {
971 if (start_gc_thread(sbi))
972 f2fs_msg(sbi->sb, KERN_WARNING,
arter97e1c42042014-08-06 23:22:50 +0900973 "background gc thread has stopped");
Gu Zheng876dc592014-04-11 17:50:00 +0800974 } else if (need_stop_gc) {
975 stop_gc_thread(sbi);
976 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900977restore_opts:
978 sbi->mount_opt = org_mount_opt;
979 sbi->active_logs = active_logs;
Chao Yu0abd6752017-07-09 00:13:07 +0800980 sb->s_flags = old_sb_flags;
Chao Yu2443b8b2016-09-26 19:45:06 +0800981#ifdef CONFIG_F2FS_FAULT_INJECTION
982 sbi->fault_info = ffi;
983#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900984 return err;
985}
986
Chao Yu0abd6752017-07-09 00:13:07 +0800987#ifdef CONFIG_QUOTA
988/* Read data from quotafile */
989static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
990 size_t len, loff_t off)
991{
992 struct inode *inode = sb_dqopt(sb)->files[type];
993 struct address_space *mapping = inode->i_mapping;
994 block_t blkidx = F2FS_BYTES_TO_BLK(off);
995 int offset = off & (sb->s_blocksize - 1);
996 int tocopy;
997 size_t toread;
998 loff_t i_size = i_size_read(inode);
999 struct page *page;
1000 char *kaddr;
1001
1002 if (off > i_size)
1003 return 0;
1004
1005 if (off + len > i_size)
1006 len = i_size - off;
1007 toread = len;
1008 while (toread > 0) {
1009 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
1010repeat:
1011 page = read_mapping_page(mapping, blkidx, NULL);
1012 if (IS_ERR(page))
1013 return PTR_ERR(page);
1014
1015 lock_page(page);
1016
1017 if (unlikely(page->mapping != mapping)) {
1018 f2fs_put_page(page, 1);
1019 goto repeat;
1020 }
1021 if (unlikely(!PageUptodate(page))) {
1022 f2fs_put_page(page, 1);
1023 return -EIO;
1024 }
1025
1026 kaddr = kmap_atomic(page);
1027 memcpy(data, kaddr + offset, tocopy);
1028 kunmap_atomic(kaddr);
1029 f2fs_put_page(page, 1);
1030
1031 offset = 0;
1032 toread -= tocopy;
1033 data += tocopy;
1034 blkidx++;
1035 }
1036 return len;
1037}
1038
1039/* Write to quotafile */
1040static ssize_t f2fs_quota_write(struct super_block *sb, int type,
1041 const char *data, size_t len, loff_t off)
1042{
1043 struct inode *inode = sb_dqopt(sb)->files[type];
1044 struct address_space *mapping = inode->i_mapping;
1045 const struct address_space_operations *a_ops = mapping->a_ops;
1046 int offset = off & (sb->s_blocksize - 1);
1047 size_t towrite = len;
1048 struct page *page;
1049 char *kaddr;
1050 int err = 0;
1051 int tocopy;
1052
1053 while (towrite > 0) {
1054 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
1055 towrite);
1056
1057 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
1058 &page, NULL);
1059 if (unlikely(err))
1060 break;
1061
1062 kaddr = kmap_atomic(page);
1063 memcpy(kaddr + offset, data, tocopy);
1064 kunmap_atomic(kaddr);
1065 flush_dcache_page(page);
1066
1067 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
1068 page, NULL);
1069 offset = 0;
1070 towrite -= tocopy;
1071 off += tocopy;
1072 data += tocopy;
1073 cond_resched();
1074 }
1075
1076 if (len == towrite)
1077 return err;
1078 inode->i_version++;
1079 inode->i_mtime = inode->i_ctime = current_time(inode);
1080 f2fs_mark_inode_dirty_sync(inode, false);
1081 return len - towrite;
1082}
1083
1084static struct dquot **f2fs_get_dquots(struct inode *inode)
1085{
1086 return F2FS_I(inode)->i_dquot;
1087}
1088
1089static qsize_t *f2fs_get_reserved_space(struct inode *inode)
1090{
1091 return &F2FS_I(inode)->i_reserved_quota;
1092}
1093
1094static int f2fs_quota_sync(struct super_block *sb, int type)
1095{
1096 struct quota_info *dqopt = sb_dqopt(sb);
1097 int cnt;
1098 int ret;
1099
1100 ret = dquot_writeback_dquots(sb, type);
1101 if (ret)
1102 return ret;
1103
1104 /*
1105 * Now when everything is written we can discard the pagecache so
1106 * that userspace sees the changes.
1107 */
1108 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1109 if (type != -1 && cnt != type)
1110 continue;
1111 if (!sb_has_quota_active(sb, cnt))
1112 continue;
1113
1114 ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping);
1115 if (ret)
1116 return ret;
1117
1118 inode_lock(dqopt->files[cnt]);
1119 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
1120 inode_unlock(dqopt->files[cnt]);
1121 }
1122 return 0;
1123}
1124
1125static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
1126 const struct path *path)
1127{
1128 struct inode *inode;
1129 int err;
1130
1131 err = f2fs_quota_sync(sb, -1);
1132 if (err)
1133 return err;
1134
1135 err = dquot_quota_on(sb, type, format_id, path);
1136 if (err)
1137 return err;
1138
1139 inode = d_inode(path->dentry);
1140
1141 inode_lock(inode);
1142 F2FS_I(inode)->i_flags |= FS_NOATIME_FL | FS_IMMUTABLE_FL;
1143 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
1144 S_NOATIME | S_IMMUTABLE);
1145 inode_unlock(inode);
1146 f2fs_mark_inode_dirty_sync(inode, false);
1147
1148 return 0;
1149}
1150
1151static int f2fs_quota_off(struct super_block *sb, int type)
1152{
1153 struct inode *inode = sb_dqopt(sb)->files[type];
1154 int err;
1155
1156 if (!inode || !igrab(inode))
1157 return dquot_quota_off(sb, type);
1158
1159 f2fs_quota_sync(sb, -1);
1160
1161 err = dquot_quota_off(sb, type);
1162 if (err)
1163 goto out_put;
1164
1165 inode_lock(inode);
1166 F2FS_I(inode)->i_flags &= ~(FS_NOATIME_FL | FS_IMMUTABLE_FL);
1167 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
1168 inode_unlock(inode);
1169 f2fs_mark_inode_dirty_sync(inode, false);
1170out_put:
1171 iput(inode);
1172 return err;
1173}
1174
1175static void f2fs_quota_off_umount(struct super_block *sb)
1176{
1177 int type;
1178
1179 for (type = 0; type < MAXQUOTAS; type++)
1180 f2fs_quota_off(sb, type);
1181}
1182
Chao Yu5c571322017-07-26 00:01:41 +08001183int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
1184{
1185 *projid = F2FS_I(inode)->i_projid;
1186 return 0;
1187}
1188
Chao Yu0abd6752017-07-09 00:13:07 +08001189static const struct dquot_operations f2fs_quota_operations = {
1190 .get_reserved_space = f2fs_get_reserved_space,
1191 .write_dquot = dquot_commit,
1192 .acquire_dquot = dquot_acquire,
1193 .release_dquot = dquot_release,
1194 .mark_dirty = dquot_mark_dquot_dirty,
1195 .write_info = dquot_commit_info,
1196 .alloc_dquot = dquot_alloc,
1197 .destroy_dquot = dquot_destroy,
Chao Yu5c571322017-07-26 00:01:41 +08001198 .get_projid = f2fs_get_projid,
Chao Yu0abd6752017-07-09 00:13:07 +08001199 .get_next_id = dquot_get_next_id,
1200};
1201
1202static const struct quotactl_ops f2fs_quotactl_ops = {
1203 .quota_on = f2fs_quota_on,
1204 .quota_off = f2fs_quota_off,
1205 .quota_sync = f2fs_quota_sync,
1206 .get_state = dquot_get_state,
1207 .set_info = dquot_set_dqinfo,
1208 .get_dqblk = dquot_get_dqblk,
1209 .set_dqblk = dquot_set_dqblk,
1210 .get_nextdqblk = dquot_get_next_dqblk,
1211};
1212#else
1213static inline void f2fs_quota_off_umount(struct super_block *sb)
1214{
1215}
1216#endif
1217
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001218static struct super_operations f2fs_sops = {
1219 .alloc_inode = f2fs_alloc_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001220 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001221 .destroy_inode = f2fs_destroy_inode,
1222 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +09001223 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001224 .show_options = f2fs_show_options,
Chao Yu0abd6752017-07-09 00:13:07 +08001225#ifdef CONFIG_QUOTA
1226 .quota_read = f2fs_quota_read,
1227 .quota_write = f2fs_quota_write,
1228 .get_dquots = f2fs_get_dquots,
1229#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001230 .evict_inode = f2fs_evict_inode,
1231 .put_super = f2fs_put_super,
1232 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +09001233 .freeze_fs = f2fs_freeze,
1234 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001235 .statfs = f2fs_statfs,
Namjae Jeon696c0182013-06-16 09:48:48 +09001236 .remount_fs = f2fs_remount,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001237};
1238
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001239#ifdef CONFIG_F2FS_FS_ENCRYPTION
1240static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
1241{
1242 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1243 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1244 ctx, len, NULL);
1245}
1246
1247static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
1248 void *fs_data)
1249{
1250 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1251 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1252 ctx, len, fs_data, XATTR_CREATE);
1253}
1254
1255static unsigned f2fs_max_namelen(struct inode *inode)
1256{
1257 return S_ISLNK(inode->i_mode) ?
1258 inode->i_sb->s_blocksize : F2FS_NAME_LEN;
1259}
1260
Eric Biggers6f69f0e2017-02-07 12:42:10 -08001261static const struct fscrypt_operations f2fs_cryptops = {
Eric Biggersa5d431e2017-01-05 13:51:18 -08001262 .key_prefix = "f2fs:",
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001263 .get_context = f2fs_get_context,
1264 .set_context = f2fs_set_context,
1265 .is_encrypted = f2fs_encrypted_inode,
1266 .empty_dir = f2fs_empty_dir,
1267 .max_namelen = f2fs_max_namelen,
1268};
1269#else
Eric Biggers6f69f0e2017-02-07 12:42:10 -08001270static const struct fscrypt_operations f2fs_cryptops = {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001271 .is_encrypted = f2fs_encrypted_inode,
1272};
1273#endif
1274
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001275static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
1276 u64 ino, u32 generation)
1277{
1278 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1279 struct inode *inode;
1280
Chao Yud6b7d4b2014-06-12 13:23:41 +08001281 if (check_nid_range(sbi, ino))
Chao Yu910bb122014-03-12 17:08:36 +08001282 return ERR_PTR(-ESTALE);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001283
1284 /*
1285 * f2fs_iget isn't quite right if the inode is currently unallocated!
1286 * However f2fs_iget currently does appropriate checks to handle stale
1287 * inodes so everything is OK.
1288 */
1289 inode = f2fs_iget(sb, ino);
1290 if (IS_ERR(inode))
1291 return ERR_CAST(inode);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001292 if (unlikely(generation && inode->i_generation != generation)) {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001293 /* we didn't find the right inode.. */
1294 iput(inode);
1295 return ERR_PTR(-ESTALE);
1296 }
1297 return inode;
1298}
1299
1300static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1301 int fh_len, int fh_type)
1302{
1303 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1304 f2fs_nfs_get_inode);
1305}
1306
1307static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
1308 int fh_len, int fh_type)
1309{
1310 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1311 f2fs_nfs_get_inode);
1312}
1313
1314static const struct export_operations f2fs_export_ops = {
1315 .fh_to_dentry = f2fs_fh_to_dentry,
1316 .fh_to_parent = f2fs_fh_to_parent,
1317 .get_parent = f2fs_get_parent,
1318};
1319
Chao Yue0afc4d2015-12-31 14:35:37 +08001320static loff_t max_file_blocks(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001321{
Chao Yu7a2af762017-07-19 00:19:06 +08001322 loff_t result = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001323 loff_t leaf_count = ADDRS_PER_BLOCK;
1324
Chao Yu7a2af762017-07-19 00:19:06 +08001325 /*
1326 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
1327 * F2FS_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
1328 * space in inode.i_addr, it will be more safe to reassign
1329 * result as zero.
1330 */
1331
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001332 /* two direct node blocks */
1333 result += (leaf_count * 2);
1334
1335 /* two indirect node blocks */
1336 leaf_count *= NIDS_PER_BLOCK;
1337 result += (leaf_count * 2);
1338
1339 /* one double indirect node block */
1340 leaf_count *= NIDS_PER_BLOCK;
1341 result += leaf_count;
1342
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001343 return result;
1344}
1345
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001346static int __f2fs_commit_super(struct buffer_head *bh,
1347 struct f2fs_super_block *super)
Chao Yu9a59b622015-12-15 09:58:18 +08001348{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001349 lock_buffer(bh);
1350 if (super)
1351 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
1352 set_buffer_uptodate(bh);
1353 set_buffer_dirty(bh);
1354 unlock_buffer(bh);
1355
1356 /* it's rare case, we can do fua all the time */
Jan Kara3adc5fcb2017-05-02 17:03:47 +02001357 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001358}
1359
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001360static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001361 struct buffer_head *bh)
1362{
1363 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1364 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001365 struct super_block *sb = sbi->sb;
Chao Yu9a59b622015-12-15 09:58:18 +08001366 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1367 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
1368 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
1369 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
1370 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1371 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1372 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
1373 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
1374 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
1375 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
1376 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
1377 u32 segment_count = le32_to_cpu(raw_super->segment_count);
1378 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001379 u64 main_end_blkaddr = main_blkaddr +
1380 (segment_count_main << log_blocks_per_seg);
1381 u64 seg_end_blkaddr = segment0_blkaddr +
1382 (segment_count << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08001383
1384 if (segment0_blkaddr != cp_blkaddr) {
1385 f2fs_msg(sb, KERN_INFO,
1386 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
1387 segment0_blkaddr, cp_blkaddr);
1388 return true;
1389 }
1390
1391 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
1392 sit_blkaddr) {
1393 f2fs_msg(sb, KERN_INFO,
1394 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
1395 cp_blkaddr, sit_blkaddr,
1396 segment_count_ckpt << log_blocks_per_seg);
1397 return true;
1398 }
1399
1400 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
1401 nat_blkaddr) {
1402 f2fs_msg(sb, KERN_INFO,
1403 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
1404 sit_blkaddr, nat_blkaddr,
1405 segment_count_sit << log_blocks_per_seg);
1406 return true;
1407 }
1408
1409 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
1410 ssa_blkaddr) {
1411 f2fs_msg(sb, KERN_INFO,
1412 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
1413 nat_blkaddr, ssa_blkaddr,
1414 segment_count_nat << log_blocks_per_seg);
1415 return true;
1416 }
1417
1418 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
1419 main_blkaddr) {
1420 f2fs_msg(sb, KERN_INFO,
1421 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
1422 ssa_blkaddr, main_blkaddr,
1423 segment_count_ssa << log_blocks_per_seg);
1424 return true;
1425 }
1426
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001427 if (main_end_blkaddr > seg_end_blkaddr) {
Chao Yu9a59b622015-12-15 09:58:18 +08001428 f2fs_msg(sb, KERN_INFO,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001429 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
Chao Yu9a59b622015-12-15 09:58:18 +08001430 main_blkaddr,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001431 segment0_blkaddr +
1432 (segment_count << log_blocks_per_seg),
Chao Yu9a59b622015-12-15 09:58:18 +08001433 segment_count_main << log_blocks_per_seg);
1434 return true;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001435 } else if (main_end_blkaddr < seg_end_blkaddr) {
1436 int err = 0;
1437 char *res;
Chao Yu9a59b622015-12-15 09:58:18 +08001438
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001439 /* fix in-memory information all the time */
1440 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
1441 segment0_blkaddr) >> log_blocks_per_seg);
1442
1443 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001444 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001445 res = "internally";
1446 } else {
1447 err = __f2fs_commit_super(bh, NULL);
1448 res = err ? "failed" : "done";
1449 }
1450 f2fs_msg(sb, KERN_INFO,
1451 "Fix alignment : %s, start(%u) end(%u) block(%u)",
1452 res, main_blkaddr,
1453 segment0_blkaddr +
1454 (segment_count << log_blocks_per_seg),
1455 segment_count_main << log_blocks_per_seg);
1456 if (err)
1457 return true;
1458 }
Chao Yu9a59b622015-12-15 09:58:18 +08001459 return false;
1460}
1461
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001462static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001463 struct buffer_head *bh)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001464{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001465 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1466 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001467 struct super_block *sb = sbi->sb;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001468 unsigned int blocksize;
1469
Namjae Jeona07ef782012-12-30 14:52:05 +09001470 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
1471 f2fs_msg(sb, KERN_INFO,
1472 "Magic Mismatch, valid(0x%x) - read(0x%x)",
1473 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001474 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001475 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001476
majianpeng5c9b4692013-02-01 19:07:57 +08001477 /* Currently, support only 4KB page cache size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001478 if (F2FS_BLKSIZE != PAGE_SIZE) {
majianpeng5c9b4692013-02-01 19:07:57 +08001479 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +08001480 "Invalid page_cache_size (%lu), supports only 4KB\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001481 PAGE_SIZE);
majianpeng5c9b4692013-02-01 19:07:57 +08001482 return 1;
1483 }
1484
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001485 /* Currently, support only 4KB block size */
1486 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +08001487 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001488 f2fs_msg(sb, KERN_INFO,
1489 "Invalid blocksize (%u), supports only 4KB\n",
1490 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001491 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001492 }
majianpeng5c9b4692013-02-01 19:07:57 +08001493
Chao Yu9a59b622015-12-15 09:58:18 +08001494 /* check log blocks per segment */
1495 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
1496 f2fs_msg(sb, KERN_INFO,
1497 "Invalid log blocks per segment (%u)\n",
1498 le32_to_cpu(raw_super->log_blocks_per_seg));
1499 return 1;
1500 }
1501
Chao Yu55cf9cb2014-09-15 18:01:10 +08001502 /* Currently, support 512/1024/2048/4096 bytes sector size */
1503 if (le32_to_cpu(raw_super->log_sectorsize) >
1504 F2FS_MAX_LOG_SECTOR_SIZE ||
1505 le32_to_cpu(raw_super->log_sectorsize) <
1506 F2FS_MIN_LOG_SECTOR_SIZE) {
1507 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
1508 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001509 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001510 }
Chao Yu55cf9cb2014-09-15 18:01:10 +08001511 if (le32_to_cpu(raw_super->log_sectors_per_block) +
1512 le32_to_cpu(raw_super->log_sectorsize) !=
1513 F2FS_MAX_LOG_SECTOR_SIZE) {
1514 f2fs_msg(sb, KERN_INFO,
1515 "Invalid log sectors per block(%u) log sectorsize(%u)",
1516 le32_to_cpu(raw_super->log_sectors_per_block),
1517 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001518 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001519 }
Chao Yu9a59b622015-12-15 09:58:18 +08001520
1521 /* check reserved ino info */
1522 if (le32_to_cpu(raw_super->node_ino) != 1 ||
1523 le32_to_cpu(raw_super->meta_ino) != 2 ||
1524 le32_to_cpu(raw_super->root_ino) != 3) {
1525 f2fs_msg(sb, KERN_INFO,
1526 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
1527 le32_to_cpu(raw_super->node_ino),
1528 le32_to_cpu(raw_super->meta_ino),
1529 le32_to_cpu(raw_super->root_ino));
1530 return 1;
1531 }
1532
Jin Qianb9dd4612017-04-25 16:28:48 -07001533 if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) {
1534 f2fs_msg(sb, KERN_INFO,
1535 "Invalid segment count (%u)",
1536 le32_to_cpu(raw_super->segment_count));
1537 return 1;
1538 }
1539
Chao Yu9a59b622015-12-15 09:58:18 +08001540 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001541 if (sanity_check_area_boundary(sbi, bh))
Chao Yu9a59b622015-12-15 09:58:18 +08001542 return 1;
1543
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001544 return 0;
1545}
1546
Shawn Lin984ec632016-02-17 11:26:32 +08001547int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001548{
1549 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001550 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1551 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08001552 unsigned int ovp_segments, reserved_segments;
Jin Qian15d30422017-05-15 10:45:08 -07001553 unsigned int main_segs, blocks_per_seg;
1554 int i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001555
1556 total = le32_to_cpu(raw_super->segment_count);
1557 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
1558 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
1559 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
1560 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
1561 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
1562
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001563 if (unlikely(fsmeta >= total))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001564 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001565
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08001566 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1567 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1568
1569 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
1570 ovp_segments == 0 || reserved_segments == 0)) {
1571 f2fs_msg(sbi->sb, KERN_ERR,
1572 "Wrong layout: check mkfs.f2fs version");
1573 return 1;
1574 }
1575
Jin Qian15d30422017-05-15 10:45:08 -07001576 main_segs = le32_to_cpu(raw_super->segment_count_main);
1577 blocks_per_seg = sbi->blocks_per_seg;
1578
1579 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1580 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
1581 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
1582 return 1;
1583 }
1584 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1585 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
1586 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
1587 return 1;
1588 }
1589
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07001590 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001591 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
1592 return 1;
1593 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001594 return 0;
1595}
1596
1597static void init_sb_info(struct f2fs_sb_info *sbi)
1598{
1599 struct f2fs_super_block *raw_super = sbi->raw_super;
Chao Yue41e6d72017-05-19 23:37:00 +08001600 int i, j;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001601
1602 sbi->log_sectors_per_block =
1603 le32_to_cpu(raw_super->log_sectors_per_block);
1604 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
1605 sbi->blocksize = 1 << sbi->log_blocksize;
1606 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
1607 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
1608 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
1609 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
1610 sbi->total_sections = le32_to_cpu(raw_super->section_count);
1611 sbi->total_node_count =
1612 (le32_to_cpu(raw_super->segment_count_nat) / 2)
1613 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
1614 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
1615 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
1616 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001617 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimb1c57c12014-01-08 13:45:08 +09001618 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001619
Jaegeuk Kimab9fa662014-02-27 20:09:05 +09001620 sbi->dir_level = DEF_DIR_LEVEL;
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001621 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001622 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
Chao Yucaf00472015-01-28 17:48:42 +08001623 clear_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001624
Jaegeuk Kim35782b22016-10-20 19:09:57 -07001625 for (i = 0; i < NR_COUNT_TYPE; i++)
1626 atomic_set(&sbi->nr_pages[i], 0);
1627
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001628 atomic_set(&sbi->wb_sync_req, 0);
1629
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001630 INIT_LIST_HEAD(&sbi->s_list);
1631 mutex_init(&sbi->umount_mutex);
Chao Yue41e6d72017-05-19 23:37:00 +08001632 for (i = 0; i < NR_PAGE_TYPE - 1; i++)
1633 for (j = HOT; j < NR_TEMP_TYPE; j++)
1634 mutex_init(&sbi->wio_mutex[i][j]);
Chao Yuaaec2b12016-09-20 11:04:18 +08001635 spin_lock_init(&sbi->cp_lock);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001636}
1637
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001638static int init_percpu_info(struct f2fs_sb_info *sbi)
1639{
Jaegeuk Kim35782b22016-10-20 19:09:57 -07001640 int err;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001641
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001642 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
1643 if (err)
1644 return err;
1645
1646 return percpu_counter_init(&sbi->total_valid_inode_count, 0,
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001647 GFP_KERNEL);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001648}
1649
Damien Le Moal178053e2016-10-28 17:45:05 +09001650#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001651static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
Damien Le Moal178053e2016-10-28 17:45:05 +09001652{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001653 struct block_device *bdev = FDEV(devi).bdev;
Damien Le Moal178053e2016-10-28 17:45:05 +09001654 sector_t nr_sectors = bdev->bd_part->nr_sects;
1655 sector_t sector = 0;
1656 struct blk_zone *zones;
1657 unsigned int i, nr_zones;
1658 unsigned int n = 0;
1659 int err = -EIO;
1660
1661 if (!f2fs_sb_mounted_blkzoned(sbi->sb))
1662 return 0;
1663
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001664 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
Damien Le Moalf99e8642017-01-12 07:58:32 -07001665 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001666 return -EINVAL;
Damien Le Moalf99e8642017-01-12 07:58:32 -07001667 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001668 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
1669 __ilog2_u32(sbi->blocks_per_blkz))
1670 return -EINVAL;
Damien Le Moal178053e2016-10-28 17:45:05 +09001671 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001672 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
1673 sbi->log_blocks_per_blkz;
Damien Le Moalf99e8642017-01-12 07:58:32 -07001674 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001675 FDEV(devi).nr_blkz++;
Damien Le Moal178053e2016-10-28 17:45:05 +09001676
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001677 FDEV(devi).blkz_type = kmalloc(FDEV(devi).nr_blkz, GFP_KERNEL);
1678 if (!FDEV(devi).blkz_type)
Damien Le Moal178053e2016-10-28 17:45:05 +09001679 return -ENOMEM;
1680
1681#define F2FS_REPORT_NR_ZONES 4096
1682
1683 zones = kcalloc(F2FS_REPORT_NR_ZONES, sizeof(struct blk_zone),
1684 GFP_KERNEL);
1685 if (!zones)
1686 return -ENOMEM;
1687
1688 /* Get block zones type */
1689 while (zones && sector < nr_sectors) {
1690
1691 nr_zones = F2FS_REPORT_NR_ZONES;
1692 err = blkdev_report_zones(bdev, sector,
1693 zones, &nr_zones,
1694 GFP_KERNEL);
1695 if (err)
1696 break;
1697 if (!nr_zones) {
1698 err = -EIO;
1699 break;
1700 }
1701
1702 for (i = 0; i < nr_zones; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001703 FDEV(devi).blkz_type[n] = zones[i].type;
Damien Le Moal178053e2016-10-28 17:45:05 +09001704 sector += zones[i].len;
1705 n++;
1706 }
1707 }
1708
1709 kfree(zones);
1710
1711 return err;
1712}
1713#endif
1714
Gu Zheng9076a752013-10-14 18:47:11 +08001715/*
1716 * Read f2fs raw super block.
Shawn Lin2b39e902016-02-17 08:59:01 +08001717 * Because we have two copies of super block, so read both of them
1718 * to get the first valid one. If any one of them is broken, we pass
1719 * them recovery flag back to the caller.
Gu Zheng9076a752013-10-14 18:47:11 +08001720 */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001721static int read_raw_super_block(struct f2fs_sb_info *sbi,
Gu Zheng9076a752013-10-14 18:47:11 +08001722 struct f2fs_super_block **raw_super,
Chao Yue8240f62015-12-15 17:19:26 +08001723 int *valid_super_block, int *recovery)
majianpeng14d7e9d2013-02-01 19:07:03 +08001724{
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001725 struct super_block *sb = sbi->sb;
Shawn Lin2b39e902016-02-17 08:59:01 +08001726 int block;
Chao Yue8240f62015-12-15 17:19:26 +08001727 struct buffer_head *bh;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001728 struct f2fs_super_block *super;
hujianyangda554e42015-05-21 14:42:53 +08001729 int err = 0;
majianpeng14d7e9d2013-02-01 19:07:03 +08001730
Yunlei Heb39f0de2015-12-15 17:17:20 +08001731 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
1732 if (!super)
1733 return -ENOMEM;
Shawn Lin2b39e902016-02-17 08:59:01 +08001734
1735 for (block = 0; block < 2; block++) {
1736 bh = sb_bread(sb, block);
1737 if (!bh) {
1738 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
Gu Zheng9076a752013-10-14 18:47:11 +08001739 block + 1);
Shawn Lin2b39e902016-02-17 08:59:01 +08001740 err = -EIO;
1741 continue;
1742 }
majianpeng14d7e9d2013-02-01 19:07:03 +08001743
Shawn Lin2b39e902016-02-17 08:59:01 +08001744 /* sanity checking of raw super */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001745 if (sanity_check_raw_super(sbi, bh)) {
Shawn Lin2b39e902016-02-17 08:59:01 +08001746 f2fs_msg(sb, KERN_ERR,
1747 "Can't find valid F2FS filesystem in %dth superblock",
1748 block + 1);
1749 err = -EINVAL;
1750 brelse(bh);
1751 continue;
1752 }
1753
1754 if (!*raw_super) {
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001755 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
1756 sizeof(*super));
Shawn Lin2b39e902016-02-17 08:59:01 +08001757 *valid_super_block = block;
1758 *raw_super = super;
1759 }
Chao Yue8240f62015-12-15 17:19:26 +08001760 brelse(bh);
Shawn Lin2b39e902016-02-17 08:59:01 +08001761 }
1762
1763 /* Fail to read any one of the superblocks*/
1764 if (err < 0)
hujianyangda554e42015-05-21 14:42:53 +08001765 *recovery = 1;
hujianyangda554e42015-05-21 14:42:53 +08001766
hujianyangda554e42015-05-21 14:42:53 +08001767 /* No valid superblock */
Shawn Lin2b39e902016-02-17 08:59:01 +08001768 if (!*raw_super)
Yunlei Heb39f0de2015-12-15 17:17:20 +08001769 kfree(super);
Shawn Lin2b39e902016-02-17 08:59:01 +08001770 else
1771 err = 0;
hujianyangda554e42015-05-21 14:42:53 +08001772
Shawn Lin2b39e902016-02-17 08:59:01 +08001773 return err;
majianpeng14d7e9d2013-02-01 19:07:03 +08001774}
1775
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001776int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001777{
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001778 struct buffer_head *bh;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001779 int err;
1780
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001781 if ((recover && f2fs_readonly(sbi->sb)) ||
1782 bdev_read_only(sbi->sb->s_bdev)) {
1783 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07001784 return -EROFS;
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001785 }
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07001786
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001787 /* write back-up superblock first */
1788 bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001789 if (!bh)
1790 return -EIO;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001791 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001792 brelse(bh);
Chao Yuc5bda1c2015-06-08 13:28:03 +08001793
1794 /* if we are in recovery path, skip writing valid superblock */
1795 if (recover || err)
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001796 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001797
Chao Yue8240f62015-12-15 17:19:26 +08001798 /* write current valid superblock */
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001799 bh = sb_getblk(sbi->sb, sbi->valid_super_block);
1800 if (!bh)
1801 return -EIO;
1802 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
1803 brelse(bh);
1804 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001805}
1806
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001807static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
1808{
1809 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001810 unsigned int max_devices = MAX_DEVICES;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001811 int i;
1812
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001813 /* Initialize single device information */
1814 if (!RDEV(0).path[0]) {
1815 if (!bdev_is_zoned(sbi->sb->s_bdev))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001816 return 0;
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001817 max_devices = 1;
1818 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001819
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001820 /*
1821 * Initialize multiple devices information, or single
1822 * zoned block device information.
1823 */
1824 sbi->devs = kcalloc(max_devices, sizeof(struct f2fs_dev_info),
1825 GFP_KERNEL);
1826 if (!sbi->devs)
1827 return -ENOMEM;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001828
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001829 for (i = 0; i < max_devices; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001830
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001831 if (i > 0 && !RDEV(i).path[0])
1832 break;
1833
1834 if (max_devices == 1) {
1835 /* Single zoned block device mount */
1836 FDEV(0).bdev =
1837 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001838 sbi->sb->s_mode, sbi->sb->s_type);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001839 } else {
1840 /* Multi-device mount */
1841 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
1842 FDEV(i).total_segments =
1843 le32_to_cpu(RDEV(i).total_segments);
1844 if (i == 0) {
1845 FDEV(i).start_blk = 0;
1846 FDEV(i).end_blk = FDEV(i).start_blk +
1847 (FDEV(i).total_segments <<
1848 sbi->log_blocks_per_seg) - 1 +
1849 le32_to_cpu(raw_super->segment0_blkaddr);
1850 } else {
1851 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
1852 FDEV(i).end_blk = FDEV(i).start_blk +
1853 (FDEV(i).total_segments <<
1854 sbi->log_blocks_per_seg) - 1;
1855 }
1856 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
1857 sbi->sb->s_mode, sbi->sb->s_type);
1858 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001859 if (IS_ERR(FDEV(i).bdev))
1860 return PTR_ERR(FDEV(i).bdev);
1861
1862 /* to release errored devices */
1863 sbi->s_ndevs = i + 1;
1864
1865#ifdef CONFIG_BLK_DEV_ZONED
1866 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
1867 !f2fs_sb_mounted_blkzoned(sbi->sb)) {
1868 f2fs_msg(sbi->sb, KERN_ERR,
1869 "Zoned block device feature not enabled\n");
1870 return -EINVAL;
1871 }
1872 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
1873 if (init_blkz_info(sbi, i)) {
1874 f2fs_msg(sbi->sb, KERN_ERR,
1875 "Failed to initialize F2FS blkzone information");
1876 return -EINVAL;
1877 }
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001878 if (max_devices == 1)
1879 break;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001880 f2fs_msg(sbi->sb, KERN_INFO,
1881 "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
1882 i, FDEV(i).path,
1883 FDEV(i).total_segments,
1884 FDEV(i).start_blk, FDEV(i).end_blk,
1885 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
1886 "Host-aware" : "Host-managed");
1887 continue;
1888 }
1889#endif
1890 f2fs_msg(sbi->sb, KERN_INFO,
1891 "Mount Device [%2d]: %20s, %8u, %8x - %8x",
1892 i, FDEV(i).path,
1893 FDEV(i).total_segments,
1894 FDEV(i).start_blk, FDEV(i).end_blk);
1895 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08001896 f2fs_msg(sbi->sb, KERN_INFO,
1897 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001898 return 0;
1899}
1900
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001901static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
1902{
1903 struct f2fs_sb_info *sbi;
hujianyangda554e42015-05-21 14:42:53 +08001904 struct f2fs_super_block *raw_super;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001905 struct inode *root;
Sheng Yong99e3e852016-05-11 17:08:14 +08001906 int err;
Chao Yu2adc3502015-03-16 21:08:44 +08001907 bool retry = true, need_fsck = false;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001908 char *options = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08001909 int recovery, i, valid_super_block;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001910 struct curseg_info *seg_i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001911
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07001912try_onemore:
hujianyangda554e42015-05-21 14:42:53 +08001913 err = -EINVAL;
1914 raw_super = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08001915 valid_super_block = -1;
hujianyangda554e42015-05-21 14:42:53 +08001916 recovery = 0;
1917
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001918 /* allocate memory for f2fs-specific super block info */
1919 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
1920 if (!sbi)
1921 return -ENOMEM;
1922
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001923 sbi->sb = sb;
1924
Keith Mok43b65732016-03-02 12:04:24 -08001925 /* Load the checksum driver */
1926 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
1927 if (IS_ERR(sbi->s_chksum_driver)) {
1928 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
1929 err = PTR_ERR(sbi->s_chksum_driver);
1930 sbi->s_chksum_driver = NULL;
1931 goto free_sbi;
1932 }
1933
Namjae Jeonff9234a2013-01-12 14:41:13 +09001934 /* set a block size */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001935 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001936 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001937 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +09001938 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001939
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001940 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
Chao Yue8240f62015-12-15 17:19:26 +08001941 &recovery);
Gu Zheng9076a752013-10-14 18:47:11 +08001942 if (err)
1943 goto free_sbi;
1944
Gu Zheng5fb08372013-06-07 14:16:53 +08001945 sb->s_fs_info = sbi;
Jaegeuk Kim52763a42016-06-13 09:47:48 -07001946 sbi->raw_super = raw_super;
1947
Damien Le Moald1b959c2016-10-28 17:45:01 +09001948 /*
1949 * The BLKZONED feature indicates that the drive was formatted with
1950 * zone alignment optimization. This is optional for host-aware
1951 * devices, but mandatory for host-managed zoned block devices.
1952 */
1953#ifndef CONFIG_BLK_DEV_ZONED
1954 if (f2fs_sb_mounted_blkzoned(sb)) {
1955 f2fs_msg(sb, KERN_ERR,
1956 "Zoned block device support is not enabled\n");
Chao Yu1727f312017-06-12 09:44:27 +08001957 err = -EOPNOTSUPP;
Damien Le Moald1b959c2016-10-28 17:45:01 +09001958 goto free_sb_buf;
1959 }
Damien Le Moald1b959c2016-10-28 17:45:01 +09001960#endif
Yunlei He498c5e92015-05-07 18:11:37 +08001961 default_options(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001962 /* parse mount options */
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001963 options = kstrdup((const char *)data, GFP_KERNEL);
1964 if (data && !options) {
1965 err = -ENOMEM;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001966 goto free_sb_buf;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001967 }
1968
1969 err = parse_options(sb, options);
1970 if (err)
1971 goto free_options;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001972
Chao Yue0afc4d2015-12-31 14:35:37 +08001973 sbi->max_file_blocks = max_file_blocks();
1974 sb->s_maxbytes = sbi->max_file_blocks <<
1975 le32_to_cpu(raw_super->log_blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001976 sb->s_max_links = F2FS_LINK_MAX;
1977 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1978
Chao Yu0abd6752017-07-09 00:13:07 +08001979#ifdef CONFIG_QUOTA
1980 sb->dq_op = &f2fs_quota_operations;
1981 sb->s_qcop = &f2fs_quotactl_ops;
Chao Yu5c571322017-07-26 00:01:41 +08001982 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
Chao Yu0abd6752017-07-09 00:13:07 +08001983#endif
1984
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001985 sb->s_op = &f2fs_sops;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001986 sb->s_cop = &f2fs_cryptops;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001987 sb->s_xattr = f2fs_xattr_handlers;
1988 sb->s_export_op = &f2fs_export_ops;
1989 sb->s_magic = F2FS_SUPER_MAGIC;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001990 sb->s_time_gran = 1;
1991 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1992 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
Christoph Hellwig85787092017-05-10 15:06:33 +02001993 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001994
1995 /* init f2fs-specific super block info */
Chao Yue8240f62015-12-15 17:19:26 +08001996 sbi->valid_super_block = valid_super_block;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001997 mutex_init(&sbi->gc_mutex);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001998 mutex_init(&sbi->cp_mutex);
Chao Yub3582c62014-07-03 18:58:39 +08001999 init_rwsem(&sbi->node_write);
Yunlei He59c90812017-03-13 20:22:18 +08002000 init_rwsem(&sbi->node_change);
Jaegeuk Kim315df832015-08-11 12:45:39 -07002001
2002 /* disallow all the data/node/meta page writes */
2003 set_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002004 spin_lock_init(&sbi->stat_lock);
Jaegeuk Kim971767c2013-11-18 17:16:17 +09002005
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002006 for (i = 0; i < NR_PAGE_TYPE; i++) {
Jaegeuk Kima912b542017-05-10 11:18:25 -07002007 int n = (i == META) ? 1: NR_TEMP_TYPE;
2008 int j;
2009
2010 sbi->write_io[i] = kmalloc(n * sizeof(struct f2fs_bio_info),
2011 GFP_KERNEL);
Christophe JAILLETb63def92017-06-11 09:21:11 +02002012 if (!sbi->write_io[i]) {
2013 err = -ENOMEM;
Jaegeuk Kima912b542017-05-10 11:18:25 -07002014 goto free_options;
Christophe JAILLETb63def92017-06-11 09:21:11 +02002015 }
Jaegeuk Kima912b542017-05-10 11:18:25 -07002016
2017 for (j = HOT; j < n; j++) {
2018 init_rwsem(&sbi->write_io[i][j].io_rwsem);
2019 sbi->write_io[i][j].sbi = sbi;
2020 sbi->write_io[i][j].bio = NULL;
Chao Yufb830fc2017-05-19 23:37:01 +08002021 spin_lock_init(&sbi->write_io[i][j].io_lock);
2022 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
Jaegeuk Kima912b542017-05-10 11:18:25 -07002023 }
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002024 }
Jaegeuk Kim971767c2013-11-18 17:16:17 +09002025
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002026 init_rwsem(&sbi->cp_rwsem);
Changman Leefb51b5e2013-11-07 12:48:25 +09002027 init_waitqueue_head(&sbi->cp_wait);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002028 init_sb_info(sbi);
2029
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002030 err = init_percpu_info(sbi);
2031 if (err)
2032 goto free_options;
2033
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002034 if (F2FS_IO_SIZE(sbi) > 1) {
2035 sbi->write_io_dummy =
Chao Yua3ebfe42017-02-27 18:43:13 +08002036 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
Chao Yu1727f312017-06-12 09:44:27 +08002037 if (!sbi->write_io_dummy) {
2038 err = -ENOMEM;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002039 goto free_options;
Chao Yu1727f312017-06-12 09:44:27 +08002040 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002041 }
2042
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002043 /* get an inode for meta space */
2044 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
2045 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002046 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002047 err = PTR_ERR(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002048 goto free_io_dummy;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002049 }
2050
2051 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002052 if (err) {
2053 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002054 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +09002055 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002056
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002057 /* Initialize device list */
2058 err = f2fs_scan_devices(sbi);
2059 if (err) {
2060 f2fs_msg(sb, KERN_ERR, "Failed to find devices");
2061 goto free_devices;
2062 }
2063
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002064 sbi->total_valid_node_count =
2065 le32_to_cpu(sbi->ckpt->valid_node_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07002066 percpu_counter_set(&sbi->total_valid_inode_count,
2067 le32_to_cpu(sbi->ckpt->valid_inode_count));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002068 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
2069 sbi->total_valid_block_count =
2070 le64_to_cpu(sbi->ckpt->valid_block_count);
2071 sbi->last_valid_block_count = sbi->total_valid_block_count;
Chao Yudaeb4332017-06-26 16:24:41 +08002072 sbi->reserved_blocks = 0;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002073
Chao Yuc227f912015-12-16 13:09:20 +08002074 for (i = 0; i < NR_INODE_TYPE; i++) {
2075 INIT_LIST_HEAD(&sbi->inode_list[i]);
2076 spin_lock_init(&sbi->inode_lock[i]);
2077 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002078
Chao Yu1dcc3362015-02-05 17:57:31 +08002079 init_extent_cache_info(sbi);
2080
Jaegeuk Kim6451e042014-07-25 15:47:17 -07002081 init_ino_entry_info(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002082
2083 /* setup f2fs internal modules */
2084 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002085 if (err) {
2086 f2fs_msg(sb, KERN_ERR,
2087 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002088 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +09002089 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002090 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002091 if (err) {
2092 f2fs_msg(sb, KERN_ERR,
2093 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002094 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +09002095 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002096
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002097 /* For write statistics */
2098 if (sb->s_bdev->bd_part)
2099 sbi->sectors_written_start =
2100 (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
2101
2102 /* Read accumulated write IO statistics if exists */
2103 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
2104 if (__exist_node_summaries(sbi))
2105 sbi->kbytes_written =
Shuoran Liub2dde6f2016-03-29 18:00:15 +08002106 le64_to_cpu(seg_i->journal->info.kbytes_written);
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002107
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002108 build_gc_manager(sbi);
2109
2110 /* get an inode for node space */
2111 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
2112 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002113 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002114 err = PTR_ERR(sbi->node_inode);
2115 goto free_nm;
2116 }
2117
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002118 f2fs_join_shrinker(sbi);
2119
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08002120 err = f2fs_build_stats(sbi);
2121 if (err)
2122 goto free_nm;
2123
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002124 /* if there are nt orphan nodes free them */
Chao Yu8c14bfa2015-08-07 17:58:43 +08002125 err = recover_orphan_inodes(sbi);
2126 if (err)
2127 goto free_node_inode;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002128
2129 /* read root inode and dentry */
2130 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
2131 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002132 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002133 err = PTR_ERR(root);
2134 goto free_node_inode;
2135 }
Chao Yu8f99a942013-11-28 15:43:43 +08002136 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
Chao Yu9d847952014-07-25 12:55:09 +08002137 iput(root);
Chao Yu8f99a942013-11-28 15:43:43 +08002138 err = -EINVAL;
Chao Yu9d847952014-07-25 12:55:09 +08002139 goto free_node_inode;
Chao Yu8f99a942013-11-28 15:43:43 +08002140 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002141
2142 sb->s_root = d_make_root(root); /* allocate root dentry */
2143 if (!sb->s_root) {
2144 err = -ENOMEM;
2145 goto free_root_inode;
2146 }
2147
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002148 err = f2fs_register_sysfs(sbi);
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09002149 if (err)
Chao Yua3981012017-06-14 17:39:46 +08002150 goto free_root_inode;
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09002151
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002152 /* recover fsynced data */
2153 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002154 /*
2155 * mount should be failed, when device has readonly mode, and
2156 * previous checkpoint was not done by clean system shutdown.
2157 */
2158 if (bdev_read_only(sb->s_bdev) &&
Chao Yuaaec2b12016-09-20 11:04:18 +08002159 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002160 err = -EROFS;
Chao Yua3981012017-06-14 17:39:46 +08002161 goto free_sysfs;
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002162 }
Chao Yu2adc3502015-03-16 21:08:44 +08002163
2164 if (need_fsck)
2165 set_sbi_flag(sbi, SBI_NEED_FSCK);
2166
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07002167 if (!retry)
2168 goto skip_recovery;
2169
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002170 err = recover_fsync_data(sbi, false);
2171 if (err < 0) {
Chao Yu2adc3502015-03-16 21:08:44 +08002172 need_fsck = true;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002173 f2fs_msg(sb, KERN_ERR,
Sheng Yong99e3e852016-05-11 17:08:14 +08002174 "Cannot recover all fsync data errno=%d", err);
Chao Yua3981012017-06-14 17:39:46 +08002175 goto free_sysfs;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002176 }
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002177 } else {
2178 err = recover_fsync_data(sbi, true);
2179
2180 if (!f2fs_readonly(sb) && err > 0) {
2181 err = -EINVAL;
2182 f2fs_msg(sb, KERN_ERR,
2183 "Need to recover fsync data");
Chao Yua3981012017-06-14 17:39:46 +08002184 goto free_sysfs;
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002185 }
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002186 }
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07002187skip_recovery:
Jaegeuk Kim315df832015-08-11 12:45:39 -07002188 /* recover_fsync_data() cleared this already */
2189 clear_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002190
2191 /*
2192 * If filesystem is not mounted as read-only then
2193 * do start the gc_thread.
2194 */
Chao Yu6c029932014-11-18 11:16:01 +08002195 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002196 /* After POR, we can run background GC thread.*/
2197 err = start_gc_thread(sbi);
2198 if (err)
Chao Yua3981012017-06-14 17:39:46 +08002199 goto free_sysfs;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002200 }
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002201 kfree(options);
hujianyangda554e42015-05-21 14:42:53 +08002202
2203 /* recover broken superblock */
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07002204 if (recovery) {
Chao Yu41214b32016-02-22 18:33:20 +08002205 err = f2fs_commit_super(sbi, true);
2206 f2fs_msg(sb, KERN_INFO,
Sheng Yong99e3e852016-05-11 17:08:14 +08002207 "Try to recover %dth superblock, ret: %d",
Chao Yu41214b32016-02-22 18:33:20 +08002208 sbi->valid_super_block ? 1 : 2, err);
hujianyangda554e42015-05-21 14:42:53 +08002209 }
2210
Jaegeuk Kim1200abb2017-02-09 13:25:35 -08002211 f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
2212 cur_cp_version(F2FS_CKPT(sbi)));
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08002213 f2fs_update_time(sbi, CP_TIME);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002214 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002215 return 0;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002216
Chao Yua3981012017-06-14 17:39:46 +08002217free_sysfs:
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07002218 f2fs_sync_inode_meta(sbi);
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002219 f2fs_unregister_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002220free_root_inode:
2221 dput(sb->s_root);
2222 sb->s_root = NULL;
2223free_node_inode:
Jaegeuk Kimbb5dada2016-09-23 11:29:00 -07002224 truncate_inode_pages_final(NODE_MAPPING(sbi));
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002225 mutex_lock(&sbi->umount_mutex);
Jaegeuk Kimd41065e2016-09-21 11:39:42 -07002226 release_ino_entry(sbi, true);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002227 f2fs_leave_shrinker(sbi);
Jaegeuk Kim09922802016-10-31 14:01:41 -07002228 /*
2229 * Some dirty meta pages can be produced by recover_orphan_inodes()
2230 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
2231 * followed by write_checkpoint() through f2fs_write_node_pages(), which
2232 * falls into an infinite loop in sync_meta_pages().
2233 */
2234 truncate_inode_pages_final(META_MAPPING(sbi));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002235 iput(sbi->node_inode);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002236 mutex_unlock(&sbi->umount_mutex);
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08002237 f2fs_destroy_stats(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002238free_nm:
2239 destroy_node_manager(sbi);
2240free_sm:
2241 destroy_segment_manager(sbi);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002242free_devices:
2243 destroy_device_list(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002244 kfree(sbi->ckpt);
2245free_meta_inode:
2246 make_bad_inode(sbi->meta_inode);
2247 iput(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002248free_io_dummy:
2249 mempool_destroy(sbi->write_io_dummy);
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002250free_options:
Jaegeuk Kima912b542017-05-10 11:18:25 -07002251 for (i = 0; i < NR_PAGE_TYPE; i++)
2252 kfree(sbi->write_io[i]);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002253 destroy_percpu_info(sbi);
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002254 kfree(options);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002255free_sb_buf:
Yunlei Heb39f0de2015-12-15 17:17:20 +08002256 kfree(raw_super);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002257free_sbi:
Keith Mok43b65732016-03-02 12:04:24 -08002258 if (sbi->s_chksum_driver)
2259 crypto_free_shash(sbi->s_chksum_driver);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002260 kfree(sbi);
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002261
2262 /* give only one another chance */
2263 if (retry) {
Taehee Yoo9df47ba2015-04-13 21:48:06 +09002264 retry = false;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002265 shrink_dcache_sb(sb);
2266 goto try_onemore;
2267 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002268 return err;
2269}
2270
2271static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
2272 const char *dev_name, void *data)
2273{
2274 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
2275}
2276
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002277static void kill_f2fs_super(struct super_block *sb)
2278{
Chao Yucce13252017-06-29 23:17:45 +08002279 if (sb->s_root) {
Chao Yucaf00472015-01-28 17:48:42 +08002280 set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
Chao Yucce13252017-06-29 23:17:45 +08002281 stop_gc_thread(F2FS_SB(sb));
2282 stop_discard_thread(F2FS_SB(sb));
2283 }
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002284 kill_block_super(sb);
2285}
2286
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002287static struct file_system_type f2fs_fs_type = {
2288 .owner = THIS_MODULE,
2289 .name = "f2fs",
2290 .mount = f2fs_mount,
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002291 .kill_sb = kill_f2fs_super,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002292 .fs_flags = FS_REQUIRES_DEV,
2293};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08002294MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002295
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002296static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002297{
Vladimir Davydov5d097052016-01-14 15:18:21 -08002298 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
2299 sizeof(struct f2fs_inode_info), 0,
2300 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002301 if (!f2fs_inode_cachep)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002302 return -ENOMEM;
2303 return 0;
2304}
2305
2306static void destroy_inodecache(void)
2307{
2308 /*
2309 * Make sure all delayed rcu free inodes are flushed before we
2310 * destroy cache.
2311 */
2312 rcu_barrier();
2313 kmem_cache_destroy(f2fs_inode_cachep);
2314}
2315
2316static int __init init_f2fs_fs(void)
2317{
2318 int err;
2319
Jaegeuk Kimc0508652015-01-07 14:07:36 -08002320 f2fs_build_trace_ios();
2321
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002322 err = init_inodecache();
2323 if (err)
2324 goto fail;
2325 err = create_node_manager_caches();
2326 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002327 goto free_inodecache;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002328 err = create_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002329 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002330 goto free_node_manager_caches;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002331 err = create_checkpoint_caches();
2332 if (err)
Chao Yu06292072014-12-29 15:56:18 +08002333 goto free_segment_manager_caches;
Chao Yu1dcc3362015-02-05 17:57:31 +08002334 err = create_extent_cache();
2335 if (err)
2336 goto free_checkpoint_caches;
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002337 err = f2fs_init_sysfs();
Chao Yua3981012017-06-14 17:39:46 +08002338 if (err)
Chao Yu1dcc3362015-02-05 17:57:31 +08002339 goto free_extent_cache;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002340 err = register_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimcfc4d972015-05-15 15:37:24 -07002341 if (err)
Chao Yua3981012017-06-14 17:39:46 +08002342 goto free_sysfs;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002343 err = register_filesystem(&f2fs_fs_type);
2344 if (err)
2345 goto free_shrinker;
Chao Yu787c7b8c2015-10-29 09:13:04 +08002346 err = f2fs_create_root_stats();
2347 if (err)
2348 goto free_filesystem;
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002349 return 0;
2350
Chao Yu787c7b8c2015-10-29 09:13:04 +08002351free_filesystem:
2352 unregister_filesystem(&f2fs_fs_type);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002353free_shrinker:
2354 unregister_shrinker(&f2fs_shrinker_info);
Chao Yua3981012017-06-14 17:39:46 +08002355free_sysfs:
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002356 f2fs_exit_sysfs();
Chao Yu1dcc3362015-02-05 17:57:31 +08002357free_extent_cache:
2358 destroy_extent_cache();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002359free_checkpoint_caches:
2360 destroy_checkpoint_caches();
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002361free_segment_manager_caches:
2362 destroy_segment_manager_caches();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002363free_node_manager_caches:
2364 destroy_node_manager_caches();
2365free_inodecache:
2366 destroy_inodecache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002367fail:
2368 return err;
2369}
2370
2371static void __exit exit_f2fs_fs(void)
2372{
Namjae Jeon4589d252013-01-15 19:58:47 +09002373 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002374 unregister_filesystem(&f2fs_fs_type);
Tiezhu Yangb8bef792016-05-18 08:02:25 +08002375 unregister_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002376 f2fs_exit_sysfs();
Wanpeng Lifdf6c8b2015-03-06 15:00:54 +08002377 destroy_extent_cache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002378 destroy_checkpoint_caches();
Changman Lee5dcd8a72013-12-11 14:32:13 +09002379 destroy_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002380 destroy_node_manager_caches();
2381 destroy_inodecache();
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -08002382 f2fs_destroy_trace_ios();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002383}
2384
2385module_init(init_f2fs_fs)
2386module_exit(exit_f2fs_fs)
2387
2388MODULE_AUTHOR("Samsung Electronics's Praesto Team");
2389MODULE_DESCRIPTION("Flash Friendly File System");
2390MODULE_LICENSE("GPL");
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -07002391