aboutsummaryrefslogtreecommitdiffstats
path: root/fs/f2fs/super.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/f2fs/super.c')
-rw-r--r--fs/f2fs/super.c204
1 files changed, 136 insertions, 68 deletions
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 6134832baaaf..15bb81f8dac2 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -126,6 +126,19 @@ static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
126 return NULL; 126 return NULL;
127} 127}
128 128
129static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
130 struct f2fs_sb_info *sbi, char *buf)
131{
132 struct super_block *sb = sbi->sb;
133
134 if (!sb->s_bdev->bd_part)
135 return snprintf(buf, PAGE_SIZE, "0\n");
136
137 return snprintf(buf, PAGE_SIZE, "%llu\n",
138 (unsigned long long)(sbi->kbytes_written +
139 BD_PART_WRITTEN(sbi)));
140}
141
129static ssize_t f2fs_sbi_show(struct f2fs_attr *a, 142static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
130 struct f2fs_sb_info *sbi, char *buf) 143 struct f2fs_sb_info *sbi, char *buf)
131{ 144{
@@ -204,6 +217,9 @@ static struct f2fs_attr f2fs_attr_##_name = { \
204 f2fs_sbi_show, f2fs_sbi_store, \ 217 f2fs_sbi_show, f2fs_sbi_store, \
205 offsetof(struct struct_name, elname)) 218 offsetof(struct struct_name, elname))
206 219
220#define F2FS_GENERAL_RO_ATTR(name) \
221static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
222
207F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time); 223F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
208F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time); 224F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
209F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time); 225F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
@@ -216,10 +232,12 @@ F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
216F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks); 232F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
217F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh); 233F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
218F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages); 234F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
235F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
219F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search); 236F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
220F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level); 237F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
221F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]); 238F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
222F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]); 239F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
240F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
223 241
224#define ATTR_LIST(name) (&f2fs_attr_##name.attr) 242#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
225static struct attribute *f2fs_attrs[] = { 243static struct attribute *f2fs_attrs[] = {
@@ -237,8 +255,10 @@ static struct attribute *f2fs_attrs[] = {
237 ATTR_LIST(dir_level), 255 ATTR_LIST(dir_level),
238 ATTR_LIST(ram_thresh), 256 ATTR_LIST(ram_thresh),
239 ATTR_LIST(ra_nid_pages), 257 ATTR_LIST(ra_nid_pages),
258 ATTR_LIST(dirty_nats_ratio),
240 ATTR_LIST(cp_interval), 259 ATTR_LIST(cp_interval),
241 ATTR_LIST(idle_interval), 260 ATTR_LIST(idle_interval),
261 ATTR_LIST(lifetime_write_kbytes),
242 NULL, 262 NULL,
243}; 263};
244 264
@@ -450,10 +470,6 @@ static struct inode *f2fs_alloc_inode(struct super_block *sb)
450 470
451 /* Will be used by directory only */ 471 /* Will be used by directory only */
452 fi->i_dir_level = F2FS_SB(sb)->dir_level; 472 fi->i_dir_level = F2FS_SB(sb)->dir_level;
453
454#ifdef CONFIG_F2FS_FS_ENCRYPTION
455 fi->i_crypt_info = NULL;
456#endif
457 return &fi->vfs_inode; 473 return &fi->vfs_inode;
458} 474}
459 475
@@ -474,7 +490,7 @@ static int f2fs_drop_inode(struct inode *inode)
474 490
475 /* some remained atomic pages should discarded */ 491 /* some remained atomic pages should discarded */
476 if (f2fs_is_atomic_file(inode)) 492 if (f2fs_is_atomic_file(inode))
477 commit_inmem_pages(inode, true); 493 drop_inmem_pages(inode);
478 494
479 /* should remain fi->extent_tree for writepage */ 495 /* should remain fi->extent_tree for writepage */
480 f2fs_destroy_extent_node(inode); 496 f2fs_destroy_extent_node(inode);
@@ -487,11 +503,7 @@ static int f2fs_drop_inode(struct inode *inode)
487 503
488 sb_end_intwrite(inode->i_sb); 504 sb_end_intwrite(inode->i_sb);
489 505
490#ifdef CONFIG_F2FS_FS_ENCRYPTION 506 fscrypt_put_encryption_info(inode, NULL);
491 if (F2FS_I(inode)->i_crypt_info)
492 f2fs_free_encryption_info(inode,
493 F2FS_I(inode)->i_crypt_info);
494#endif
495 spin_lock(&inode->i_lock); 507 spin_lock(&inode->i_lock);
496 atomic_dec(&inode->i_count); 508 atomic_dec(&inode->i_count);
497 } 509 }
@@ -562,6 +574,10 @@ static void f2fs_put_super(struct super_block *sb)
562 f2fs_leave_shrinker(sbi); 574 f2fs_leave_shrinker(sbi);
563 mutex_unlock(&sbi->umount_mutex); 575 mutex_unlock(&sbi->umount_mutex);
564 576
577 /* our cp_error case, we can wait for any writeback page */
578 if (get_pages(sbi, F2FS_WRITEBACK))
579 f2fs_flush_merged_bios(sbi);
580
565 iput(sbi->node_inode); 581 iput(sbi->node_inode);
566 iput(sbi->meta_inode); 582 iput(sbi->meta_inode);
567 583
@@ -574,6 +590,8 @@ static void f2fs_put_super(struct super_block *sb)
574 wait_for_completion(&sbi->s_kobj_unregister); 590 wait_for_completion(&sbi->s_kobj_unregister);
575 591
576 sb->s_fs_info = NULL; 592 sb->s_fs_info = NULL;
593 if (sbi->s_chksum_driver)
594 crypto_free_shash(sbi->s_chksum_driver);
577 kfree(sbi->raw_super); 595 kfree(sbi->raw_super);
578 kfree(sbi); 596 kfree(sbi);
579} 597}
@@ -766,8 +784,6 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
766 bool need_stop_gc = false; 784 bool need_stop_gc = false;
767 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE); 785 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
768 786
769 sync_filesystem(sb);
770
771 /* 787 /*
772 * Save the old mount options in case we 788 * Save the old mount options in case we
773 * need to restore them. 789 * need to restore them.
@@ -775,6 +791,13 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
775 org_mount_opt = sbi->mount_opt; 791 org_mount_opt = sbi->mount_opt;
776 active_logs = sbi->active_logs; 792 active_logs = sbi->active_logs;
777 793
794 if (*flags & MS_RDONLY) {
795 set_opt(sbi, FASTBOOT);
796 set_sbi_flag(sbi, SBI_IS_DIRTY);
797 }
798
799 sync_filesystem(sb);
800
778 sbi->mount_opt.opt = 0; 801 sbi->mount_opt.opt = 0;
779 default_options(sbi); 802 default_options(sbi);
780 803
@@ -862,6 +885,41 @@ static struct super_operations f2fs_sops = {
862 .remount_fs = f2fs_remount, 885 .remount_fs = f2fs_remount,
863}; 886};
864 887
888#ifdef CONFIG_F2FS_FS_ENCRYPTION
889static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
890{
891 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
892 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
893 ctx, len, NULL);
894}
895
896static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
897 void *fs_data)
898{
899 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
900 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
901 ctx, len, fs_data, XATTR_CREATE);
902}
903
904static unsigned f2fs_max_namelen(struct inode *inode)
905{
906 return S_ISLNK(inode->i_mode) ?
907 inode->i_sb->s_blocksize : F2FS_NAME_LEN;
908}
909
910static struct fscrypt_operations f2fs_cryptops = {
911 .get_context = f2fs_get_context,
912 .set_context = f2fs_set_context,
913 .is_encrypted = f2fs_encrypted_inode,
914 .empty_dir = f2fs_empty_dir,
915 .max_namelen = f2fs_max_namelen,
916};
917#else
918static struct fscrypt_operations f2fs_cryptops = {
919 .is_encrypted = f2fs_encrypted_inode,
920};
921#endif
922
865static struct inode *f2fs_nfs_get_inode(struct super_block *sb, 923static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
866 u64 ino, u32 generation) 924 u64 ino, u32 generation)
867{ 925{
@@ -1074,7 +1132,7 @@ static int sanity_check_raw_super(struct super_block *sb,
1074 return 0; 1132 return 0;
1075} 1133}
1076 1134
1077static int sanity_check_ckpt(struct f2fs_sb_info *sbi) 1135int sanity_check_ckpt(struct f2fs_sb_info *sbi)
1078{ 1136{
1079 unsigned int total, fsmeta; 1137 unsigned int total, fsmeta;
1080 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 1138 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
@@ -1134,14 +1192,15 @@ static void init_sb_info(struct f2fs_sb_info *sbi)
1134 1192
1135/* 1193/*
1136 * Read f2fs raw super block. 1194 * Read f2fs raw super block.
1137 * Because we have two copies of super block, so read the first one at first, 1195 * Because we have two copies of super block, so read both of them
1138 * if the first one is invalid, move to read the second one. 1196 * to get the first valid one. If any one of them is broken, we pass
1197 * them recovery flag back to the caller.
1139 */ 1198 */
1140static int read_raw_super_block(struct super_block *sb, 1199static int read_raw_super_block(struct super_block *sb,
1141 struct f2fs_super_block **raw_super, 1200 struct f2fs_super_block **raw_super,
1142 int *valid_super_block, int *recovery) 1201 int *valid_super_block, int *recovery)
1143{ 1202{
1144 int block = 0; 1203 int block;
1145 struct buffer_head *bh; 1204 struct buffer_head *bh;
1146 struct f2fs_super_block *super, *buf; 1205 struct f2fs_super_block *super, *buf;
1147 int err = 0; 1206 int err = 0;
@@ -1149,50 +1208,48 @@ static int read_raw_super_block(struct super_block *sb,
1149 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL); 1208 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
1150 if (!super) 1209 if (!super)
1151 return -ENOMEM; 1210 return -ENOMEM;
1152retry: 1211
1153 bh = sb_bread(sb, block); 1212 for (block = 0; block < 2; block++) {
1154 if (!bh) { 1213 bh = sb_bread(sb, block);
1155 *recovery = 1; 1214 if (!bh) {
1156 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock", 1215 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
1157 block + 1); 1216 block + 1);
1158 err = -EIO; 1217 err = -EIO;
1159 goto next; 1218 continue;
1160 } 1219 }
1161 1220
1162 buf = (struct f2fs_super_block *)(bh->b_data + F2FS_SUPER_OFFSET); 1221 buf = (struct f2fs_super_block *)
1222 (bh->b_data + F2FS_SUPER_OFFSET);
1163 1223
1164 /* sanity checking of raw super */ 1224 /* sanity checking of raw super */
1165 if (sanity_check_raw_super(sb, buf)) { 1225 if (sanity_check_raw_super(sb, buf)) {
1166 brelse(bh); 1226 f2fs_msg(sb, KERN_ERR,
1167 *recovery = 1; 1227 "Can't find valid F2FS filesystem in %dth superblock",
1168 f2fs_msg(sb, KERN_ERR, 1228 block + 1);
1169 "Can't find valid F2FS filesystem in %dth superblock", 1229 err = -EINVAL;
1170 block + 1); 1230 brelse(bh);
1171 err = -EINVAL; 1231 continue;
1172 goto next; 1232 }
1173 }
1174 1233
1175 if (!*raw_super) { 1234 if (!*raw_super) {
1176 memcpy(super, buf, sizeof(*super)); 1235 memcpy(super, buf, sizeof(*super));
1177 *valid_super_block = block; 1236 *valid_super_block = block;
1178 *raw_super = super; 1237 *raw_super = super;
1238 }
1239 brelse(bh);
1179 } 1240 }
1180 brelse(bh);
1181 1241
1182next: 1242 /* Fail to read any one of the superblocks*/
1183 /* check the validity of the second superblock */ 1243 if (err < 0)
1184 if (block == 0) { 1244 *recovery = 1;
1185 block++;
1186 goto retry;
1187 }
1188 1245
1189 /* No valid superblock */ 1246 /* No valid superblock */
1190 if (!*raw_super) { 1247 if (!*raw_super)
1191 kfree(super); 1248 kfree(super);
1192 return err; 1249 else
1193 } 1250 err = 0;
1194 1251
1195 return 0; 1252 return err;
1196} 1253}
1197 1254
1198static int __f2fs_commit_super(struct f2fs_sb_info *sbi, int block) 1255static int __f2fs_commit_super(struct f2fs_sb_info *sbi, int block)
@@ -1242,6 +1299,7 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
1242 bool retry = true, need_fsck = false; 1299 bool retry = true, need_fsck = false;
1243 char *options = NULL; 1300 char *options = NULL;
1244 int recovery, i, valid_super_block; 1301 int recovery, i, valid_super_block;
1302 struct curseg_info *seg_i;
1245 1303
1246try_onemore: 1304try_onemore:
1247 err = -EINVAL; 1305 err = -EINVAL;
@@ -1254,6 +1312,15 @@ try_onemore:
1254 if (!sbi) 1312 if (!sbi)
1255 return -ENOMEM; 1313 return -ENOMEM;
1256 1314
1315 /* Load the checksum driver */
1316 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
1317 if (IS_ERR(sbi->s_chksum_driver)) {
1318 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
1319 err = PTR_ERR(sbi->s_chksum_driver);
1320 sbi->s_chksum_driver = NULL;
1321 goto free_sbi;
1322 }
1323
1257 /* set a block size */ 1324 /* set a block size */
1258 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) { 1325 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
1259 f2fs_msg(sb, KERN_ERR, "unable to set blocksize"); 1326 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
@@ -1285,6 +1352,7 @@ try_onemore:
1285 get_random_bytes(&sbi->s_next_generation, sizeof(u32)); 1352 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1286 1353
1287 sb->s_op = &f2fs_sops; 1354 sb->s_op = &f2fs_sops;
1355 sb->s_cop = &f2fs_cryptops;
1288 sb->s_xattr = f2fs_xattr_handlers; 1356 sb->s_xattr = f2fs_xattr_handlers;
1289 sb->s_export_op = &f2fs_export_ops; 1357 sb->s_export_op = &f2fs_export_ops;
1290 sb->s_magic = F2FS_SUPER_MAGIC; 1358 sb->s_magic = F2FS_SUPER_MAGIC;
@@ -1333,13 +1401,6 @@ try_onemore:
1333 goto free_meta_inode; 1401 goto free_meta_inode;
1334 } 1402 }
1335 1403
1336 /* sanity checking of checkpoint */
1337 err = -EINVAL;
1338 if (sanity_check_ckpt(sbi)) {
1339 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
1340 goto free_cp;
1341 }
1342
1343 sbi->total_valid_node_count = 1404 sbi->total_valid_node_count =
1344 le32_to_cpu(sbi->ckpt->valid_node_count); 1405 le32_to_cpu(sbi->ckpt->valid_node_count);
1345 sbi->total_valid_inode_count = 1406 sbi->total_valid_inode_count =
@@ -1372,6 +1433,17 @@ try_onemore:
1372 goto free_nm; 1433 goto free_nm;
1373 } 1434 }
1374 1435
1436 /* For write statistics */
1437 if (sb->s_bdev->bd_part)
1438 sbi->sectors_written_start =
1439 (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
1440
1441 /* Read accumulated write IO statistics if exists */
1442 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1443 if (__exist_node_summaries(sbi))
1444 sbi->kbytes_written =
1445 le64_to_cpu(seg_i->sum_blk->journal.info.kbytes_written);
1446
1375 build_gc_manager(sbi); 1447 build_gc_manager(sbi);
1376 1448
1377 /* get an inode for node space */ 1449 /* get an inode for node space */
@@ -1466,8 +1538,10 @@ try_onemore:
1466 1538
1467 /* recover broken superblock */ 1539 /* recover broken superblock */
1468 if (recovery && !f2fs_readonly(sb) && !bdev_read_only(sb->s_bdev)) { 1540 if (recovery && !f2fs_readonly(sb) && !bdev_read_only(sb->s_bdev)) {
1469 f2fs_msg(sb, KERN_INFO, "Recover invalid superblock"); 1541 err = f2fs_commit_super(sbi, true);
1470 f2fs_commit_super(sbi, true); 1542 f2fs_msg(sb, KERN_INFO,
1543 "Try to recover %dth superblock, ret: %ld",
1544 sbi->valid_super_block ? 1 : 2, err);
1471 } 1545 }
1472 1546
1473 f2fs_update_time(sbi, CP_TIME); 1547 f2fs_update_time(sbi, CP_TIME);
@@ -1496,7 +1570,6 @@ free_nm:
1496 destroy_node_manager(sbi); 1570 destroy_node_manager(sbi);
1497free_sm: 1571free_sm:
1498 destroy_segment_manager(sbi); 1572 destroy_segment_manager(sbi);
1499free_cp:
1500 kfree(sbi->ckpt); 1573 kfree(sbi->ckpt);
1501free_meta_inode: 1574free_meta_inode:
1502 make_bad_inode(sbi->meta_inode); 1575 make_bad_inode(sbi->meta_inode);
@@ -1506,6 +1579,8 @@ free_options:
1506free_sb_buf: 1579free_sb_buf:
1507 kfree(raw_super); 1580 kfree(raw_super);
1508free_sbi: 1581free_sbi:
1582 if (sbi->s_chksum_driver)
1583 crypto_free_shash(sbi->s_chksum_driver);
1509 kfree(sbi); 1584 kfree(sbi);
1510 1585
1511 /* give only one another chance */ 1586 /* give only one another chance */
@@ -1585,13 +1660,9 @@ static int __init init_f2fs_fs(void)
1585 err = -ENOMEM; 1660 err = -ENOMEM;
1586 goto free_extent_cache; 1661 goto free_extent_cache;
1587 } 1662 }
1588 err = f2fs_init_crypto();
1589 if (err)
1590 goto free_kset;
1591
1592 err = register_shrinker(&f2fs_shrinker_info); 1663 err = register_shrinker(&f2fs_shrinker_info);
1593 if (err) 1664 if (err)
1594 goto free_crypto; 1665 goto free_kset;
1595 1666
1596 err = register_filesystem(&f2fs_fs_type); 1667 err = register_filesystem(&f2fs_fs_type);
1597 if (err) 1668 if (err)
@@ -1606,8 +1677,6 @@ free_filesystem:
1606 unregister_filesystem(&f2fs_fs_type); 1677 unregister_filesystem(&f2fs_fs_type);
1607free_shrinker: 1678free_shrinker:
1608 unregister_shrinker(&f2fs_shrinker_info); 1679 unregister_shrinker(&f2fs_shrinker_info);
1609free_crypto:
1610 f2fs_exit_crypto();
1611free_kset: 1680free_kset:
1612 kset_unregister(f2fs_kset); 1681 kset_unregister(f2fs_kset);
1613free_extent_cache: 1682free_extent_cache:
@@ -1630,7 +1699,6 @@ static void __exit exit_f2fs_fs(void)
1630 f2fs_destroy_root_stats(); 1699 f2fs_destroy_root_stats();
1631 unregister_shrinker(&f2fs_shrinker_info); 1700 unregister_shrinker(&f2fs_shrinker_info);
1632 unregister_filesystem(&f2fs_fs_type); 1701 unregister_filesystem(&f2fs_fs_type);
1633 f2fs_exit_crypto();
1634 destroy_extent_cache(); 1702 destroy_extent_cache();
1635 destroy_checkpoint_caches(); 1703 destroy_checkpoint_caches();
1636 destroy_segment_manager_caches(); 1704 destroy_segment_manager_caches();