kernel: Copy patches from kernel 4.14 to 4.19
[openwrt/staging/chunkeey.git] / target / linux / generic / backport-4.19 / 040-v4.17-0001-mtd-move-code-adding-master-MTD-out-of-mtd_add_devic.patch
1 From 2c77c57d22adb05b21cdb333a0c42bdfa0e19835 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Tue, 16 Jan 2018 16:45:41 +0100
4 Subject: [PATCH] mtd: move code adding master MTD out of
5 mtd_add_device_partitions()
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 This change is a small cleanup of mtd_device_parse_register(). When
11 using MTD_PARTITIONED_MASTER it makes sure a master MTD is registered
12 before dealing with partitions. The advantage of this is not mixing
13 code handling master MTD with code handling partitions.
14
15 This commit doesn't change any behavior except from a slightly different
16 failure code path. The new code may need to call del_mtd_device when
17 something goes wrong.
18
19 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
20 Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
21 ---
22 drivers/mtd/mtdcore.c | 25 +++++++++++++------------
23 1 file changed, 13 insertions(+), 12 deletions(-)
24
25 --- a/drivers/mtd/mtdcore.c
26 +++ b/drivers/mtd/mtdcore.c
27 @@ -641,20 +641,12 @@ static int mtd_add_device_partitions(str
28 {
29 const struct mtd_partition *real_parts = parts->parts;
30 int nbparts = parts->nr_parts;
31 - int ret;
32
33 - if (nbparts == 0 || IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) {
34 - ret = add_mtd_device(mtd);
35 - if (ret)
36 - return ret;
37 - }
38 + if (!nbparts && !device_is_registered(&mtd->dev))
39 + return add_mtd_device(mtd);
40
41 - if (nbparts > 0) {
42 - ret = add_mtd_partitions(mtd, real_parts, nbparts);
43 - if (ret && IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
44 - del_mtd_device(mtd);
45 - return ret;
46 - }
47 + if (nbparts > 0)
48 + return add_mtd_partitions(mtd, real_parts, nbparts);
49
50 return 0;
51 }
52 @@ -714,6 +706,12 @@ int mtd_device_parse_register(struct mtd
53
54 mtd_set_dev_defaults(mtd);
55
56 + if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) {
57 + ret = add_mtd_device(mtd);
58 + if (ret)
59 + return ret;
60 + }
61 +
62 memset(&parsed, 0, sizeof(parsed));
63
64 ret = parse_mtd_partitions(mtd, types, &parsed, parser_data);
65 @@ -753,6 +751,9 @@ int mtd_device_parse_register(struct mtd
66 out:
67 /* Cleanup any parsed partitions */
68 mtd_part_parser_cleanup(&parsed);
69 + if (ret && device_is_registered(&mtd->dev))
70 + del_mtd_device(mtd);
71 +
72 return ret;
73 }
74 EXPORT_SYMBOL_GPL(mtd_device_parse_register);