hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
kernel/drivers/mtd/ubi/build.c
....@@ -1,20 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Copyright (c) International Business Machines Corp., 2006
34 * Copyright (c) Nokia Corporation, 2007
4
- *
5
- * This program is free software; you can redistribute it and/or modify
6
- * it under the terms of the GNU General Public License as published by
7
- * the Free Software Foundation; either version 2 of the License, or
8
- * (at your option) any later version.
9
- *
10
- * This program is distributed in the hope that it will be useful,
11
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13
- * the GNU General Public License for more details.
14
- *
15
- * You should have received a copy of the GNU General Public License
16
- * along with this program; if not, write to the Free Software
17
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
185 *
196 * Author: Artem Bityutskiy (Битюцкий Артём),
207 * Frank Haverkamp
....@@ -363,9 +350,6 @@
363350 * we still can use 'ubi->ubi_num'.
364351 */
365352 ubi = container_of(dev, struct ubi_device, dev);
366
- ubi = ubi_get_device(ubi->ubi_num);
367
- if (!ubi)
368
- return -ENODEV;
369353
370354 if (attr == &dev_eraseblock_size)
371355 ret = sprintf(buf, "%d\n", ubi->leb_size);
....@@ -394,7 +378,6 @@
394378 else
395379 ret = -EINVAL;
396380
397
- ubi_put_device(ubi);
398381 return ret;
399382 }
400383
....@@ -484,6 +467,7 @@
484467 err = ubi_add_volume(ubi, ubi->volumes[i]);
485468 if (err) {
486469 ubi_err(ubi, "cannot add volume %d", i);
470
+ ubi->volumes[i] = NULL;
487471 goto out_volumes;
488472 }
489473 }
....@@ -516,19 +500,40 @@
516500 }
517501
518502 /**
503
+ * ubi_free_volumes_from - free volumes from specific index.
504
+ * @ubi: UBI device description object
505
+ * @from: the start index used for volume free.
506
+ */
507
+static void ubi_free_volumes_from(struct ubi_device *ubi, int from)
508
+{
509
+ int i;
510
+
511
+ for (i = from; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
512
+ if (!ubi->volumes[i])
513
+ continue;
514
+ ubi_eba_replace_table(ubi->volumes[i], NULL);
515
+ ubi_fastmap_destroy_checkmap(ubi->volumes[i]);
516
+ kfree(ubi->volumes[i]);
517
+ ubi->volumes[i] = NULL;
518
+ }
519
+}
520
+
521
+/**
522
+ * ubi_free_all_volumes - free all volumes.
523
+ * @ubi: UBI device description object
524
+ */
525
+void ubi_free_all_volumes(struct ubi_device *ubi)
526
+{
527
+ ubi_free_volumes_from(ubi, 0);
528
+}
529
+
530
+/**
519531 * ubi_free_internal_volumes - free internal volumes.
520532 * @ubi: UBI device description object
521533 */
522534 void ubi_free_internal_volumes(struct ubi_device *ubi)
523535 {
524
- int i;
525
-
526
- for (i = ubi->vtbl_slots;
527
- i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
528
- ubi_eba_replace_table(ubi->volumes[i], NULL);
529
- ubi_fastmap_destroy_checkmap(ubi->volumes[i]);
530
- kfree(ubi->volumes[i]);
531
- }
536
+ ubi_free_volumes_from(ubi, ubi->vtbl_slots);
532537 }
533538
534539 static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
....@@ -675,6 +680,21 @@
675680 ~(ubi->hdrs_min_io_size - 1);
676681 ubi->vid_hdr_shift = ubi->vid_hdr_offset -
677682 ubi->vid_hdr_aloffset;
683
+ }
684
+
685
+ /*
686
+ * Memory allocation for VID header is ubi->vid_hdr_alsize
687
+ * which is described in comments in io.c.
688
+ * Make sure VID header shift + UBI_VID_HDR_SIZE not exceeds
689
+ * ubi->vid_hdr_alsize, so that all vid header operations
690
+ * won't access memory out of bounds.
691
+ */
692
+ if ((ubi->vid_hdr_shift + UBI_VID_HDR_SIZE) > ubi->vid_hdr_alsize) {
693
+ ubi_err(ubi, "Invalid VID header offset %d, VID header shift(%d)"
694
+ " + VID header size(%zu) > VID header aligned size(%d).",
695
+ ubi->vid_hdr_offset, ubi->vid_hdr_shift,
696
+ UBI_VID_HDR_SIZE, ubi->vid_hdr_alsize);
697
+ return -EINVAL;
678698 }
679699
680700 /* Similar for the data offset */
....@@ -859,9 +879,19 @@
859879 * Both UBI and UBIFS have been designed for SLC NAND and NOR flashes.
860880 * MLC NAND is different and needs special care, otherwise UBI or UBIFS
861881 * will die soon and you will lose all your data.
882
+ * Relax this rule if the partition we're attaching to operates in SLC
883
+ * mode.
862884 */
863
- if (mtd->type == MTD_MLCNANDFLASH) {
885
+ if (mtd->type == MTD_MLCNANDFLASH &&
886
+ !(mtd->flags & MTD_SLC_ON_MLC_EMULATION)) {
864887 pr_err("ubi: refuse attaching mtd%d - MLC NAND is not supported\n",
888
+ mtd->index);
889
+ return -EINVAL;
890
+ }
891
+
892
+ /* UBI cannot work on flashes with zero erasesize. */
893
+ if (!mtd->erasesize) {
894
+ pr_err("ubi: refuse attaching mtd%d - zero erasesize flash is not supported\n",
865895 mtd->index);
866896 return -EINVAL;
867897 }
....@@ -969,9 +999,6 @@
969999 goto out_detach;
9701000 }
9711001
972
- /* Make device "available" before it becomes accessible via sysfs */
973
- ubi_devices[ubi_num] = ubi;
974
-
9751002 err = uif_init(ubi);
9761003 if (err)
9771004 goto out_detach;
....@@ -1016,6 +1043,7 @@
10161043 wake_up_process(ubi->bgt_thread);
10171044 spin_unlock(&ubi->wl_lock);
10181045
1046
+ ubi_devices[ubi_num] = ubi;
10191047 ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL);
10201048 return ubi_num;
10211049
....@@ -1024,9 +1052,8 @@
10241052 out_uif:
10251053 uif_close(ubi);
10261054 out_detach:
1027
- ubi_devices[ubi_num] = NULL;
10281055 ubi_wl_close(ubi);
1029
- ubi_free_internal_volumes(ubi);
1056
+ ubi_free_all_volumes(ubi);
10301057 vfree(ubi->vtbl);
10311058 out_free:
10321059 vfree(ubi->peb_buf);
....@@ -1172,7 +1199,7 @@
11721199 * MTD device name.
11731200 */
11741201 mtd = get_mtd_device_nm(mtd_dev);
1175
- if (IS_ERR(mtd) && PTR_ERR(mtd) == -ENODEV)
1202
+ if (PTR_ERR(mtd) == -ENODEV)
11761203 /* Probably this is an MTD character device node path */
11771204 mtd = open_mtd_by_chdev(mtd_dev);
11781205 } else
....@@ -1334,8 +1361,10 @@
13341361 switch (*endp) {
13351362 case 'G':
13361363 result *= 1024;
1364
+ fallthrough;
13371365 case 'M':
13381366 result *= 1024;
1367
+ fallthrough;
13391368 case 'K':
13401369 result *= 1024;
13411370 if (endp[1] == 'i' && endp[2] == 'B')
....@@ -1463,3 +1492,4 @@
14631492 MODULE_DESCRIPTION("UBI - Unsorted Block Images");
14641493 MODULE_AUTHOR("Artem Bityutskiy");
14651494 MODULE_LICENSE("GPL");
1495
+MODULE_IMPORT_NS(VFS_internal_I_am_really_a_filesystem_and_am_NOT_a_driver);