1 /*
2 * fs/f2fs/super.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/fs.h>
14 #include <linux/statfs.h>
15 #include <linux/buffer_head.h>
16 #include <linux/backing-dev.h>
17 #include <linux/kthread.h>
18 #include <linux/parser.h>
19 #include <linux/mount.h>
20 #include <linux/seq_file.h>
21 #include <linux/proc_fs.h>
22 #include <linux/random.h>
23 #include <linux/exportfs.h>
24 #include <linux/blkdev.h>
25 #include <linux/quotaops.h>
26 #include <linux/f2fs_fs.h>
27 #include <linux/sysfs.h>
28 #include <linux/quota.h>
29
30 #include "f2fs.h"
31 #include "node.h"
32 #include "segment.h"
33 #include "xattr.h"
34 #include "gc.h"
35 #include "trace.h"
36
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/f2fs.h>
39
40 static struct kmem_cache *f2fs_inode_cachep;
41
42 #ifdef CONFIG_F2FS_FAULT_INJECTION
43
44 char *f2fs_fault_name[FAULT_MAX] = {
45 [FAULT_KMALLOC] = "kmalloc",
46 [FAULT_KVMALLOC] = "kvmalloc",
47 [FAULT_PAGE_ALLOC] = "page alloc",
48 [FAULT_PAGE_GET] = "page get",
49 [FAULT_ALLOC_BIO] = "alloc bio",
50 [FAULT_ALLOC_NID] = "alloc nid",
51 [FAULT_ORPHAN] = "orphan",
52 [FAULT_BLOCK] = "no more block",
53 [FAULT_DIR_DEPTH] = "too big dir depth",
54 [FAULT_EVICT_INODE] = "evict_inode fail",
55 [FAULT_TRUNCATE] = "truncate fail",
56 [FAULT_IO] = "IO error",
57 [FAULT_CHECKPOINT] = "checkpoint error",
58 [FAULT_DISCARD] = "discard error",
59 };
60
f2fs_build_fault_attr(struct f2fs_sb_info * sbi,unsigned int rate,unsigned int type)61 void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
62 unsigned int type)
63 {
64 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
65
66 if (rate) {
67 atomic_set(&ffi->inject_ops, 0);
68 ffi->inject_rate = rate;
69 }
70
71 if (type)
72 ffi->inject_type = type;
73
74 if (!rate && !type)
75 memset(ffi, 0, sizeof(struct f2fs_fault_info));
76 }
77 #endif
78
79 /* f2fs-wide shrinker description */
80 static struct shrinker f2fs_shrinker_info = {
81 .scan_objects = f2fs_shrink_scan,
82 .count_objects = f2fs_shrink_count,
83 .seeks = DEFAULT_SEEKS,
84 };
85
86 enum {
87 Opt_gc_background,
88 Opt_disable_roll_forward,
89 Opt_norecovery,
90 Opt_discard,
91 Opt_nodiscard,
92 Opt_noheap,
93 Opt_heap,
94 Opt_user_xattr,
95 Opt_nouser_xattr,
96 Opt_acl,
97 Opt_noacl,
98 Opt_active_logs,
99 Opt_disable_ext_identify,
100 Opt_inline_xattr,
101 Opt_noinline_xattr,
102 Opt_inline_xattr_size,
103 Opt_inline_data,
104 Opt_inline_dentry,
105 Opt_noinline_dentry,
106 Opt_flush_merge,
107 Opt_noflush_merge,
108 Opt_nobarrier,
109 Opt_fastboot,
110 Opt_extent_cache,
111 Opt_noextent_cache,
112 Opt_noinline_data,
113 Opt_data_flush,
114 Opt_reserve_root,
115 Opt_resgid,
116 Opt_resuid,
117 Opt_mode,
118 Opt_io_size_bits,
119 Opt_fault_injection,
120 Opt_fault_type,
121 Opt_lazytime,
122 Opt_nolazytime,
123 Opt_quota,
124 Opt_noquota,
125 Opt_usrquota,
126 Opt_grpquota,
127 Opt_prjquota,
128 Opt_usrjquota,
129 Opt_grpjquota,
130 Opt_prjjquota,
131 Opt_offusrjquota,
132 Opt_offgrpjquota,
133 Opt_offprjjquota,
134 Opt_jqfmt_vfsold,
135 Opt_jqfmt_vfsv0,
136 Opt_jqfmt_vfsv1,
137 Opt_whint,
138 Opt_alloc,
139 Opt_fsync,
140 Opt_test_dummy_encryption,
141 Opt_err,
142 };
143
144 static match_table_t f2fs_tokens = {
145 {Opt_gc_background, "background_gc=%s"},
146 {Opt_disable_roll_forward, "disable_roll_forward"},
147 {Opt_norecovery, "norecovery"},
148 {Opt_discard, "discard"},
149 {Opt_nodiscard, "nodiscard"},
150 {Opt_noheap, "no_heap"},
151 {Opt_heap, "heap"},
152 {Opt_user_xattr, "user_xattr"},
153 {Opt_nouser_xattr, "nouser_xattr"},
154 {Opt_acl, "acl"},
155 {Opt_noacl, "noacl"},
156 {Opt_active_logs, "active_logs=%u"},
157 {Opt_disable_ext_identify, "disable_ext_identify"},
158 {Opt_inline_xattr, "inline_xattr"},
159 {Opt_noinline_xattr, "noinline_xattr"},
160 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
161 {Opt_inline_data, "inline_data"},
162 {Opt_inline_dentry, "inline_dentry"},
163 {Opt_noinline_dentry, "noinline_dentry"},
164 {Opt_flush_merge, "flush_merge"},
165 {Opt_noflush_merge, "noflush_merge"},
166 {Opt_nobarrier, "nobarrier"},
167 {Opt_fastboot, "fastboot"},
168 {Opt_extent_cache, "extent_cache"},
169 {Opt_noextent_cache, "noextent_cache"},
170 {Opt_noinline_data, "noinline_data"},
171 {Opt_data_flush, "data_flush"},
172 {Opt_reserve_root, "reserve_root=%u"},
173 {Opt_resgid, "resgid=%u"},
174 {Opt_resuid, "resuid=%u"},
175 {Opt_mode, "mode=%s"},
176 {Opt_io_size_bits, "io_bits=%u"},
177 {Opt_fault_injection, "fault_injection=%u"},
178 {Opt_fault_type, "fault_type=%u"},
179 {Opt_lazytime, "lazytime"},
180 {Opt_nolazytime, "nolazytime"},
181 {Opt_quota, "quota"},
182 {Opt_noquota, "noquota"},
183 {Opt_usrquota, "usrquota"},
184 {Opt_grpquota, "grpquota"},
185 {Opt_prjquota, "prjquota"},
186 {Opt_usrjquota, "usrjquota=%s"},
187 {Opt_grpjquota, "grpjquota=%s"},
188 {Opt_prjjquota, "prjjquota=%s"},
189 {Opt_offusrjquota, "usrjquota="},
190 {Opt_offgrpjquota, "grpjquota="},
191 {Opt_offprjjquota, "prjjquota="},
192 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
193 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
194 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
195 {Opt_whint, "whint_mode=%s"},
196 {Opt_alloc, "alloc_mode=%s"},
197 {Opt_fsync, "fsync_mode=%s"},
198 {Opt_test_dummy_encryption, "test_dummy_encryption"},
199 {Opt_err, NULL},
200 };
201
f2fs_msg(struct super_block * sb,const char * level,const char * fmt,...)202 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
203 {
204 struct va_format vaf;
205 va_list args;
206
207 va_start(args, fmt);
208 vaf.fmt = fmt;
209 vaf.va = &args;
210 printk_ratelimited("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
211 va_end(args);
212 }
213
limit_reserve_root(struct f2fs_sb_info * sbi)214 static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
215 {
216 block_t limit = (sbi->user_block_count << 1) / 1000;
217
218 /* limit is 0.2% */
219 if (test_opt(sbi, RESERVE_ROOT) &&
220 F2FS_OPTION(sbi).root_reserved_blocks > limit) {
221 F2FS_OPTION(sbi).root_reserved_blocks = limit;
222 f2fs_msg(sbi->sb, KERN_INFO,
223 "Reduce reserved blocks for root = %u",
224 F2FS_OPTION(sbi).root_reserved_blocks);
225 }
226 if (!test_opt(sbi, RESERVE_ROOT) &&
227 (!uid_eq(F2FS_OPTION(sbi).s_resuid,
228 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
229 !gid_eq(F2FS_OPTION(sbi).s_resgid,
230 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
231 f2fs_msg(sbi->sb, KERN_INFO,
232 "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
233 from_kuid_munged(&init_user_ns,
234 F2FS_OPTION(sbi).s_resuid),
235 from_kgid_munged(&init_user_ns,
236 F2FS_OPTION(sbi).s_resgid));
237 }
238
init_once(void * foo)239 static void init_once(void *foo)
240 {
241 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
242
243 inode_init_once(&fi->vfs_inode);
244 }
245
246 #ifdef CONFIG_QUOTA
247 static const char * const quotatypes[] = INITQFNAMES;
248 #define QTYPE2NAME(t) (quotatypes[t])
f2fs_set_qf_name(struct super_block * sb,int qtype,substring_t * args)249 static int f2fs_set_qf_name(struct super_block *sb, int qtype,
250 substring_t *args)
251 {
252 struct f2fs_sb_info *sbi = F2FS_SB(sb);
253 char *qname;
254 int ret = -EINVAL;
255
256 if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
257 f2fs_msg(sb, KERN_ERR,
258 "Cannot change journaled "
259 "quota options when quota turned on");
260 return -EINVAL;
261 }
262 if (f2fs_sb_has_quota_ino(sb)) {
263 f2fs_msg(sb, KERN_INFO,
264 "QUOTA feature is enabled, so ignore qf_name");
265 return 0;
266 }
267
268 qname = match_strdup(args);
269 if (!qname) {
270 f2fs_msg(sb, KERN_ERR,
271 "Not enough memory for storing quotafile name");
272 return -EINVAL;
273 }
274 if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
275 if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
276 ret = 0;
277 else
278 f2fs_msg(sb, KERN_ERR,
279 "%s quota file already specified",
280 QTYPE2NAME(qtype));
281 goto errout;
282 }
283 if (strchr(qname, '/')) {
284 f2fs_msg(sb, KERN_ERR,
285 "quotafile must be on filesystem root");
286 goto errout;
287 }
288 F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
289 set_opt(sbi, QUOTA);
290 return 0;
291 errout:
292 kfree(qname);
293 return ret;
294 }
295
f2fs_clear_qf_name(struct super_block * sb,int qtype)296 static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
297 {
298 struct f2fs_sb_info *sbi = F2FS_SB(sb);
299
300 if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
301 f2fs_msg(sb, KERN_ERR, "Cannot change journaled quota options"
302 " when quota turned on");
303 return -EINVAL;
304 }
305 kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
306 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
307 return 0;
308 }
309
f2fs_check_quota_options(struct f2fs_sb_info * sbi)310 static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
311 {
312 /*
313 * We do the test below only for project quotas. 'usrquota' and
314 * 'grpquota' mount options are allowed even without quota feature
315 * to support legacy quotas in quota files.
316 */
317 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi->sb)) {
318 f2fs_msg(sbi->sb, KERN_ERR, "Project quota feature not enabled. "
319 "Cannot enable project quota enforcement.");
320 return -1;
321 }
322 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
323 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
324 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
325 if (test_opt(sbi, USRQUOTA) &&
326 F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
327 clear_opt(sbi, USRQUOTA);
328
329 if (test_opt(sbi, GRPQUOTA) &&
330 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
331 clear_opt(sbi, GRPQUOTA);
332
333 if (test_opt(sbi, PRJQUOTA) &&
334 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
335 clear_opt(sbi, PRJQUOTA);
336
337 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
338 test_opt(sbi, PRJQUOTA)) {
339 f2fs_msg(sbi->sb, KERN_ERR, "old and new quota "
340 "format mixing");
341 return -1;
342 }
343
344 if (!F2FS_OPTION(sbi).s_jquota_fmt) {
345 f2fs_msg(sbi->sb, KERN_ERR, "journaled quota format "
346 "not specified");
347 return -1;
348 }
349 }
350
351 if (f2fs_sb_has_quota_ino(sbi->sb) && F2FS_OPTION(sbi).s_jquota_fmt) {
352 f2fs_msg(sbi->sb, KERN_INFO,
353 "QUOTA feature is enabled, so ignore jquota_fmt");
354 F2FS_OPTION(sbi).s_jquota_fmt = 0;
355 }
356 return 0;
357 }
358 #endif
359
parse_options(struct super_block * sb,char * options)360 static int parse_options(struct super_block *sb, char *options)
361 {
362 struct f2fs_sb_info *sbi = F2FS_SB(sb);
363 substring_t args[MAX_OPT_ARGS];
364 char *p, *name;
365 int arg = 0;
366 kuid_t uid;
367 kgid_t gid;
368 #ifdef CONFIG_QUOTA
369 int ret;
370 #endif
371
372 if (!options)
373 return 0;
374
375 while ((p = strsep(&options, ",")) != NULL) {
376 int token;
377 if (!*p)
378 continue;
379 /*
380 * Initialize args struct so we know whether arg was
381 * found; some options take optional arguments.
382 */
383 args[0].to = args[0].from = NULL;
384 token = match_token(p, f2fs_tokens, args);
385
386 switch (token) {
387 case Opt_gc_background:
388 name = match_strdup(&args[0]);
389
390 if (!name)
391 return -ENOMEM;
392 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
393 set_opt(sbi, BG_GC);
394 clear_opt(sbi, FORCE_FG_GC);
395 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
396 clear_opt(sbi, BG_GC);
397 clear_opt(sbi, FORCE_FG_GC);
398 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
399 set_opt(sbi, BG_GC);
400 set_opt(sbi, FORCE_FG_GC);
401 } else {
402 kfree(name);
403 return -EINVAL;
404 }
405 kfree(name);
406 break;
407 case Opt_disable_roll_forward:
408 set_opt(sbi, DISABLE_ROLL_FORWARD);
409 break;
410 case Opt_norecovery:
411 /* this option mounts f2fs with ro */
412 set_opt(sbi, DISABLE_ROLL_FORWARD);
413 if (!f2fs_readonly(sb))
414 return -EINVAL;
415 break;
416 case Opt_discard:
417 set_opt(sbi, DISCARD);
418 break;
419 case Opt_nodiscard:
420 if (f2fs_sb_has_blkzoned(sb)) {
421 f2fs_msg(sb, KERN_WARNING,
422 "discard is required for zoned block devices");
423 return -EINVAL;
424 }
425 clear_opt(sbi, DISCARD);
426 break;
427 case Opt_noheap:
428 set_opt(sbi, NOHEAP);
429 break;
430 case Opt_heap:
431 clear_opt(sbi, NOHEAP);
432 break;
433 #ifdef CONFIG_F2FS_FS_XATTR
434 case Opt_user_xattr:
435 set_opt(sbi, XATTR_USER);
436 break;
437 case Opt_nouser_xattr:
438 clear_opt(sbi, XATTR_USER);
439 break;
440 case Opt_inline_xattr:
441 set_opt(sbi, INLINE_XATTR);
442 break;
443 case Opt_noinline_xattr:
444 clear_opt(sbi, INLINE_XATTR);
445 break;
446 case Opt_inline_xattr_size:
447 if (args->from && match_int(args, &arg))
448 return -EINVAL;
449 set_opt(sbi, INLINE_XATTR_SIZE);
450 F2FS_OPTION(sbi).inline_xattr_size = arg;
451 break;
452 #else
453 case Opt_user_xattr:
454 f2fs_msg(sb, KERN_INFO,
455 "user_xattr options not supported");
456 break;
457 case Opt_nouser_xattr:
458 f2fs_msg(sb, KERN_INFO,
459 "nouser_xattr options not supported");
460 break;
461 case Opt_inline_xattr:
462 f2fs_msg(sb, KERN_INFO,
463 "inline_xattr options not supported");
464 break;
465 case Opt_noinline_xattr:
466 f2fs_msg(sb, KERN_INFO,
467 "noinline_xattr options not supported");
468 break;
469 #endif
470 #ifdef CONFIG_F2FS_FS_POSIX_ACL
471 case Opt_acl:
472 set_opt(sbi, POSIX_ACL);
473 break;
474 case Opt_noacl:
475 clear_opt(sbi, POSIX_ACL);
476 break;
477 #else
478 case Opt_acl:
479 f2fs_msg(sb, KERN_INFO, "acl options not supported");
480 break;
481 case Opt_noacl:
482 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
483 break;
484 #endif
485 case Opt_active_logs:
486 if (args->from && match_int(args, &arg))
487 return -EINVAL;
488 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
489 return -EINVAL;
490 F2FS_OPTION(sbi).active_logs = arg;
491 break;
492 case Opt_disable_ext_identify:
493 set_opt(sbi, DISABLE_EXT_IDENTIFY);
494 break;
495 case Opt_inline_data:
496 set_opt(sbi, INLINE_DATA);
497 break;
498 case Opt_inline_dentry:
499 set_opt(sbi, INLINE_DENTRY);
500 break;
501 case Opt_noinline_dentry:
502 clear_opt(sbi, INLINE_DENTRY);
503 break;
504 case Opt_flush_merge:
505 set_opt(sbi, FLUSH_MERGE);
506 break;
507 case Opt_noflush_merge:
508 clear_opt(sbi, FLUSH_MERGE);
509 break;
510 case Opt_nobarrier:
511 set_opt(sbi, NOBARRIER);
512 break;
513 case Opt_fastboot:
514 set_opt(sbi, FASTBOOT);
515 break;
516 case Opt_extent_cache:
517 set_opt(sbi, EXTENT_CACHE);
518 break;
519 case Opt_noextent_cache:
520 clear_opt(sbi, EXTENT_CACHE);
521 break;
522 case Opt_noinline_data:
523 clear_opt(sbi, INLINE_DATA);
524 break;
525 case Opt_data_flush:
526 set_opt(sbi, DATA_FLUSH);
527 break;
528 case Opt_reserve_root:
529 if (args->from && match_int(args, &arg))
530 return -EINVAL;
531 if (test_opt(sbi, RESERVE_ROOT)) {
532 f2fs_msg(sb, KERN_INFO,
533 "Preserve previous reserve_root=%u",
534 F2FS_OPTION(sbi).root_reserved_blocks);
535 } else {
536 F2FS_OPTION(sbi).root_reserved_blocks = arg;
537 set_opt(sbi, RESERVE_ROOT);
538 }
539 break;
540 case Opt_resuid:
541 if (args->from && match_int(args, &arg))
542 return -EINVAL;
543 uid = make_kuid(current_user_ns(), arg);
544 if (!uid_valid(uid)) {
545 f2fs_msg(sb, KERN_ERR,
546 "Invalid uid value %d", arg);
547 return -EINVAL;
548 }
549 F2FS_OPTION(sbi).s_resuid = uid;
550 break;
551 case Opt_resgid:
552 if (args->from && match_int(args, &arg))
553 return -EINVAL;
554 gid = make_kgid(current_user_ns(), arg);
555 if (!gid_valid(gid)) {
556 f2fs_msg(sb, KERN_ERR,
557 "Invalid gid value %d", arg);
558 return -EINVAL;
559 }
560 F2FS_OPTION(sbi).s_resgid = gid;
561 break;
562 case Opt_mode:
563 name = match_strdup(&args[0]);
564
565 if (!name)
566 return -ENOMEM;
567 if (strlen(name) == 8 &&
568 !strncmp(name, "adaptive", 8)) {
569 if (f2fs_sb_has_blkzoned(sb)) {
570 f2fs_msg(sb, KERN_WARNING,
571 "adaptive mode is not allowed with "
572 "zoned block device feature");
573 kfree(name);
574 return -EINVAL;
575 }
576 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
577 } else if (strlen(name) == 3 &&
578 !strncmp(name, "lfs", 3)) {
579 set_opt_mode(sbi, F2FS_MOUNT_LFS);
580 } else {
581 kfree(name);
582 return -EINVAL;
583 }
584 kfree(name);
585 break;
586 case Opt_io_size_bits:
587 if (args->from && match_int(args, &arg))
588 return -EINVAL;
589 if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
590 f2fs_msg(sb, KERN_WARNING,
591 "Not support %d, larger than %d",
592 1 << arg, BIO_MAX_PAGES);
593 return -EINVAL;
594 }
595 F2FS_OPTION(sbi).write_io_size_bits = arg;
596 break;
597 case Opt_fault_injection:
598 if (args->from && match_int(args, &arg))
599 return -EINVAL;
600 #ifdef CONFIG_F2FS_FAULT_INJECTION
601 f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
602 set_opt(sbi, FAULT_INJECTION);
603 #else
604 f2fs_msg(sb, KERN_INFO,
605 "FAULT_INJECTION was not selected");
606 #endif
607 break;
608 case Opt_fault_type:
609 if (args->from && match_int(args, &arg))
610 return -EINVAL;
611 #ifdef CONFIG_F2FS_FAULT_INJECTION
612 f2fs_build_fault_attr(sbi, 0, arg);
613 set_opt(sbi, FAULT_INJECTION);
614 #else
615 f2fs_msg(sb, KERN_INFO,
616 "FAULT_INJECTION was not selected");
617 #endif
618 break;
619 case Opt_lazytime:
620 sb->s_flags |= SB_LAZYTIME;
621 break;
622 case Opt_nolazytime:
623 sb->s_flags &= ~SB_LAZYTIME;
624 break;
625 #ifdef CONFIG_QUOTA
626 case Opt_quota:
627 case Opt_usrquota:
628 set_opt(sbi, USRQUOTA);
629 break;
630 case Opt_grpquota:
631 set_opt(sbi, GRPQUOTA);
632 break;
633 case Opt_prjquota:
634 set_opt(sbi, PRJQUOTA);
635 break;
636 case Opt_usrjquota:
637 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
638 if (ret)
639 return ret;
640 break;
641 case Opt_grpjquota:
642 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
643 if (ret)
644 return ret;
645 break;
646 case Opt_prjjquota:
647 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
648 if (ret)
649 return ret;
650 break;
651 case Opt_offusrjquota:
652 ret = f2fs_clear_qf_name(sb, USRQUOTA);
653 if (ret)
654 return ret;
655 break;
656 case Opt_offgrpjquota:
657 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
658 if (ret)
659 return ret;
660 break;
661 case Opt_offprjjquota:
662 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
663 if (ret)
664 return ret;
665 break;
666 case Opt_jqfmt_vfsold:
667 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
668 break;
669 case Opt_jqfmt_vfsv0:
670 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
671 break;
672 case Opt_jqfmt_vfsv1:
673 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
674 break;
675 case Opt_noquota:
676 clear_opt(sbi, QUOTA);
677 clear_opt(sbi, USRQUOTA);
678 clear_opt(sbi, GRPQUOTA);
679 clear_opt(sbi, PRJQUOTA);
680 break;
681 #else
682 case Opt_quota:
683 case Opt_usrquota:
684 case Opt_grpquota:
685 case Opt_prjquota:
686 case Opt_usrjquota:
687 case Opt_grpjquota:
688 case Opt_prjjquota:
689 case Opt_offusrjquota:
690 case Opt_offgrpjquota:
691 case Opt_offprjjquota:
692 case Opt_jqfmt_vfsold:
693 case Opt_jqfmt_vfsv0:
694 case Opt_jqfmt_vfsv1:
695 case Opt_noquota:
696 f2fs_msg(sb, KERN_INFO,
697 "quota operations not supported");
698 break;
699 #endif
700 case Opt_whint:
701 name = match_strdup(&args[0]);
702 if (!name)
703 return -ENOMEM;
704 if (strlen(name) == 10 &&
705 !strncmp(name, "user-based", 10)) {
706 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_USER;
707 } else if (strlen(name) == 3 &&
708 !strncmp(name, "off", 3)) {
709 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
710 } else if (strlen(name) == 8 &&
711 !strncmp(name, "fs-based", 8)) {
712 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_FS;
713 } else {
714 kfree(name);
715 return -EINVAL;
716 }
717 kfree(name);
718 break;
719 case Opt_alloc:
720 name = match_strdup(&args[0]);
721 if (!name)
722 return -ENOMEM;
723
724 if (strlen(name) == 7 &&
725 !strncmp(name, "default", 7)) {
726 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
727 } else if (strlen(name) == 5 &&
728 !strncmp(name, "reuse", 5)) {
729 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
730 } else {
731 kfree(name);
732 return -EINVAL;
733 }
734 kfree(name);
735 break;
736 case Opt_fsync:
737 name = match_strdup(&args[0]);
738 if (!name)
739 return -ENOMEM;
740 if (strlen(name) == 5 &&
741 !strncmp(name, "posix", 5)) {
742 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
743 } else if (strlen(name) == 6 &&
744 !strncmp(name, "strict", 6)) {
745 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
746 } else if (strlen(name) == 9 &&
747 !strncmp(name, "nobarrier", 9)) {
748 F2FS_OPTION(sbi).fsync_mode =
749 FSYNC_MODE_NOBARRIER;
750 } else {
751 kfree(name);
752 return -EINVAL;
753 }
754 kfree(name);
755 break;
756 case Opt_test_dummy_encryption:
757 #ifdef CONFIG_F2FS_FS_ENCRYPTION
758 if (!f2fs_sb_has_encrypt(sb)) {
759 f2fs_msg(sb, KERN_ERR, "Encrypt feature is off");
760 return -EINVAL;
761 }
762
763 F2FS_OPTION(sbi).test_dummy_encryption = true;
764 f2fs_msg(sb, KERN_INFO,
765 "Test dummy encryption mode enabled");
766 #else
767 f2fs_msg(sb, KERN_INFO,
768 "Test dummy encryption mount option ignored");
769 #endif
770 break;
771 default:
772 f2fs_msg(sb, KERN_ERR,
773 "Unrecognized mount option \"%s\" or missing value",
774 p);
775 return -EINVAL;
776 }
777 }
778 #ifdef CONFIG_QUOTA
779 if (f2fs_check_quota_options(sbi))
780 return -EINVAL;
781 #else
782 if (f2fs_sb_has_quota_ino(sbi->sb) && !f2fs_readonly(sbi->sb)) {
783 f2fs_msg(sbi->sb, KERN_INFO,
784 "Filesystem with quota feature cannot be mounted RDWR "
785 "without CONFIG_QUOTA");
786 return -EINVAL;
787 }
788 if (f2fs_sb_has_project_quota(sbi->sb) && !f2fs_readonly(sbi->sb)) {
789 f2fs_msg(sb, KERN_ERR,
790 "Filesystem with project quota feature cannot be "
791 "mounted RDWR without CONFIG_QUOTA");
792 return -EINVAL;
793 }
794 #endif
795
796 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
797 f2fs_msg(sb, KERN_ERR,
798 "Should set mode=lfs with %uKB-sized IO",
799 F2FS_IO_SIZE_KB(sbi));
800 return -EINVAL;
801 }
802
803 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
804 if (!f2fs_sb_has_extra_attr(sb) ||
805 !f2fs_sb_has_flexible_inline_xattr(sb)) {
806 f2fs_msg(sb, KERN_ERR,
807 "extra_attr or flexible_inline_xattr "
808 "feature is off");
809 return -EINVAL;
810 }
811 if (!test_opt(sbi, INLINE_XATTR)) {
812 f2fs_msg(sb, KERN_ERR,
813 "inline_xattr_size option should be "
814 "set with inline_xattr option");
815 return -EINVAL;
816 }
817 if (F2FS_OPTION(sbi).inline_xattr_size <
818 sizeof(struct f2fs_xattr_header) / sizeof(__le32) ||
819 F2FS_OPTION(sbi).inline_xattr_size >
820 DEF_ADDRS_PER_INODE -
821 F2FS_TOTAL_EXTRA_ATTR_SIZE / sizeof(__le32) -
822 DEF_INLINE_RESERVED_SIZE -
823 MIN_INLINE_DENTRY_SIZE / sizeof(__le32)) {
824 f2fs_msg(sb, KERN_ERR,
825 "inline xattr size is out of range");
826 return -EINVAL;
827 }
828 }
829
830 /* Not pass down write hints if the number of active logs is lesser
831 * than NR_CURSEG_TYPE.
832 */
833 if (F2FS_OPTION(sbi).active_logs != NR_CURSEG_TYPE)
834 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
835 return 0;
836 }
837
f2fs_alloc_inode(struct super_block * sb)838 static struct inode *f2fs_alloc_inode(struct super_block *sb)
839 {
840 struct f2fs_inode_info *fi;
841
842 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
843 if (!fi)
844 return NULL;
845
846 init_once((void *) fi);
847
848 /* Initialize f2fs-specific inode info */
849 atomic_set(&fi->dirty_pages, 0);
850 init_rwsem(&fi->i_sem);
851 INIT_LIST_HEAD(&fi->dirty_list);
852 INIT_LIST_HEAD(&fi->gdirty_list);
853 INIT_LIST_HEAD(&fi->inmem_ilist);
854 INIT_LIST_HEAD(&fi->inmem_pages);
855 mutex_init(&fi->inmem_lock);
856 init_rwsem(&fi->i_gc_rwsem[READ]);
857 init_rwsem(&fi->i_gc_rwsem[WRITE]);
858 init_rwsem(&fi->i_mmap_sem);
859 init_rwsem(&fi->i_xattr_sem);
860
861 /* Will be used by directory only */
862 fi->i_dir_level = F2FS_SB(sb)->dir_level;
863
864 return &fi->vfs_inode;
865 }
866
f2fs_drop_inode(struct inode * inode)867 static int f2fs_drop_inode(struct inode *inode)
868 {
869 int ret;
870 /*
871 * This is to avoid a deadlock condition like below.
872 * writeback_single_inode(inode)
873 * - f2fs_write_data_page
874 * - f2fs_gc -> iput -> evict
875 * - inode_wait_for_writeback(inode)
876 */
877 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
878 if (!inode->i_nlink && !is_bad_inode(inode)) {
879 /* to avoid evict_inode call simultaneously */
880 atomic_inc(&inode->i_count);
881 spin_unlock(&inode->i_lock);
882
883 /* some remained atomic pages should discarded */
884 if (f2fs_is_atomic_file(inode))
885 f2fs_drop_inmem_pages(inode);
886
887 /* should remain fi->extent_tree for writepage */
888 f2fs_destroy_extent_node(inode);
889
890 sb_start_intwrite(inode->i_sb);
891 f2fs_i_size_write(inode, 0);
892
893 if (F2FS_HAS_BLOCKS(inode))
894 f2fs_truncate(inode);
895
896 sb_end_intwrite(inode->i_sb);
897
898 spin_lock(&inode->i_lock);
899 atomic_dec(&inode->i_count);
900 }
901 trace_f2fs_drop_inode(inode, 0);
902 return 0;
903 }
904 ret = generic_drop_inode(inode);
905 trace_f2fs_drop_inode(inode, ret);
906 return ret;
907 }
908
f2fs_inode_dirtied(struct inode * inode,bool sync)909 int f2fs_inode_dirtied(struct inode *inode, bool sync)
910 {
911 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
912 int ret = 0;
913
914 spin_lock(&sbi->inode_lock[DIRTY_META]);
915 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
916 ret = 1;
917 } else {
918 set_inode_flag(inode, FI_DIRTY_INODE);
919 stat_inc_dirty_inode(sbi, DIRTY_META);
920 }
921 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
922 list_add_tail(&F2FS_I(inode)->gdirty_list,
923 &sbi->inode_list[DIRTY_META]);
924 inc_page_count(sbi, F2FS_DIRTY_IMETA);
925 }
926 spin_unlock(&sbi->inode_lock[DIRTY_META]);
927 return ret;
928 }
929
f2fs_inode_synced(struct inode * inode)930 void f2fs_inode_synced(struct inode *inode)
931 {
932 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
933
934 spin_lock(&sbi->inode_lock[DIRTY_META]);
935 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
936 spin_unlock(&sbi->inode_lock[DIRTY_META]);
937 return;
938 }
939 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
940 list_del_init(&F2FS_I(inode)->gdirty_list);
941 dec_page_count(sbi, F2FS_DIRTY_IMETA);
942 }
943 clear_inode_flag(inode, FI_DIRTY_INODE);
944 clear_inode_flag(inode, FI_AUTO_RECOVER);
945 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
946 spin_unlock(&sbi->inode_lock[DIRTY_META]);
947 }
948
949 /*
950 * f2fs_dirty_inode() is called from __mark_inode_dirty()
951 *
952 * We should call set_dirty_inode to write the dirty inode through write_inode.
953 */
f2fs_dirty_inode(struct inode * inode,int flags)954 static void f2fs_dirty_inode(struct inode *inode, int flags)
955 {
956 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
957
958 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
959 inode->i_ino == F2FS_META_INO(sbi))
960 return;
961
962 if (flags == I_DIRTY_TIME)
963 return;
964
965 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
966 clear_inode_flag(inode, FI_AUTO_RECOVER);
967
968 f2fs_inode_dirtied(inode, false);
969 }
970
f2fs_i_callback(struct rcu_head * head)971 static void f2fs_i_callback(struct rcu_head *head)
972 {
973 struct inode *inode = container_of(head, struct inode, i_rcu);
974 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
975 }
976
f2fs_destroy_inode(struct inode * inode)977 static void f2fs_destroy_inode(struct inode *inode)
978 {
979 call_rcu(&inode->i_rcu, f2fs_i_callback);
980 }
981
destroy_percpu_info(struct f2fs_sb_info * sbi)982 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
983 {
984 percpu_counter_destroy(&sbi->alloc_valid_block_count);
985 percpu_counter_destroy(&sbi->total_valid_inode_count);
986 }
987
destroy_device_list(struct f2fs_sb_info * sbi)988 static void destroy_device_list(struct f2fs_sb_info *sbi)
989 {
990 int i;
991
992 for (i = 0; i < sbi->s_ndevs; i++) {
993 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
994 #ifdef CONFIG_BLK_DEV_ZONED
995 kfree(FDEV(i).blkz_type);
996 #endif
997 }
998 kfree(sbi->devs);
999 }
1000
f2fs_put_super(struct super_block * sb)1001 static void f2fs_put_super(struct super_block *sb)
1002 {
1003 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1004 int i;
1005 bool dropped;
1006
1007 /* unregister procfs/sysfs entries in advance to avoid race case */
1008 f2fs_unregister_sysfs(sbi);
1009
1010 f2fs_quota_off_umount(sb);
1011
1012 /* prevent remaining shrinker jobs */
1013 mutex_lock(&sbi->umount_mutex);
1014
1015 /*
1016 * We don't need to do checkpoint when superblock is clean.
1017 * But, the previous checkpoint was not done by umount, it needs to do
1018 * clean checkpoint again.
1019 */
1020 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1021 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
1022 struct cp_control cpc = {
1023 .reason = CP_UMOUNT,
1024 };
1025 f2fs_write_checkpoint(sbi, &cpc);
1026 }
1027
1028 /* be sure to wait for any on-going discard commands */
1029 dropped = f2fs_wait_discard_bios(sbi);
1030
1031 if ((f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) &&
1032 !sbi->discard_blks && !dropped) {
1033 struct cp_control cpc = {
1034 .reason = CP_UMOUNT | CP_TRIMMED,
1035 };
1036 f2fs_write_checkpoint(sbi, &cpc);
1037 }
1038
1039 /*
1040 * normally superblock is clean, so we need to release this.
1041 * In addition, EIO will skip do checkpoint, we need this as well.
1042 */
1043 f2fs_release_ino_entry(sbi, true);
1044
1045 f2fs_leave_shrinker(sbi);
1046 mutex_unlock(&sbi->umount_mutex);
1047
1048 /* our cp_error case, we can wait for any writeback page */
1049 f2fs_flush_merged_writes(sbi);
1050
1051 f2fs_wait_on_all_pages_writeback(sbi);
1052
1053 f2fs_bug_on(sbi, sbi->fsync_node_num);
1054
1055 iput(sbi->node_inode);
1056 sbi->node_inode = NULL;
1057
1058 iput(sbi->meta_inode);
1059 sbi->meta_inode = NULL;
1060
1061 /*
1062 * iput() can update stat information, if f2fs_write_checkpoint()
1063 * above failed with error.
1064 */
1065 f2fs_destroy_stats(sbi);
1066
1067 /* destroy f2fs internal modules */
1068 f2fs_destroy_node_manager(sbi);
1069 f2fs_destroy_segment_manager(sbi);
1070
1071 kfree(sbi->ckpt);
1072
1073 sb->s_fs_info = NULL;
1074 if (sbi->s_chksum_driver)
1075 crypto_free_shash(sbi->s_chksum_driver);
1076 kfree(sbi->raw_super);
1077
1078 destroy_device_list(sbi);
1079 mempool_destroy(sbi->write_io_dummy);
1080 #ifdef CONFIG_QUOTA
1081 for (i = 0; i < MAXQUOTAS; i++)
1082 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1083 #endif
1084 destroy_percpu_info(sbi);
1085 for (i = 0; i < NR_PAGE_TYPE; i++)
1086 kfree(sbi->write_io[i]);
1087 kfree(sbi);
1088 }
1089
f2fs_sync_fs(struct super_block * sb,int sync)1090 int f2fs_sync_fs(struct super_block *sb, int sync)
1091 {
1092 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1093 int err = 0;
1094
1095 if (unlikely(f2fs_cp_error(sbi)))
1096 return 0;
1097
1098 trace_f2fs_sync_fs(sb, sync);
1099
1100 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1101 return -EAGAIN;
1102
1103 if (sync) {
1104 struct cp_control cpc;
1105
1106 cpc.reason = __get_cp_reason(sbi);
1107
1108 mutex_lock(&sbi->gc_mutex);
1109 err = f2fs_write_checkpoint(sbi, &cpc);
1110 mutex_unlock(&sbi->gc_mutex);
1111 }
1112 f2fs_trace_ios(NULL, 1);
1113
1114 return err;
1115 }
1116
f2fs_freeze(struct super_block * sb)1117 static int f2fs_freeze(struct super_block *sb)
1118 {
1119 if (f2fs_readonly(sb))
1120 return 0;
1121
1122 /* IO error happened before */
1123 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1124 return -EIO;
1125
1126 /* must be clean, since sync_filesystem() was already called */
1127 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1128 return -EINVAL;
1129 return 0;
1130 }
1131
f2fs_unfreeze(struct super_block * sb)1132 static int f2fs_unfreeze(struct super_block *sb)
1133 {
1134 return 0;
1135 }
1136
1137 #ifdef CONFIG_QUOTA
f2fs_statfs_project(struct super_block * sb,kprojid_t projid,struct kstatfs * buf)1138 static int f2fs_statfs_project(struct super_block *sb,
1139 kprojid_t projid, struct kstatfs *buf)
1140 {
1141 struct kqid qid;
1142 struct dquot *dquot;
1143 u64 limit;
1144 u64 curblock;
1145
1146 qid = make_kqid_projid(projid);
1147 dquot = dqget(sb, qid);
1148 if (IS_ERR(dquot))
1149 return PTR_ERR(dquot);
1150 spin_lock(&dquot->dq_dqb_lock);
1151
1152 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
1153 dquot->dq_dqb.dqb_bhardlimit);
1154 if (limit)
1155 limit >>= sb->s_blocksize_bits;
1156
1157 if (limit && buf->f_blocks > limit) {
1158 curblock = (dquot->dq_dqb.dqb_curspace +
1159 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
1160 buf->f_blocks = limit;
1161 buf->f_bfree = buf->f_bavail =
1162 (buf->f_blocks > curblock) ?
1163 (buf->f_blocks - curblock) : 0;
1164 }
1165
1166 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
1167 dquot->dq_dqb.dqb_ihardlimit);
1168
1169 if (limit && buf->f_files > limit) {
1170 buf->f_files = limit;
1171 buf->f_ffree =
1172 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1173 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1174 }
1175
1176 spin_unlock(&dquot->dq_dqb_lock);
1177 dqput(dquot);
1178 return 0;
1179 }
1180 #endif
1181
f2fs_statfs(struct dentry * dentry,struct kstatfs * buf)1182 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1183 {
1184 struct super_block *sb = dentry->d_sb;
1185 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1186 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1187 block_t total_count, user_block_count, start_count;
1188 u64 avail_node_count;
1189
1190 total_count = le64_to_cpu(sbi->raw_super->block_count);
1191 user_block_count = sbi->user_block_count;
1192 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
1193 buf->f_type = F2FS_SUPER_MAGIC;
1194 buf->f_bsize = sbi->blocksize;
1195
1196 buf->f_blocks = total_count - start_count;
1197 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
1198 sbi->current_reserved_blocks;
1199 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1200 buf->f_bavail = buf->f_bfree -
1201 F2FS_OPTION(sbi).root_reserved_blocks;
1202 else
1203 buf->f_bavail = 0;
1204
1205 avail_node_count = sbi->total_node_count - sbi->nquota_files -
1206 F2FS_RESERVED_NODE_NUM;
1207
1208 if (avail_node_count > user_block_count) {
1209 buf->f_files = user_block_count;
1210 buf->f_ffree = buf->f_bavail;
1211 } else {
1212 buf->f_files = avail_node_count;
1213 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
1214 buf->f_bavail);
1215 }
1216
1217 buf->f_namelen = F2FS_NAME_LEN;
1218 buf->f_fsid.val[0] = (u32)id;
1219 buf->f_fsid.val[1] = (u32)(id >> 32);
1220
1221 #ifdef CONFIG_QUOTA
1222 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1223 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1224 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1225 }
1226 #endif
1227 return 0;
1228 }
1229
f2fs_show_quota_options(struct seq_file * seq,struct super_block * sb)1230 static inline void f2fs_show_quota_options(struct seq_file *seq,
1231 struct super_block *sb)
1232 {
1233 #ifdef CONFIG_QUOTA
1234 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1235
1236 if (F2FS_OPTION(sbi).s_jquota_fmt) {
1237 char *fmtname = "";
1238
1239 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
1240 case QFMT_VFS_OLD:
1241 fmtname = "vfsold";
1242 break;
1243 case QFMT_VFS_V0:
1244 fmtname = "vfsv0";
1245 break;
1246 case QFMT_VFS_V1:
1247 fmtname = "vfsv1";
1248 break;
1249 }
1250 seq_printf(seq, ",jqfmt=%s", fmtname);
1251 }
1252
1253 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1254 seq_show_option(seq, "usrjquota",
1255 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
1256
1257 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1258 seq_show_option(seq, "grpjquota",
1259 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
1260
1261 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1262 seq_show_option(seq, "prjjquota",
1263 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
1264 #endif
1265 }
1266
f2fs_show_options(struct seq_file * seq,struct dentry * root)1267 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1268 {
1269 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1270
1271 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
1272 if (test_opt(sbi, FORCE_FG_GC))
1273 seq_printf(seq, ",background_gc=%s", "sync");
1274 else
1275 seq_printf(seq, ",background_gc=%s", "on");
1276 } else {
1277 seq_printf(seq, ",background_gc=%s", "off");
1278 }
1279 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1280 seq_puts(seq, ",disable_roll_forward");
1281 if (test_opt(sbi, DISCARD))
1282 seq_puts(seq, ",discard");
1283 if (test_opt(sbi, NOHEAP))
1284 seq_puts(seq, ",no_heap");
1285 else
1286 seq_puts(seq, ",heap");
1287 #ifdef CONFIG_F2FS_FS_XATTR
1288 if (test_opt(sbi, XATTR_USER))
1289 seq_puts(seq, ",user_xattr");
1290 else
1291 seq_puts(seq, ",nouser_xattr");
1292 if (test_opt(sbi, INLINE_XATTR))
1293 seq_puts(seq, ",inline_xattr");
1294 else
1295 seq_puts(seq, ",noinline_xattr");
1296 if (test_opt(sbi, INLINE_XATTR_SIZE))
1297 seq_printf(seq, ",inline_xattr_size=%u",
1298 F2FS_OPTION(sbi).inline_xattr_size);
1299 #endif
1300 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1301 if (test_opt(sbi, POSIX_ACL))
1302 seq_puts(seq, ",acl");
1303 else
1304 seq_puts(seq, ",noacl");
1305 #endif
1306 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
1307 seq_puts(seq, ",disable_ext_identify");
1308 if (test_opt(sbi, INLINE_DATA))
1309 seq_puts(seq, ",inline_data");
1310 else
1311 seq_puts(seq, ",noinline_data");
1312 if (test_opt(sbi, INLINE_DENTRY))
1313 seq_puts(seq, ",inline_dentry");
1314 else
1315 seq_puts(seq, ",noinline_dentry");
1316 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
1317 seq_puts(seq, ",flush_merge");
1318 if (test_opt(sbi, NOBARRIER))
1319 seq_puts(seq, ",nobarrier");
1320 if (test_opt(sbi, FASTBOOT))
1321 seq_puts(seq, ",fastboot");
1322 if (test_opt(sbi, EXTENT_CACHE))
1323 seq_puts(seq, ",extent_cache");
1324 else
1325 seq_puts(seq, ",noextent_cache");
1326 if (test_opt(sbi, DATA_FLUSH))
1327 seq_puts(seq, ",data_flush");
1328
1329 seq_puts(seq, ",mode=");
1330 if (test_opt(sbi, ADAPTIVE))
1331 seq_puts(seq, "adaptive");
1332 else if (test_opt(sbi, LFS))
1333 seq_puts(seq, "lfs");
1334 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
1335 if (test_opt(sbi, RESERVE_ROOT))
1336 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
1337 F2FS_OPTION(sbi).root_reserved_blocks,
1338 from_kuid_munged(&init_user_ns,
1339 F2FS_OPTION(sbi).s_resuid),
1340 from_kgid_munged(&init_user_ns,
1341 F2FS_OPTION(sbi).s_resgid));
1342 if (F2FS_IO_SIZE_BITS(sbi))
1343 seq_printf(seq, ",io_bits=%u",
1344 F2FS_OPTION(sbi).write_io_size_bits);
1345 #ifdef CONFIG_F2FS_FAULT_INJECTION
1346 if (test_opt(sbi, FAULT_INJECTION)) {
1347 seq_printf(seq, ",fault_injection=%u",
1348 F2FS_OPTION(sbi).fault_info.inject_rate);
1349 seq_printf(seq, ",fault_type=%u",
1350 F2FS_OPTION(sbi).fault_info.inject_type);
1351 }
1352 #endif
1353 #ifdef CONFIG_QUOTA
1354 if (test_opt(sbi, QUOTA))
1355 seq_puts(seq, ",quota");
1356 if (test_opt(sbi, USRQUOTA))
1357 seq_puts(seq, ",usrquota");
1358 if (test_opt(sbi, GRPQUOTA))
1359 seq_puts(seq, ",grpquota");
1360 if (test_opt(sbi, PRJQUOTA))
1361 seq_puts(seq, ",prjquota");
1362 #endif
1363 f2fs_show_quota_options(seq, sbi->sb);
1364 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER)
1365 seq_printf(seq, ",whint_mode=%s", "user-based");
1366 else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS)
1367 seq_printf(seq, ",whint_mode=%s", "fs-based");
1368 #ifdef CONFIG_F2FS_FS_ENCRYPTION
1369 if (F2FS_OPTION(sbi).test_dummy_encryption)
1370 seq_puts(seq, ",test_dummy_encryption");
1371 #endif
1372
1373 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
1374 seq_printf(seq, ",alloc_mode=%s", "default");
1375 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
1376 seq_printf(seq, ",alloc_mode=%s", "reuse");
1377
1378 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
1379 seq_printf(seq, ",fsync_mode=%s", "posix");
1380 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
1381 seq_printf(seq, ",fsync_mode=%s", "strict");
1382 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
1383 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
1384 return 0;
1385 }
1386
default_options(struct f2fs_sb_info * sbi)1387 static void default_options(struct f2fs_sb_info *sbi)
1388 {
1389 /* init some FS parameters */
1390 F2FS_OPTION(sbi).active_logs = NR_CURSEG_TYPE;
1391 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
1392 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1393 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
1394 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
1395 F2FS_OPTION(sbi).test_dummy_encryption = false;
1396 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
1397 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
1398
1399 set_opt(sbi, BG_GC);
1400 set_opt(sbi, INLINE_XATTR);
1401 set_opt(sbi, INLINE_DATA);
1402 set_opt(sbi, INLINE_DENTRY);
1403 set_opt(sbi, EXTENT_CACHE);
1404 set_opt(sbi, NOHEAP);
1405 sbi->sb->s_flags |= SB_LAZYTIME;
1406 set_opt(sbi, FLUSH_MERGE);
1407 set_opt(sbi, DISCARD);
1408 if (f2fs_sb_has_blkzoned(sbi->sb))
1409 set_opt_mode(sbi, F2FS_MOUNT_LFS);
1410 else
1411 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
1412
1413 #ifdef CONFIG_F2FS_FS_XATTR
1414 set_opt(sbi, XATTR_USER);
1415 #endif
1416 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1417 set_opt(sbi, POSIX_ACL);
1418 #endif
1419
1420 f2fs_build_fault_attr(sbi, 0, 0);
1421 }
1422
1423 #ifdef CONFIG_QUOTA
1424 static int f2fs_enable_quotas(struct super_block *sb);
1425 #endif
f2fs_remount(struct super_block * sb,int * flags,char * data)1426 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1427 {
1428 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1429 struct f2fs_mount_info org_mount_opt;
1430 unsigned long old_sb_flags;
1431 int err;
1432 bool need_restart_gc = false;
1433 bool need_stop_gc = false;
1434 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
1435 #ifdef CONFIG_QUOTA
1436 int i, j;
1437 #endif
1438
1439 /*
1440 * Save the old mount options in case we
1441 * need to restore them.
1442 */
1443 org_mount_opt = sbi->mount_opt;
1444 old_sb_flags = sb->s_flags;
1445
1446 #ifdef CONFIG_QUOTA
1447 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
1448 for (i = 0; i < MAXQUOTAS; i++) {
1449 if (F2FS_OPTION(sbi).s_qf_names[i]) {
1450 org_mount_opt.s_qf_names[i] =
1451 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
1452 GFP_KERNEL);
1453 if (!org_mount_opt.s_qf_names[i]) {
1454 for (j = 0; j < i; j++)
1455 kfree(org_mount_opt.s_qf_names[j]);
1456 return -ENOMEM;
1457 }
1458 } else {
1459 org_mount_opt.s_qf_names[i] = NULL;
1460 }
1461 }
1462 #endif
1463
1464 /* recover superblocks we couldn't write due to previous RO mount */
1465 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
1466 err = f2fs_commit_super(sbi, false);
1467 f2fs_msg(sb, KERN_INFO,
1468 "Try to recover all the superblocks, ret: %d", err);
1469 if (!err)
1470 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1471 }
1472
1473 default_options(sbi);
1474
1475 /* parse mount options */
1476 err = parse_options(sb, data);
1477 if (err)
1478 goto restore_opts;
1479
1480 /*
1481 * Previous and new state of filesystem is RO,
1482 * so skip checking GC and FLUSH_MERGE conditions.
1483 */
1484 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
1485 goto skip;
1486
1487 #ifdef CONFIG_QUOTA
1488 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
1489 err = dquot_suspend(sb, -1);
1490 if (err < 0)
1491 goto restore_opts;
1492 } else if (f2fs_readonly(sb) && !(*flags & MS_RDONLY)) {
1493 /* dquot_resume needs RW */
1494 sb->s_flags &= ~SB_RDONLY;
1495 if (sb_any_quota_suspended(sb)) {
1496 dquot_resume(sb, -1);
1497 } else if (f2fs_sb_has_quota_ino(sb)) {
1498 err = f2fs_enable_quotas(sb);
1499 if (err)
1500 goto restore_opts;
1501 }
1502 }
1503 #endif
1504 /* disallow enable/disable extent_cache dynamically */
1505 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
1506 err = -EINVAL;
1507 f2fs_msg(sbi->sb, KERN_WARNING,
1508 "switch extent_cache option is not allowed");
1509 goto restore_opts;
1510 }
1511
1512 /*
1513 * We stop the GC thread if FS is mounted as RO
1514 * or if background_gc = off is passed in mount
1515 * option. Also sync the filesystem.
1516 */
1517 if ((*flags & SB_RDONLY) || !test_opt(sbi, BG_GC)) {
1518 if (sbi->gc_thread) {
1519 f2fs_stop_gc_thread(sbi);
1520 need_restart_gc = true;
1521 }
1522 } else if (!sbi->gc_thread) {
1523 err = f2fs_start_gc_thread(sbi);
1524 if (err)
1525 goto restore_opts;
1526 need_stop_gc = true;
1527 }
1528
1529 if (*flags & SB_RDONLY ||
1530 F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
1531 writeback_inodes_sb(sb, WB_REASON_SYNC);
1532 sync_inodes_sb(sb);
1533
1534 set_sbi_flag(sbi, SBI_IS_DIRTY);
1535 set_sbi_flag(sbi, SBI_IS_CLOSE);
1536 f2fs_sync_fs(sb, 1);
1537 clear_sbi_flag(sbi, SBI_IS_CLOSE);
1538 }
1539
1540 /*
1541 * We stop issue flush thread if FS is mounted as RO
1542 * or if flush_merge is not passed in mount option.
1543 */
1544 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
1545 clear_opt(sbi, FLUSH_MERGE);
1546 f2fs_destroy_flush_cmd_control(sbi, false);
1547 } else {
1548 err = f2fs_create_flush_cmd_control(sbi);
1549 if (err)
1550 goto restore_gc;
1551 }
1552 skip:
1553 #ifdef CONFIG_QUOTA
1554 /* Release old quota file names */
1555 for (i = 0; i < MAXQUOTAS; i++)
1556 kfree(org_mount_opt.s_qf_names[i]);
1557 #endif
1558 /* Update the POSIXACL Flag */
1559 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
1560 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
1561
1562 limit_reserve_root(sbi);
1563 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
1564 return 0;
1565 restore_gc:
1566 if (need_restart_gc) {
1567 if (f2fs_start_gc_thread(sbi))
1568 f2fs_msg(sbi->sb, KERN_WARNING,
1569 "background gc thread has stopped");
1570 } else if (need_stop_gc) {
1571 f2fs_stop_gc_thread(sbi);
1572 }
1573 restore_opts:
1574 #ifdef CONFIG_QUOTA
1575 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
1576 for (i = 0; i < MAXQUOTAS; i++) {
1577 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1578 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
1579 }
1580 #endif
1581 sbi->mount_opt = org_mount_opt;
1582 sb->s_flags = old_sb_flags;
1583 return err;
1584 }
1585
1586 #ifdef CONFIG_QUOTA
1587 /* Read data from quotafile */
f2fs_quota_read(struct super_block * sb,int type,char * data,size_t len,loff_t off)1588 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
1589 size_t len, loff_t off)
1590 {
1591 struct inode *inode = sb_dqopt(sb)->files[type];
1592 struct address_space *mapping = inode->i_mapping;
1593 block_t blkidx = F2FS_BYTES_TO_BLK(off);
1594 int offset = off & (sb->s_blocksize - 1);
1595 int tocopy;
1596 size_t toread;
1597 loff_t i_size = i_size_read(inode);
1598 struct page *page;
1599 char *kaddr;
1600
1601 if (off > i_size)
1602 return 0;
1603
1604 if (off + len > i_size)
1605 len = i_size - off;
1606 toread = len;
1607 while (toread > 0) {
1608 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
1609 repeat:
1610 page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
1611 if (IS_ERR(page)) {
1612 if (PTR_ERR(page) == -ENOMEM) {
1613 congestion_wait(BLK_RW_ASYNC, HZ/50);
1614 goto repeat;
1615 }
1616 return PTR_ERR(page);
1617 }
1618
1619 lock_page(page);
1620
1621 if (unlikely(page->mapping != mapping)) {
1622 f2fs_put_page(page, 1);
1623 goto repeat;
1624 }
1625 if (unlikely(!PageUptodate(page))) {
1626 f2fs_put_page(page, 1);
1627 return -EIO;
1628 }
1629
1630 kaddr = kmap_atomic(page);
1631 memcpy(data, kaddr + offset, tocopy);
1632 kunmap_atomic(kaddr);
1633 f2fs_put_page(page, 1);
1634
1635 offset = 0;
1636 toread -= tocopy;
1637 data += tocopy;
1638 blkidx++;
1639 }
1640 return len;
1641 }
1642
1643 /* Write to quotafile */
f2fs_quota_write(struct super_block * sb,int type,const char * data,size_t len,loff_t off)1644 static ssize_t f2fs_quota_write(struct super_block *sb, int type,
1645 const char *data, size_t len, loff_t off)
1646 {
1647 struct inode *inode = sb_dqopt(sb)->files[type];
1648 struct address_space *mapping = inode->i_mapping;
1649 const struct address_space_operations *a_ops = mapping->a_ops;
1650 int offset = off & (sb->s_blocksize - 1);
1651 size_t towrite = len;
1652 struct page *page;
1653 void *fsdata = NULL;
1654 char *kaddr;
1655 int err = 0;
1656 int tocopy;
1657
1658 while (towrite > 0) {
1659 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
1660 towrite);
1661 retry:
1662 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
1663 &page, &fsdata);
1664 if (unlikely(err)) {
1665 if (err == -ENOMEM) {
1666 congestion_wait(BLK_RW_ASYNC, HZ/50);
1667 goto retry;
1668 }
1669 break;
1670 }
1671
1672 kaddr = kmap_atomic(page);
1673 memcpy(kaddr + offset, data, tocopy);
1674 kunmap_atomic(kaddr);
1675 flush_dcache_page(page);
1676
1677 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
1678 page, fsdata);
1679 offset = 0;
1680 towrite -= tocopy;
1681 off += tocopy;
1682 data += tocopy;
1683 cond_resched();
1684 }
1685
1686 if (len == towrite)
1687 return err;
1688 inode->i_mtime = inode->i_ctime = current_time(inode);
1689 f2fs_mark_inode_dirty_sync(inode, false);
1690 return len - towrite;
1691 }
1692
f2fs_get_dquots(struct inode * inode)1693 static struct dquot **f2fs_get_dquots(struct inode *inode)
1694 {
1695 return F2FS_I(inode)->i_dquot;
1696 }
1697
f2fs_get_reserved_space(struct inode * inode)1698 static qsize_t *f2fs_get_reserved_space(struct inode *inode)
1699 {
1700 return &F2FS_I(inode)->i_reserved_quota;
1701 }
1702
f2fs_quota_on_mount(struct f2fs_sb_info * sbi,int type)1703 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
1704 {
1705 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
1706 F2FS_OPTION(sbi).s_jquota_fmt, type);
1707 }
1708
f2fs_enable_quota_files(struct f2fs_sb_info * sbi,bool rdonly)1709 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
1710 {
1711 int enabled = 0;
1712 int i, err;
1713
1714 if (f2fs_sb_has_quota_ino(sbi->sb) && rdonly) {
1715 err = f2fs_enable_quotas(sbi->sb);
1716 if (err) {
1717 f2fs_msg(sbi->sb, KERN_ERR,
1718 "Cannot turn on quota_ino: %d", err);
1719 return 0;
1720 }
1721 return 1;
1722 }
1723
1724 for (i = 0; i < MAXQUOTAS; i++) {
1725 if (F2FS_OPTION(sbi).s_qf_names[i]) {
1726 err = f2fs_quota_on_mount(sbi, i);
1727 if (!err) {
1728 enabled = 1;
1729 continue;
1730 }
1731 f2fs_msg(sbi->sb, KERN_ERR,
1732 "Cannot turn on quotas: %d on %d", err, i);
1733 }
1734 }
1735 return enabled;
1736 }
1737
f2fs_quota_enable(struct super_block * sb,int type,int format_id,unsigned int flags)1738 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
1739 unsigned int flags)
1740 {
1741 struct inode *qf_inode;
1742 unsigned long qf_inum;
1743 int err;
1744
1745 BUG_ON(!f2fs_sb_has_quota_ino(sb));
1746
1747 qf_inum = f2fs_qf_ino(sb, type);
1748 if (!qf_inum)
1749 return -EPERM;
1750
1751 qf_inode = f2fs_iget(sb, qf_inum);
1752 if (IS_ERR(qf_inode)) {
1753 f2fs_msg(sb, KERN_ERR,
1754 "Bad quota inode %u:%lu", type, qf_inum);
1755 return PTR_ERR(qf_inode);
1756 }
1757
1758 /* Don't account quota for quota files to avoid recursion */
1759 qf_inode->i_flags |= S_NOQUOTA;
1760 err = dquot_enable(qf_inode, type, format_id, flags);
1761 iput(qf_inode);
1762 return err;
1763 }
1764
f2fs_enable_quotas(struct super_block * sb)1765 static int f2fs_enable_quotas(struct super_block *sb)
1766 {
1767 int type, err = 0;
1768 unsigned long qf_inum;
1769 bool quota_mopt[MAXQUOTAS] = {
1770 test_opt(F2FS_SB(sb), USRQUOTA),
1771 test_opt(F2FS_SB(sb), GRPQUOTA),
1772 test_opt(F2FS_SB(sb), PRJQUOTA),
1773 };
1774
1775 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
1776 for (type = 0; type < MAXQUOTAS; type++) {
1777 qf_inum = f2fs_qf_ino(sb, type);
1778 if (qf_inum) {
1779 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
1780 DQUOT_USAGE_ENABLED |
1781 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
1782 if (err) {
1783 f2fs_msg(sb, KERN_ERR,
1784 "Failed to enable quota tracking "
1785 "(type=%d, err=%d). Please run "
1786 "fsck to fix.", type, err);
1787 for (type--; type >= 0; type--)
1788 dquot_quota_off(sb, type);
1789 return err;
1790 }
1791 }
1792 }
1793 return 0;
1794 }
1795
f2fs_quota_sync(struct super_block * sb,int type)1796 static int f2fs_quota_sync(struct super_block *sb, int type)
1797 {
1798 struct quota_info *dqopt = sb_dqopt(sb);
1799 int cnt;
1800 int ret;
1801
1802 ret = dquot_writeback_dquots(sb, type);
1803 if (ret)
1804 return ret;
1805
1806 /*
1807 * Now when everything is written we can discard the pagecache so
1808 * that userspace sees the changes.
1809 */
1810 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1811 if (type != -1 && cnt != type)
1812 continue;
1813 if (!sb_has_quota_active(sb, cnt))
1814 continue;
1815
1816 ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping);
1817 if (ret)
1818 return ret;
1819
1820 inode_lock(dqopt->files[cnt]);
1821 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
1822 inode_unlock(dqopt->files[cnt]);
1823 }
1824 return 0;
1825 }
1826
f2fs_quota_on(struct super_block * sb,int type,int format_id,const struct path * path)1827 static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
1828 const struct path *path)
1829 {
1830 struct inode *inode;
1831 int err;
1832
1833 err = f2fs_quota_sync(sb, type);
1834 if (err)
1835 return err;
1836
1837 err = dquot_quota_on(sb, type, format_id, path);
1838 if (err)
1839 return err;
1840
1841 inode = d_inode(path->dentry);
1842
1843 inode_lock(inode);
1844 F2FS_I(inode)->i_flags |= F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
1845 f2fs_set_inode_flags(inode);
1846 inode_unlock(inode);
1847 f2fs_mark_inode_dirty_sync(inode, false);
1848
1849 return 0;
1850 }
1851
f2fs_quota_off(struct super_block * sb,int type)1852 static int f2fs_quota_off(struct super_block *sb, int type)
1853 {
1854 struct inode *inode = sb_dqopt(sb)->files[type];
1855 int err;
1856
1857 if (!inode || !igrab(inode))
1858 return dquot_quota_off(sb, type);
1859
1860 err = f2fs_quota_sync(sb, type);
1861 if (err)
1862 goto out_put;
1863
1864 err = dquot_quota_off(sb, type);
1865 if (err || f2fs_sb_has_quota_ino(sb))
1866 goto out_put;
1867
1868 inode_lock(inode);
1869 F2FS_I(inode)->i_flags &= ~(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL);
1870 f2fs_set_inode_flags(inode);
1871 inode_unlock(inode);
1872 f2fs_mark_inode_dirty_sync(inode, false);
1873 out_put:
1874 iput(inode);
1875 return err;
1876 }
1877
f2fs_quota_off_umount(struct super_block * sb)1878 void f2fs_quota_off_umount(struct super_block *sb)
1879 {
1880 int type;
1881 int err;
1882
1883 for (type = 0; type < MAXQUOTAS; type++) {
1884 err = f2fs_quota_off(sb, type);
1885 if (err) {
1886 int ret = dquot_quota_off(sb, type);
1887
1888 f2fs_msg(sb, KERN_ERR,
1889 "Fail to turn off disk quota "
1890 "(type: %d, err: %d, ret:%d), Please "
1891 "run fsck to fix it.", type, err, ret);
1892 set_sbi_flag(F2FS_SB(sb), SBI_NEED_FSCK);
1893 }
1894 }
1895 /*
1896 * In case of checkpoint=disable, we must flush quota blocks.
1897 * This can cause NULL exception for node_inode in end_io, since
1898 * put_super already dropped it.
1899 */
1900 sync_filesystem(sb);
1901 }
1902
f2fs_truncate_quota_inode_pages(struct super_block * sb)1903 static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
1904 {
1905 struct quota_info *dqopt = sb_dqopt(sb);
1906 int type;
1907
1908 for (type = 0; type < MAXQUOTAS; type++) {
1909 if (!dqopt->files[type])
1910 continue;
1911 f2fs_inode_synced(dqopt->files[type]);
1912 }
1913 }
1914
1915
f2fs_get_projid(struct inode * inode,kprojid_t * projid)1916 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
1917 {
1918 *projid = F2FS_I(inode)->i_projid;
1919 return 0;
1920 }
1921
1922 static const struct dquot_operations f2fs_quota_operations = {
1923 .get_reserved_space = f2fs_get_reserved_space,
1924 .write_dquot = dquot_commit,
1925 .acquire_dquot = dquot_acquire,
1926 .release_dquot = dquot_release,
1927 .mark_dirty = dquot_mark_dquot_dirty,
1928 .write_info = dquot_commit_info,
1929 .alloc_dquot = dquot_alloc,
1930 .destroy_dquot = dquot_destroy,
1931 .get_projid = f2fs_get_projid,
1932 .get_next_id = dquot_get_next_id,
1933 };
1934
1935 static const struct quotactl_ops f2fs_quotactl_ops = {
1936 .quota_on = f2fs_quota_on,
1937 .quota_off = f2fs_quota_off,
1938 .quota_sync = f2fs_quota_sync,
1939 .get_state = dquot_get_state,
1940 .set_info = dquot_set_dqinfo,
1941 .get_dqblk = dquot_get_dqblk,
1942 .set_dqblk = dquot_set_dqblk,
1943 .get_nextdqblk = dquot_get_next_dqblk,
1944 };
1945 #else
f2fs_quota_off_umount(struct super_block * sb)1946 void f2fs_quota_off_umount(struct super_block *sb)
1947 {
1948 }
1949 #endif
1950
1951 static const struct super_operations f2fs_sops = {
1952 .alloc_inode = f2fs_alloc_inode,
1953 .drop_inode = f2fs_drop_inode,
1954 .destroy_inode = f2fs_destroy_inode,
1955 .write_inode = f2fs_write_inode,
1956 .dirty_inode = f2fs_dirty_inode,
1957 .show_options = f2fs_show_options,
1958 #ifdef CONFIG_QUOTA
1959 .quota_read = f2fs_quota_read,
1960 .quota_write = f2fs_quota_write,
1961 .get_dquots = f2fs_get_dquots,
1962 #endif
1963 .evict_inode = f2fs_evict_inode,
1964 .put_super = f2fs_put_super,
1965 .sync_fs = f2fs_sync_fs,
1966 .freeze_fs = f2fs_freeze,
1967 .unfreeze_fs = f2fs_unfreeze,
1968 .statfs = f2fs_statfs,
1969 .remount_fs = f2fs_remount,
1970 };
1971
1972 #ifdef CONFIG_F2FS_FS_ENCRYPTION
f2fs_get_context(struct inode * inode,void * ctx,size_t len)1973 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
1974 {
1975 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1976 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1977 ctx, len, NULL);
1978 }
1979
f2fs_set_context(struct inode * inode,const void * ctx,size_t len,void * fs_data)1980 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
1981 void *fs_data)
1982 {
1983 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1984
1985 /*
1986 * Encrypting the root directory is not allowed because fsck
1987 * expects lost+found directory to exist and remain unencrypted
1988 * if LOST_FOUND feature is enabled.
1989 *
1990 */
1991 if (f2fs_sb_has_lost_found(sbi->sb) &&
1992 inode->i_ino == F2FS_ROOT_INO(sbi))
1993 return -EPERM;
1994
1995 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1996 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1997 ctx, len, fs_data, XATTR_CREATE);
1998 }
1999
f2fs_dummy_context(struct inode * inode)2000 static bool f2fs_dummy_context(struct inode *inode)
2001 {
2002 return DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(inode));
2003 }
2004
2005 static const struct fscrypt_operations f2fs_cryptops = {
2006 .key_prefix = "f2fs:",
2007 .get_context = f2fs_get_context,
2008 .set_context = f2fs_set_context,
2009 .dummy_context = f2fs_dummy_context,
2010 .empty_dir = f2fs_empty_dir,
2011 .max_namelen = F2FS_NAME_LEN,
2012 };
2013 #endif
2014
f2fs_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)2015 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
2016 u64 ino, u32 generation)
2017 {
2018 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2019 struct inode *inode;
2020
2021 if (f2fs_check_nid_range(sbi, ino))
2022 return ERR_PTR(-ESTALE);
2023
2024 /*
2025 * f2fs_iget isn't quite right if the inode is currently unallocated!
2026 * However f2fs_iget currently does appropriate checks to handle stale
2027 * inodes so everything is OK.
2028 */
2029 inode = f2fs_iget(sb, ino);
2030 if (IS_ERR(inode))
2031 return ERR_CAST(inode);
2032 if (unlikely(generation && inode->i_generation != generation)) {
2033 /* we didn't find the right inode.. */
2034 iput(inode);
2035 return ERR_PTR(-ESTALE);
2036 }
2037 return inode;
2038 }
2039
f2fs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)2040 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
2041 int fh_len, int fh_type)
2042 {
2043 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2044 f2fs_nfs_get_inode);
2045 }
2046
f2fs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)2047 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
2048 int fh_len, int fh_type)
2049 {
2050 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2051 f2fs_nfs_get_inode);
2052 }
2053
2054 static const struct export_operations f2fs_export_ops = {
2055 .fh_to_dentry = f2fs_fh_to_dentry,
2056 .fh_to_parent = f2fs_fh_to_parent,
2057 .get_parent = f2fs_get_parent,
2058 };
2059
max_file_blocks(void)2060 static loff_t max_file_blocks(void)
2061 {
2062 loff_t result = 0;
2063 loff_t leaf_count = ADDRS_PER_BLOCK;
2064
2065 /*
2066 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
2067 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
2068 * space in inode.i_addr, it will be more safe to reassign
2069 * result as zero.
2070 */
2071
2072 /* two direct node blocks */
2073 result += (leaf_count * 2);
2074
2075 /* two indirect node blocks */
2076 leaf_count *= NIDS_PER_BLOCK;
2077 result += (leaf_count * 2);
2078
2079 /* one double indirect node block */
2080 leaf_count *= NIDS_PER_BLOCK;
2081 result += leaf_count;
2082
2083 return result;
2084 }
2085
__f2fs_commit_super(struct buffer_head * bh,struct f2fs_super_block * super)2086 static int __f2fs_commit_super(struct buffer_head *bh,
2087 struct f2fs_super_block *super)
2088 {
2089 lock_buffer(bh);
2090 if (super)
2091 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
2092 set_buffer_dirty(bh);
2093 unlock_buffer(bh);
2094
2095 /* it's rare case, we can do fua all the time */
2096 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
2097 }
2098
sanity_check_area_boundary(struct f2fs_sb_info * sbi,struct buffer_head * bh)2099 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
2100 struct buffer_head *bh)
2101 {
2102 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2103 (bh->b_data + F2FS_SUPER_OFFSET);
2104 struct super_block *sb = sbi->sb;
2105 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2106 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
2107 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
2108 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
2109 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2110 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2111 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
2112 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
2113 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
2114 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
2115 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2116 u32 segment_count = le32_to_cpu(raw_super->segment_count);
2117 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2118 u64 main_end_blkaddr = main_blkaddr +
2119 (segment_count_main << log_blocks_per_seg);
2120 u64 seg_end_blkaddr = segment0_blkaddr +
2121 (segment_count << log_blocks_per_seg);
2122
2123 if (segment0_blkaddr != cp_blkaddr) {
2124 f2fs_msg(sb, KERN_INFO,
2125 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
2126 segment0_blkaddr, cp_blkaddr);
2127 return true;
2128 }
2129
2130 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
2131 sit_blkaddr) {
2132 f2fs_msg(sb, KERN_INFO,
2133 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
2134 cp_blkaddr, sit_blkaddr,
2135 segment_count_ckpt << log_blocks_per_seg);
2136 return true;
2137 }
2138
2139 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
2140 nat_blkaddr) {
2141 f2fs_msg(sb, KERN_INFO,
2142 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
2143 sit_blkaddr, nat_blkaddr,
2144 segment_count_sit << log_blocks_per_seg);
2145 return true;
2146 }
2147
2148 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
2149 ssa_blkaddr) {
2150 f2fs_msg(sb, KERN_INFO,
2151 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
2152 nat_blkaddr, ssa_blkaddr,
2153 segment_count_nat << log_blocks_per_seg);
2154 return true;
2155 }
2156
2157 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
2158 main_blkaddr) {
2159 f2fs_msg(sb, KERN_INFO,
2160 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
2161 ssa_blkaddr, main_blkaddr,
2162 segment_count_ssa << log_blocks_per_seg);
2163 return true;
2164 }
2165
2166 if (main_end_blkaddr > seg_end_blkaddr) {
2167 f2fs_msg(sb, KERN_INFO,
2168 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
2169 main_blkaddr,
2170 segment0_blkaddr +
2171 (segment_count << log_blocks_per_seg),
2172 segment_count_main << log_blocks_per_seg);
2173 return true;
2174 } else if (main_end_blkaddr < seg_end_blkaddr) {
2175 int err = 0;
2176 char *res;
2177
2178 /* fix in-memory information all the time */
2179 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
2180 segment0_blkaddr) >> log_blocks_per_seg);
2181
2182 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
2183 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2184 res = "internally";
2185 } else {
2186 err = __f2fs_commit_super(bh, NULL);
2187 res = err ? "failed" : "done";
2188 }
2189 f2fs_msg(sb, KERN_INFO,
2190 "Fix alignment : %s, start(%u) end(%u) block(%u)",
2191 res, main_blkaddr,
2192 segment0_blkaddr +
2193 (segment_count << log_blocks_per_seg),
2194 segment_count_main << log_blocks_per_seg);
2195 if (err)
2196 return true;
2197 }
2198 return false;
2199 }
2200
sanity_check_raw_super(struct f2fs_sb_info * sbi,struct buffer_head * bh)2201 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
2202 struct buffer_head *bh)
2203 {
2204 block_t segment_count, segs_per_sec, secs_per_zone;
2205 block_t total_sections, blocks_per_seg;
2206 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2207 (bh->b_data + F2FS_SUPER_OFFSET);
2208 struct super_block *sb = sbi->sb;
2209 unsigned int blocksize;
2210
2211 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
2212 f2fs_msg(sb, KERN_INFO,
2213 "Magic Mismatch, valid(0x%x) - read(0x%x)",
2214 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
2215 return -EINVAL;
2216 }
2217
2218 /* Currently, support only 4KB page cache size */
2219 if (F2FS_BLKSIZE != PAGE_SIZE) {
2220 f2fs_msg(sb, KERN_INFO,
2221 "Invalid page_cache_size (%lu), supports only 4KB\n",
2222 PAGE_SIZE);
2223 return -EFSCORRUPTED;
2224 }
2225
2226 /* Currently, support only 4KB block size */
2227 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
2228 if (blocksize != F2FS_BLKSIZE) {
2229 f2fs_msg(sb, KERN_INFO,
2230 "Invalid blocksize (%u), supports only 4KB\n",
2231 blocksize);
2232 return -EFSCORRUPTED;
2233 }
2234
2235 /* check log blocks per segment */
2236 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
2237 f2fs_msg(sb, KERN_INFO,
2238 "Invalid log blocks per segment (%u)\n",
2239 le32_to_cpu(raw_super->log_blocks_per_seg));
2240 return -EFSCORRUPTED;
2241 }
2242
2243 /* Currently, support 512/1024/2048/4096 bytes sector size */
2244 if (le32_to_cpu(raw_super->log_sectorsize) >
2245 F2FS_MAX_LOG_SECTOR_SIZE ||
2246 le32_to_cpu(raw_super->log_sectorsize) <
2247 F2FS_MIN_LOG_SECTOR_SIZE) {
2248 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
2249 le32_to_cpu(raw_super->log_sectorsize));
2250 return -EFSCORRUPTED;
2251 }
2252 if (le32_to_cpu(raw_super->log_sectors_per_block) +
2253 le32_to_cpu(raw_super->log_sectorsize) !=
2254 F2FS_MAX_LOG_SECTOR_SIZE) {
2255 f2fs_msg(sb, KERN_INFO,
2256 "Invalid log sectors per block(%u) log sectorsize(%u)",
2257 le32_to_cpu(raw_super->log_sectors_per_block),
2258 le32_to_cpu(raw_super->log_sectorsize));
2259 return -EFSCORRUPTED;
2260 }
2261
2262 segment_count = le32_to_cpu(raw_super->segment_count);
2263 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2264 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2265 total_sections = le32_to_cpu(raw_super->section_count);
2266
2267 /* blocks_per_seg should be 512, given the above check */
2268 blocks_per_seg = 1 << le32_to_cpu(raw_super->log_blocks_per_seg);
2269
2270 if (segment_count > F2FS_MAX_SEGMENT ||
2271 segment_count < F2FS_MIN_SEGMENTS) {
2272 f2fs_msg(sb, KERN_INFO,
2273 "Invalid segment count (%u)",
2274 segment_count);
2275 return -EFSCORRUPTED;
2276 }
2277
2278 if (total_sections > segment_count ||
2279 total_sections < F2FS_MIN_SEGMENTS ||
2280 segs_per_sec > segment_count || !segs_per_sec) {
2281 f2fs_msg(sb, KERN_INFO,
2282 "Invalid segment/section count (%u, %u x %u)",
2283 segment_count, total_sections, segs_per_sec);
2284 return -EFSCORRUPTED;
2285 }
2286
2287 if ((segment_count / segs_per_sec) < total_sections) {
2288 f2fs_msg(sb, KERN_INFO,
2289 "Small segment_count (%u < %u * %u)",
2290 segment_count, segs_per_sec, total_sections);
2291 return -EFSCORRUPTED;
2292 }
2293
2294 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
2295 f2fs_msg(sb, KERN_INFO,
2296 "Wrong segment_count / block_count (%u > %llu)",
2297 segment_count, le64_to_cpu(raw_super->block_count));
2298 return -EFSCORRUPTED;
2299 }
2300
2301 if (secs_per_zone > total_sections || !secs_per_zone) {
2302 f2fs_msg(sb, KERN_INFO,
2303 "Wrong secs_per_zone / total_sections (%u, %u)",
2304 secs_per_zone, total_sections);
2305 return -EFSCORRUPTED;
2306 }
2307 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
2308 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
2309 (le32_to_cpu(raw_super->extension_count) +
2310 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
2311 f2fs_msg(sb, KERN_INFO,
2312 "Corrupted extension count (%u + %u > %u)",
2313 le32_to_cpu(raw_super->extension_count),
2314 raw_super->hot_ext_count,
2315 F2FS_MAX_EXTENSION);
2316 return -EFSCORRUPTED;
2317 }
2318
2319 if (le32_to_cpu(raw_super->cp_payload) >
2320 (blocks_per_seg - F2FS_CP_PACKS)) {
2321 f2fs_msg(sb, KERN_INFO,
2322 "Insane cp_payload (%u > %u)",
2323 le32_to_cpu(raw_super->cp_payload),
2324 blocks_per_seg - F2FS_CP_PACKS);
2325 return -EFSCORRUPTED;
2326 }
2327
2328 /* check reserved ino info */
2329 if (le32_to_cpu(raw_super->node_ino) != 1 ||
2330 le32_to_cpu(raw_super->meta_ino) != 2 ||
2331 le32_to_cpu(raw_super->root_ino) != 3) {
2332 f2fs_msg(sb, KERN_INFO,
2333 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
2334 le32_to_cpu(raw_super->node_ino),
2335 le32_to_cpu(raw_super->meta_ino),
2336 le32_to_cpu(raw_super->root_ino));
2337 return -EFSCORRUPTED;
2338 }
2339
2340 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
2341 if (sanity_check_area_boundary(sbi, bh))
2342 return -EFSCORRUPTED;
2343
2344 return 0;
2345 }
2346
f2fs_sanity_check_ckpt(struct f2fs_sb_info * sbi)2347 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
2348 {
2349 unsigned int total, fsmeta;
2350 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2351 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2352 unsigned int ovp_segments, reserved_segments;
2353 unsigned int main_segs, blocks_per_seg;
2354 unsigned int sit_segs, nat_segs;
2355 unsigned int sit_bitmap_size, nat_bitmap_size;
2356 unsigned int log_blocks_per_seg;
2357 unsigned int segment_count_main;
2358 unsigned int cp_pack_start_sum, cp_payload;
2359 block_t user_block_count;
2360 int i, j;
2361
2362 total = le32_to_cpu(raw_super->segment_count);
2363 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
2364 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
2365 fsmeta += sit_segs;
2366 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
2367 fsmeta += nat_segs;
2368 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
2369 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
2370
2371 if (unlikely(fsmeta >= total))
2372 return 1;
2373
2374 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2375 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2376
2377 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
2378 ovp_segments == 0 || reserved_segments == 0)) {
2379 f2fs_msg(sbi->sb, KERN_ERR,
2380 "Wrong layout: check mkfs.f2fs version");
2381 return 1;
2382 }
2383
2384 user_block_count = le64_to_cpu(ckpt->user_block_count);
2385 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2386 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2387 if (!user_block_count || user_block_count >=
2388 segment_count_main << log_blocks_per_seg) {
2389 f2fs_msg(sbi->sb, KERN_ERR,
2390 "Wrong user_block_count: %u", user_block_count);
2391 return 1;
2392 }
2393
2394 main_segs = le32_to_cpu(raw_super->segment_count_main);
2395 blocks_per_seg = sbi->blocks_per_seg;
2396
2397 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2398 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
2399 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
2400 return 1;
2401 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
2402 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
2403 le32_to_cpu(ckpt->cur_node_segno[j])) {
2404 f2fs_msg(sbi->sb, KERN_ERR,
2405 "Node segment (%u, %u) has the same "
2406 "segno: %u", i, j,
2407 le32_to_cpu(ckpt->cur_node_segno[i]));
2408 return 1;
2409 }
2410 }
2411 }
2412 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
2413 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
2414 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
2415 return 1;
2416 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
2417 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
2418 le32_to_cpu(ckpt->cur_data_segno[j])) {
2419 f2fs_msg(sbi->sb, KERN_ERR,
2420 "Data segment (%u, %u) has the same "
2421 "segno: %u", i, j,
2422 le32_to_cpu(ckpt->cur_data_segno[i]));
2423 return 1;
2424 }
2425 }
2426 }
2427 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2428 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
2429 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
2430 le32_to_cpu(ckpt->cur_data_segno[j])) {
2431 f2fs_msg(sbi->sb, KERN_ERR,
2432 "Node segment (%u) and Data segment (%u)"
2433 " has the same segno: %u", i, j,
2434 le32_to_cpu(ckpt->cur_node_segno[i]));
2435 return 1;
2436 }
2437 }
2438 }
2439
2440 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
2441 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
2442
2443 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
2444 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
2445 f2fs_msg(sbi->sb, KERN_ERR,
2446 "Wrong bitmap size: sit: %u, nat:%u",
2447 sit_bitmap_size, nat_bitmap_size);
2448 return 1;
2449 }
2450
2451 cp_pack_start_sum = __start_sum_addr(sbi);
2452 cp_payload = __cp_payload(sbi);
2453 if (cp_pack_start_sum < cp_payload + 1 ||
2454 cp_pack_start_sum > blocks_per_seg - 1 -
2455 NR_CURSEG_TYPE) {
2456 f2fs_msg(sbi->sb, KERN_ERR,
2457 "Wrong cp_pack_start_sum: %u",
2458 cp_pack_start_sum);
2459 return 1;
2460 }
2461
2462 if (unlikely(f2fs_cp_error(sbi))) {
2463 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
2464 return 1;
2465 }
2466 return 0;
2467 }
2468
init_sb_info(struct f2fs_sb_info * sbi)2469 static void init_sb_info(struct f2fs_sb_info *sbi)
2470 {
2471 struct f2fs_super_block *raw_super = sbi->raw_super;
2472 int i, j;
2473
2474 sbi->log_sectors_per_block =
2475 le32_to_cpu(raw_super->log_sectors_per_block);
2476 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
2477 sbi->blocksize = 1 << sbi->log_blocksize;
2478 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2479 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
2480 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2481 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2482 sbi->total_sections = le32_to_cpu(raw_super->section_count);
2483 sbi->total_node_count =
2484 (le32_to_cpu(raw_super->segment_count_nat) / 2)
2485 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
2486 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
2487 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
2488 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
2489 sbi->cur_victim_sec = NULL_SECNO;
2490 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
2491
2492 sbi->dir_level = DEF_DIR_LEVEL;
2493 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
2494 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
2495 clear_sbi_flag(sbi, SBI_NEED_FSCK);
2496
2497 for (i = 0; i < NR_COUNT_TYPE; i++)
2498 atomic_set(&sbi->nr_pages[i], 0);
2499
2500 for (i = 0; i < META; i++)
2501 atomic_set(&sbi->wb_sync_req[i], 0);
2502
2503 INIT_LIST_HEAD(&sbi->s_list);
2504 mutex_init(&sbi->umount_mutex);
2505 for (i = 0; i < NR_PAGE_TYPE - 1; i++)
2506 for (j = HOT; j < NR_TEMP_TYPE; j++)
2507 mutex_init(&sbi->wio_mutex[i][j]);
2508 init_rwsem(&sbi->io_order_lock);
2509 spin_lock_init(&sbi->cp_lock);
2510
2511 sbi->dirty_device = 0;
2512 spin_lock_init(&sbi->dev_lock);
2513
2514 init_rwsem(&sbi->sb_lock);
2515 }
2516
init_percpu_info(struct f2fs_sb_info * sbi)2517 static int init_percpu_info(struct f2fs_sb_info *sbi)
2518 {
2519 int err;
2520
2521 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
2522 if (err)
2523 return err;
2524
2525 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
2526 GFP_KERNEL);
2527 if (err)
2528 percpu_counter_destroy(&sbi->alloc_valid_block_count);
2529
2530 return err;
2531 }
2532
2533 #ifdef CONFIG_BLK_DEV_ZONED
init_blkz_info(struct f2fs_sb_info * sbi,int devi)2534 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
2535 {
2536 struct block_device *bdev = FDEV(devi).bdev;
2537 sector_t nr_sectors = bdev->bd_part->nr_sects;
2538 sector_t sector = 0;
2539 struct blk_zone *zones;
2540 unsigned int i, nr_zones;
2541 unsigned int n = 0;
2542 int err = -EIO;
2543
2544 if (!f2fs_sb_has_blkzoned(sbi->sb))
2545 return 0;
2546
2547 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
2548 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
2549 return -EINVAL;
2550 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
2551 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
2552 __ilog2_u32(sbi->blocks_per_blkz))
2553 return -EINVAL;
2554 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
2555 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
2556 sbi->log_blocks_per_blkz;
2557 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
2558 FDEV(devi).nr_blkz++;
2559
2560 FDEV(devi).blkz_type = f2fs_kmalloc(sbi, FDEV(devi).nr_blkz,
2561 GFP_KERNEL);
2562 if (!FDEV(devi).blkz_type)
2563 return -ENOMEM;
2564
2565 #define F2FS_REPORT_NR_ZONES 4096
2566
2567 zones = f2fs_kzalloc(sbi,
2568 array_size(F2FS_REPORT_NR_ZONES,
2569 sizeof(struct blk_zone)),
2570 GFP_KERNEL);
2571 if (!zones)
2572 return -ENOMEM;
2573
2574 /* Get block zones type */
2575 while (zones && sector < nr_sectors) {
2576
2577 nr_zones = F2FS_REPORT_NR_ZONES;
2578 err = blkdev_report_zones(bdev, sector,
2579 zones, &nr_zones,
2580 GFP_KERNEL);
2581 if (err)
2582 break;
2583 if (!nr_zones) {
2584 err = -EIO;
2585 break;
2586 }
2587
2588 for (i = 0; i < nr_zones; i++) {
2589 FDEV(devi).blkz_type[n] = zones[i].type;
2590 sector += zones[i].len;
2591 n++;
2592 }
2593 }
2594
2595 kfree(zones);
2596
2597 return err;
2598 }
2599 #endif
2600
2601 /*
2602 * Read f2fs raw super block.
2603 * Because we have two copies of super block, so read both of them
2604 * to get the first valid one. If any one of them is broken, we pass
2605 * them recovery flag back to the caller.
2606 */
read_raw_super_block(struct f2fs_sb_info * sbi,struct f2fs_super_block ** raw_super,int * valid_super_block,int * recovery)2607 static int read_raw_super_block(struct f2fs_sb_info *sbi,
2608 struct f2fs_super_block **raw_super,
2609 int *valid_super_block, int *recovery)
2610 {
2611 struct super_block *sb = sbi->sb;
2612 int block;
2613 struct buffer_head *bh;
2614 struct f2fs_super_block *super;
2615 int err = 0;
2616
2617 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
2618 if (!super)
2619 return -ENOMEM;
2620
2621 for (block = 0; block < 2; block++) {
2622 bh = sb_bread(sb, block);
2623 if (!bh) {
2624 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
2625 block + 1);
2626 err = -EIO;
2627 continue;
2628 }
2629
2630 /* sanity checking of raw super */
2631 err = sanity_check_raw_super(sbi, bh);
2632 if (err) {
2633 f2fs_msg(sb, KERN_ERR,
2634 "Can't find valid F2FS filesystem in %dth superblock",
2635 block + 1);
2636 brelse(bh);
2637 continue;
2638 }
2639
2640 if (!*raw_super) {
2641 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
2642 sizeof(*super));
2643 *valid_super_block = block;
2644 *raw_super = super;
2645 }
2646 brelse(bh);
2647 }
2648
2649 /* Fail to read any one of the superblocks*/
2650 if (err < 0)
2651 *recovery = 1;
2652
2653 /* No valid superblock */
2654 if (!*raw_super)
2655 kfree(super);
2656 else
2657 err = 0;
2658
2659 return err;
2660 }
2661
f2fs_commit_super(struct f2fs_sb_info * sbi,bool recover)2662 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
2663 {
2664 struct buffer_head *bh;
2665 int err;
2666
2667 if ((recover && f2fs_readonly(sbi->sb)) ||
2668 bdev_read_only(sbi->sb->s_bdev)) {
2669 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2670 return -EROFS;
2671 }
2672
2673 /* write back-up superblock first */
2674 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
2675 if (!bh)
2676 return -EIO;
2677 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
2678 brelse(bh);
2679
2680 /* if we are in recovery path, skip writing valid superblock */
2681 if (recover || err)
2682 return err;
2683
2684 /* write current valid superblock */
2685 bh = sb_bread(sbi->sb, sbi->valid_super_block);
2686 if (!bh)
2687 return -EIO;
2688 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
2689 brelse(bh);
2690 return err;
2691 }
2692
f2fs_scan_devices(struct f2fs_sb_info * sbi)2693 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
2694 {
2695 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2696 unsigned int max_devices = MAX_DEVICES;
2697 int i;
2698
2699 /* Initialize single device information */
2700 if (!RDEV(0).path[0]) {
2701 if (!bdev_is_zoned(sbi->sb->s_bdev))
2702 return 0;
2703 max_devices = 1;
2704 }
2705
2706 /*
2707 * Initialize multiple devices information, or single
2708 * zoned block device information.
2709 */
2710 sbi->devs = f2fs_kzalloc(sbi,
2711 array_size(max_devices,
2712 sizeof(struct f2fs_dev_info)),
2713 GFP_KERNEL);
2714 if (!sbi->devs)
2715 return -ENOMEM;
2716
2717 for (i = 0; i < max_devices; i++) {
2718
2719 if (i > 0 && !RDEV(i).path[0])
2720 break;
2721
2722 if (max_devices == 1) {
2723 /* Single zoned block device mount */
2724 FDEV(0).bdev =
2725 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
2726 sbi->sb->s_mode, sbi->sb->s_type);
2727 } else {
2728 /* Multi-device mount */
2729 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
2730 FDEV(i).total_segments =
2731 le32_to_cpu(RDEV(i).total_segments);
2732 if (i == 0) {
2733 FDEV(i).start_blk = 0;
2734 FDEV(i).end_blk = FDEV(i).start_blk +
2735 (FDEV(i).total_segments <<
2736 sbi->log_blocks_per_seg) - 1 +
2737 le32_to_cpu(raw_super->segment0_blkaddr);
2738 } else {
2739 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
2740 FDEV(i).end_blk = FDEV(i).start_blk +
2741 (FDEV(i).total_segments <<
2742 sbi->log_blocks_per_seg) - 1;
2743 }
2744 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
2745 sbi->sb->s_mode, sbi->sb->s_type);
2746 }
2747 if (IS_ERR(FDEV(i).bdev))
2748 return PTR_ERR(FDEV(i).bdev);
2749
2750 /* to release errored devices */
2751 sbi->s_ndevs = i + 1;
2752
2753 #ifdef CONFIG_BLK_DEV_ZONED
2754 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
2755 !f2fs_sb_has_blkzoned(sbi->sb)) {
2756 f2fs_msg(sbi->sb, KERN_ERR,
2757 "Zoned block device feature not enabled\n");
2758 return -EINVAL;
2759 }
2760 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
2761 if (init_blkz_info(sbi, i)) {
2762 f2fs_msg(sbi->sb, KERN_ERR,
2763 "Failed to initialize F2FS blkzone information");
2764 return -EINVAL;
2765 }
2766 if (max_devices == 1)
2767 break;
2768 f2fs_msg(sbi->sb, KERN_INFO,
2769 "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
2770 i, FDEV(i).path,
2771 FDEV(i).total_segments,
2772 FDEV(i).start_blk, FDEV(i).end_blk,
2773 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
2774 "Host-aware" : "Host-managed");
2775 continue;
2776 }
2777 #endif
2778 f2fs_msg(sbi->sb, KERN_INFO,
2779 "Mount Device [%2d]: %20s, %8u, %8x - %8x",
2780 i, FDEV(i).path,
2781 FDEV(i).total_segments,
2782 FDEV(i).start_blk, FDEV(i).end_blk);
2783 }
2784 f2fs_msg(sbi->sb, KERN_INFO,
2785 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
2786 return 0;
2787 }
2788
f2fs_tuning_parameters(struct f2fs_sb_info * sbi)2789 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
2790 {
2791 struct f2fs_sm_info *sm_i = SM_I(sbi);
2792
2793 /* adjust parameters according to the volume size */
2794 if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
2795 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
2796 sm_i->dcc_info->discard_granularity = 1;
2797 sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
2798 }
2799
2800 sbi->readdir_ra = 1;
2801 }
2802
f2fs_fill_super(struct super_block * sb,void * data,int silent)2803 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
2804 {
2805 struct f2fs_sb_info *sbi;
2806 struct f2fs_super_block *raw_super;
2807 struct inode *root;
2808 int err;
2809 bool retry = true, need_fsck = false;
2810 char *options = NULL;
2811 int recovery, i, valid_super_block;
2812 struct curseg_info *seg_i;
2813
2814 try_onemore:
2815 err = -EINVAL;
2816 raw_super = NULL;
2817 valid_super_block = -1;
2818 recovery = 0;
2819
2820 /* allocate memory for f2fs-specific super block info */
2821 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
2822 if (!sbi)
2823 return -ENOMEM;
2824
2825 sbi->sb = sb;
2826
2827 /* Load the checksum driver */
2828 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
2829 if (IS_ERR(sbi->s_chksum_driver)) {
2830 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
2831 err = PTR_ERR(sbi->s_chksum_driver);
2832 sbi->s_chksum_driver = NULL;
2833 goto free_sbi;
2834 }
2835
2836 /* set a block size */
2837 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
2838 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
2839 goto free_sbi;
2840 }
2841
2842 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
2843 &recovery);
2844 if (err)
2845 goto free_sbi;
2846
2847 sb->s_fs_info = sbi;
2848 sbi->raw_super = raw_super;
2849
2850 /* precompute checksum seed for metadata */
2851 if (f2fs_sb_has_inode_chksum(sb))
2852 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
2853 sizeof(raw_super->uuid));
2854
2855 /*
2856 * The BLKZONED feature indicates that the drive was formatted with
2857 * zone alignment optimization. This is optional for host-aware
2858 * devices, but mandatory for host-managed zoned block devices.
2859 */
2860 #ifndef CONFIG_BLK_DEV_ZONED
2861 if (f2fs_sb_has_blkzoned(sb)) {
2862 f2fs_msg(sb, KERN_ERR,
2863 "Zoned block device support is not enabled\n");
2864 err = -EOPNOTSUPP;
2865 goto free_sb_buf;
2866 }
2867 #endif
2868 default_options(sbi);
2869 /* parse mount options */
2870 options = kstrdup((const char *)data, GFP_KERNEL);
2871 if (data && !options) {
2872 err = -ENOMEM;
2873 goto free_sb_buf;
2874 }
2875
2876 err = parse_options(sb, options);
2877 if (err)
2878 goto free_options;
2879
2880 sbi->max_file_blocks = max_file_blocks();
2881 sb->s_maxbytes = sbi->max_file_blocks <<
2882 le32_to_cpu(raw_super->log_blocksize);
2883 sb->s_max_links = F2FS_LINK_MAX;
2884 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2885
2886 #ifdef CONFIG_QUOTA
2887 sb->dq_op = &f2fs_quota_operations;
2888 if (f2fs_sb_has_quota_ino(sb))
2889 sb->s_qcop = &dquot_quotactl_sysfile_ops;
2890 else
2891 sb->s_qcop = &f2fs_quotactl_ops;
2892 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
2893
2894 if (f2fs_sb_has_quota_ino(sbi->sb)) {
2895 for (i = 0; i < MAXQUOTAS; i++) {
2896 if (f2fs_qf_ino(sbi->sb, i))
2897 sbi->nquota_files++;
2898 }
2899 }
2900 #endif
2901
2902 sb->s_op = &f2fs_sops;
2903 #ifdef CONFIG_F2FS_FS_ENCRYPTION
2904 sb->s_cop = &f2fs_cryptops;
2905 #endif
2906 sb->s_xattr = f2fs_xattr_handlers;
2907 sb->s_export_op = &f2fs_export_ops;
2908 sb->s_magic = F2FS_SUPER_MAGIC;
2909 sb->s_time_gran = 1;
2910 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2911 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
2912 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
2913 sb->s_iflags |= SB_I_CGROUPWB;
2914
2915 /* init f2fs-specific super block info */
2916 sbi->valid_super_block = valid_super_block;
2917 mutex_init(&sbi->gc_mutex);
2918 mutex_init(&sbi->writepages);
2919 mutex_init(&sbi->cp_mutex);
2920 init_rwsem(&sbi->node_write);
2921 init_rwsem(&sbi->node_change);
2922
2923 /* disallow all the data/node/meta page writes */
2924 set_sbi_flag(sbi, SBI_POR_DOING);
2925 spin_lock_init(&sbi->stat_lock);
2926
2927 /* init iostat info */
2928 spin_lock_init(&sbi->iostat_lock);
2929 sbi->iostat_enable = false;
2930
2931 for (i = 0; i < NR_PAGE_TYPE; i++) {
2932 int n = (i == META) ? 1: NR_TEMP_TYPE;
2933 int j;
2934
2935 sbi->write_io[i] =
2936 f2fs_kmalloc(sbi,
2937 array_size(n,
2938 sizeof(struct f2fs_bio_info)),
2939 GFP_KERNEL);
2940 if (!sbi->write_io[i]) {
2941 err = -ENOMEM;
2942 goto free_bio_info;
2943 }
2944
2945 for (j = HOT; j < n; j++) {
2946 init_rwsem(&sbi->write_io[i][j].io_rwsem);
2947 sbi->write_io[i][j].sbi = sbi;
2948 sbi->write_io[i][j].bio = NULL;
2949 spin_lock_init(&sbi->write_io[i][j].io_lock);
2950 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
2951 }
2952 }
2953
2954 init_rwsem(&sbi->cp_rwsem);
2955 init_waitqueue_head(&sbi->cp_wait);
2956 init_sb_info(sbi);
2957
2958 err = init_percpu_info(sbi);
2959 if (err)
2960 goto free_bio_info;
2961
2962 if (F2FS_IO_SIZE(sbi) > 1) {
2963 sbi->write_io_dummy =
2964 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
2965 if (!sbi->write_io_dummy) {
2966 err = -ENOMEM;
2967 goto free_percpu;
2968 }
2969 }
2970
2971 /* get an inode for meta space */
2972 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
2973 if (IS_ERR(sbi->meta_inode)) {
2974 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
2975 err = PTR_ERR(sbi->meta_inode);
2976 goto free_io_dummy;
2977 }
2978
2979 err = f2fs_get_valid_checkpoint(sbi);
2980 if (err) {
2981 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
2982 goto free_meta_inode;
2983 }
2984
2985 /* Initialize device list */
2986 err = f2fs_scan_devices(sbi);
2987 if (err) {
2988 f2fs_msg(sb, KERN_ERR, "Failed to find devices");
2989 goto free_devices;
2990 }
2991
2992 sbi->total_valid_node_count =
2993 le32_to_cpu(sbi->ckpt->valid_node_count);
2994 percpu_counter_set(&sbi->total_valid_inode_count,
2995 le32_to_cpu(sbi->ckpt->valid_inode_count));
2996 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
2997 sbi->total_valid_block_count =
2998 le64_to_cpu(sbi->ckpt->valid_block_count);
2999 sbi->last_valid_block_count = sbi->total_valid_block_count;
3000 sbi->reserved_blocks = 0;
3001 sbi->current_reserved_blocks = 0;
3002 limit_reserve_root(sbi);
3003
3004 for (i = 0; i < NR_INODE_TYPE; i++) {
3005 INIT_LIST_HEAD(&sbi->inode_list[i]);
3006 spin_lock_init(&sbi->inode_lock[i]);
3007 }
3008
3009 f2fs_init_extent_cache_info(sbi);
3010
3011 f2fs_init_ino_entry_info(sbi);
3012
3013 f2fs_init_fsync_node_info(sbi);
3014
3015 /* setup f2fs internal modules */
3016 err = f2fs_build_segment_manager(sbi);
3017 if (err) {
3018 f2fs_msg(sb, KERN_ERR,
3019 "Failed to initialize F2FS segment manager");
3020 goto free_sm;
3021 }
3022 err = f2fs_build_node_manager(sbi);
3023 if (err) {
3024 f2fs_msg(sb, KERN_ERR,
3025 "Failed to initialize F2FS node manager");
3026 goto free_nm;
3027 }
3028
3029 /* For write statistics */
3030 if (sb->s_bdev->bd_part)
3031 sbi->sectors_written_start =
3032 (u64)part_stat_read(sb->s_bdev->bd_part,
3033 sectors[STAT_WRITE]);
3034
3035 /* Read accumulated write IO statistics if exists */
3036 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
3037 if (__exist_node_summaries(sbi))
3038 sbi->kbytes_written =
3039 le64_to_cpu(seg_i->journal->info.kbytes_written);
3040
3041 f2fs_build_gc_manager(sbi);
3042
3043 err = f2fs_build_stats(sbi);
3044 if (err)
3045 goto free_nm;
3046
3047 /* get an inode for node space */
3048 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
3049 if (IS_ERR(sbi->node_inode)) {
3050 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
3051 err = PTR_ERR(sbi->node_inode);
3052 goto free_stats;
3053 }
3054
3055 /* read root inode and dentry */
3056 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
3057 if (IS_ERR(root)) {
3058 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
3059 err = PTR_ERR(root);
3060 goto free_node_inode;
3061 }
3062 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
3063 !root->i_size || !root->i_nlink) {
3064 iput(root);
3065 err = -EINVAL;
3066 goto free_node_inode;
3067 }
3068
3069 sb->s_root = d_make_root(root); /* allocate root dentry */
3070 if (!sb->s_root) {
3071 err = -ENOMEM;
3072 goto free_root_inode;
3073 }
3074
3075 err = f2fs_register_sysfs(sbi);
3076 if (err)
3077 goto free_root_inode;
3078
3079 #ifdef CONFIG_QUOTA
3080 /* Enable quota usage during mount */
3081 if (f2fs_sb_has_quota_ino(sb) && !f2fs_readonly(sb)) {
3082 err = f2fs_enable_quotas(sb);
3083 if (err) {
3084 f2fs_msg(sb, KERN_ERR,
3085 "Cannot turn on quotas: error %d", err);
3086 goto free_sysfs;
3087 }
3088 }
3089 #endif
3090 /* if there are nt orphan nodes free them */
3091 err = f2fs_recover_orphan_inodes(sbi);
3092 if (err)
3093 goto free_meta;
3094
3095 /* recover fsynced data */
3096 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
3097 /*
3098 * mount should be failed, when device has readonly mode, and
3099 * previous checkpoint was not done by clean system shutdown.
3100 */
3101 if (bdev_read_only(sb->s_bdev) &&
3102 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3103 err = -EROFS;
3104 goto free_meta;
3105 }
3106
3107 if (need_fsck)
3108 set_sbi_flag(sbi, SBI_NEED_FSCK);
3109
3110 if (!retry)
3111 goto skip_recovery;
3112
3113 err = f2fs_recover_fsync_data(sbi, false);
3114 if (err < 0) {
3115 need_fsck = true;
3116 f2fs_msg(sb, KERN_ERR,
3117 "Cannot recover all fsync data errno=%d", err);
3118 goto free_meta;
3119 }
3120 } else {
3121 err = f2fs_recover_fsync_data(sbi, true);
3122
3123 if (!f2fs_readonly(sb) && err > 0) {
3124 err = -EINVAL;
3125 f2fs_msg(sb, KERN_ERR,
3126 "Need to recover fsync data");
3127 goto free_meta;
3128 }
3129 }
3130 skip_recovery:
3131 /* f2fs_recover_fsync_data() cleared this already */
3132 clear_sbi_flag(sbi, SBI_POR_DOING);
3133
3134 /*
3135 * If filesystem is not mounted as read-only then
3136 * do start the gc_thread.
3137 */
3138 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
3139 /* After POR, we can run background GC thread.*/
3140 err = f2fs_start_gc_thread(sbi);
3141 if (err)
3142 goto free_meta;
3143 }
3144 kfree(options);
3145
3146 /* recover broken superblock */
3147 if (recovery) {
3148 err = f2fs_commit_super(sbi, true);
3149 f2fs_msg(sb, KERN_INFO,
3150 "Try to recover %dth superblock, ret: %d",
3151 sbi->valid_super_block ? 1 : 2, err);
3152 }
3153
3154 f2fs_join_shrinker(sbi);
3155
3156 f2fs_tuning_parameters(sbi);
3157
3158 f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
3159 cur_cp_version(F2FS_CKPT(sbi)));
3160 f2fs_update_time(sbi, CP_TIME);
3161 f2fs_update_time(sbi, REQ_TIME);
3162 return 0;
3163
3164 free_meta:
3165 #ifdef CONFIG_QUOTA
3166 f2fs_truncate_quota_inode_pages(sb);
3167 if (f2fs_sb_has_quota_ino(sb) && !f2fs_readonly(sb))
3168 f2fs_quota_off_umount(sbi->sb);
3169 #endif
3170 /*
3171 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
3172 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
3173 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
3174 * falls into an infinite loop in f2fs_sync_meta_pages().
3175 */
3176 truncate_inode_pages_final(META_MAPPING(sbi));
3177 #ifdef CONFIG_QUOTA
3178 free_sysfs:
3179 #endif
3180 f2fs_unregister_sysfs(sbi);
3181 free_root_inode:
3182 dput(sb->s_root);
3183 sb->s_root = NULL;
3184 free_node_inode:
3185 f2fs_release_ino_entry(sbi, true);
3186 truncate_inode_pages_final(NODE_MAPPING(sbi));
3187 iput(sbi->node_inode);
3188 sbi->node_inode = NULL;
3189 free_stats:
3190 f2fs_destroy_stats(sbi);
3191 free_nm:
3192 f2fs_destroy_node_manager(sbi);
3193 free_sm:
3194 f2fs_destroy_segment_manager(sbi);
3195 free_devices:
3196 destroy_device_list(sbi);
3197 kfree(sbi->ckpt);
3198 free_meta_inode:
3199 make_bad_inode(sbi->meta_inode);
3200 iput(sbi->meta_inode);
3201 sbi->meta_inode = NULL;
3202 free_io_dummy:
3203 mempool_destroy(sbi->write_io_dummy);
3204 free_percpu:
3205 destroy_percpu_info(sbi);
3206 free_bio_info:
3207 for (i = 0; i < NR_PAGE_TYPE; i++)
3208 kfree(sbi->write_io[i]);
3209 free_options:
3210 #ifdef CONFIG_QUOTA
3211 for (i = 0; i < MAXQUOTAS; i++)
3212 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
3213 #endif
3214 kfree(options);
3215 free_sb_buf:
3216 kfree(raw_super);
3217 free_sbi:
3218 if (sbi->s_chksum_driver)
3219 crypto_free_shash(sbi->s_chksum_driver);
3220 kfree(sbi);
3221
3222 /* give only one another chance */
3223 if (retry) {
3224 retry = false;
3225 shrink_dcache_sb(sb);
3226 goto try_onemore;
3227 }
3228 return err;
3229 }
3230
f2fs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)3231 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
3232 const char *dev_name, void *data)
3233 {
3234 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
3235 }
3236
kill_f2fs_super(struct super_block * sb)3237 static void kill_f2fs_super(struct super_block *sb)
3238 {
3239 if (sb->s_root) {
3240 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3241
3242 set_sbi_flag(sbi, SBI_IS_CLOSE);
3243 f2fs_stop_gc_thread(sbi);
3244 f2fs_stop_discard_thread(sbi);
3245
3246 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
3247 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3248 struct cp_control cpc = {
3249 .reason = CP_UMOUNT,
3250 };
3251 f2fs_write_checkpoint(sbi, &cpc);
3252 }
3253
3254 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
3255 sb->s_flags &= ~SB_RDONLY;
3256 }
3257 kill_block_super(sb);
3258 }
3259
3260 static struct file_system_type f2fs_fs_type = {
3261 .owner = THIS_MODULE,
3262 .name = "f2fs",
3263 .mount = f2fs_mount,
3264 .kill_sb = kill_f2fs_super,
3265 .fs_flags = FS_REQUIRES_DEV,
3266 };
3267 MODULE_ALIAS_FS("f2fs");
3268
init_inodecache(void)3269 static int __init init_inodecache(void)
3270 {
3271 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
3272 sizeof(struct f2fs_inode_info), 0,
3273 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
3274 if (!f2fs_inode_cachep)
3275 return -ENOMEM;
3276 return 0;
3277 }
3278
destroy_inodecache(void)3279 static void destroy_inodecache(void)
3280 {
3281 /*
3282 * Make sure all delayed rcu free inodes are flushed before we
3283 * destroy cache.
3284 */
3285 rcu_barrier();
3286 kmem_cache_destroy(f2fs_inode_cachep);
3287 }
3288
init_f2fs_fs(void)3289 static int __init init_f2fs_fs(void)
3290 {
3291 int err;
3292
3293 if (PAGE_SIZE != F2FS_BLKSIZE) {
3294 printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
3295 PAGE_SIZE, F2FS_BLKSIZE);
3296 return -EINVAL;
3297 }
3298
3299 f2fs_build_trace_ios();
3300
3301 err = init_inodecache();
3302 if (err)
3303 goto fail;
3304 err = f2fs_create_node_manager_caches();
3305 if (err)
3306 goto free_inodecache;
3307 err = f2fs_create_segment_manager_caches();
3308 if (err)
3309 goto free_node_manager_caches;
3310 err = f2fs_create_checkpoint_caches();
3311 if (err)
3312 goto free_segment_manager_caches;
3313 err = f2fs_create_extent_cache();
3314 if (err)
3315 goto free_checkpoint_caches;
3316 err = f2fs_init_sysfs();
3317 if (err)
3318 goto free_extent_cache;
3319 err = register_shrinker(&f2fs_shrinker_info);
3320 if (err)
3321 goto free_sysfs;
3322 err = register_filesystem(&f2fs_fs_type);
3323 if (err)
3324 goto free_shrinker;
3325 err = f2fs_create_root_stats();
3326 if (err)
3327 goto free_filesystem;
3328 err = f2fs_init_post_read_processing();
3329 if (err)
3330 goto free_root_stats;
3331 return 0;
3332
3333 free_root_stats:
3334 f2fs_destroy_root_stats();
3335 free_filesystem:
3336 unregister_filesystem(&f2fs_fs_type);
3337 free_shrinker:
3338 unregister_shrinker(&f2fs_shrinker_info);
3339 free_sysfs:
3340 f2fs_exit_sysfs();
3341 free_extent_cache:
3342 f2fs_destroy_extent_cache();
3343 free_checkpoint_caches:
3344 f2fs_destroy_checkpoint_caches();
3345 free_segment_manager_caches:
3346 f2fs_destroy_segment_manager_caches();
3347 free_node_manager_caches:
3348 f2fs_destroy_node_manager_caches();
3349 free_inodecache:
3350 destroy_inodecache();
3351 fail:
3352 return err;
3353 }
3354
exit_f2fs_fs(void)3355 static void __exit exit_f2fs_fs(void)
3356 {
3357 f2fs_destroy_post_read_processing();
3358 f2fs_destroy_root_stats();
3359 unregister_filesystem(&f2fs_fs_type);
3360 unregister_shrinker(&f2fs_shrinker_info);
3361 f2fs_exit_sysfs();
3362 f2fs_destroy_extent_cache();
3363 f2fs_destroy_checkpoint_caches();
3364 f2fs_destroy_segment_manager_caches();
3365 f2fs_destroy_node_manager_caches();
3366 destroy_inodecache();
3367 f2fs_destroy_trace_ios();
3368 }
3369
3370 module_init(init_f2fs_fs)
3371 module_exit(exit_f2fs_fs)
3372
3373 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
3374 MODULE_DESCRIPTION("Flash Friendly File System");
3375 MODULE_LICENSE("GPL");
3376
3377