hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/fs/nilfs2/super.c
....@@ -53,6 +53,7 @@
5353 MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
5454 "(NILFS)");
5555 MODULE_LICENSE("GPL");
56
+MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
5657
5758 static struct kmem_cache *nilfs_inode_cachep;
5859 struct kmem_cache *nilfs_transaction_cachep;
....@@ -62,19 +63,25 @@
6263 static int nilfs_setup_super(struct super_block *sb, int is_mount);
6364 static int nilfs_remount(struct super_block *sb, int *flags, char *data);
6465
65
-void __nilfs_msg(struct super_block *sb, const char *level, const char *fmt,
66
- ...)
66
+void __nilfs_msg(struct super_block *sb, const char *fmt, ...)
6767 {
6868 struct va_format vaf;
6969 va_list args;
70
+ int level;
7071
7172 va_start(args, fmt);
72
- vaf.fmt = fmt;
73
+
74
+ level = printk_get_level(fmt);
75
+ vaf.fmt = printk_skip_level(fmt);
7376 vaf.va = &args;
77
+
7478 if (sb)
75
- printk("%sNILFS (%s): %pV\n", level, sb->s_id, &vaf);
79
+ printk("%c%cNILFS (%s): %pV\n",
80
+ KERN_SOH_ASCII, level, sb->s_id, &vaf);
7681 else
77
- printk("%sNILFS: %pV\n", level, &vaf);
82
+ printk("%c%cNILFS: %pV\n",
83
+ KERN_SOH_ASCII, level, &vaf);
84
+
7885 va_end(args);
7986 }
8087
....@@ -106,7 +113,7 @@
106113 *
107114 * This implements the body of nilfs_error() macro. Normally,
108115 * nilfs_error() should be used. As for sustainable errors such as a
109
- * single-shot I/O error, nilfs_msg() should be used instead.
116
+ * single-shot I/O error, nilfs_err() should be used instead.
110117 *
111118 * Callers should not add a trailing newline since this will do it.
112119 */
....@@ -151,23 +158,17 @@
151158 ii->i_bh = NULL;
152159 ii->i_state = 0;
153160 ii->i_cno = 0;
154
- nilfs_mapping_init(&ii->i_btnode_cache, &ii->vfs_inode);
161
+ ii->i_assoc_inode = NULL;
162
+ ii->i_bmap = &ii->i_bmap_data;
155163 return &ii->vfs_inode;
156164 }
157165
158
-static void nilfs_i_callback(struct rcu_head *head)
166
+static void nilfs_free_inode(struct inode *inode)
159167 {
160
- struct inode *inode = container_of(head, struct inode, i_rcu);
161
-
162168 if (nilfs_is_metadata_file_inode(inode))
163169 nilfs_mdt_destroy(inode);
164170
165171 kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
166
-}
167
-
168
-void nilfs_destroy_inode(struct inode *inode)
169
-{
170
- call_rcu(&inode->i_rcu, nilfs_i_callback);
171172 }
172173
173174 static int nilfs_sync_super(struct super_block *sb, int flag)
....@@ -185,8 +186,7 @@
185186 }
186187
187188 if (unlikely(err)) {
188
- nilfs_msg(sb, KERN_ERR, "unable to write superblock: err=%d",
189
- err);
189
+ nilfs_err(sb, "unable to write superblock: err=%d", err);
190190 if (err == -EIO && nilfs->ns_sbh[1]) {
191191 /*
192192 * sbp[0] points to newer log than sbp[1],
....@@ -256,7 +256,7 @@
256256 sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
257257 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
258258 } else {
259
- nilfs_msg(sb, KERN_CRIT, "superblock broke");
259
+ nilfs_crit(sb, "superblock broke");
260260 return NULL;
261261 }
262262 } else if (sbp[1] &&
....@@ -366,17 +366,38 @@
366366 offset = sb2off & (nilfs->ns_blocksize - 1);
367367 nsbh = sb_getblk(sb, newblocknr);
368368 if (!nsbh) {
369
- nilfs_msg(sb, KERN_WARNING,
370
- "unable to move secondary superblock to block %llu",
371
- (unsigned long long)newblocknr);
369
+ nilfs_warn(sb,
370
+ "unable to move secondary superblock to block %llu",
371
+ (unsigned long long)newblocknr);
372372 ret = -EIO;
373373 goto out;
374374 }
375375 nsbp = (void *)nsbh->b_data + offset;
376
- memset(nsbp, 0, nilfs->ns_blocksize);
376
+
377
+ lock_buffer(nsbh);
378
+ if (sb2i >= 0) {
379
+ /*
380
+ * The position of the second superblock only changes by 4KiB,
381
+ * which is larger than the maximum superblock data size
382
+ * (= 1KiB), so there is no need to use memmove() to allow
383
+ * overlap between source and destination.
384
+ */
385
+ memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
386
+
387
+ /*
388
+ * Zero fill after copy to avoid overwriting in case of move
389
+ * within the same block.
390
+ */
391
+ memset(nsbh->b_data, 0, offset);
392
+ memset((void *)nsbp + nilfs->ns_sbsize, 0,
393
+ nsbh->b_size - offset - nilfs->ns_sbsize);
394
+ } else {
395
+ memset(nsbh->b_data, 0, nsbh->b_size);
396
+ }
397
+ set_buffer_uptodate(nsbh);
398
+ unlock_buffer(nsbh);
377399
378400 if (sb2i >= 0) {
379
- memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
380401 brelse(nilfs->ns_sbh[sb2i]);
381402 nilfs->ns_sbh[sb2i] = nsbh;
382403 nilfs->ns_sbp[sb2i] = nsbp;
....@@ -408,6 +429,15 @@
408429 devsize = i_size_read(sb->s_bdev->bd_inode);
409430 if (newsize > devsize)
410431 goto out;
432
+
433
+ /*
434
+ * Prevent underflow in second superblock position calculation.
435
+ * The exact minimum size check is done in nilfs_sufile_resize().
436
+ */
437
+ if (newsize < 4096) {
438
+ ret = -ENOSPC;
439
+ goto out;
440
+ }
411441
412442 /*
413443 * Write lock is required to protect some functions depending
....@@ -474,6 +504,7 @@
474504 up_write(&nilfs->ns_sem);
475505 }
476506
507
+ nilfs_sysfs_delete_device_group(nilfs);
477508 iput(nilfs->ns_sufile);
478509 iput(nilfs->ns_cpfile);
479510 iput(nilfs->ns_dat);
....@@ -531,7 +562,7 @@
531562 up_read(&nilfs->ns_segctor_sem);
532563 if (unlikely(err)) {
533564 if (err == -ENOENT || err == -EINVAL) {
534
- nilfs_msg(sb, KERN_ERR,
565
+ nilfs_err(sb,
535566 "Invalid checkpoint (checkpoint number=%llu)",
536567 (unsigned long long)cno);
537568 err = -EINVAL;
....@@ -629,8 +660,7 @@
629660 err = nilfs_ifile_count_free_inodes(root->ifile,
630661 &nmaxinodes, &nfreeinodes);
631662 if (unlikely(err)) {
632
- nilfs_msg(sb, KERN_WARNING,
633
- "failed to count free inodes: err=%d", err);
663
+ nilfs_warn(sb, "failed to count free inodes: err=%d", err);
634664 if (err == -ERANGE) {
635665 /*
636666 * If nilfs_palloc_count_max_entries() returns
....@@ -654,8 +684,7 @@
654684 buf->f_files = nmaxinodes;
655685 buf->f_ffree = nfreeinodes;
656686 buf->f_namelen = NILFS_NAME_LEN;
657
- buf->f_fsid.val[0] = (u32)id;
658
- buf->f_fsid.val[1] = (u32)(id >> 32);
687
+ buf->f_fsid = u64_to_fsid(id);
659688
660689 return 0;
661690 }
....@@ -686,7 +715,7 @@
686715
687716 static const struct super_operations nilfs_sops = {
688717 .alloc_inode = nilfs_alloc_inode,
689
- .destroy_inode = nilfs_destroy_inode,
718
+ .free_inode = nilfs_free_inode,
690719 .dirty_inode = nilfs_dirty_inode,
691720 .evict_inode = nilfs_evict_inode,
692721 .put_super = nilfs_put_super,
....@@ -762,7 +791,7 @@
762791 break;
763792 case Opt_snapshot:
764793 if (is_remount) {
765
- nilfs_msg(sb, KERN_ERR,
794
+ nilfs_err(sb,
766795 "\"%s\" option is invalid for remount",
767796 p);
768797 return 0;
....@@ -778,8 +807,7 @@
778807 nilfs_clear_opt(nilfs, DISCARD);
779808 break;
780809 default:
781
- nilfs_msg(sb, KERN_ERR,
782
- "unrecognized mount option \"%s\"", p);
810
+ nilfs_err(sb, "unrecognized mount option \"%s\"", p);
783811 return 0;
784812 }
785813 }
....@@ -815,10 +843,10 @@
815843 mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
816844
817845 if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
818
- nilfs_msg(sb, KERN_WARNING, "mounting fs with errors");
846
+ nilfs_warn(sb, "mounting fs with errors");
819847 #if 0
820848 } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
821
- nilfs_msg(sb, KERN_WARNING, "maximal mount count reached");
849
+ nilfs_warn(sb, "maximal mount count reached");
822850 #endif
823851 }
824852 if (!max_mnt_count)
....@@ -881,7 +909,7 @@
881909 features = le64_to_cpu(sbp->s_feature_incompat) &
882910 ~NILFS_FEATURE_INCOMPAT_SUPP;
883911 if (features) {
884
- nilfs_msg(sb, KERN_ERR,
912
+ nilfs_err(sb,
885913 "couldn't mount because of unsupported optional features (%llx)",
886914 (unsigned long long)features);
887915 return -EINVAL;
....@@ -889,7 +917,7 @@
889917 features = le64_to_cpu(sbp->s_feature_compat_ro) &
890918 ~NILFS_FEATURE_COMPAT_RO_SUPP;
891919 if (!sb_rdonly(sb) && features) {
892
- nilfs_msg(sb, KERN_ERR,
920
+ nilfs_err(sb,
893921 "couldn't mount RDWR because of unsupported optional features (%llx)",
894922 (unsigned long long)features);
895923 return -EINVAL;
....@@ -908,12 +936,12 @@
908936 inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
909937 if (IS_ERR(inode)) {
910938 ret = PTR_ERR(inode);
911
- nilfs_msg(sb, KERN_ERR, "error %d getting root inode", ret);
939
+ nilfs_err(sb, "error %d getting root inode", ret);
912940 goto out;
913941 }
914942 if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
915943 iput(inode);
916
- nilfs_msg(sb, KERN_ERR, "corrupt root inode");
944
+ nilfs_err(sb, "corrupt root inode");
917945 ret = -EINVAL;
918946 goto out;
919947 }
....@@ -941,7 +969,7 @@
941969 return ret;
942970
943971 failed_dentry:
944
- nilfs_msg(sb, KERN_ERR, "error %d getting root dentry", ret);
972
+ nilfs_err(sb, "error %d getting root dentry", ret);
945973 goto out;
946974 }
947975
....@@ -961,7 +989,7 @@
961989 ret = (ret == -ENOENT) ? -EINVAL : ret;
962990 goto out;
963991 } else if (!ret) {
964
- nilfs_msg(s, KERN_ERR,
992
+ nilfs_err(s,
965993 "The specified checkpoint is not a snapshot (checkpoint number=%llu)",
966994 (unsigned long long)cno);
967995 ret = -EINVAL;
....@@ -970,7 +998,7 @@
970998
971999 ret = nilfs_attach_checkpoint(s, cno, false, &root);
9721000 if (ret) {
973
- nilfs_msg(s, KERN_ERR,
1001
+ nilfs_err(s,
9741002 "error %d while loading snapshot (checkpoint number=%llu)",
9751003 ret, (unsigned long long)cno);
9761004 goto out;
....@@ -1067,7 +1095,7 @@
10671095 cno = nilfs_last_cno(nilfs);
10681096 err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
10691097 if (err) {
1070
- nilfs_msg(sb, KERN_ERR,
1098
+ nilfs_err(sb,
10711099 "error %d while loading last checkpoint (checkpoint number=%llu)",
10721100 err, (unsigned long long)cno);
10731101 goto failed_unload;
....@@ -1100,6 +1128,7 @@
11001128 nilfs_put_root(fsroot);
11011129
11021130 failed_unload:
1131
+ nilfs_sysfs_delete_device_group(nilfs);
11031132 iput(nilfs->ns_sufile);
11041133 iput(nilfs->ns_cpfile);
11051134 iput(nilfs->ns_dat);
....@@ -1129,16 +1158,14 @@
11291158 err = -EINVAL;
11301159
11311160 if (!nilfs_valid_fs(nilfs)) {
1132
- nilfs_msg(sb, KERN_WARNING,
1133
- "couldn't remount because the filesystem is in an incomplete recovery state");
1161
+ nilfs_warn(sb,
1162
+ "couldn't remount because the filesystem is in an incomplete recovery state");
11341163 goto restore_opts;
11351164 }
11361165
11371166 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
11381167 goto out;
11391168 if (*flags & SB_RDONLY) {
1140
- /* Shutting down log writer */
1141
- nilfs_detach_log_writer(sb);
11421169 sb->s_flags |= SB_RDONLY;
11431170
11441171 /*
....@@ -1162,9 +1189,9 @@
11621189 ~NILFS_FEATURE_COMPAT_RO_SUPP;
11631190 up_read(&nilfs->ns_sem);
11641191 if (features) {
1165
- nilfs_msg(sb, KERN_WARNING,
1166
- "couldn't remount RDWR because of unsupported optional features (%llx)",
1167
- (unsigned long long)features);
1192
+ nilfs_warn(sb,
1193
+ "couldn't remount RDWR because of unsupported optional features (%llx)",
1194
+ (unsigned long long)features);
11681195 err = -EROFS;
11691196 goto restore_opts;
11701197 }
....@@ -1223,7 +1250,7 @@
12231250 return 0;
12241251
12251252 parse_error:
1226
- nilfs_msg(NULL, KERN_ERR, "invalid option \"%s\": %s", option, msg);
1253
+ nilfs_err(NULL, "invalid option \"%s\": %s", option, msg);
12271254 return 1;
12281255 }
12291256
....@@ -1326,7 +1353,7 @@
13261353 } else if (!sd.cno) {
13271354 if (nilfs_tree_is_busy(s->s_root)) {
13281355 if ((flags ^ s->s_flags) & SB_RDONLY) {
1329
- nilfs_msg(s, KERN_ERR,
1356
+ nilfs_err(s,
13301357 "the device already has a %s mount.",
13311358 sb_rdonly(s) ? "read-only" : "read/write");
13321359 err = -EBUSY;
....@@ -1382,8 +1409,6 @@
13821409 #ifdef CONFIG_NILFS_XATTR
13831410 init_rwsem(&ii->xattr_sem);
13841411 #endif
1385
- address_space_init_once(&ii->i_btnode_cache);
1386
- ii->i_bmap = &ii->i_bmap_data;
13871412 inode_init_once(&ii->vfs_inode);
13881413 }
13891414