blob: f56230759b381c60c4deb347c46ff3078b4dcb12 [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>
Chao Yu4b2414d2017-08-08 10:54:31 +080028#include <linux/quota.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090029
30#include "f2fs.h"
31#include "node.h"
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +090032#include "segment.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090033#include "xattr.h"
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090034#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080035#include "trace.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090036
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090037#define CREATE_TRACE_POINTS
38#include <trace/events/f2fs.h>
39
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090040static struct kmem_cache *f2fs_inode_cachep;
41
Jaegeuk Kim73faec42016-04-29 15:34:32 -070042#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070043
44char *fault_name[FAULT_MAX] = {
45 [FAULT_KMALLOC] = "kmalloc",
Chao Yu628b3d12017-11-30 19:28:18 +080046 [FAULT_KVMALLOC] = "kvmalloc",
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -070047 [FAULT_PAGE_ALLOC] = "page alloc",
Chao Yu01eccef2017-10-28 16:52:30 +080048 [FAULT_PAGE_GET] = "page get",
Chao Yud62fe972017-10-28 16:52:31 +080049 [FAULT_ALLOC_BIO] = "alloc bio",
Jaegeuk Kimcb789422016-04-29 16:29:22 -070050 [FAULT_ALLOC_NID] = "alloc nid",
51 [FAULT_ORPHAN] = "orphan",
52 [FAULT_BLOCK] = "no more block",
53 [FAULT_DIR_DEPTH] = "too big dir depth",
Jaegeuk Kim53aa6bb2016-05-25 15:24:18 -070054 [FAULT_EVICT_INODE] = "evict_inode fail",
Jaegeuk Kim14b44d22017-03-09 15:24:24 -080055 [FAULT_TRUNCATE] = "truncate fail",
Chao Yu8b038c72016-09-18 23:30:07 +080056 [FAULT_IO] = "IO error",
Chao Yu0f348022016-09-26 19:45:55 +080057 [FAULT_CHECKPOINT] = "checkpoint error",
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070058};
Sheng Yong08796892016-05-16 12:38:50 +080059
Chao Yu1ecc0c52016-09-23 21:30:09 +080060static void f2fs_build_fault_attr(struct f2fs_sb_info *sbi,
61 unsigned int rate)
Sheng Yong08796892016-05-16 12:38:50 +080062{
Chao Yu1ecc0c52016-09-23 21:30:09 +080063 struct f2fs_fault_info *ffi = &sbi->fault_info;
64
Sheng Yong08796892016-05-16 12:38:50 +080065 if (rate) {
Chao Yu1ecc0c52016-09-23 21:30:09 +080066 atomic_set(&ffi->inject_ops, 0);
67 ffi->inject_rate = rate;
68 ffi->inject_type = (1 << FAULT_MAX) - 1;
Sheng Yong08796892016-05-16 12:38:50 +080069 } else {
Chao Yu1ecc0c52016-09-23 21:30:09 +080070 memset(ffi, 0, sizeof(struct f2fs_fault_info));
Sheng Yong08796892016-05-16 12:38:50 +080071 }
72}
Jaegeuk Kim73faec42016-04-29 15:34:32 -070073#endif
74
Jaegeuk Kim2658e502015-06-19 12:01:21 -070075/* f2fs-wide shrinker description */
76static struct shrinker f2fs_shrinker_info = {
77 .scan_objects = f2fs_shrink_scan,
78 .count_objects = f2fs_shrink_count,
79 .seeks = DEFAULT_SEEKS,
80};
81
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090082enum {
Namjae Jeon696c0182013-06-16 09:48:48 +090083 Opt_gc_background,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090084 Opt_disable_roll_forward,
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -080085 Opt_norecovery,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090086 Opt_discard,
Chao Yu64058be2016-07-03 22:05:14 +080087 Opt_nodiscard,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090088 Opt_noheap,
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -040089 Opt_heap,
Kelly Anderson4058c512013-10-07 11:36:20 +090090 Opt_user_xattr,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090091 Opt_nouser_xattr,
Kelly Anderson4058c512013-10-07 11:36:20 +090092 Opt_acl,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090093 Opt_noacl,
94 Opt_active_logs,
95 Opt_disable_ext_identify,
Jaegeuk Kim444c5802013-08-08 15:16:22 +090096 Opt_inline_xattr,
Chao Yu23cf7212017-02-15 10:34:45 +080097 Opt_noinline_xattr,
Chao Yu6afc6622017-09-06 21:59:50 +080098 Opt_inline_xattr_size,
Huajun Li8274de72013-11-10 23:13:17 +080099 Opt_inline_data,
Chao Yu5efd3c62014-09-24 18:16:13 +0800100 Opt_inline_dentry,
Chao Yu97c17942016-05-09 19:56:34 +0800101 Opt_noinline_dentry,
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900102 Opt_flush_merge,
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700103 Opt_noflush_merge,
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700104 Opt_nobarrier,
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700105 Opt_fastboot,
Chao Yu89672152015-02-05 17:55:51 +0800106 Opt_extent_cache,
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700107 Opt_noextent_cache,
Wanpeng Li75342792015-03-24 10:20:27 +0800108 Opt_noinline_data,
Chao Yu343f40f2015-12-16 13:12:16 +0800109 Opt_data_flush,
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700110 Opt_mode,
Jaegeuk Kimec915382016-12-21 17:09:19 -0800111 Opt_io_size_bits,
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700112 Opt_fault_injection,
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700113 Opt_lazytime,
114 Opt_nolazytime,
Chao Yu4b2414d2017-08-08 10:54:31 +0800115 Opt_quota,
116 Opt_noquota,
Chao Yu0abd6752017-07-09 00:13:07 +0800117 Opt_usrquota,
118 Opt_grpquota,
Chao Yu5c571322017-07-26 00:01:41 +0800119 Opt_prjquota,
Chao Yu4b2414d2017-08-08 10:54:31 +0800120 Opt_usrjquota,
121 Opt_grpjquota,
122 Opt_prjjquota,
123 Opt_offusrjquota,
124 Opt_offgrpjquota,
125 Opt_offprjjquota,
126 Opt_jqfmt_vfsold,
127 Opt_jqfmt_vfsv0,
128 Opt_jqfmt_vfsv1,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900129 Opt_err,
130};
131
132static match_table_t f2fs_tokens = {
Namjae Jeon696c0182013-06-16 09:48:48 +0900133 {Opt_gc_background, "background_gc=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900134 {Opt_disable_roll_forward, "disable_roll_forward"},
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800135 {Opt_norecovery, "norecovery"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900136 {Opt_discard, "discard"},
Chao Yu64058be2016-07-03 22:05:14 +0800137 {Opt_nodiscard, "nodiscard"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900138 {Opt_noheap, "no_heap"},
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400139 {Opt_heap, "heap"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900140 {Opt_user_xattr, "user_xattr"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900141 {Opt_nouser_xattr, "nouser_xattr"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900142 {Opt_acl, "acl"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900143 {Opt_noacl, "noacl"},
144 {Opt_active_logs, "active_logs=%u"},
145 {Opt_disable_ext_identify, "disable_ext_identify"},
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900146 {Opt_inline_xattr, "inline_xattr"},
Chao Yu23cf7212017-02-15 10:34:45 +0800147 {Opt_noinline_xattr, "noinline_xattr"},
Chao Yu6afc6622017-09-06 21:59:50 +0800148 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
Huajun Li8274de72013-11-10 23:13:17 +0800149 {Opt_inline_data, "inline_data"},
Chao Yu5efd3c62014-09-24 18:16:13 +0800150 {Opt_inline_dentry, "inline_dentry"},
Chao Yu97c17942016-05-09 19:56:34 +0800151 {Opt_noinline_dentry, "noinline_dentry"},
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900152 {Opt_flush_merge, "flush_merge"},
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700153 {Opt_noflush_merge, "noflush_merge"},
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700154 {Opt_nobarrier, "nobarrier"},
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700155 {Opt_fastboot, "fastboot"},
Chao Yu89672152015-02-05 17:55:51 +0800156 {Opt_extent_cache, "extent_cache"},
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700157 {Opt_noextent_cache, "noextent_cache"},
Wanpeng Li75342792015-03-24 10:20:27 +0800158 {Opt_noinline_data, "noinline_data"},
Chao Yu343f40f2015-12-16 13:12:16 +0800159 {Opt_data_flush, "data_flush"},
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700160 {Opt_mode, "mode=%s"},
Jaegeuk Kimec915382016-12-21 17:09:19 -0800161 {Opt_io_size_bits, "io_bits=%u"},
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700162 {Opt_fault_injection, "fault_injection=%u"},
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700163 {Opt_lazytime, "lazytime"},
164 {Opt_nolazytime, "nolazytime"},
Chao Yu4b2414d2017-08-08 10:54:31 +0800165 {Opt_quota, "quota"},
166 {Opt_noquota, "noquota"},
Chao Yu0abd6752017-07-09 00:13:07 +0800167 {Opt_usrquota, "usrquota"},
168 {Opt_grpquota, "grpquota"},
Chao Yu5c571322017-07-26 00:01:41 +0800169 {Opt_prjquota, "prjquota"},
Chao Yu4b2414d2017-08-08 10:54:31 +0800170 {Opt_usrjquota, "usrjquota=%s"},
171 {Opt_grpjquota, "grpjquota=%s"},
172 {Opt_prjjquota, "prjjquota=%s"},
173 {Opt_offusrjquota, "usrjquota="},
174 {Opt_offgrpjquota, "grpjquota="},
175 {Opt_offprjjquota, "prjjquota="},
176 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
177 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
178 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900179 {Opt_err, NULL},
180};
181
Namjae Jeona07ef782012-12-30 14:52:05 +0900182void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
183{
184 struct va_format vaf;
185 va_list args;
186
187 va_start(args, fmt);
188 vaf.fmt = fmt;
189 vaf.va = &args;
Jaegeuk Kima36c1062017-08-02 20:58:29 -0700190 printk_ratelimited("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
Namjae Jeona07ef782012-12-30 14:52:05 +0900191 va_end(args);
192}
193
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900194static void init_once(void *foo)
195{
196 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
197
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900198 inode_init_once(&fi->vfs_inode);
199}
200
Chao Yu4b2414d2017-08-08 10:54:31 +0800201#ifdef CONFIG_QUOTA
202static const char * const quotatypes[] = INITQFNAMES;
203#define QTYPE2NAME(t) (quotatypes[t])
204static int f2fs_set_qf_name(struct super_block *sb, int qtype,
205 substring_t *args)
206{
207 struct f2fs_sb_info *sbi = F2FS_SB(sb);
208 char *qname;
209 int ret = -EINVAL;
210
211 if (sb_any_quota_loaded(sb) && !sbi->s_qf_names[qtype]) {
212 f2fs_msg(sb, KERN_ERR,
213 "Cannot change journaled "
214 "quota options when quota turned on");
215 return -EINVAL;
216 }
Jaegeuk Kimea676732017-10-06 09:14:28 -0700217 if (f2fs_sb_has_quota_ino(sb)) {
218 f2fs_msg(sb, KERN_INFO,
219 "QUOTA feature is enabled, so ignore qf_name");
220 return 0;
221 }
222
Chao Yu4b2414d2017-08-08 10:54:31 +0800223 qname = match_strdup(args);
224 if (!qname) {
225 f2fs_msg(sb, KERN_ERR,
226 "Not enough memory for storing quotafile name");
227 return -EINVAL;
228 }
229 if (sbi->s_qf_names[qtype]) {
230 if (strcmp(sbi->s_qf_names[qtype], qname) == 0)
231 ret = 0;
232 else
233 f2fs_msg(sb, KERN_ERR,
234 "%s quota file already specified",
235 QTYPE2NAME(qtype));
236 goto errout;
237 }
238 if (strchr(qname, '/')) {
239 f2fs_msg(sb, KERN_ERR,
240 "quotafile must be on filesystem root");
241 goto errout;
242 }
243 sbi->s_qf_names[qtype] = qname;
244 set_opt(sbi, QUOTA);
245 return 0;
246errout:
247 kfree(qname);
248 return ret;
249}
250
251static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
252{
253 struct f2fs_sb_info *sbi = F2FS_SB(sb);
254
255 if (sb_any_quota_loaded(sb) && sbi->s_qf_names[qtype]) {
256 f2fs_msg(sb, KERN_ERR, "Cannot change journaled quota options"
257 " when quota turned on");
258 return -EINVAL;
259 }
260 kfree(sbi->s_qf_names[qtype]);
261 sbi->s_qf_names[qtype] = NULL;
262 return 0;
263}
264
265static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
266{
267 /*
268 * We do the test below only for project quotas. 'usrquota' and
269 * 'grpquota' mount options are allowed even without quota feature
270 * to support legacy quotas in quota files.
271 */
272 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi->sb)) {
273 f2fs_msg(sbi->sb, KERN_ERR, "Project quota feature not enabled. "
274 "Cannot enable project quota enforcement.");
275 return -1;
276 }
277 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA] ||
278 sbi->s_qf_names[PRJQUOTA]) {
279 if (test_opt(sbi, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
280 clear_opt(sbi, USRQUOTA);
281
282 if (test_opt(sbi, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
283 clear_opt(sbi, GRPQUOTA);
284
285 if (test_opt(sbi, PRJQUOTA) && sbi->s_qf_names[PRJQUOTA])
286 clear_opt(sbi, PRJQUOTA);
287
288 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
289 test_opt(sbi, PRJQUOTA)) {
290 f2fs_msg(sbi->sb, KERN_ERR, "old and new quota "
291 "format mixing");
292 return -1;
293 }
294
295 if (!sbi->s_jquota_fmt) {
296 f2fs_msg(sbi->sb, KERN_ERR, "journaled quota format "
297 "not specified");
298 return -1;
299 }
300 }
Jaegeuk Kimea676732017-10-06 09:14:28 -0700301
302 if (f2fs_sb_has_quota_ino(sbi->sb) && sbi->s_jquota_fmt) {
303 f2fs_msg(sbi->sb, KERN_INFO,
304 "QUOTA feature is enabled, so ignore jquota_fmt");
305 sbi->s_jquota_fmt = 0;
306 }
307 if (f2fs_sb_has_quota_ino(sbi->sb) && sb_rdonly(sbi->sb)) {
308 f2fs_msg(sbi->sb, KERN_INFO,
309 "Filesystem with quota feature cannot be mounted RDWR "
310 "without CONFIG_QUOTA");
311 return -1;
312 }
Chao Yu4b2414d2017-08-08 10:54:31 +0800313 return 0;
314}
315#endif
316
Namjae Jeon696c0182013-06-16 09:48:48 +0900317static int parse_options(struct super_block *sb, char *options)
318{
319 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yu09d54cd2015-06-08 13:20:10 +0800320 struct request_queue *q;
Namjae Jeon696c0182013-06-16 09:48:48 +0900321 substring_t args[MAX_OPT_ARGS];
322 char *p, *name;
323 int arg = 0;
Chao Yu4b2414d2017-08-08 10:54:31 +0800324#ifdef CONFIG_QUOTA
325 int ret;
326#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900327
328 if (!options)
329 return 0;
330
331 while ((p = strsep(&options, ",")) != NULL) {
332 int token;
333 if (!*p)
334 continue;
335 /*
336 * Initialize args struct so we know whether arg was
337 * found; some options take optional arguments.
338 */
339 args[0].to = args[0].from = NULL;
340 token = match_token(p, f2fs_tokens, args);
341
342 switch (token) {
343 case Opt_gc_background:
344 name = match_strdup(&args[0]);
345
346 if (!name)
347 return -ENOMEM;
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700348 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900349 set_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700350 clear_opt(sbi, FORCE_FG_GC);
351 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900352 clear_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700353 clear_opt(sbi, FORCE_FG_GC);
354 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
355 set_opt(sbi, BG_GC);
356 set_opt(sbi, FORCE_FG_GC);
357 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +0900358 kfree(name);
359 return -EINVAL;
360 }
361 kfree(name);
362 break;
363 case Opt_disable_roll_forward:
364 set_opt(sbi, DISABLE_ROLL_FORWARD);
365 break;
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800366 case Opt_norecovery:
367 /* this option mounts f2fs with ro */
368 set_opt(sbi, DISABLE_ROLL_FORWARD);
369 if (!f2fs_readonly(sb))
370 return -EINVAL;
371 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900372 case Opt_discard:
Chao Yu09d54cd2015-06-08 13:20:10 +0800373 q = bdev_get_queue(sb->s_bdev);
374 if (blk_queue_discard(q)) {
375 set_opt(sbi, DISCARD);
Damien Le Moal0ab02992016-10-28 17:45:02 +0900376 } else if (!f2fs_sb_mounted_blkzoned(sb)) {
Chao Yu09d54cd2015-06-08 13:20:10 +0800377 f2fs_msg(sb, KERN_WARNING,
378 "mounting with \"discard\" option, but "
379 "the device does not support discard");
380 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900381 break;
Chao Yu64058be2016-07-03 22:05:14 +0800382 case Opt_nodiscard:
Damien Le Moal96ba2de2016-10-28 17:45:03 +0900383 if (f2fs_sb_mounted_blkzoned(sb)) {
384 f2fs_msg(sb, KERN_WARNING,
385 "discard is required for zoned block devices");
386 return -EINVAL;
387 }
Chao Yu64058be2016-07-03 22:05:14 +0800388 clear_opt(sbi, DISCARD);
Damien Le Moal487df612016-10-28 17:44:59 +0900389 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900390 case Opt_noheap:
391 set_opt(sbi, NOHEAP);
392 break;
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400393 case Opt_heap:
394 clear_opt(sbi, NOHEAP);
395 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900396#ifdef CONFIG_F2FS_FS_XATTR
Kelly Anderson4058c512013-10-07 11:36:20 +0900397 case Opt_user_xattr:
398 set_opt(sbi, XATTR_USER);
399 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900400 case Opt_nouser_xattr:
401 clear_opt(sbi, XATTR_USER);
402 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900403 case Opt_inline_xattr:
404 set_opt(sbi, INLINE_XATTR);
405 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800406 case Opt_noinline_xattr:
407 clear_opt(sbi, INLINE_XATTR);
408 break;
Chao Yu6afc6622017-09-06 21:59:50 +0800409 case Opt_inline_xattr_size:
410 if (args->from && match_int(args, &arg))
411 return -EINVAL;
412 set_opt(sbi, INLINE_XATTR_SIZE);
413 sbi->inline_xattr_size = arg;
414 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900415#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900416 case Opt_user_xattr:
417 f2fs_msg(sb, KERN_INFO,
418 "user_xattr options not supported");
419 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900420 case Opt_nouser_xattr:
421 f2fs_msg(sb, KERN_INFO,
422 "nouser_xattr options not supported");
423 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900424 case Opt_inline_xattr:
425 f2fs_msg(sb, KERN_INFO,
426 "inline_xattr options not supported");
427 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800428 case Opt_noinline_xattr:
429 f2fs_msg(sb, KERN_INFO,
430 "noinline_xattr options not supported");
431 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900432#endif
433#ifdef CONFIG_F2FS_FS_POSIX_ACL
Kelly Anderson4058c512013-10-07 11:36:20 +0900434 case Opt_acl:
435 set_opt(sbi, POSIX_ACL);
436 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900437 case Opt_noacl:
438 clear_opt(sbi, POSIX_ACL);
439 break;
440#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900441 case Opt_acl:
442 f2fs_msg(sb, KERN_INFO, "acl options not supported");
443 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900444 case Opt_noacl:
445 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
446 break;
447#endif
448 case Opt_active_logs:
449 if (args->from && match_int(args, &arg))
450 return -EINVAL;
451 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
452 return -EINVAL;
453 sbi->active_logs = arg;
454 break;
455 case Opt_disable_ext_identify:
456 set_opt(sbi, DISABLE_EXT_IDENTIFY);
457 break;
Huajun Li8274de72013-11-10 23:13:17 +0800458 case Opt_inline_data:
459 set_opt(sbi, INLINE_DATA);
460 break;
Chao Yu5efd3c62014-09-24 18:16:13 +0800461 case Opt_inline_dentry:
462 set_opt(sbi, INLINE_DENTRY);
463 break;
Chao Yu97c17942016-05-09 19:56:34 +0800464 case Opt_noinline_dentry:
465 clear_opt(sbi, INLINE_DENTRY);
466 break;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900467 case Opt_flush_merge:
468 set_opt(sbi, FLUSH_MERGE);
469 break;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700470 case Opt_noflush_merge:
471 clear_opt(sbi, FLUSH_MERGE);
472 break;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700473 case Opt_nobarrier:
474 set_opt(sbi, NOBARRIER);
475 break;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700476 case Opt_fastboot:
477 set_opt(sbi, FASTBOOT);
478 break;
Chao Yu89672152015-02-05 17:55:51 +0800479 case Opt_extent_cache:
480 set_opt(sbi, EXTENT_CACHE);
481 break;
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700482 case Opt_noextent_cache:
483 clear_opt(sbi, EXTENT_CACHE);
484 break;
Wanpeng Li75342792015-03-24 10:20:27 +0800485 case Opt_noinline_data:
486 clear_opt(sbi, INLINE_DATA);
487 break;
Chao Yu343f40f2015-12-16 13:12:16 +0800488 case Opt_data_flush:
489 set_opt(sbi, DATA_FLUSH);
490 break;
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700491 case Opt_mode:
492 name = match_strdup(&args[0]);
493
494 if (!name)
495 return -ENOMEM;
496 if (strlen(name) == 8 &&
497 !strncmp(name, "adaptive", 8)) {
Damien Le Moal3adc57e2016-10-28 17:45:04 +0900498 if (f2fs_sb_mounted_blkzoned(sb)) {
499 f2fs_msg(sb, KERN_WARNING,
500 "adaptive mode is not allowed with "
501 "zoned block device feature");
502 kfree(name);
503 return -EINVAL;
504 }
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700505 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700506 } else if (strlen(name) == 3 &&
507 !strncmp(name, "lfs", 3)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700508 set_opt_mode(sbi, F2FS_MOUNT_LFS);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700509 } else {
510 kfree(name);
511 return -EINVAL;
512 }
513 kfree(name);
514 break;
Jaegeuk Kimec915382016-12-21 17:09:19 -0800515 case Opt_io_size_bits:
516 if (args->from && match_int(args, &arg))
517 return -EINVAL;
518 if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
519 f2fs_msg(sb, KERN_WARNING,
520 "Not support %d, larger than %d",
521 1 << arg, BIO_MAX_PAGES);
522 return -EINVAL;
523 }
524 sbi->write_io_size_bits = arg;
525 break;
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700526 case Opt_fault_injection:
527 if (args->from && match_int(args, &arg))
528 return -EINVAL;
529#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu1ecc0c52016-09-23 21:30:09 +0800530 f2fs_build_fault_attr(sbi, arg);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800531 set_opt(sbi, FAULT_INJECTION);
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700532#else
533 f2fs_msg(sb, KERN_INFO,
534 "FAULT_INJECTION was not selected");
535#endif
536 break;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700537 case Opt_lazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800538 sb->s_flags |= SB_LAZYTIME;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700539 break;
540 case Opt_nolazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800541 sb->s_flags &= ~SB_LAZYTIME;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700542 break;
Chao Yu0abd6752017-07-09 00:13:07 +0800543#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +0800544 case Opt_quota:
Chao Yu0abd6752017-07-09 00:13:07 +0800545 case Opt_usrquota:
546 set_opt(sbi, USRQUOTA);
547 break;
548 case Opt_grpquota:
549 set_opt(sbi, GRPQUOTA);
550 break;
Chao Yu5c571322017-07-26 00:01:41 +0800551 case Opt_prjquota:
552 set_opt(sbi, PRJQUOTA);
553 break;
Chao Yu4b2414d2017-08-08 10:54:31 +0800554 case Opt_usrjquota:
555 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
556 if (ret)
557 return ret;
558 break;
559 case Opt_grpjquota:
560 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
561 if (ret)
562 return ret;
563 break;
564 case Opt_prjjquota:
565 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
566 if (ret)
567 return ret;
568 break;
569 case Opt_offusrjquota:
570 ret = f2fs_clear_qf_name(sb, USRQUOTA);
571 if (ret)
572 return ret;
573 break;
574 case Opt_offgrpjquota:
575 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
576 if (ret)
577 return ret;
578 break;
579 case Opt_offprjjquota:
580 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
581 if (ret)
582 return ret;
583 break;
584 case Opt_jqfmt_vfsold:
585 sbi->s_jquota_fmt = QFMT_VFS_OLD;
586 break;
587 case Opt_jqfmt_vfsv0:
588 sbi->s_jquota_fmt = QFMT_VFS_V0;
589 break;
590 case Opt_jqfmt_vfsv1:
591 sbi->s_jquota_fmt = QFMT_VFS_V1;
592 break;
593 case Opt_noquota:
594 clear_opt(sbi, QUOTA);
595 clear_opt(sbi, USRQUOTA);
596 clear_opt(sbi, GRPQUOTA);
597 clear_opt(sbi, PRJQUOTA);
598 break;
Chao Yu0abd6752017-07-09 00:13:07 +0800599#else
Chao Yu4b2414d2017-08-08 10:54:31 +0800600 case Opt_quota:
Chao Yu0abd6752017-07-09 00:13:07 +0800601 case Opt_usrquota:
602 case Opt_grpquota:
Chao Yu5c571322017-07-26 00:01:41 +0800603 case Opt_prjquota:
Chao Yu4b2414d2017-08-08 10:54:31 +0800604 case Opt_usrjquota:
605 case Opt_grpjquota:
606 case Opt_prjjquota:
607 case Opt_offusrjquota:
608 case Opt_offgrpjquota:
609 case Opt_offprjjquota:
610 case Opt_jqfmt_vfsold:
611 case Opt_jqfmt_vfsv0:
612 case Opt_jqfmt_vfsv1:
613 case Opt_noquota:
Chao Yu0abd6752017-07-09 00:13:07 +0800614 f2fs_msg(sb, KERN_INFO,
615 "quota operations not supported");
616 break;
617#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900618 default:
619 f2fs_msg(sb, KERN_ERR,
620 "Unrecognized mount option \"%s\" or missing value",
621 p);
622 return -EINVAL;
623 }
624 }
Chao Yu4b2414d2017-08-08 10:54:31 +0800625#ifdef CONFIG_QUOTA
626 if (f2fs_check_quota_options(sbi))
627 return -EINVAL;
628#endif
Jaegeuk Kimec915382016-12-21 17:09:19 -0800629
630 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
631 f2fs_msg(sb, KERN_ERR,
632 "Should set mode=lfs with %uKB-sized IO",
633 F2FS_IO_SIZE_KB(sbi));
634 return -EINVAL;
635 }
Chao Yu6afc6622017-09-06 21:59:50 +0800636
637 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
638 if (!test_opt(sbi, INLINE_XATTR)) {
639 f2fs_msg(sb, KERN_ERR,
640 "inline_xattr_size option should be "
641 "set with inline_xattr option");
642 return -EINVAL;
643 }
644 if (!sbi->inline_xattr_size ||
645 sbi->inline_xattr_size >= DEF_ADDRS_PER_INODE -
646 F2FS_TOTAL_EXTRA_ATTR_SIZE -
647 DEF_INLINE_RESERVED_SIZE -
648 DEF_MIN_INLINE_SIZE) {
649 f2fs_msg(sb, KERN_ERR,
650 "inline xattr size is out of range");
651 return -EINVAL;
652 }
653 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900654 return 0;
655}
656
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900657static struct inode *f2fs_alloc_inode(struct super_block *sb)
658{
659 struct f2fs_inode_info *fi;
660
Chao Yua0acdfe2013-12-05 09:54:00 +0800661 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900662 if (!fi)
663 return NULL;
664
665 init_once((void *) fi);
666
Masanari Iida434720f2013-03-19 08:03:35 +0900667 /* Initialize f2fs-specific inode info */
Jaegeuk Kim204706c2016-12-02 15:11:32 -0800668 atomic_set(&fi->dirty_pages, 0);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900669 fi->i_current_depth = 1;
670 fi->i_advise = 0;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900671 init_rwsem(&fi->i_sem);
Chao Yu2710fd72015-12-15 13:30:45 +0800672 INIT_LIST_HEAD(&fi->dirty_list);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700673 INIT_LIST_HEAD(&fi->gdirty_list);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -0700674 INIT_LIST_HEAD(&fi->inmem_ilist);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700675 INIT_LIST_HEAD(&fi->inmem_pages);
676 mutex_init(&fi->inmem_lock);
Chao Yu82e0a5a2016-07-13 09:18:29 +0800677 init_rwsem(&fi->dio_rwsem[READ]);
678 init_rwsem(&fi->dio_rwsem[WRITE]);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800679 init_rwsem(&fi->i_mmap_sem);
Yunlei He27161f12017-09-07 10:40:54 +0800680 init_rwsem(&fi->i_xattr_sem);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900681
Chao Yu0abd6752017-07-09 00:13:07 +0800682#ifdef CONFIG_QUOTA
683 memset(&fi->i_dquot, 0, sizeof(fi->i_dquot));
684 fi->i_reserved_quota = 0;
685#endif
Jaegeuk Kimab9fa662014-02-27 20:09:05 +0900686 /* Will be used by directory only */
687 fi->i_dir_level = F2FS_SB(sb)->dir_level;
Chao Yuf2470372017-07-19 00:19:05 +0800688
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900689 return &fi->vfs_inode;
690}
691
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900692static int f2fs_drop_inode(struct inode *inode)
693{
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000694 int ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900695 /*
696 * This is to avoid a deadlock condition like below.
697 * writeback_single_inode(inode)
698 * - f2fs_write_data_page
699 * - f2fs_gc -> iput -> evict
700 * - inode_wait_for_writeback(inode)
701 */
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700702 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700703 if (!inode->i_nlink && !is_bad_inode(inode)) {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700704 /* to avoid evict_inode call simultaneously */
705 atomic_inc(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700706 spin_unlock(&inode->i_lock);
707
708 /* some remained atomic pages should discarded */
709 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +0800710 drop_inmem_pages(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700711
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700712 /* should remain fi->extent_tree for writepage */
713 f2fs_destroy_extent_node(inode);
714
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700715 sb_start_intwrite(inode->i_sb);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700716 f2fs_i_size_write(inode, 0);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700717
718 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700719 f2fs_truncate(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700720
721 sb_end_intwrite(inode->i_sb);
722
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700723 spin_lock(&inode->i_lock);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700724 atomic_dec(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700725 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000726 trace_f2fs_drop_inode(inode, 0);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900727 return 0;
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700728 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000729 ret = generic_drop_inode(inode);
730 trace_f2fs_drop_inode(inode, ret);
731 return ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900732}
733
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700734int f2fs_inode_dirtied(struct inode *inode, bool sync)
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700735{
736 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700737 int ret = 0;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700738
739 spin_lock(&sbi->inode_lock[DIRTY_META]);
740 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700741 ret = 1;
742 } else {
743 set_inode_flag(inode, FI_DIRTY_INODE);
744 stat_inc_dirty_inode(sbi, DIRTY_META);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700745 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700746 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
747 list_add_tail(&F2FS_I(inode)->gdirty_list,
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700748 &sbi->inode_list[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700749 inc_page_count(sbi, F2FS_DIRTY_IMETA);
750 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700751 spin_unlock(&sbi->inode_lock[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700752 return ret;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700753}
754
755void f2fs_inode_synced(struct inode *inode)
756{
757 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
758
759 spin_lock(&sbi->inode_lock[DIRTY_META]);
760 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
761 spin_unlock(&sbi->inode_lock[DIRTY_META]);
762 return;
763 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700764 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
765 list_del_init(&F2FS_I(inode)->gdirty_list);
766 dec_page_count(sbi, F2FS_DIRTY_IMETA);
767 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700768 clear_inode_flag(inode, FI_DIRTY_INODE);
769 clear_inode_flag(inode, FI_AUTO_RECOVER);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700770 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
771 spin_unlock(&sbi->inode_lock[DIRTY_META]);
772}
773
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900774/*
775 * f2fs_dirty_inode() is called from __mark_inode_dirty()
776 *
777 * We should call set_dirty_inode to write the dirty inode through write_inode.
778 */
779static void f2fs_dirty_inode(struct inode *inode, int flags)
780{
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700781 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
782
783 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
784 inode->i_ino == F2FS_META_INO(sbi))
785 return;
786
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700787 if (flags == I_DIRTY_TIME)
788 return;
789
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700790 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
791 clear_inode_flag(inode, FI_AUTO_RECOVER);
792
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700793 f2fs_inode_dirtied(inode, false);
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900794}
795
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900796static void f2fs_i_callback(struct rcu_head *head)
797{
798 struct inode *inode = container_of(head, struct inode, i_rcu);
799 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
800}
801
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900802static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900803{
804 call_rcu(&inode->i_rcu, f2fs_i_callback);
805}
806
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700807static void destroy_percpu_info(struct f2fs_sb_info *sbi)
808{
Jaegeuk Kim41382ec2016-05-16 11:06:50 -0700809 percpu_counter_destroy(&sbi->alloc_valid_block_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -0700810 percpu_counter_destroy(&sbi->total_valid_inode_count);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700811}
812
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700813static void destroy_device_list(struct f2fs_sb_info *sbi)
814{
815 int i;
816
817 for (i = 0; i < sbi->s_ndevs; i++) {
818 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
819#ifdef CONFIG_BLK_DEV_ZONED
820 kfree(FDEV(i).blkz_type);
821#endif
822 }
823 kfree(sbi->devs);
824}
825
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900826static void f2fs_put_super(struct super_block *sb)
827{
828 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yua3981012017-06-14 17:39:46 +0800829 int i;
Chao Yucf5c7592017-10-04 09:08:37 +0800830 bool dropped;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900831
Chao Yu0abd6752017-07-09 00:13:07 +0800832 f2fs_quota_off_umount(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900833
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700834 /* prevent remaining shrinker jobs */
835 mutex_lock(&sbi->umount_mutex);
836
Jaegeuk Kim85dc2f22015-01-14 17:41:41 -0800837 /*
838 * We don't need to do checkpoint when superblock is clean.
839 * But, the previous checkpoint was not done by umount, it needs to do
840 * clean checkpoint again.
841 */
Chao Yucaf00472015-01-28 17:48:42 +0800842 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
Chao Yuaaec2b12016-09-20 11:04:18 +0800843 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700844 struct cp_control cpc = {
845 .reason = CP_UMOUNT,
846 };
847 write_checkpoint(sbi, &cpc);
848 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900849
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -0800850 /* be sure to wait for any on-going discard commands */
Chao Yucf5c7592017-10-04 09:08:37 +0800851 dropped = f2fs_wait_discard_bios(sbi);
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -0800852
Chao Yucf5c7592017-10-04 09:08:37 +0800853 if (f2fs_discard_en(sbi) && !sbi->discard_blks && !dropped) {
Chao Yu1f43e2a2017-04-28 13:56:08 +0800854 struct cp_control cpc = {
855 .reason = CP_UMOUNT | CP_TRIMMED,
856 };
857 write_checkpoint(sbi, &cpc);
858 }
859
Jaegeuk Kimeca616f2015-06-15 14:52:29 -0700860 /* write_checkpoint can update stat informaion */
861 f2fs_destroy_stats(sbi);
862
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700863 /*
864 * normally superblock is clean, so we need to release this.
865 * In addition, EIO will skip do checkpoint, we need this as well.
866 */
Jaegeuk Kim74ef9242016-05-02 22:09:56 -0700867 release_ino_entry(sbi, true);
Jaegeuk Kim6f12ac22014-08-19 09:48:22 -0700868
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700869 f2fs_leave_shrinker(sbi);
870 mutex_unlock(&sbi->umount_mutex);
871
Jaegeuk Kim17c19122016-01-29 08:57:59 -0800872 /* our cp_error case, we can wait for any writeback page */
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700873 f2fs_flush_merged_writes(sbi);
Jaegeuk Kim17c19122016-01-29 08:57:59 -0800874
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900875 iput(sbi->node_inode);
876 iput(sbi->meta_inode);
877
878 /* destroy f2fs internal modules */
879 destroy_node_manager(sbi);
880 destroy_segment_manager(sbi);
881
882 kfree(sbi->ckpt);
Chao Yua3981012017-06-14 17:39:46 +0800883
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -0700884 f2fs_unregister_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900885
886 sb->s_fs_info = NULL;
Keith Mok43b65732016-03-02 12:04:24 -0800887 if (sbi->s_chksum_driver)
888 crypto_free_shash(sbi->s_chksum_driver);
Yunlei Heb39f0de2015-12-15 17:17:20 +0800889 kfree(sbi->raw_super);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700890
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700891 destroy_device_list(sbi);
Chao Yub6895e82017-02-27 18:43:12 +0800892 mempool_destroy(sbi->write_io_dummy);
Chao Yu4b2414d2017-08-08 10:54:31 +0800893#ifdef CONFIG_QUOTA
894 for (i = 0; i < MAXQUOTAS; i++)
895 kfree(sbi->s_qf_names[i]);
896#endif
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700897 destroy_percpu_info(sbi);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700898 for (i = 0; i < NR_PAGE_TYPE; i++)
899 kfree(sbi->write_io[i]);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900900 kfree(sbi);
901}
902
903int f2fs_sync_fs(struct super_block *sb, int sync)
904{
905 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yuc34f42e2015-12-23 17:50:30 +0800906 int err = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900907
Jaegeuk Kim1f227a32017-10-23 23:48:49 +0200908 if (unlikely(f2fs_cp_error(sbi)))
909 return 0;
910
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900911 trace_f2fs_sync_fs(sb, sync);
912
Chao Yu4b2414d2017-08-08 10:54:31 +0800913 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
914 return -EAGAIN;
915
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900916 if (sync) {
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700917 struct cp_control cpc;
918
Jaegeuk Kim119ee912015-01-29 11:45:33 -0800919 cpc.reason = __get_cp_reason(sbi);
920
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900921 mutex_lock(&sbi->gc_mutex);
Chao Yuc34f42e2015-12-23 17:50:30 +0800922 err = write_checkpoint(sbi, &cpc);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900923 mutex_unlock(&sbi->gc_mutex);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900924 }
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700925 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900926
Chao Yuc34f42e2015-12-23 17:50:30 +0800927 return err;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900928}
929
Changman Leed6212a52013-01-29 18:30:07 +0900930static int f2fs_freeze(struct super_block *sb)
931{
Jaegeuk Kim77888c12013-05-20 20:28:47 +0900932 if (f2fs_readonly(sb))
Changman Leed6212a52013-01-29 18:30:07 +0900933 return 0;
934
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -0700935 /* IO error happened before */
936 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
937 return -EIO;
938
939 /* must be clean, since sync_filesystem() was already called */
940 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
941 return -EINVAL;
942 return 0;
Changman Leed6212a52013-01-29 18:30:07 +0900943}
944
945static int f2fs_unfreeze(struct super_block *sb)
946{
947 return 0;
948}
949
Chao Yuddc34e32017-07-29 00:32:53 +0800950#ifdef CONFIG_QUOTA
951static int f2fs_statfs_project(struct super_block *sb,
952 kprojid_t projid, struct kstatfs *buf)
953{
954 struct kqid qid;
955 struct dquot *dquot;
956 u64 limit;
957 u64 curblock;
958
959 qid = make_kqid_projid(projid);
960 dquot = dqget(sb, qid);
961 if (IS_ERR(dquot))
962 return PTR_ERR(dquot);
963 spin_lock(&dq_data_lock);
964
965 limit = (dquot->dq_dqb.dqb_bsoftlimit ?
966 dquot->dq_dqb.dqb_bsoftlimit :
967 dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
968 if (limit && buf->f_blocks > limit) {
969 curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits;
970 buf->f_blocks = limit;
971 buf->f_bfree = buf->f_bavail =
972 (buf->f_blocks > curblock) ?
973 (buf->f_blocks - curblock) : 0;
974 }
975
976 limit = dquot->dq_dqb.dqb_isoftlimit ?
977 dquot->dq_dqb.dqb_isoftlimit :
978 dquot->dq_dqb.dqb_ihardlimit;
979 if (limit && buf->f_files > limit) {
980 buf->f_files = limit;
981 buf->f_ffree =
982 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
983 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
984 }
985
986 spin_unlock(&dq_data_lock);
987 dqput(dquot);
988 return 0;
989}
990#endif
991
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900992static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
993{
994 struct super_block *sb = dentry->d_sb;
995 struct f2fs_sb_info *sbi = F2FS_SB(sb);
996 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
997 block_t total_count, user_block_count, start_count, ovp_count;
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -0700998 u64 avail_node_count;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900999
1000 total_count = le64_to_cpu(sbi->raw_super->block_count);
1001 user_block_count = sbi->user_block_count;
1002 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
1003 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
1004 buf->f_type = F2FS_SUPER_MAGIC;
1005 buf->f_bsize = sbi->blocksize;
1006
1007 buf->f_blocks = total_count - start_count;
Chao Yu3e6d0b42016-07-06 14:13:07 +08001008 buf->f_bfree = user_block_count - valid_user_blocks(sbi) + ovp_count;
Chao Yudaeb4332017-06-26 16:24:41 +08001009 buf->f_bavail = user_block_count - valid_user_blocks(sbi) -
Yunlong Song80d42142017-10-27 20:45:05 +08001010 sbi->current_reserved_blocks;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001011
Chao Yu292c196a2017-11-16 16:59:14 +08001012 avail_node_count = sbi->total_node_count - sbi->nquota_files -
1013 F2FS_RESERVED_NODE_NUM;
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -07001014
1015 if (avail_node_count > user_block_count) {
1016 buf->f_files = user_block_count;
1017 buf->f_ffree = buf->f_bavail;
1018 } else {
1019 buf->f_files = avail_node_count;
1020 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
1021 buf->f_bavail);
1022 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001023
Jaegeuk Kim5a20d332013-03-03 13:58:05 +09001024 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001025 buf->f_fsid.val[0] = (u32)id;
1026 buf->f_fsid.val[1] = (u32)(id >> 32);
1027
Chao Yuddc34e32017-07-29 00:32:53 +08001028#ifdef CONFIG_QUOTA
1029 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1030 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1031 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1032 }
1033#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001034 return 0;
1035}
1036
Chao Yu4b2414d2017-08-08 10:54:31 +08001037static inline void f2fs_show_quota_options(struct seq_file *seq,
1038 struct super_block *sb)
1039{
1040#ifdef CONFIG_QUOTA
1041 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1042
1043 if (sbi->s_jquota_fmt) {
1044 char *fmtname = "";
1045
1046 switch (sbi->s_jquota_fmt) {
1047 case QFMT_VFS_OLD:
1048 fmtname = "vfsold";
1049 break;
1050 case QFMT_VFS_V0:
1051 fmtname = "vfsv0";
1052 break;
1053 case QFMT_VFS_V1:
1054 fmtname = "vfsv1";
1055 break;
1056 }
1057 seq_printf(seq, ",jqfmt=%s", fmtname);
1058 }
1059
1060 if (sbi->s_qf_names[USRQUOTA])
1061 seq_show_option(seq, "usrjquota", sbi->s_qf_names[USRQUOTA]);
1062
1063 if (sbi->s_qf_names[GRPQUOTA])
1064 seq_show_option(seq, "grpjquota", sbi->s_qf_names[GRPQUOTA]);
1065
1066 if (sbi->s_qf_names[PRJQUOTA])
1067 seq_show_option(seq, "prjjquota", sbi->s_qf_names[PRJQUOTA]);
1068#endif
1069}
1070
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001071static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1072{
1073 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1074
Jaegeuk Kim6aefd932015-10-05 11:02:54 -07001075 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
1076 if (test_opt(sbi, FORCE_FG_GC))
1077 seq_printf(seq, ",background_gc=%s", "sync");
1078 else
1079 seq_printf(seq, ",background_gc=%s", "on");
1080 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +09001081 seq_printf(seq, ",background_gc=%s", "off");
Jaegeuk Kim6aefd932015-10-05 11:02:54 -07001082 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001083 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1084 seq_puts(seq, ",disable_roll_forward");
1085 if (test_opt(sbi, DISCARD))
1086 seq_puts(seq, ",discard");
1087 if (test_opt(sbi, NOHEAP))
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001088 seq_puts(seq, ",no_heap");
1089 else
1090 seq_puts(seq, ",heap");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001091#ifdef CONFIG_F2FS_FS_XATTR
1092 if (test_opt(sbi, XATTR_USER))
1093 seq_puts(seq, ",user_xattr");
1094 else
1095 seq_puts(seq, ",nouser_xattr");
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001096 if (test_opt(sbi, INLINE_XATTR))
1097 seq_puts(seq, ",inline_xattr");
Chao Yu23cf7212017-02-15 10:34:45 +08001098 else
1099 seq_puts(seq, ",noinline_xattr");
Chao Yu6afc6622017-09-06 21:59:50 +08001100 if (test_opt(sbi, INLINE_XATTR_SIZE))
1101 seq_printf(seq, ",inline_xattr_size=%u",
1102 sbi->inline_xattr_size);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001103#endif
1104#ifdef CONFIG_F2FS_FS_POSIX_ACL
1105 if (test_opt(sbi, POSIX_ACL))
1106 seq_puts(seq, ",acl");
1107 else
1108 seq_puts(seq, ",noacl");
1109#endif
1110 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +01001111 seq_puts(seq, ",disable_ext_identify");
Huajun Li8274de72013-11-10 23:13:17 +08001112 if (test_opt(sbi, INLINE_DATA))
1113 seq_puts(seq, ",inline_data");
Wanpeng Li75342792015-03-24 10:20:27 +08001114 else
1115 seq_puts(seq, ",noinline_data");
Chao Yu5efd3c62014-09-24 18:16:13 +08001116 if (test_opt(sbi, INLINE_DENTRY))
1117 seq_puts(seq, ",inline_dentry");
Chao Yu97c17942016-05-09 19:56:34 +08001118 else
1119 seq_puts(seq, ",noinline_dentry");
Gu Zhengb270ad62014-04-11 17:49:55 +08001120 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09001121 seq_puts(seq, ",flush_merge");
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -07001122 if (test_opt(sbi, NOBARRIER))
1123 seq_puts(seq, ",nobarrier");
Jaegeuk Kimd5053a342014-10-30 22:47:03 -07001124 if (test_opt(sbi, FASTBOOT))
1125 seq_puts(seq, ",fastboot");
Chao Yu89672152015-02-05 17:55:51 +08001126 if (test_opt(sbi, EXTENT_CACHE))
1127 seq_puts(seq, ",extent_cache");
Jaegeuk Kim7daaea22015-06-25 17:43:04 -07001128 else
1129 seq_puts(seq, ",noextent_cache");
Chao Yu343f40f2015-12-16 13:12:16 +08001130 if (test_opt(sbi, DATA_FLUSH))
1131 seq_puts(seq, ",data_flush");
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001132
1133 seq_puts(seq, ",mode=");
1134 if (test_opt(sbi, ADAPTIVE))
1135 seq_puts(seq, "adaptive");
1136 else if (test_opt(sbi, LFS))
1137 seq_puts(seq, "lfs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001138 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
Jaegeuk Kimec915382016-12-21 17:09:19 -08001139 if (F2FS_IO_SIZE_BITS(sbi))
1140 seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
Kaixu Xia0cc0dec2017-01-27 09:35:37 +08001141#ifdef CONFIG_F2FS_FAULT_INJECTION
1142 if (test_opt(sbi, FAULT_INJECTION))
Chao Yu44529f82017-06-12 09:44:24 +08001143 seq_printf(seq, ",fault_injection=%u",
1144 sbi->fault_info.inject_rate);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +08001145#endif
Chao Yu0abd6752017-07-09 00:13:07 +08001146#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +08001147 if (test_opt(sbi, QUOTA))
1148 seq_puts(seq, ",quota");
Chao Yu0abd6752017-07-09 00:13:07 +08001149 if (test_opt(sbi, USRQUOTA))
1150 seq_puts(seq, ",usrquota");
1151 if (test_opt(sbi, GRPQUOTA))
1152 seq_puts(seq, ",grpquota");
Chao Yu5c571322017-07-26 00:01:41 +08001153 if (test_opt(sbi, PRJQUOTA))
1154 seq_puts(seq, ",prjquota");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001155#endif
Chao Yu4b2414d2017-08-08 10:54:31 +08001156 f2fs_show_quota_options(seq, sbi->sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001157
1158 return 0;
1159}
1160
Yunlei He498c5e92015-05-07 18:11:37 +08001161static void default_options(struct f2fs_sb_info *sbi)
1162{
1163 /* init some FS parameters */
1164 sbi->active_logs = NR_CURSEG_TYPE;
Chao Yu6afc6622017-09-06 21:59:50 +08001165 sbi->inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
Yunlei He498c5e92015-05-07 18:11:37 +08001166
1167 set_opt(sbi, BG_GC);
Chao Yu39133a52017-02-08 17:39:44 +08001168 set_opt(sbi, INLINE_XATTR);
Yunlei He498c5e92015-05-07 18:11:37 +08001169 set_opt(sbi, INLINE_DATA);
Chao Yu97c17942016-05-09 19:56:34 +08001170 set_opt(sbi, INLINE_DENTRY);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07001171 set_opt(sbi, EXTENT_CACHE);
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001172 set_opt(sbi, NOHEAP);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001173 sbi->sb->s_flags |= SB_LAZYTIME;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -07001174 set_opt(sbi, FLUSH_MERGE);
Damien Le Moal0bfd7a02016-10-28 17:45:00 +09001175 if (f2fs_sb_mounted_blkzoned(sbi->sb)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -07001176 set_opt_mode(sbi, F2FS_MOUNT_LFS);
1177 set_opt(sbi, DISCARD);
1178 } else {
1179 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
1180 }
Yunlei He498c5e92015-05-07 18:11:37 +08001181
1182#ifdef CONFIG_F2FS_FS_XATTR
1183 set_opt(sbi, XATTR_USER);
1184#endif
1185#ifdef CONFIG_F2FS_FS_POSIX_ACL
1186 set_opt(sbi, POSIX_ACL);
1187#endif
Chao Yu36dbd322016-09-26 19:45:05 +08001188
1189#ifdef CONFIG_F2FS_FAULT_INJECTION
1190 f2fs_build_fault_attr(sbi, 0);
1191#endif
Yunlei He498c5e92015-05-07 18:11:37 +08001192}
1193
Jaegeuk Kimea676732017-10-06 09:14:28 -07001194#ifdef CONFIG_QUOTA
1195static int f2fs_enable_quotas(struct super_block *sb);
1196#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001197static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1198{
1199 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1200 struct f2fs_mount_info org_mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +08001201 unsigned long old_sb_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +09001202 int err, active_logs;
Gu Zheng876dc592014-04-11 17:50:00 +08001203 bool need_restart_gc = false;
1204 bool need_stop_gc = false;
Chao Yu9cd81ce2015-09-18 16:55:26 +08001205 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
Chao Yu2443b8b2016-09-26 19:45:06 +08001206#ifdef CONFIG_F2FS_FAULT_INJECTION
1207 struct f2fs_fault_info ffi = sbi->fault_info;
1208#endif
Chao Yu4b2414d2017-08-08 10:54:31 +08001209#ifdef CONFIG_QUOTA
1210 int s_jquota_fmt;
1211 char *s_qf_names[MAXQUOTAS];
1212 int i, j;
1213#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001214
1215 /*
1216 * Save the old mount options in case we
1217 * need to restore them.
1218 */
1219 org_mount_opt = sbi->mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +08001220 old_sb_flags = sb->s_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +09001221 active_logs = sbi->active_logs;
1222
Chao Yu4b2414d2017-08-08 10:54:31 +08001223#ifdef CONFIG_QUOTA
1224 s_jquota_fmt = sbi->s_jquota_fmt;
1225 for (i = 0; i < MAXQUOTAS; i++) {
1226 if (sbi->s_qf_names[i]) {
1227 s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
1228 GFP_KERNEL);
1229 if (!s_qf_names[i]) {
1230 for (j = 0; j < i; j++)
1231 kfree(s_qf_names[j]);
1232 return -ENOMEM;
1233 }
1234 } else {
1235 s_qf_names[i] = NULL;
1236 }
1237 }
1238#endif
1239
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001240 /* recover superblocks we couldn't write due to previous RO mount */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001241 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001242 err = f2fs_commit_super(sbi, false);
1243 f2fs_msg(sb, KERN_INFO,
1244 "Try to recover all the superblocks, ret: %d", err);
1245 if (!err)
1246 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1247 }
1248
Yunlei He498c5e92015-05-07 18:11:37 +08001249 default_options(sbi);
Chao Yu26666c82014-09-15 18:04:44 +08001250
Namjae Jeon696c0182013-06-16 09:48:48 +09001251 /* parse mount options */
1252 err = parse_options(sb, data);
1253 if (err)
1254 goto restore_opts;
1255
1256 /*
1257 * Previous and new state of filesystem is RO,
Gu Zheng876dc592014-04-11 17:50:00 +08001258 * so skip checking GC and FLUSH_MERGE conditions.
Namjae Jeon696c0182013-06-16 09:48:48 +09001259 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001260 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
Namjae Jeon696c0182013-06-16 09:48:48 +09001261 goto skip;
1262
Jaegeuk Kimea676732017-10-06 09:14:28 -07001263#ifdef CONFIG_QUOTA
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001264 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
Chao Yu0abd6752017-07-09 00:13:07 +08001265 err = dquot_suspend(sb, -1);
1266 if (err < 0)
1267 goto restore_opts;
1268 } else {
1269 /* dquot_resume needs RW */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001270 sb->s_flags &= ~SB_RDONLY;
Jaegeuk Kimea676732017-10-06 09:14:28 -07001271 if (sb_any_quota_suspended(sb)) {
1272 dquot_resume(sb, -1);
1273 } else if (f2fs_sb_has_quota_ino(sb)) {
1274 err = f2fs_enable_quotas(sb);
1275 if (err)
1276 goto restore_opts;
1277 }
Chao Yu0abd6752017-07-09 00:13:07 +08001278 }
Jaegeuk Kimea676732017-10-06 09:14:28 -07001279#endif
Chao Yu9cd81ce2015-09-18 16:55:26 +08001280 /* disallow enable/disable extent_cache dynamically */
1281 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
1282 err = -EINVAL;
1283 f2fs_msg(sbi->sb, KERN_WARNING,
1284 "switch extent_cache option is not allowed");
1285 goto restore_opts;
1286 }
1287
Namjae Jeon696c0182013-06-16 09:48:48 +09001288 /*
1289 * We stop the GC thread if FS is mounted as RO
1290 * or if background_gc = off is passed in mount
1291 * option. Also sync the filesystem.
1292 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001293 if ((*flags & SB_RDONLY) || !test_opt(sbi, BG_GC)) {
Namjae Jeon696c0182013-06-16 09:48:48 +09001294 if (sbi->gc_thread) {
1295 stop_gc_thread(sbi);
Gu Zheng876dc592014-04-11 17:50:00 +08001296 need_restart_gc = true;
Namjae Jeon696c0182013-06-16 09:48:48 +09001297 }
Chao Yuaba291b2014-11-18 11:17:20 +08001298 } else if (!sbi->gc_thread) {
Namjae Jeon696c0182013-06-16 09:48:48 +09001299 err = start_gc_thread(sbi);
1300 if (err)
1301 goto restore_opts;
Gu Zheng876dc592014-04-11 17:50:00 +08001302 need_stop_gc = true;
1303 }
1304
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001305 if (*flags & SB_RDONLY) {
Jaegeuk Kimfaa0e552016-03-24 10:29:39 -07001306 writeback_inodes_sb(sb, WB_REASON_SYNC);
1307 sync_inodes_sb(sb);
1308
1309 set_sbi_flag(sbi, SBI_IS_DIRTY);
1310 set_sbi_flag(sbi, SBI_IS_CLOSE);
1311 f2fs_sync_fs(sb, 1);
1312 clear_sbi_flag(sbi, SBI_IS_CLOSE);
1313 }
1314
Gu Zheng876dc592014-04-11 17:50:00 +08001315 /*
1316 * We stop issue flush thread if FS is mounted as RO
1317 * or if flush_merge is not passed in mount option.
1318 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001319 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -08001320 clear_opt(sbi, FLUSH_MERGE);
1321 destroy_flush_cmd_control(sbi, false);
1322 } else {
Gu Zheng2163d192014-04-27 14:21:33 +08001323 err = create_flush_cmd_control(sbi);
1324 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +08001325 goto restore_gc;
Namjae Jeon696c0182013-06-16 09:48:48 +09001326 }
1327skip:
Chao Yu4b2414d2017-08-08 10:54:31 +08001328#ifdef CONFIG_QUOTA
1329 /* Release old quota file names */
1330 for (i = 0; i < MAXQUOTAS; i++)
1331 kfree(s_qf_names[i]);
1332#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001333 /* Update the POSIXACL Flag */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001334 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
1335 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001336
Namjae Jeon696c0182013-06-16 09:48:48 +09001337 return 0;
Gu Zheng876dc592014-04-11 17:50:00 +08001338restore_gc:
1339 if (need_restart_gc) {
1340 if (start_gc_thread(sbi))
1341 f2fs_msg(sbi->sb, KERN_WARNING,
arter97e1c42042014-08-06 23:22:50 +09001342 "background gc thread has stopped");
Gu Zheng876dc592014-04-11 17:50:00 +08001343 } else if (need_stop_gc) {
1344 stop_gc_thread(sbi);
1345 }
Namjae Jeon696c0182013-06-16 09:48:48 +09001346restore_opts:
Chao Yu4b2414d2017-08-08 10:54:31 +08001347#ifdef CONFIG_QUOTA
1348 sbi->s_jquota_fmt = s_jquota_fmt;
1349 for (i = 0; i < MAXQUOTAS; i++) {
1350 kfree(sbi->s_qf_names[i]);
1351 sbi->s_qf_names[i] = s_qf_names[i];
1352 }
1353#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001354 sbi->mount_opt = org_mount_opt;
1355 sbi->active_logs = active_logs;
Chao Yu0abd6752017-07-09 00:13:07 +08001356 sb->s_flags = old_sb_flags;
Chao Yu2443b8b2016-09-26 19:45:06 +08001357#ifdef CONFIG_F2FS_FAULT_INJECTION
1358 sbi->fault_info = ffi;
1359#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001360 return err;
1361}
1362
Chao Yu0abd6752017-07-09 00:13:07 +08001363#ifdef CONFIG_QUOTA
1364/* Read data from quotafile */
1365static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
1366 size_t len, loff_t off)
1367{
1368 struct inode *inode = sb_dqopt(sb)->files[type];
1369 struct address_space *mapping = inode->i_mapping;
1370 block_t blkidx = F2FS_BYTES_TO_BLK(off);
1371 int offset = off & (sb->s_blocksize - 1);
1372 int tocopy;
1373 size_t toread;
1374 loff_t i_size = i_size_read(inode);
1375 struct page *page;
1376 char *kaddr;
1377
1378 if (off > i_size)
1379 return 0;
1380
1381 if (off + len > i_size)
1382 len = i_size - off;
1383 toread = len;
1384 while (toread > 0) {
1385 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
1386repeat:
1387 page = read_mapping_page(mapping, blkidx, NULL);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001388 if (IS_ERR(page)) {
1389 if (PTR_ERR(page) == -ENOMEM) {
1390 congestion_wait(BLK_RW_ASYNC, HZ/50);
1391 goto repeat;
1392 }
Chao Yu0abd6752017-07-09 00:13:07 +08001393 return PTR_ERR(page);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001394 }
Chao Yu0abd6752017-07-09 00:13:07 +08001395
1396 lock_page(page);
1397
1398 if (unlikely(page->mapping != mapping)) {
1399 f2fs_put_page(page, 1);
1400 goto repeat;
1401 }
1402 if (unlikely(!PageUptodate(page))) {
1403 f2fs_put_page(page, 1);
1404 return -EIO;
1405 }
1406
1407 kaddr = kmap_atomic(page);
1408 memcpy(data, kaddr + offset, tocopy);
1409 kunmap_atomic(kaddr);
1410 f2fs_put_page(page, 1);
1411
1412 offset = 0;
1413 toread -= tocopy;
1414 data += tocopy;
1415 blkidx++;
1416 }
1417 return len;
1418}
1419
1420/* Write to quotafile */
1421static ssize_t f2fs_quota_write(struct super_block *sb, int type,
1422 const char *data, size_t len, loff_t off)
1423{
1424 struct inode *inode = sb_dqopt(sb)->files[type];
1425 struct address_space *mapping = inode->i_mapping;
1426 const struct address_space_operations *a_ops = mapping->a_ops;
1427 int offset = off & (sb->s_blocksize - 1);
1428 size_t towrite = len;
1429 struct page *page;
1430 char *kaddr;
1431 int err = 0;
1432 int tocopy;
1433
1434 while (towrite > 0) {
1435 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
1436 towrite);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001437retry:
Chao Yu0abd6752017-07-09 00:13:07 +08001438 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
1439 &page, NULL);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001440 if (unlikely(err)) {
1441 if (err == -ENOMEM) {
1442 congestion_wait(BLK_RW_ASYNC, HZ/50);
1443 goto retry;
1444 }
Chao Yu0abd6752017-07-09 00:13:07 +08001445 break;
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07001446 }
Chao Yu0abd6752017-07-09 00:13:07 +08001447
1448 kaddr = kmap_atomic(page);
1449 memcpy(kaddr + offset, data, tocopy);
1450 kunmap_atomic(kaddr);
1451 flush_dcache_page(page);
1452
1453 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
1454 page, NULL);
1455 offset = 0;
1456 towrite -= tocopy;
1457 off += tocopy;
1458 data += tocopy;
1459 cond_resched();
1460 }
1461
1462 if (len == towrite)
Jaegeuk Kim6e5b5d42017-10-19 12:07:11 -07001463 return err;
Chao Yu0abd6752017-07-09 00:13:07 +08001464 inode->i_mtime = inode->i_ctime = current_time(inode);
1465 f2fs_mark_inode_dirty_sync(inode, false);
1466 return len - towrite;
1467}
1468
1469static struct dquot **f2fs_get_dquots(struct inode *inode)
1470{
1471 return F2FS_I(inode)->i_dquot;
1472}
1473
1474static qsize_t *f2fs_get_reserved_space(struct inode *inode)
1475{
1476 return &F2FS_I(inode)->i_reserved_quota;
1477}
1478
Chao Yu4b2414d2017-08-08 10:54:31 +08001479static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
1480{
1481 return dquot_quota_on_mount(sbi->sb, sbi->s_qf_names[type],
1482 sbi->s_jquota_fmt, type);
1483}
1484
Jaegeuk Kimea676732017-10-06 09:14:28 -07001485int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
Chao Yu4b2414d2017-08-08 10:54:31 +08001486{
Jaegeuk Kimea676732017-10-06 09:14:28 -07001487 int enabled = 0;
1488 int i, err;
1489
1490 if (f2fs_sb_has_quota_ino(sbi->sb) && rdonly) {
1491 err = f2fs_enable_quotas(sbi->sb);
1492 if (err) {
1493 f2fs_msg(sbi->sb, KERN_ERR,
1494 "Cannot turn on quota_ino: %d", err);
1495 return 0;
1496 }
1497 return 1;
1498 }
Chao Yu4b2414d2017-08-08 10:54:31 +08001499
1500 for (i = 0; i < MAXQUOTAS; i++) {
1501 if (sbi->s_qf_names[i]) {
Jaegeuk Kimea676732017-10-06 09:14:28 -07001502 err = f2fs_quota_on_mount(sbi, i);
1503 if (!err) {
1504 enabled = 1;
1505 continue;
1506 }
1507 f2fs_msg(sbi->sb, KERN_ERR,
1508 "Cannot turn on quotas: %d on %d", err, i);
Chao Yu4b2414d2017-08-08 10:54:31 +08001509 }
1510 }
Jaegeuk Kimea676732017-10-06 09:14:28 -07001511 return enabled;
1512}
1513
1514static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
1515 unsigned int flags)
1516{
1517 struct inode *qf_inode;
1518 unsigned long qf_inum;
1519 int err;
1520
1521 BUG_ON(!f2fs_sb_has_quota_ino(sb));
1522
1523 qf_inum = f2fs_qf_ino(sb, type);
1524 if (!qf_inum)
1525 return -EPERM;
1526
1527 qf_inode = f2fs_iget(sb, qf_inum);
1528 if (IS_ERR(qf_inode)) {
1529 f2fs_msg(sb, KERN_ERR,
1530 "Bad quota inode %u:%lu", type, qf_inum);
1531 return PTR_ERR(qf_inode);
1532 }
1533
1534 /* Don't account quota for quota files to avoid recursion */
1535 qf_inode->i_flags |= S_NOQUOTA;
1536 err = dquot_enable(qf_inode, type, format_id, flags);
1537 iput(qf_inode);
1538 return err;
1539}
1540
1541static int f2fs_enable_quotas(struct super_block *sb)
1542{
1543 int type, err = 0;
1544 unsigned long qf_inum;
1545 bool quota_mopt[MAXQUOTAS] = {
1546 test_opt(F2FS_SB(sb), USRQUOTA),
1547 test_opt(F2FS_SB(sb), GRPQUOTA),
1548 test_opt(F2FS_SB(sb), PRJQUOTA),
1549 };
1550
1551 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
1552 for (type = 0; type < MAXQUOTAS; type++) {
1553 qf_inum = f2fs_qf_ino(sb, type);
1554 if (qf_inum) {
1555 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
1556 DQUOT_USAGE_ENABLED |
1557 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
1558 if (err) {
1559 f2fs_msg(sb, KERN_ERR,
1560 "Failed to enable quota tracking "
1561 "(type=%d, err=%d). Please run "
1562 "fsck to fix.", type, err);
1563 for (type--; type >= 0; type--)
1564 dquot_quota_off(sb, type);
1565 return err;
1566 }
1567 }
1568 }
1569 return 0;
Chao Yu4b2414d2017-08-08 10:54:31 +08001570}
1571
Chao Yu0abd6752017-07-09 00:13:07 +08001572static int f2fs_quota_sync(struct super_block *sb, int type)
1573{
1574 struct quota_info *dqopt = sb_dqopt(sb);
1575 int cnt;
1576 int ret;
1577
1578 ret = dquot_writeback_dquots(sb, type);
1579 if (ret)
1580 return ret;
1581
1582 /*
1583 * Now when everything is written we can discard the pagecache so
1584 * that userspace sees the changes.
1585 */
1586 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1587 if (type != -1 && cnt != type)
1588 continue;
1589 if (!sb_has_quota_active(sb, cnt))
1590 continue;
1591
1592 ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping);
1593 if (ret)
1594 return ret;
1595
1596 inode_lock(dqopt->files[cnt]);
1597 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
1598 inode_unlock(dqopt->files[cnt]);
1599 }
1600 return 0;
1601}
1602
1603static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
1604 const struct path *path)
1605{
1606 struct inode *inode;
1607 int err;
1608
Chao Yu9a20d392017-08-07 16:37:59 +08001609 err = f2fs_quota_sync(sb, type);
Chao Yu0abd6752017-07-09 00:13:07 +08001610 if (err)
1611 return err;
1612
1613 err = dquot_quota_on(sb, type, format_id, path);
1614 if (err)
1615 return err;
1616
1617 inode = d_inode(path->dentry);
1618
1619 inode_lock(inode);
1620 F2FS_I(inode)->i_flags |= FS_NOATIME_FL | FS_IMMUTABLE_FL;
1621 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
1622 S_NOATIME | S_IMMUTABLE);
1623 inode_unlock(inode);
1624 f2fs_mark_inode_dirty_sync(inode, false);
1625
1626 return 0;
1627}
1628
1629static int f2fs_quota_off(struct super_block *sb, int type)
1630{
1631 struct inode *inode = sb_dqopt(sb)->files[type];
1632 int err;
1633
1634 if (!inode || !igrab(inode))
1635 return dquot_quota_off(sb, type);
1636
Chao Yu9a20d392017-08-07 16:37:59 +08001637 f2fs_quota_sync(sb, type);
Chao Yu0abd6752017-07-09 00:13:07 +08001638
1639 err = dquot_quota_off(sb, type);
Jaegeuk Kimea676732017-10-06 09:14:28 -07001640 if (err || f2fs_sb_has_quota_ino(sb))
Chao Yu0abd6752017-07-09 00:13:07 +08001641 goto out_put;
1642
1643 inode_lock(inode);
1644 F2FS_I(inode)->i_flags &= ~(FS_NOATIME_FL | FS_IMMUTABLE_FL);
1645 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
1646 inode_unlock(inode);
1647 f2fs_mark_inode_dirty_sync(inode, false);
1648out_put:
1649 iput(inode);
1650 return err;
1651}
1652
Chao Yu4b2414d2017-08-08 10:54:31 +08001653void f2fs_quota_off_umount(struct super_block *sb)
Chao Yu0abd6752017-07-09 00:13:07 +08001654{
1655 int type;
1656
1657 for (type = 0; type < MAXQUOTAS; type++)
1658 f2fs_quota_off(sb, type);
1659}
1660
Chao Yu5c571322017-07-26 00:01:41 +08001661int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
1662{
1663 *projid = F2FS_I(inode)->i_projid;
1664 return 0;
1665}
1666
Chao Yu0abd6752017-07-09 00:13:07 +08001667static const struct dquot_operations f2fs_quota_operations = {
1668 .get_reserved_space = f2fs_get_reserved_space,
1669 .write_dquot = dquot_commit,
1670 .acquire_dquot = dquot_acquire,
1671 .release_dquot = dquot_release,
1672 .mark_dirty = dquot_mark_dquot_dirty,
1673 .write_info = dquot_commit_info,
1674 .alloc_dquot = dquot_alloc,
1675 .destroy_dquot = dquot_destroy,
Chao Yu5c571322017-07-26 00:01:41 +08001676 .get_projid = f2fs_get_projid,
Chao Yu0abd6752017-07-09 00:13:07 +08001677 .get_next_id = dquot_get_next_id,
1678};
1679
1680static const struct quotactl_ops f2fs_quotactl_ops = {
1681 .quota_on = f2fs_quota_on,
1682 .quota_off = f2fs_quota_off,
1683 .quota_sync = f2fs_quota_sync,
1684 .get_state = dquot_get_state,
1685 .set_info = dquot_set_dqinfo,
1686 .get_dqblk = dquot_get_dqblk,
1687 .set_dqblk = dquot_set_dqblk,
1688 .get_nextdqblk = dquot_get_next_dqblk,
1689};
1690#else
Chao Yu4b2414d2017-08-08 10:54:31 +08001691void f2fs_quota_off_umount(struct super_block *sb)
Chao Yu0abd6752017-07-09 00:13:07 +08001692{
1693}
1694#endif
1695
Arvind Yadavf62fc9f2017-08-31 15:06:24 +05301696static const struct super_operations f2fs_sops = {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001697 .alloc_inode = f2fs_alloc_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001698 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001699 .destroy_inode = f2fs_destroy_inode,
1700 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +09001701 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001702 .show_options = f2fs_show_options,
Chao Yu0abd6752017-07-09 00:13:07 +08001703#ifdef CONFIG_QUOTA
1704 .quota_read = f2fs_quota_read,
1705 .quota_write = f2fs_quota_write,
1706 .get_dquots = f2fs_get_dquots,
1707#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001708 .evict_inode = f2fs_evict_inode,
1709 .put_super = f2fs_put_super,
1710 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +09001711 .freeze_fs = f2fs_freeze,
1712 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001713 .statfs = f2fs_statfs,
Namjae Jeon696c0182013-06-16 09:48:48 +09001714 .remount_fs = f2fs_remount,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001715};
1716
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001717#ifdef CONFIG_F2FS_FS_ENCRYPTION
1718static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
1719{
1720 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1721 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1722 ctx, len, NULL);
1723}
1724
1725static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
1726 void *fs_data)
1727{
1728 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1729 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1730 ctx, len, fs_data, XATTR_CREATE);
1731}
1732
1733static unsigned f2fs_max_namelen(struct inode *inode)
1734{
1735 return S_ISLNK(inode->i_mode) ?
1736 inode->i_sb->s_blocksize : F2FS_NAME_LEN;
1737}
1738
Eric Biggers6f69f0e2017-02-07 12:42:10 -08001739static const struct fscrypt_operations f2fs_cryptops = {
Eric Biggersa5d431e2017-01-05 13:51:18 -08001740 .key_prefix = "f2fs:",
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001741 .get_context = f2fs_get_context,
1742 .set_context = f2fs_set_context,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001743 .empty_dir = f2fs_empty_dir,
1744 .max_namelen = f2fs_max_namelen,
1745};
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001746#endif
1747
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001748static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
1749 u64 ino, u32 generation)
1750{
1751 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1752 struct inode *inode;
1753
Chao Yud6b7d4b2014-06-12 13:23:41 +08001754 if (check_nid_range(sbi, ino))
Chao Yu910bb122014-03-12 17:08:36 +08001755 return ERR_PTR(-ESTALE);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001756
1757 /*
1758 * f2fs_iget isn't quite right if the inode is currently unallocated!
1759 * However f2fs_iget currently does appropriate checks to handle stale
1760 * inodes so everything is OK.
1761 */
1762 inode = f2fs_iget(sb, ino);
1763 if (IS_ERR(inode))
1764 return ERR_CAST(inode);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001765 if (unlikely(generation && inode->i_generation != generation)) {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001766 /* we didn't find the right inode.. */
1767 iput(inode);
1768 return ERR_PTR(-ESTALE);
1769 }
1770 return inode;
1771}
1772
1773static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1774 int fh_len, int fh_type)
1775{
1776 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1777 f2fs_nfs_get_inode);
1778}
1779
1780static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
1781 int fh_len, int fh_type)
1782{
1783 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1784 f2fs_nfs_get_inode);
1785}
1786
1787static const struct export_operations f2fs_export_ops = {
1788 .fh_to_dentry = f2fs_fh_to_dentry,
1789 .fh_to_parent = f2fs_fh_to_parent,
1790 .get_parent = f2fs_get_parent,
1791};
1792
Chao Yue0afc4d2015-12-31 14:35:37 +08001793static loff_t max_file_blocks(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001794{
Chao Yu7a2af762017-07-19 00:19:06 +08001795 loff_t result = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001796 loff_t leaf_count = ADDRS_PER_BLOCK;
1797
Chao Yu7a2af762017-07-19 00:19:06 +08001798 /*
1799 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
Chao Yu6afc6622017-09-06 21:59:50 +08001800 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
Chao Yu7a2af762017-07-19 00:19:06 +08001801 * space in inode.i_addr, it will be more safe to reassign
1802 * result as zero.
1803 */
1804
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001805 /* two direct node blocks */
1806 result += (leaf_count * 2);
1807
1808 /* two indirect node blocks */
1809 leaf_count *= NIDS_PER_BLOCK;
1810 result += (leaf_count * 2);
1811
1812 /* one double indirect node block */
1813 leaf_count *= NIDS_PER_BLOCK;
1814 result += leaf_count;
1815
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001816 return result;
1817}
1818
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001819static int __f2fs_commit_super(struct buffer_head *bh,
1820 struct f2fs_super_block *super)
Chao Yu9a59b622015-12-15 09:58:18 +08001821{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001822 lock_buffer(bh);
1823 if (super)
1824 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
1825 set_buffer_uptodate(bh);
1826 set_buffer_dirty(bh);
1827 unlock_buffer(bh);
1828
1829 /* it's rare case, we can do fua all the time */
Jan Kara3adc5fcb2017-05-02 17:03:47 +02001830 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001831}
1832
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001833static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001834 struct buffer_head *bh)
1835{
1836 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1837 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001838 struct super_block *sb = sbi->sb;
Chao Yu9a59b622015-12-15 09:58:18 +08001839 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1840 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
1841 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
1842 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
1843 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1844 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1845 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
1846 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
1847 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
1848 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
1849 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
1850 u32 segment_count = le32_to_cpu(raw_super->segment_count);
1851 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001852 u64 main_end_blkaddr = main_blkaddr +
1853 (segment_count_main << log_blocks_per_seg);
1854 u64 seg_end_blkaddr = segment0_blkaddr +
1855 (segment_count << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08001856
1857 if (segment0_blkaddr != cp_blkaddr) {
1858 f2fs_msg(sb, KERN_INFO,
1859 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
1860 segment0_blkaddr, cp_blkaddr);
1861 return true;
1862 }
1863
1864 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
1865 sit_blkaddr) {
1866 f2fs_msg(sb, KERN_INFO,
1867 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
1868 cp_blkaddr, sit_blkaddr,
1869 segment_count_ckpt << log_blocks_per_seg);
1870 return true;
1871 }
1872
1873 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
1874 nat_blkaddr) {
1875 f2fs_msg(sb, KERN_INFO,
1876 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
1877 sit_blkaddr, nat_blkaddr,
1878 segment_count_sit << log_blocks_per_seg);
1879 return true;
1880 }
1881
1882 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
1883 ssa_blkaddr) {
1884 f2fs_msg(sb, KERN_INFO,
1885 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
1886 nat_blkaddr, ssa_blkaddr,
1887 segment_count_nat << log_blocks_per_seg);
1888 return true;
1889 }
1890
1891 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
1892 main_blkaddr) {
1893 f2fs_msg(sb, KERN_INFO,
1894 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
1895 ssa_blkaddr, main_blkaddr,
1896 segment_count_ssa << log_blocks_per_seg);
1897 return true;
1898 }
1899
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001900 if (main_end_blkaddr > seg_end_blkaddr) {
Chao Yu9a59b622015-12-15 09:58:18 +08001901 f2fs_msg(sb, KERN_INFO,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001902 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
Chao Yu9a59b622015-12-15 09:58:18 +08001903 main_blkaddr,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001904 segment0_blkaddr +
1905 (segment_count << log_blocks_per_seg),
Chao Yu9a59b622015-12-15 09:58:18 +08001906 segment_count_main << log_blocks_per_seg);
1907 return true;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001908 } else if (main_end_blkaddr < seg_end_blkaddr) {
1909 int err = 0;
1910 char *res;
Chao Yu9a59b622015-12-15 09:58:18 +08001911
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001912 /* fix in-memory information all the time */
1913 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
1914 segment0_blkaddr) >> log_blocks_per_seg);
1915
1916 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001917 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001918 res = "internally";
1919 } else {
1920 err = __f2fs_commit_super(bh, NULL);
1921 res = err ? "failed" : "done";
1922 }
1923 f2fs_msg(sb, KERN_INFO,
1924 "Fix alignment : %s, start(%u) end(%u) block(%u)",
1925 res, main_blkaddr,
1926 segment0_blkaddr +
1927 (segment_count << log_blocks_per_seg),
1928 segment_count_main << log_blocks_per_seg);
1929 if (err)
1930 return true;
1931 }
Chao Yu9a59b622015-12-15 09:58:18 +08001932 return false;
1933}
1934
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001935static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001936 struct buffer_head *bh)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001937{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001938 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1939 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001940 struct super_block *sb = sbi->sb;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001941 unsigned int blocksize;
1942
Namjae Jeona07ef782012-12-30 14:52:05 +09001943 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
1944 f2fs_msg(sb, KERN_INFO,
1945 "Magic Mismatch, valid(0x%x) - read(0x%x)",
1946 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001947 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001948 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001949
majianpeng5c9b4692013-02-01 19:07:57 +08001950 /* Currently, support only 4KB page cache size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001951 if (F2FS_BLKSIZE != PAGE_SIZE) {
majianpeng5c9b4692013-02-01 19:07:57 +08001952 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +08001953 "Invalid page_cache_size (%lu), supports only 4KB\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001954 PAGE_SIZE);
majianpeng5c9b4692013-02-01 19:07:57 +08001955 return 1;
1956 }
1957
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001958 /* Currently, support only 4KB block size */
1959 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +08001960 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001961 f2fs_msg(sb, KERN_INFO,
1962 "Invalid blocksize (%u), supports only 4KB\n",
1963 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001964 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001965 }
majianpeng5c9b4692013-02-01 19:07:57 +08001966
Chao Yu9a59b622015-12-15 09:58:18 +08001967 /* check log blocks per segment */
1968 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
1969 f2fs_msg(sb, KERN_INFO,
1970 "Invalid log blocks per segment (%u)\n",
1971 le32_to_cpu(raw_super->log_blocks_per_seg));
1972 return 1;
1973 }
1974
Chao Yu55cf9cb2014-09-15 18:01:10 +08001975 /* Currently, support 512/1024/2048/4096 bytes sector size */
1976 if (le32_to_cpu(raw_super->log_sectorsize) >
1977 F2FS_MAX_LOG_SECTOR_SIZE ||
1978 le32_to_cpu(raw_super->log_sectorsize) <
1979 F2FS_MIN_LOG_SECTOR_SIZE) {
1980 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
1981 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001982 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001983 }
Chao Yu55cf9cb2014-09-15 18:01:10 +08001984 if (le32_to_cpu(raw_super->log_sectors_per_block) +
1985 le32_to_cpu(raw_super->log_sectorsize) !=
1986 F2FS_MAX_LOG_SECTOR_SIZE) {
1987 f2fs_msg(sb, KERN_INFO,
1988 "Invalid log sectors per block(%u) log sectorsize(%u)",
1989 le32_to_cpu(raw_super->log_sectors_per_block),
1990 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001991 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001992 }
Chao Yu9a59b622015-12-15 09:58:18 +08001993
1994 /* check reserved ino info */
1995 if (le32_to_cpu(raw_super->node_ino) != 1 ||
1996 le32_to_cpu(raw_super->meta_ino) != 2 ||
1997 le32_to_cpu(raw_super->root_ino) != 3) {
1998 f2fs_msg(sb, KERN_INFO,
1999 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
2000 le32_to_cpu(raw_super->node_ino),
2001 le32_to_cpu(raw_super->meta_ino),
2002 le32_to_cpu(raw_super->root_ino));
2003 return 1;
2004 }
2005
Jin Qianb9dd4612017-04-25 16:28:48 -07002006 if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) {
2007 f2fs_msg(sb, KERN_INFO,
2008 "Invalid segment count (%u)",
2009 le32_to_cpu(raw_super->segment_count));
2010 return 1;
2011 }
2012
Chao Yu9a59b622015-12-15 09:58:18 +08002013 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002014 if (sanity_check_area_boundary(sbi, bh))
Chao Yu9a59b622015-12-15 09:58:18 +08002015 return 1;
2016
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002017 return 0;
2018}
2019
Shawn Lin984ec632016-02-17 11:26:32 +08002020int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002021{
2022 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09002023 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2024 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08002025 unsigned int ovp_segments, reserved_segments;
Jin Qian15d30422017-05-15 10:45:08 -07002026 unsigned int main_segs, blocks_per_seg;
2027 int i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002028
2029 total = le32_to_cpu(raw_super->segment_count);
2030 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
2031 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
2032 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
2033 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
2034 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
2035
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002036 if (unlikely(fsmeta >= total))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002037 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09002038
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08002039 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2040 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2041
2042 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
2043 ovp_segments == 0 || reserved_segments == 0)) {
2044 f2fs_msg(sbi->sb, KERN_ERR,
2045 "Wrong layout: check mkfs.f2fs version");
2046 return 1;
2047 }
2048
Jin Qian15d30422017-05-15 10:45:08 -07002049 main_segs = le32_to_cpu(raw_super->segment_count_main);
2050 blocks_per_seg = sbi->blocks_per_seg;
2051
2052 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2053 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
2054 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
2055 return 1;
2056 }
2057 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
2058 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
2059 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
2060 return 1;
2061 }
2062
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07002063 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim577e3492013-01-24 19:56:11 +09002064 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
2065 return 1;
2066 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002067 return 0;
2068}
2069
2070static void init_sb_info(struct f2fs_sb_info *sbi)
2071{
2072 struct f2fs_super_block *raw_super = sbi->raw_super;
Chao Yue41e6d72017-05-19 23:37:00 +08002073 int i, j;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002074
2075 sbi->log_sectors_per_block =
2076 le32_to_cpu(raw_super->log_sectors_per_block);
2077 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
2078 sbi->blocksize = 1 << sbi->log_blocksize;
2079 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2080 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
2081 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2082 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2083 sbi->total_sections = le32_to_cpu(raw_super->section_count);
2084 sbi->total_node_count =
2085 (le32_to_cpu(raw_super->segment_count_nat) / 2)
2086 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
2087 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
2088 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
2089 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09002090 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimb1c57c12014-01-08 13:45:08 +09002091 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002092
Jaegeuk Kimab9fa662014-02-27 20:09:05 +09002093 sbi->dir_level = DEF_DIR_LEVEL;
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08002094 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002095 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
Chao Yucaf00472015-01-28 17:48:42 +08002096 clear_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002097
Jaegeuk Kim35782b22016-10-20 19:09:57 -07002098 for (i = 0; i < NR_COUNT_TYPE; i++)
2099 atomic_set(&sbi->nr_pages[i], 0);
2100
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002101 atomic_set(&sbi->wb_sync_req, 0);
2102
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002103 INIT_LIST_HEAD(&sbi->s_list);
2104 mutex_init(&sbi->umount_mutex);
Chao Yue41e6d72017-05-19 23:37:00 +08002105 for (i = 0; i < NR_PAGE_TYPE - 1; i++)
2106 for (j = HOT; j < NR_TEMP_TYPE; j++)
2107 mutex_init(&sbi->wio_mutex[i][j]);
Chao Yuaaec2b12016-09-20 11:04:18 +08002108 spin_lock_init(&sbi->cp_lock);
Chao Yu1228b482017-09-29 13:59:39 +08002109
2110 sbi->dirty_device = 0;
2111 spin_lock_init(&sbi->dev_lock);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002112}
2113
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002114static int init_percpu_info(struct f2fs_sb_info *sbi)
2115{
Jaegeuk Kim35782b22016-10-20 19:09:57 -07002116 int err;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002117
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07002118 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
2119 if (err)
2120 return err;
2121
2122 return percpu_counter_init(&sbi->total_valid_inode_count, 0,
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002123 GFP_KERNEL);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002124}
2125
Damien Le Moal178053e2016-10-28 17:45:05 +09002126#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002127static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
Damien Le Moal178053e2016-10-28 17:45:05 +09002128{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002129 struct block_device *bdev = FDEV(devi).bdev;
Damien Le Moal178053e2016-10-28 17:45:05 +09002130 sector_t nr_sectors = bdev->bd_part->nr_sects;
2131 sector_t sector = 0;
2132 struct blk_zone *zones;
2133 unsigned int i, nr_zones;
2134 unsigned int n = 0;
2135 int err = -EIO;
2136
2137 if (!f2fs_sb_mounted_blkzoned(sbi->sb))
2138 return 0;
2139
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002140 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
Damien Le Moalf99e8642017-01-12 07:58:32 -07002141 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002142 return -EINVAL;
Damien Le Moalf99e8642017-01-12 07:58:32 -07002143 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002144 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
2145 __ilog2_u32(sbi->blocks_per_blkz))
2146 return -EINVAL;
Damien Le Moal178053e2016-10-28 17:45:05 +09002147 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002148 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
2149 sbi->log_blocks_per_blkz;
Damien Le Moalf99e8642017-01-12 07:58:32 -07002150 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002151 FDEV(devi).nr_blkz++;
Damien Le Moal178053e2016-10-28 17:45:05 +09002152
Chao Yu4e6aad22017-11-30 19:28:19 +08002153 FDEV(devi).blkz_type = f2fs_kmalloc(sbi, FDEV(devi).nr_blkz,
2154 GFP_KERNEL);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002155 if (!FDEV(devi).blkz_type)
Damien Le Moal178053e2016-10-28 17:45:05 +09002156 return -ENOMEM;
2157
2158#define F2FS_REPORT_NR_ZONES 4096
2159
Chao Yu4e6aad22017-11-30 19:28:19 +08002160 zones = f2fs_kzalloc(sbi, sizeof(struct blk_zone) *
2161 F2FS_REPORT_NR_ZONES, GFP_KERNEL);
Damien Le Moal178053e2016-10-28 17:45:05 +09002162 if (!zones)
2163 return -ENOMEM;
2164
2165 /* Get block zones type */
2166 while (zones && sector < nr_sectors) {
2167
2168 nr_zones = F2FS_REPORT_NR_ZONES;
2169 err = blkdev_report_zones(bdev, sector,
2170 zones, &nr_zones,
2171 GFP_KERNEL);
2172 if (err)
2173 break;
2174 if (!nr_zones) {
2175 err = -EIO;
2176 break;
2177 }
2178
2179 for (i = 0; i < nr_zones; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002180 FDEV(devi).blkz_type[n] = zones[i].type;
Damien Le Moal178053e2016-10-28 17:45:05 +09002181 sector += zones[i].len;
2182 n++;
2183 }
2184 }
2185
2186 kfree(zones);
2187
2188 return err;
2189}
2190#endif
2191
Gu Zheng9076a752013-10-14 18:47:11 +08002192/*
2193 * Read f2fs raw super block.
Shawn Lin2b39e902016-02-17 08:59:01 +08002194 * Because we have two copies of super block, so read both of them
2195 * to get the first valid one. If any one of them is broken, we pass
2196 * them recovery flag back to the caller.
Gu Zheng9076a752013-10-14 18:47:11 +08002197 */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002198static int read_raw_super_block(struct f2fs_sb_info *sbi,
Gu Zheng9076a752013-10-14 18:47:11 +08002199 struct f2fs_super_block **raw_super,
Chao Yue8240f62015-12-15 17:19:26 +08002200 int *valid_super_block, int *recovery)
majianpeng14d7e9d2013-02-01 19:07:03 +08002201{
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002202 struct super_block *sb = sbi->sb;
Shawn Lin2b39e902016-02-17 08:59:01 +08002203 int block;
Chao Yue8240f62015-12-15 17:19:26 +08002204 struct buffer_head *bh;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002205 struct f2fs_super_block *super;
hujianyangda554e42015-05-21 14:42:53 +08002206 int err = 0;
majianpeng14d7e9d2013-02-01 19:07:03 +08002207
Yunlei Heb39f0de2015-12-15 17:17:20 +08002208 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
2209 if (!super)
2210 return -ENOMEM;
Shawn Lin2b39e902016-02-17 08:59:01 +08002211
2212 for (block = 0; block < 2; block++) {
2213 bh = sb_bread(sb, block);
2214 if (!bh) {
2215 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
Gu Zheng9076a752013-10-14 18:47:11 +08002216 block + 1);
Shawn Lin2b39e902016-02-17 08:59:01 +08002217 err = -EIO;
2218 continue;
2219 }
majianpeng14d7e9d2013-02-01 19:07:03 +08002220
Shawn Lin2b39e902016-02-17 08:59:01 +08002221 /* sanity checking of raw super */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002222 if (sanity_check_raw_super(sbi, bh)) {
Shawn Lin2b39e902016-02-17 08:59:01 +08002223 f2fs_msg(sb, KERN_ERR,
2224 "Can't find valid F2FS filesystem in %dth superblock",
2225 block + 1);
2226 err = -EINVAL;
2227 brelse(bh);
2228 continue;
2229 }
2230
2231 if (!*raw_super) {
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002232 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
2233 sizeof(*super));
Shawn Lin2b39e902016-02-17 08:59:01 +08002234 *valid_super_block = block;
2235 *raw_super = super;
2236 }
Chao Yue8240f62015-12-15 17:19:26 +08002237 brelse(bh);
Shawn Lin2b39e902016-02-17 08:59:01 +08002238 }
2239
2240 /* Fail to read any one of the superblocks*/
2241 if (err < 0)
hujianyangda554e42015-05-21 14:42:53 +08002242 *recovery = 1;
hujianyangda554e42015-05-21 14:42:53 +08002243
hujianyangda554e42015-05-21 14:42:53 +08002244 /* No valid superblock */
Shawn Lin2b39e902016-02-17 08:59:01 +08002245 if (!*raw_super)
Yunlei Heb39f0de2015-12-15 17:17:20 +08002246 kfree(super);
Shawn Lin2b39e902016-02-17 08:59:01 +08002247 else
2248 err = 0;
hujianyangda554e42015-05-21 14:42:53 +08002249
Shawn Lin2b39e902016-02-17 08:59:01 +08002250 return err;
majianpeng14d7e9d2013-02-01 19:07:03 +08002251}
2252
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002253int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002254{
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002255 struct buffer_head *bh;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002256 int err;
2257
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002258 if ((recover && f2fs_readonly(sbi->sb)) ||
2259 bdev_read_only(sbi->sb->s_bdev)) {
2260 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimf2353d7b2016-03-23 10:42:01 -07002261 return -EROFS;
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002262 }
Jaegeuk Kimf2353d7b2016-03-23 10:42:01 -07002263
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002264 /* write back-up superblock first */
2265 bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002266 if (!bh)
2267 return -EIO;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002268 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002269 brelse(bh);
Chao Yuc5bda1c2015-06-08 13:28:03 +08002270
2271 /* if we are in recovery path, skip writing valid superblock */
2272 if (recover || err)
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08002273 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002274
Chao Yue8240f62015-12-15 17:19:26 +08002275 /* write current valid superblock */
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002276 bh = sb_getblk(sbi->sb, sbi->valid_super_block);
2277 if (!bh)
2278 return -EIO;
2279 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
2280 brelse(bh);
2281 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07002282}
2283
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002284static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
2285{
2286 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002287 unsigned int max_devices = MAX_DEVICES;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002288 int i;
2289
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002290 /* Initialize single device information */
2291 if (!RDEV(0).path[0]) {
2292 if (!bdev_is_zoned(sbi->sb->s_bdev))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002293 return 0;
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002294 max_devices = 1;
2295 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002296
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002297 /*
2298 * Initialize multiple devices information, or single
2299 * zoned block device information.
2300 */
Chao Yu4e6aad22017-11-30 19:28:19 +08002301 sbi->devs = f2fs_kzalloc(sbi, sizeof(struct f2fs_dev_info) *
2302 max_devices, GFP_KERNEL);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002303 if (!sbi->devs)
2304 return -ENOMEM;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002305
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002306 for (i = 0; i < max_devices; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002307
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002308 if (i > 0 && !RDEV(i).path[0])
2309 break;
2310
2311 if (max_devices == 1) {
2312 /* Single zoned block device mount */
2313 FDEV(0).bdev =
2314 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002315 sbi->sb->s_mode, sbi->sb->s_type);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002316 } else {
2317 /* Multi-device mount */
2318 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
2319 FDEV(i).total_segments =
2320 le32_to_cpu(RDEV(i).total_segments);
2321 if (i == 0) {
2322 FDEV(i).start_blk = 0;
2323 FDEV(i).end_blk = FDEV(i).start_blk +
2324 (FDEV(i).total_segments <<
2325 sbi->log_blocks_per_seg) - 1 +
2326 le32_to_cpu(raw_super->segment0_blkaddr);
2327 } else {
2328 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
2329 FDEV(i).end_blk = FDEV(i).start_blk +
2330 (FDEV(i).total_segments <<
2331 sbi->log_blocks_per_seg) - 1;
2332 }
2333 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
2334 sbi->sb->s_mode, sbi->sb->s_type);
2335 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002336 if (IS_ERR(FDEV(i).bdev))
2337 return PTR_ERR(FDEV(i).bdev);
2338
2339 /* to release errored devices */
2340 sbi->s_ndevs = i + 1;
2341
2342#ifdef CONFIG_BLK_DEV_ZONED
2343 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
2344 !f2fs_sb_mounted_blkzoned(sbi->sb)) {
2345 f2fs_msg(sbi->sb, KERN_ERR,
2346 "Zoned block device feature not enabled\n");
2347 return -EINVAL;
2348 }
2349 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
2350 if (init_blkz_info(sbi, i)) {
2351 f2fs_msg(sbi->sb, KERN_ERR,
2352 "Failed to initialize F2FS blkzone information");
2353 return -EINVAL;
2354 }
Masato Suzuki7bb3a372017-02-27 20:52:49 +09002355 if (max_devices == 1)
2356 break;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002357 f2fs_msg(sbi->sb, KERN_INFO,
2358 "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
2359 i, FDEV(i).path,
2360 FDEV(i).total_segments,
2361 FDEV(i).start_blk, FDEV(i).end_blk,
2362 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
2363 "Host-aware" : "Host-managed");
2364 continue;
2365 }
2366#endif
2367 f2fs_msg(sbi->sb, KERN_INFO,
2368 "Mount Device [%2d]: %20s, %8u, %8x - %8x",
2369 i, FDEV(i).path,
2370 FDEV(i).total_segments,
2371 FDEV(i).start_blk, FDEV(i).end_blk);
2372 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002373 f2fs_msg(sbi->sb, KERN_INFO,
2374 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002375 return 0;
2376}
2377
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002378static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
2379{
2380 struct f2fs_sb_info *sbi;
hujianyangda554e42015-05-21 14:42:53 +08002381 struct f2fs_super_block *raw_super;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002382 struct inode *root;
Sheng Yong99e3e852016-05-11 17:08:14 +08002383 int err;
Chao Yu2adc3502015-03-16 21:08:44 +08002384 bool retry = true, need_fsck = false;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002385 char *options = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08002386 int recovery, i, valid_super_block;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002387 struct curseg_info *seg_i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002388
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002389try_onemore:
hujianyangda554e42015-05-21 14:42:53 +08002390 err = -EINVAL;
2391 raw_super = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08002392 valid_super_block = -1;
hujianyangda554e42015-05-21 14:42:53 +08002393 recovery = 0;
2394
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002395 /* allocate memory for f2fs-specific super block info */
2396 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
2397 if (!sbi)
2398 return -ENOMEM;
2399
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002400 sbi->sb = sb;
2401
Keith Mok43b65732016-03-02 12:04:24 -08002402 /* Load the checksum driver */
2403 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
2404 if (IS_ERR(sbi->s_chksum_driver)) {
2405 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
2406 err = PTR_ERR(sbi->s_chksum_driver);
2407 sbi->s_chksum_driver = NULL;
2408 goto free_sbi;
2409 }
2410
Namjae Jeonff9234a2013-01-12 14:41:13 +09002411 /* set a block size */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002412 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002413 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002414 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +09002415 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002416
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002417 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
Chao Yue8240f62015-12-15 17:19:26 +08002418 &recovery);
Gu Zheng9076a752013-10-14 18:47:11 +08002419 if (err)
2420 goto free_sbi;
2421
Gu Zheng5fb08372013-06-07 14:16:53 +08002422 sb->s_fs_info = sbi;
Jaegeuk Kim52763a42016-06-13 09:47:48 -07002423 sbi->raw_super = raw_super;
2424
Chao Yu704956e2017-07-31 20:19:09 +08002425 /* precompute checksum seed for metadata */
2426 if (f2fs_sb_has_inode_chksum(sb))
2427 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
2428 sizeof(raw_super->uuid));
2429
Damien Le Moald1b959c2016-10-28 17:45:01 +09002430 /*
2431 * The BLKZONED feature indicates that the drive was formatted with
2432 * zone alignment optimization. This is optional for host-aware
2433 * devices, but mandatory for host-managed zoned block devices.
2434 */
2435#ifndef CONFIG_BLK_DEV_ZONED
2436 if (f2fs_sb_mounted_blkzoned(sb)) {
2437 f2fs_msg(sb, KERN_ERR,
2438 "Zoned block device support is not enabled\n");
Chao Yu1727f312017-06-12 09:44:27 +08002439 err = -EOPNOTSUPP;
Damien Le Moald1b959c2016-10-28 17:45:01 +09002440 goto free_sb_buf;
2441 }
Damien Le Moald1b959c2016-10-28 17:45:01 +09002442#endif
Yunlei He498c5e92015-05-07 18:11:37 +08002443 default_options(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002444 /* parse mount options */
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002445 options = kstrdup((const char *)data, GFP_KERNEL);
2446 if (data && !options) {
2447 err = -ENOMEM;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002448 goto free_sb_buf;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002449 }
2450
2451 err = parse_options(sb, options);
2452 if (err)
2453 goto free_options;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002454
Chao Yue0afc4d2015-12-31 14:35:37 +08002455 sbi->max_file_blocks = max_file_blocks();
2456 sb->s_maxbytes = sbi->max_file_blocks <<
2457 le32_to_cpu(raw_super->log_blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002458 sb->s_max_links = F2FS_LINK_MAX;
2459 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2460
Chao Yu0abd6752017-07-09 00:13:07 +08002461#ifdef CONFIG_QUOTA
2462 sb->dq_op = &f2fs_quota_operations;
Jaegeuk Kimea676732017-10-06 09:14:28 -07002463 if (f2fs_sb_has_quota_ino(sb))
2464 sb->s_qcop = &dquot_quotactl_sysfile_ops;
2465 else
2466 sb->s_qcop = &f2fs_quotactl_ops;
Chao Yu5c571322017-07-26 00:01:41 +08002467 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
Chao Yu292c196a2017-11-16 16:59:14 +08002468
2469 if (f2fs_sb_has_quota_ino(sbi->sb)) {
2470 for (i = 0; i < MAXQUOTAS; i++) {
2471 if (f2fs_qf_ino(sbi->sb, i))
2472 sbi->nquota_files++;
2473 }
2474 }
Chao Yu0abd6752017-07-09 00:13:07 +08002475#endif
2476
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002477 sb->s_op = &f2fs_sops;
Eric Biggersffcc41822017-10-09 12:15:38 -07002478#ifdef CONFIG_F2FS_FS_ENCRYPTION
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002479 sb->s_cop = &f2fs_cryptops;
Eric Biggersffcc41822017-10-09 12:15:38 -07002480#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002481 sb->s_xattr = f2fs_xattr_handlers;
2482 sb->s_export_op = &f2fs_export_ops;
2483 sb->s_magic = F2FS_SUPER_MAGIC;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002484 sb->s_time_gran = 1;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002485 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2486 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
Christoph Hellwig85787092017-05-10 15:06:33 +02002487 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002488
2489 /* init f2fs-specific super block info */
Chao Yue8240f62015-12-15 17:19:26 +08002490 sbi->valid_super_block = valid_super_block;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002491 mutex_init(&sbi->gc_mutex);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002492 mutex_init(&sbi->cp_mutex);
Chao Yub3582c62014-07-03 18:58:39 +08002493 init_rwsem(&sbi->node_write);
Yunlei He59c90812017-03-13 20:22:18 +08002494 init_rwsem(&sbi->node_change);
Jaegeuk Kim315df832015-08-11 12:45:39 -07002495
2496 /* disallow all the data/node/meta page writes */
2497 set_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002498 spin_lock_init(&sbi->stat_lock);
Jaegeuk Kim971767c2013-11-18 17:16:17 +09002499
Chao Yub0af6d42017-08-02 23:21:48 +08002500 /* init iostat info */
2501 spin_lock_init(&sbi->iostat_lock);
2502 sbi->iostat_enable = false;
2503
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002504 for (i = 0; i < NR_PAGE_TYPE; i++) {
Jaegeuk Kima912b542017-05-10 11:18:25 -07002505 int n = (i == META) ? 1: NR_TEMP_TYPE;
2506 int j;
2507
Chao Yu4e6aad22017-11-30 19:28:19 +08002508 sbi->write_io[i] = f2fs_kmalloc(sbi,
2509 n * sizeof(struct f2fs_bio_info),
2510 GFP_KERNEL);
Christophe JAILLETb63def92017-06-11 09:21:11 +02002511 if (!sbi->write_io[i]) {
2512 err = -ENOMEM;
Jaegeuk Kima912b542017-05-10 11:18:25 -07002513 goto free_options;
Christophe JAILLETb63def92017-06-11 09:21:11 +02002514 }
Jaegeuk Kima912b542017-05-10 11:18:25 -07002515
2516 for (j = HOT; j < n; j++) {
2517 init_rwsem(&sbi->write_io[i][j].io_rwsem);
2518 sbi->write_io[i][j].sbi = sbi;
2519 sbi->write_io[i][j].bio = NULL;
Chao Yufb830fc2017-05-19 23:37:01 +08002520 spin_lock_init(&sbi->write_io[i][j].io_lock);
2521 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
Jaegeuk Kima912b542017-05-10 11:18:25 -07002522 }
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002523 }
Jaegeuk Kim971767c2013-11-18 17:16:17 +09002524
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002525 init_rwsem(&sbi->cp_rwsem);
Changman Leefb51b5e2013-11-07 12:48:25 +09002526 init_waitqueue_head(&sbi->cp_wait);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002527 init_sb_info(sbi);
2528
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002529 err = init_percpu_info(sbi);
2530 if (err)
2531 goto free_options;
2532
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002533 if (F2FS_IO_SIZE(sbi) > 1) {
2534 sbi->write_io_dummy =
Chao Yua3ebfe42017-02-27 18:43:13 +08002535 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
Chao Yu1727f312017-06-12 09:44:27 +08002536 if (!sbi->write_io_dummy) {
2537 err = -ENOMEM;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002538 goto free_options;
Chao Yu1727f312017-06-12 09:44:27 +08002539 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002540 }
2541
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002542 /* get an inode for meta space */
2543 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
2544 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002545 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002546 err = PTR_ERR(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002547 goto free_io_dummy;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002548 }
2549
2550 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002551 if (err) {
2552 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002553 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +09002554 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002555
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002556 /* Initialize device list */
2557 err = f2fs_scan_devices(sbi);
2558 if (err) {
2559 f2fs_msg(sb, KERN_ERR, "Failed to find devices");
2560 goto free_devices;
2561 }
2562
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002563 sbi->total_valid_node_count =
2564 le32_to_cpu(sbi->ckpt->valid_node_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07002565 percpu_counter_set(&sbi->total_valid_inode_count,
2566 le32_to_cpu(sbi->ckpt->valid_inode_count));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002567 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
2568 sbi->total_valid_block_count =
2569 le64_to_cpu(sbi->ckpt->valid_block_count);
2570 sbi->last_valid_block_count = sbi->total_valid_block_count;
Chao Yudaeb4332017-06-26 16:24:41 +08002571 sbi->reserved_blocks = 0;
Yunlong Song80d42142017-10-27 20:45:05 +08002572 sbi->current_reserved_blocks = 0;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002573
Chao Yuc227f912015-12-16 13:09:20 +08002574 for (i = 0; i < NR_INODE_TYPE; i++) {
2575 INIT_LIST_HEAD(&sbi->inode_list[i]);
2576 spin_lock_init(&sbi->inode_lock[i]);
2577 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002578
Chao Yu1dcc3362015-02-05 17:57:31 +08002579 init_extent_cache_info(sbi);
2580
Jaegeuk Kim6451e042014-07-25 15:47:17 -07002581 init_ino_entry_info(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002582
2583 /* setup f2fs internal modules */
2584 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002585 if (err) {
2586 f2fs_msg(sb, KERN_ERR,
2587 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002588 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +09002589 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002590 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002591 if (err) {
2592 f2fs_msg(sb, KERN_ERR,
2593 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002594 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +09002595 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002596
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002597 /* For write statistics */
2598 if (sb->s_bdev->bd_part)
2599 sbi->sectors_written_start =
2600 (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
2601
2602 /* Read accumulated write IO statistics if exists */
2603 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
2604 if (__exist_node_summaries(sbi))
2605 sbi->kbytes_written =
Shuoran Liub2dde6f2016-03-29 18:00:15 +08002606 le64_to_cpu(seg_i->journal->info.kbytes_written);
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002607
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002608 build_gc_manager(sbi);
2609
2610 /* get an inode for node space */
2611 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
2612 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002613 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002614 err = PTR_ERR(sbi->node_inode);
2615 goto free_nm;
2616 }
2617
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08002618 err = f2fs_build_stats(sbi);
2619 if (err)
Chao Yubae01ed2017-11-30 19:28:20 +08002620 goto free_node_inode;
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08002621
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002622 /* read root inode and dentry */
2623 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
2624 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002625 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002626 err = PTR_ERR(root);
Chao Yubae01ed2017-11-30 19:28:20 +08002627 goto free_stats;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002628 }
Chao Yu8f99a942013-11-28 15:43:43 +08002629 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
Chao Yu9d847952014-07-25 12:55:09 +08002630 iput(root);
Chao Yu8f99a942013-11-28 15:43:43 +08002631 err = -EINVAL;
Chao Yu9d847952014-07-25 12:55:09 +08002632 goto free_node_inode;
Chao Yu8f99a942013-11-28 15:43:43 +08002633 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002634
2635 sb->s_root = d_make_root(root); /* allocate root dentry */
2636 if (!sb->s_root) {
2637 err = -ENOMEM;
2638 goto free_root_inode;
2639 }
2640
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002641 err = f2fs_register_sysfs(sbi);
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09002642 if (err)
Chao Yua3981012017-06-14 17:39:46 +08002643 goto free_root_inode;
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09002644
Jaegeuk Kimea676732017-10-06 09:14:28 -07002645#ifdef CONFIG_QUOTA
2646 /*
2647 * Turn on quotas which were not enabled for read-only mounts if
2648 * filesystem has quota feature, so that they are updated correctly.
2649 */
2650 if (f2fs_sb_has_quota_ino(sb) && !sb_rdonly(sb)) {
2651 err = f2fs_enable_quotas(sb);
2652 if (err) {
2653 f2fs_msg(sb, KERN_ERR,
2654 "Cannot turn on quotas: error %d", err);
2655 goto free_sysfs;
2656 }
2657 }
2658#endif
Chao Yu4b2414d2017-08-08 10:54:31 +08002659 /* if there are nt orphan nodes free them */
2660 err = recover_orphan_inodes(sbi);
2661 if (err)
Jaegeuk Kimea676732017-10-06 09:14:28 -07002662 goto free_meta;
Chao Yu4b2414d2017-08-08 10:54:31 +08002663
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002664 /* recover fsynced data */
2665 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002666 /*
2667 * mount should be failed, when device has readonly mode, and
2668 * previous checkpoint was not done by clean system shutdown.
2669 */
2670 if (bdev_read_only(sb->s_bdev) &&
Chao Yuaaec2b12016-09-20 11:04:18 +08002671 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002672 err = -EROFS;
Chao Yu4b2414d2017-08-08 10:54:31 +08002673 goto free_meta;
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002674 }
Chao Yu2adc3502015-03-16 21:08:44 +08002675
2676 if (need_fsck)
2677 set_sbi_flag(sbi, SBI_NEED_FSCK);
2678
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07002679 if (!retry)
2680 goto skip_recovery;
2681
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002682 err = recover_fsync_data(sbi, false);
2683 if (err < 0) {
Chao Yu2adc3502015-03-16 21:08:44 +08002684 need_fsck = true;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002685 f2fs_msg(sb, KERN_ERR,
Sheng Yong99e3e852016-05-11 17:08:14 +08002686 "Cannot recover all fsync data errno=%d", err);
Chao Yu4b2414d2017-08-08 10:54:31 +08002687 goto free_meta;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002688 }
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002689 } else {
2690 err = recover_fsync_data(sbi, true);
2691
2692 if (!f2fs_readonly(sb) && err > 0) {
2693 err = -EINVAL;
2694 f2fs_msg(sb, KERN_ERR,
2695 "Need to recover fsync data");
Jaegeuk Kimea676732017-10-06 09:14:28 -07002696 goto free_meta;
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002697 }
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002698 }
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07002699skip_recovery:
Jaegeuk Kim315df832015-08-11 12:45:39 -07002700 /* recover_fsync_data() cleared this already */
2701 clear_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002702
2703 /*
2704 * If filesystem is not mounted as read-only then
2705 * do start the gc_thread.
2706 */
Chao Yu6c029932014-11-18 11:16:01 +08002707 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002708 /* After POR, we can run background GC thread.*/
2709 err = start_gc_thread(sbi);
2710 if (err)
Chao Yu4b2414d2017-08-08 10:54:31 +08002711 goto free_meta;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002712 }
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002713 kfree(options);
hujianyangda554e42015-05-21 14:42:53 +08002714
2715 /* recover broken superblock */
Jaegeuk Kimf2353d7b2016-03-23 10:42:01 -07002716 if (recovery) {
Chao Yu41214b32016-02-22 18:33:20 +08002717 err = f2fs_commit_super(sbi, true);
2718 f2fs_msg(sb, KERN_INFO,
Sheng Yong99e3e852016-05-11 17:08:14 +08002719 "Try to recover %dth superblock, ret: %d",
Chao Yu41214b32016-02-22 18:33:20 +08002720 sbi->valid_super_block ? 1 : 2, err);
hujianyangda554e42015-05-21 14:42:53 +08002721 }
2722
Chao Yubae01ed2017-11-30 19:28:20 +08002723 f2fs_join_shrinker(sbi);
2724
Jaegeuk Kim1200abb2017-02-09 13:25:35 -08002725 f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
2726 cur_cp_version(F2FS_CKPT(sbi)));
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08002727 f2fs_update_time(sbi, CP_TIME);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002728 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002729 return 0;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002730
Chao Yu4b2414d2017-08-08 10:54:31 +08002731free_meta:
Jaegeuk Kimea676732017-10-06 09:14:28 -07002732#ifdef CONFIG_QUOTA
2733 if (f2fs_sb_has_quota_ino(sb) && !sb_rdonly(sb))
2734 f2fs_quota_off_umount(sbi->sb);
2735#endif
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07002736 f2fs_sync_inode_meta(sbi);
Chao Yu4b2414d2017-08-08 10:54:31 +08002737 /*
2738 * Some dirty meta pages can be produced by recover_orphan_inodes()
2739 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
2740 * followed by write_checkpoint() through f2fs_write_node_pages(), which
2741 * falls into an infinite loop in sync_meta_pages().
2742 */
2743 truncate_inode_pages_final(META_MAPPING(sbi));
Jaegeuk Kimea676732017-10-06 09:14:28 -07002744#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +08002745free_sysfs:
Jaegeuk Kimea676732017-10-06 09:14:28 -07002746#endif
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002747 f2fs_unregister_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002748free_root_inode:
2749 dput(sb->s_root);
2750 sb->s_root = NULL;
Chao Yubae01ed2017-11-30 19:28:20 +08002751free_stats:
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08002752 f2fs_destroy_stats(sbi);
Chao Yubae01ed2017-11-30 19:28:20 +08002753free_node_inode:
2754 release_ino_entry(sbi, true);
2755 truncate_inode_pages_final(NODE_MAPPING(sbi));
2756 iput(sbi->node_inode);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002757free_nm:
2758 destroy_node_manager(sbi);
2759free_sm:
2760 destroy_segment_manager(sbi);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002761free_devices:
2762 destroy_device_list(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002763 kfree(sbi->ckpt);
2764free_meta_inode:
2765 make_bad_inode(sbi->meta_inode);
2766 iput(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002767free_io_dummy:
2768 mempool_destroy(sbi->write_io_dummy);
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002769free_options:
Jaegeuk Kima912b542017-05-10 11:18:25 -07002770 for (i = 0; i < NR_PAGE_TYPE; i++)
2771 kfree(sbi->write_io[i]);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002772 destroy_percpu_info(sbi);
Chao Yu4b2414d2017-08-08 10:54:31 +08002773#ifdef CONFIG_QUOTA
2774 for (i = 0; i < MAXQUOTAS; i++)
2775 kfree(sbi->s_qf_names[i]);
2776#endif
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002777 kfree(options);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002778free_sb_buf:
Yunlei Heb39f0de2015-12-15 17:17:20 +08002779 kfree(raw_super);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002780free_sbi:
Keith Mok43b65732016-03-02 12:04:24 -08002781 if (sbi->s_chksum_driver)
2782 crypto_free_shash(sbi->s_chksum_driver);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002783 kfree(sbi);
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002784
2785 /* give only one another chance */
2786 if (retry) {
Taehee Yoo9df47ba2015-04-13 21:48:06 +09002787 retry = false;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002788 shrink_dcache_sb(sb);
2789 goto try_onemore;
2790 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002791 return err;
2792}
2793
2794static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
2795 const char *dev_name, void *data)
2796{
2797 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
2798}
2799
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002800static void kill_f2fs_super(struct super_block *sb)
2801{
Chao Yucce13252017-06-29 23:17:45 +08002802 if (sb->s_root) {
Chao Yucaf00472015-01-28 17:48:42 +08002803 set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
Chao Yucce13252017-06-29 23:17:45 +08002804 stop_gc_thread(F2FS_SB(sb));
2805 stop_discard_thread(F2FS_SB(sb));
2806 }
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002807 kill_block_super(sb);
2808}
2809
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002810static struct file_system_type f2fs_fs_type = {
2811 .owner = THIS_MODULE,
2812 .name = "f2fs",
2813 .mount = f2fs_mount,
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002814 .kill_sb = kill_f2fs_super,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002815 .fs_flags = FS_REQUIRES_DEV,
2816};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08002817MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002818
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002819static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002820{
Vladimir Davydov5d097052016-01-14 15:18:21 -08002821 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
2822 sizeof(struct f2fs_inode_info), 0,
2823 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002824 if (!f2fs_inode_cachep)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002825 return -ENOMEM;
2826 return 0;
2827}
2828
2829static void destroy_inodecache(void)
2830{
2831 /*
2832 * Make sure all delayed rcu free inodes are flushed before we
2833 * destroy cache.
2834 */
2835 rcu_barrier();
2836 kmem_cache_destroy(f2fs_inode_cachep);
2837}
2838
2839static int __init init_f2fs_fs(void)
2840{
2841 int err;
2842
Jaegeuk Kimc0508652015-01-07 14:07:36 -08002843 f2fs_build_trace_ios();
2844
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002845 err = init_inodecache();
2846 if (err)
2847 goto fail;
2848 err = create_node_manager_caches();
2849 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002850 goto free_inodecache;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002851 err = create_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002852 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002853 goto free_node_manager_caches;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002854 err = create_checkpoint_caches();
2855 if (err)
Chao Yu06292072014-12-29 15:56:18 +08002856 goto free_segment_manager_caches;
Chao Yu1dcc3362015-02-05 17:57:31 +08002857 err = create_extent_cache();
2858 if (err)
2859 goto free_checkpoint_caches;
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002860 err = f2fs_init_sysfs();
Chao Yua3981012017-06-14 17:39:46 +08002861 if (err)
Chao Yu1dcc3362015-02-05 17:57:31 +08002862 goto free_extent_cache;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002863 err = register_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimcfc4d972015-05-15 15:37:24 -07002864 if (err)
Chao Yua3981012017-06-14 17:39:46 +08002865 goto free_sysfs;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002866 err = register_filesystem(&f2fs_fs_type);
2867 if (err)
2868 goto free_shrinker;
Chao Yu787c7b8c2015-10-29 09:13:04 +08002869 err = f2fs_create_root_stats();
2870 if (err)
2871 goto free_filesystem;
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002872 return 0;
2873
Chao Yu787c7b8c2015-10-29 09:13:04 +08002874free_filesystem:
2875 unregister_filesystem(&f2fs_fs_type);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002876free_shrinker:
2877 unregister_shrinker(&f2fs_shrinker_info);
Chao Yua3981012017-06-14 17:39:46 +08002878free_sysfs:
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002879 f2fs_exit_sysfs();
Chao Yu1dcc3362015-02-05 17:57:31 +08002880free_extent_cache:
2881 destroy_extent_cache();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002882free_checkpoint_caches:
2883 destroy_checkpoint_caches();
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002884free_segment_manager_caches:
2885 destroy_segment_manager_caches();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002886free_node_manager_caches:
2887 destroy_node_manager_caches();
2888free_inodecache:
2889 destroy_inodecache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002890fail:
2891 return err;
2892}
2893
2894static void __exit exit_f2fs_fs(void)
2895{
Namjae Jeon4589d252013-01-15 19:58:47 +09002896 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002897 unregister_filesystem(&f2fs_fs_type);
Tiezhu Yangb8bef792016-05-18 08:02:25 +08002898 unregister_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07002899 f2fs_exit_sysfs();
Wanpeng Lifdf6c8b2015-03-06 15:00:54 +08002900 destroy_extent_cache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002901 destroy_checkpoint_caches();
Changman Lee5dcd8a72013-12-11 14:32:13 +09002902 destroy_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002903 destroy_node_manager_caches();
2904 destroy_inodecache();
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -08002905 f2fs_destroy_trace_ios();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002906}
2907
2908module_init(init_f2fs_fs)
2909module_exit(exit_f2fs_fs)
2910
2911MODULE_AUTHOR("Samsung Electronics's Praesto Team");
2912MODULE_DESCRIPTION("Flash Friendly File System");
2913MODULE_LICENSE("GPL");
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -07002914