forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/fs/ubifs/super.c
....@@ -1,20 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * This file is part of UBIFS.
34 *
45 * Copyright (C) 2006-2008 Nokia Corporation.
5
- *
6
- * This program is free software; you can redistribute it and/or modify it
7
- * under the terms of the GNU General Public License version 2 as published by
8
- * the Free Software Foundation.
9
- *
10
- * This program is distributed in the hope that it will be useful, but WITHOUT
11
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13
- * more details.
14
- *
15
- * You should have received a copy of the GNU General Public License along with
16
- * this program; if not, write to the Free Software Foundation, Inc., 51
17
- * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
186 *
197 * Authors: Artem Bityutskiy (Битюцкий Артём)
208 * Adrian Hunter
....@@ -37,6 +25,24 @@
3725 #include <linux/math64.h>
3826 #include <linux/writeback.h>
3927 #include "ubifs.h"
28
+
29
+static int ubifs_default_version_set(const char *val, const struct kernel_param *kp)
30
+{
31
+ int n = 0, ret;
32
+
33
+ ret = kstrtoint(val, 10, &n);
34
+ if (ret != 0 || n < 4 || n > UBIFS_FORMAT_VERSION)
35
+ return -EINVAL;
36
+ return param_set_int(val, kp);
37
+}
38
+
39
+static const struct kernel_param_ops ubifs_default_version_ops = {
40
+ .set = ubifs_default_version_set,
41
+ .get = param_get_int,
42
+};
43
+
44
+int ubifs_default_version = UBIFS_FORMAT_VERSION;
45
+module_param_cb(default_version, &ubifs_default_version_ops, &ubifs_default_version, 0600);
4046
4147 /*
4248 * Maximum amount of memory we may 'kmalloc()' without worrying that we are
....@@ -129,9 +135,10 @@
129135 goto out_ino;
130136
131137 inode->i_flags |= S_NOCMTIME;
132
-#ifndef CONFIG_UBIFS_ATIME_SUPPORT
133
- inode->i_flags |= S_NOATIME;
134
-#endif
138
+
139
+ if (!IS_ENABLED(CONFIG_UBIFS_ATIME_SUPPORT))
140
+ inode->i_flags |= S_NOATIME;
141
+
135142 set_nlink(inode, le32_to_cpu(ino->nlink));
136143 i_uid_write(inode, le32_to_cpu(ino->uid));
137144 i_gid_write(inode, le32_to_cpu(ino->gid));
....@@ -268,25 +275,19 @@
268275 memset((void *)ui + sizeof(struct inode), 0,
269276 sizeof(struct ubifs_inode) - sizeof(struct inode));
270277 mutex_init(&ui->ui_mutex);
278
+ init_rwsem(&ui->xattr_sem);
271279 spin_lock_init(&ui->ui_lock);
272280 return &ui->vfs_inode;
273281 };
274282
275
-static void ubifs_i_callback(struct rcu_head *head)
276
-{
277
- struct inode *inode = container_of(head, struct inode, i_rcu);
278
- struct ubifs_inode *ui = ubifs_inode(inode);
279
-
280
- fscrypt_free_inode(inode);
281
- kmem_cache_free(ubifs_inode_slab, ui);
282
-}
283
-
284
-static void ubifs_destroy_inode(struct inode *inode)
283
+static void ubifs_free_inode(struct inode *inode)
285284 {
286285 struct ubifs_inode *ui = ubifs_inode(inode);
287286
288287 kfree(ui->data);
289
- call_rcu(&inode->i_rcu, ubifs_i_callback);
288
+ fscrypt_free_inode(inode);
289
+
290
+ kmem_cache_free(ubifs_inode_slab, ui);
290291 }
291292
292293 /*
....@@ -591,6 +592,11 @@
591592 c->ranges[UBIFS_REF_NODE].len = UBIFS_REF_NODE_SZ;
592593 c->ranges[UBIFS_TRUN_NODE].len = UBIFS_TRUN_NODE_SZ;
593594 c->ranges[UBIFS_CS_NODE].len = UBIFS_CS_NODE_SZ;
595
+ c->ranges[UBIFS_AUTH_NODE].min_len = UBIFS_AUTH_NODE_SZ;
596
+ c->ranges[UBIFS_AUTH_NODE].max_len = UBIFS_AUTH_NODE_SZ +
597
+ UBIFS_MAX_HMAC_LEN;
598
+ c->ranges[UBIFS_SIG_NODE].min_len = UBIFS_SIG_NODE_SZ;
599
+ c->ranges[UBIFS_SIG_NODE].max_len = c->leb_size - UBIFS_SB_NODE_SZ;
594600
595601 c->ranges[UBIFS_INO_NODE].min_len = UBIFS_INO_NODE_SZ;
596602 c->ranges[UBIFS_INO_NODE].max_len = UBIFS_MAX_INO_NODE_SZ;
....@@ -632,6 +638,10 @@
632638 c->max_bu_buf_len = UBIFS_MAX_BULK_READ * UBIFS_MAX_DATA_NODE_SZ;
633639 if (c->max_bu_buf_len > c->leb_size)
634640 c->max_bu_buf_len = c->leb_size;
641
+
642
+ /* Log is ready, preserve one LEB for commits. */
643
+ c->min_log_bytes = c->leb_size;
644
+
635645 return 0;
636646 }
637647
....@@ -828,6 +838,11 @@
828838 c->jheads[i].wbuf.sync_callback = &bud_wbuf_callback;
829839 c->jheads[i].wbuf.jhead = i;
830840 c->jheads[i].grouped = 1;
841
+ c->jheads[i].log_hash = ubifs_hash_get_desc(c);
842
+ if (IS_ERR(c->jheads[i].log_hash)) {
843
+ err = PTR_ERR(c->jheads[i].log_hash);
844
+ goto out;
845
+ }
831846 }
832847
833848 /*
....@@ -838,6 +853,12 @@
838853 c->jheads[GCHD].grouped = 0;
839854
840855 return 0;
856
+
857
+out:
858
+ while (i--)
859
+ kfree(c->jheads[i].log_hash);
860
+
861
+ return err;
841862 }
842863
843864 /**
....@@ -852,6 +873,7 @@
852873 for (i = 0; i < c->jhead_cnt; i++) {
853874 kfree(c->jheads[i].wbuf.buf);
854875 kfree(c->jheads[i].wbuf.inodes);
876
+ kfree(c->jheads[i].log_hash);
855877 }
856878 kfree(c->jheads);
857879 c->jheads = NULL;
....@@ -936,6 +958,8 @@
936958 * Opt_no_chk_data_crc: do not check CRCs when reading data nodes
937959 * Opt_override_compr: override default compressor
938960 * Opt_assert: set ubifs_assert() action
961
+ * Opt_auth_key: The key name used for authentication
962
+ * Opt_auth_hash_name: The hash type used for authentication
939963 * Opt_err: just end of array marker
940964 */
941965 enum {
....@@ -947,6 +971,8 @@
947971 Opt_no_chk_data_crc,
948972 Opt_override_compr,
949973 Opt_assert,
974
+ Opt_auth_key,
975
+ Opt_auth_hash_name,
950976 Opt_ignore,
951977 Opt_err,
952978 };
....@@ -959,6 +985,8 @@
959985 {Opt_chk_data_crc, "chk_data_crc"},
960986 {Opt_no_chk_data_crc, "no_chk_data_crc"},
961987 {Opt_override_compr, "compr=%s"},
988
+ {Opt_auth_key, "auth_key=%s"},
989
+ {Opt_auth_hash_name, "auth_hash_name=%s"},
962990 {Opt_ignore, "ubi=%s"},
963991 {Opt_ignore, "vol=%s"},
964992 {Opt_assert, "assert=%s"},
....@@ -1052,6 +1080,8 @@
10521080 c->mount_opts.compr_type = UBIFS_COMPR_LZO;
10531081 else if (!strcmp(name, "zlib"))
10541082 c->mount_opts.compr_type = UBIFS_COMPR_ZLIB;
1083
+ else if (!strcmp(name, "zstd"))
1084
+ c->mount_opts.compr_type = UBIFS_COMPR_ZSTD;
10551085 else {
10561086 ubifs_err(c, "unknown compressor \"%s\"", name); //FIXME: is c ready?
10571087 kfree(name);
....@@ -1082,6 +1112,22 @@
10821112 kfree(act);
10831113 break;
10841114 }
1115
+ case Opt_auth_key:
1116
+ if (!is_remount) {
1117
+ c->auth_key_name = kstrdup(args[0].from,
1118
+ GFP_KERNEL);
1119
+ if (!c->auth_key_name)
1120
+ return -ENOMEM;
1121
+ }
1122
+ break;
1123
+ case Opt_auth_hash_name:
1124
+ if (!is_remount) {
1125
+ c->auth_hash_name = kstrdup(args[0].from,
1126
+ GFP_KERNEL);
1127
+ if (!c->auth_hash_name)
1128
+ return -ENOMEM;
1129
+ }
1130
+ break;
10851131 case Opt_ignore:
10861132 break;
10871133 default:
....@@ -1102,6 +1148,18 @@
11021148 }
11031149
11041150 return 0;
1151
+}
1152
+
1153
+/*
1154
+ * ubifs_release_options - release mount parameters which have been dumped.
1155
+ * @c: UBIFS file-system description object
1156
+ */
1157
+static void ubifs_release_options(struct ubifs_info *c)
1158
+{
1159
+ kfree(c->auth_key_name);
1160
+ c->auth_key_name = NULL;
1161
+ kfree(c->auth_hash_name);
1162
+ c->auth_hash_name = NULL;
11051163 }
11061164
11071165 /**
....@@ -1261,9 +1319,22 @@
12611319
12621320 c->mounting = 1;
12631321
1322
+ if (c->auth_key_name) {
1323
+ if (IS_ENABLED(CONFIG_UBIFS_FS_AUTHENTICATION)) {
1324
+ err = ubifs_init_authentication(c);
1325
+ if (err)
1326
+ goto out_free;
1327
+ } else {
1328
+ ubifs_err(c, "auth_key_name, but UBIFS is built without"
1329
+ " authentication support");
1330
+ err = -EINVAL;
1331
+ goto out_free;
1332
+ }
1333
+ }
1334
+
12641335 err = ubifs_read_superblock(c);
12651336 if (err)
1266
- goto out_free;
1337
+ goto out_auth;
12671338
12681339 c->probing = 0;
12691340
....@@ -1275,19 +1346,18 @@
12751346 ubifs_err(c, "'compressor \"%s\" is not compiled in",
12761347 ubifs_compr_name(c, c->default_compr));
12771348 err = -ENOTSUPP;
1278
- goto out_free;
1349
+ goto out_auth;
12791350 }
12801351
12811352 err = init_constants_sb(c);
12821353 if (err)
1283
- goto out_free;
1354
+ goto out_auth;
12841355
1285
- sz = ALIGN(c->max_idx_node_sz, c->min_io_size);
1286
- sz = ALIGN(sz + c->max_idx_node_sz, c->min_io_size);
1356
+ sz = ALIGN(c->max_idx_node_sz, c->min_io_size) * 2;
12871357 c->cbuf = kmalloc(sz, GFP_NOFS);
12881358 if (!c->cbuf) {
12891359 err = -ENOMEM;
1290
- goto out_free;
1360
+ goto out_auth;
12911361 }
12921362
12931363 err = alloc_wbufs(c);
....@@ -1346,6 +1416,26 @@
13461416 goto out_lpt;
13471417 }
13481418
1419
+ /*
1420
+ * Handle offline signed images: Now that the master node is
1421
+ * written and its validation no longer depends on the hash
1422
+ * in the superblock, we can update the offline signed
1423
+ * superblock with a HMAC version,
1424
+ */
1425
+ if (ubifs_authenticated(c) && ubifs_hmac_zero(c, c->sup_node->hmac)) {
1426
+ err = ubifs_hmac_wkm(c, c->sup_node->hmac_wkm);
1427
+ if (err)
1428
+ goto out_lpt;
1429
+ c->superblock_need_write = 1;
1430
+ }
1431
+
1432
+ if (!c->ro_mount && c->superblock_need_write) {
1433
+ err = ubifs_write_sb_node(c, c->sup_node);
1434
+ if (err)
1435
+ goto out_lpt;
1436
+ c->superblock_need_write = 0;
1437
+ }
1438
+
13491439 err = dbg_check_idx_size(c, c->bi.old_idx_sz);
13501440 if (err)
13511441 goto out_lpt;
....@@ -1379,12 +1469,21 @@
13791469 }
13801470
13811471 if (c->need_recovery) {
1382
- err = ubifs_recover_size(c);
1383
- if (err)
1384
- goto out_orphans;
1472
+ if (!ubifs_authenticated(c)) {
1473
+ err = ubifs_recover_size(c, true);
1474
+ if (err)
1475
+ goto out_orphans;
1476
+ }
1477
+
13851478 err = ubifs_rcvry_gc_commit(c);
13861479 if (err)
13871480 goto out_orphans;
1481
+
1482
+ if (ubifs_authenticated(c)) {
1483
+ err = ubifs_recover_size(c, false);
1484
+ if (err)
1485
+ goto out_orphans;
1486
+ }
13881487 } else {
13891488 err = take_gc_lnum(c);
13901489 if (err)
....@@ -1403,7 +1502,7 @@
14031502 if (err)
14041503 goto out_orphans;
14051504 } else if (c->need_recovery) {
1406
- err = ubifs_recover_size(c);
1505
+ err = ubifs_recover_size(c, false);
14071506 if (err)
14081507 goto out_orphans;
14091508 } else {
....@@ -1442,9 +1541,7 @@
14421541 if (err)
14431542 goto out_infos;
14441543
1445
- err = dbg_debugfs_init_fs(c);
1446
- if (err)
1447
- goto out_infos;
1544
+ dbg_debugfs_init_fs(c);
14481545
14491546 c->mounting = 0;
14501547
....@@ -1511,6 +1608,8 @@
15111608 c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20);
15121609 dbg_gen("max. seq. number: %llu", c->max_sqnum);
15131610 dbg_gen("commit number: %llu", c->cmt_no);
1611
+ dbg_gen("max. xattrs per inode: %d", ubifs_xattr_max_cnt(c));
1612
+ dbg_gen("max orphans: %d", c->max_orphans);
15141613
15151614 return 0;
15161615
....@@ -1533,12 +1632,15 @@
15331632 free_wbufs(c);
15341633 out_cbuf:
15351634 kfree(c->cbuf);
1635
+out_auth:
1636
+ ubifs_exit_authentication(c);
15361637 out_free:
15371638 kfree(c->write_reserve_buf);
15381639 kfree(c->bu.buf);
15391640 vfree(c->ileb_buf);
15401641 vfree(c->sbuf);
15411642 kfree(c->bottom_up_buf);
1643
+ kfree(c->sup_node);
15421644 ubifs_debugging_exit(c);
15431645 return err;
15441646 }
....@@ -1569,7 +1671,9 @@
15691671 free_wbufs(c);
15701672 free_orphans(c);
15711673 ubifs_lpt_free(c, 0);
1674
+ ubifs_exit_authentication(c);
15721675
1676
+ ubifs_release_options(c);
15731677 kfree(c->cbuf);
15741678 kfree(c->rcvrd_mst_node);
15751679 kfree(c->mst_node);
....@@ -1578,6 +1682,7 @@
15781682 vfree(c->ileb_buf);
15791683 vfree(c->sbuf);
15801684 kfree(c->bottom_up_buf);
1685
+ kfree(c->sup_node);
15811686 ubifs_debugging_exit(c);
15821687 }
15831688
....@@ -1616,29 +1721,16 @@
16161721 if (err)
16171722 goto out;
16181723
1619
- if (c->old_leb_cnt != c->leb_cnt) {
1620
- struct ubifs_sb_node *sup;
1621
-
1622
- sup = ubifs_read_sb_node(c);
1623
- if (IS_ERR(sup)) {
1624
- err = PTR_ERR(sup);
1625
- goto out;
1626
- }
1627
- sup->leb_cnt = cpu_to_le32(c->leb_cnt);
1628
- err = ubifs_write_sb_node(c, sup);
1629
- kfree(sup);
1630
- if (err)
1631
- goto out;
1632
- }
1633
-
16341724 if (c->need_recovery) {
16351725 ubifs_msg(c, "completing deferred recovery");
16361726 err = ubifs_write_rcvrd_mst_node(c);
16371727 if (err)
16381728 goto out;
1639
- err = ubifs_recover_size(c);
1640
- if (err)
1641
- goto out;
1729
+ if (!ubifs_authenticated(c)) {
1730
+ err = ubifs_recover_size(c, true);
1731
+ if (err)
1732
+ goto out;
1733
+ }
16421734 err = ubifs_clean_lebs(c, c->sbuf);
16431735 if (err)
16441736 goto out;
....@@ -1658,6 +1750,16 @@
16581750 err = ubifs_write_master(c);
16591751 if (err)
16601752 goto out;
1753
+ }
1754
+
1755
+ if (c->superblock_need_write) {
1756
+ struct ubifs_sb_node *sup = c->sup_node;
1757
+
1758
+ err = ubifs_write_sb_node(c, sup);
1759
+ if (err)
1760
+ goto out;
1761
+
1762
+ c->superblock_need_write = 0;
16611763 }
16621764
16631765 c->ileb_buf = vmalloc(c->leb_size);
....@@ -1704,10 +1806,19 @@
17041806 goto out;
17051807 }
17061808
1707
- if (c->need_recovery)
1809
+ if (c->need_recovery) {
17081810 err = ubifs_rcvry_gc_commit(c);
1709
- else
1811
+ if (err)
1812
+ goto out;
1813
+
1814
+ if (ubifs_authenticated(c)) {
1815
+ err = ubifs_recover_size(c, false);
1816
+ if (err)
1817
+ goto out;
1818
+ }
1819
+ } else {
17101820 err = ubifs_leb_unmap(c, c->gc_lnum);
1821
+ }
17111822 if (err)
17121823 goto out;
17131824
....@@ -1931,7 +2042,7 @@
19312042
19322043 const struct super_operations ubifs_super_operations = {
19332044 .alloc_inode = ubifs_alloc_inode,
1934
- .destroy_inode = ubifs_destroy_inode,
2045
+ .free_inode = ubifs_free_inode,
19352046 .put_super = ubifs_put_super,
19362047 .write_inode = ubifs_write_inode,
19372048 .drop_inode = ubifs_drop_inode,
....@@ -2087,6 +2198,8 @@
20872198 c->vi.vol_id);
20882199 if (err)
20892200 goto out_close;
2201
+ sb->s_bdi->ra_pages = 0;
2202
+ sb->s_bdi->io_pages = 0;
20902203
20912204 sb->s_fs_info = c;
20922205 sb->s_magic = UBIFS_SUPER_MAGIC;
....@@ -2099,9 +2212,7 @@
20992212 #ifdef CONFIG_UBIFS_FS_XATTR
21002213 sb->s_xattr = ubifs_xattr_handlers;
21012214 #endif
2102
-#ifdef CONFIG_FS_ENCRYPTION
2103
- sb->s_cop = &ubifs_crypt_operations;
2104
-#endif
2215
+ fscrypt_set_ops(sb, &ubifs_crypt_operations);
21052216
21062217 mutex_lock(&c->umount_mutex);
21072218 err = mount_ubifs(c);
....@@ -2131,6 +2242,7 @@
21312242 out_unlock:
21322243 mutex_unlock(&c->umount_mutex);
21332244 out_close:
2245
+ ubifs_release_options(c);
21342246 ubi_close_volume(c->ubi);
21352247 out:
21362248 return err;
....@@ -2203,11 +2315,10 @@
22032315 goto out_deact;
22042316 /* We do not support atime */
22052317 sb->s_flags |= SB_ACTIVE;
2206
-#ifndef CONFIG_UBIFS_ATIME_SUPPORT
2207
- sb->s_flags |= SB_NOATIME;
2208
-#else
2209
- ubifs_msg(c, "full atime support is enabled.");
2210
-#endif
2318
+ if (IS_ENABLED(CONFIG_UBIFS_ATIME_SUPPORT))
2319
+ ubifs_msg(c, "full atime support is enabled.");
2320
+ else
2321
+ sb->s_flags |= SB_NOATIME;
22112322 }
22122323
22132324 /* 'fill_super()' opens ubi again so we must close it here */
....@@ -2322,9 +2433,7 @@
23222433 if (err)
23232434 goto out_shrinker;
23242435
2325
- err = dbg_debugfs_init();
2326
- if (err)
2327
- goto out_compr;
2436
+ dbg_debugfs_init();
23282437
23292438 err = register_filesystem(&ubifs_fs_type);
23302439 if (err) {
....@@ -2336,7 +2445,6 @@
23362445
23372446 out_dbg:
23382447 dbg_debugfs_exit();
2339
-out_compr:
23402448 ubifs_compressors_exit();
23412449 out_shrinker:
23422450 unregister_shrinker(&ubifs_shrinker_info);
....@@ -2367,6 +2475,7 @@
23672475 module_exit(ubifs_exit);
23682476
23692477 MODULE_LICENSE("GPL");
2478
+MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
23702479 MODULE_VERSION(__stringify(UBIFS_VERSION));
23712480 MODULE_AUTHOR("Artem Bityutskiy, Adrian Hunter");
23722481 MODULE_DESCRIPTION("UBIFS - UBI File System");