29aa3b9e8d10936c4a8fd6ce255e6271597e1b19
[openwrt/staging/rmilecki.git] /
1 From 5ac67ce36cfe38b4c104a42ce52c5c8d526f1c95 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Tue, 27 Mar 2018 22:35:41 +0200
4 Subject: [PATCH] mtd: move code adding (registering) partitions to the
5 parse_mtd_partitions()
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 This commit slightly simplifies the code. Every parse_mtd_partitions()
11 caller (out of two existing ones) had to add partitions & cleanup parser
12 on its own. This moves that responsibility into the function.
13
14 That change also allows dropping struct mtd_partitions argument.
15
16 There is one minor behavior change caused by this cleanup. If
17 parse_mtd_partitions() fails to add partitions (add_mtd_partitions()
18 return an error) then mtd_device_parse_register() will still try to
19 add (register) fallback partitions. It's a real corner case affecting
20 one of uncommon error paths and shouldn't cause any harm.
21
22 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
23 Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
24 ---
25 drivers/mtd/mtdcore.c | 14 ++++----------
26 drivers/mtd/mtdcore.h | 1 -
27 drivers/mtd/mtdpart.c | 44 ++++++++++++++++----------------------------
28 3 files changed, 20 insertions(+), 39 deletions(-)
29
30 --- a/drivers/mtd/mtdcore.c
31 +++ b/drivers/mtd/mtdcore.c
32 @@ -676,7 +676,6 @@ int mtd_device_parse_register(struct mtd
33 const struct mtd_partition *parts,
34 int nr_parts)
35 {
36 - struct mtd_partitions parsed = { };
37 int ret;
38
39 mtd_set_dev_defaults(mtd);
40 @@ -688,13 +687,10 @@ int mtd_device_parse_register(struct mtd
41 }
42
43 /* Prefer parsed partitions over driver-provided fallback */
44 - ret = parse_mtd_partitions(mtd, types, &parsed, parser_data);
45 - if (!ret && parsed.nr_parts) {
46 - parts = parsed.parts;
47 - nr_parts = parsed.nr_parts;
48 - }
49 -
50 - if (nr_parts)
51 + ret = parse_mtd_partitions(mtd, types, parser_data);
52 + if (ret > 0)
53 + ret = 0;
54 + else if (nr_parts)
55 ret = add_mtd_partitions(mtd, parts, nr_parts);
56 else if (!device_is_registered(&mtd->dev))
57 ret = add_mtd_device(mtd);
58 @@ -720,8 +716,6 @@ int mtd_device_parse_register(struct mtd
59 }
60
61 out:
62 - /* Cleanup any parsed partitions */
63 - mtd_part_parser_cleanup(&parsed);
64 if (ret && device_is_registered(&mtd->dev))
65 del_mtd_device(mtd);
66
67 --- a/drivers/mtd/mtdcore.h
68 +++ b/drivers/mtd/mtdcore.h
69 @@ -14,7 +14,6 @@ int del_mtd_partitions(struct mtd_info *
70 struct mtd_partitions;
71
72 int parse_mtd_partitions(struct mtd_info *master, const char * const *types,
73 - struct mtd_partitions *pparts,
74 struct mtd_part_parser_data *data);
75
76 void mtd_part_parser_cleanup(struct mtd_partitions *parts);
77 --- a/drivers/mtd/mtdpart.c
78 +++ b/drivers/mtd/mtdpart.c
79 @@ -383,20 +383,7 @@ static inline void free_partition(struct
80 */
81 static int mtd_parse_part(struct mtd_part *slave, const char *const *types)
82 {
83 - struct mtd_partitions parsed;
84 - int err;
85 -
86 - err = parse_mtd_partitions(&slave->mtd, types, &parsed, NULL);
87 - if (err)
88 - return err;
89 - else if (!parsed.nr_parts)
90 - return -ENOENT;
91 -
92 - err = add_mtd_partitions(&slave->mtd, parsed.parts, parsed.nr_parts);
93 -
94 - mtd_part_parser_cleanup(&parsed);
95 -
96 - return err;
97 + return parse_mtd_partitions(&slave->mtd, types, NULL);
98 }
99
100 static struct mtd_part *allocate_partition(struct mtd_info *parent,
101 @@ -973,30 +960,27 @@ static int mtd_part_of_parse(struct mtd_
102 }
103
104 /**
105 - * parse_mtd_partitions - parse MTD partitions
106 + * parse_mtd_partitions - parse and register MTD partitions
107 + *
108 * @master: the master partition (describes whole MTD device)
109 * @types: names of partition parsers to try or %NULL
110 - * @pparts: info about partitions found is returned here
111 * @data: MTD partition parser-specific data
112 *
113 - * This function tries to find partition on MTD device @master. It uses MTD
114 - * partition parsers, specified in @types. However, if @types is %NULL, then
115 - * the default list of parsers is used. The default list contains only the
116 + * This function tries to find & register partitions on MTD device @master. It
117 + * uses MTD partition parsers, specified in @types. However, if @types is %NULL,
118 + * then the default list of parsers is used. The default list contains only the
119 * "cmdlinepart" and "ofpart" parsers ATM.
120 * Note: If there are more then one parser in @types, the kernel only takes the
121 * partitions parsed out by the first parser.
122 *
123 * This function may return:
124 * o a negative error code in case of failure
125 - * o zero otherwise, and @pparts will describe the partitions, number of
126 - * partitions, and the parser which parsed them. Caller must release
127 - * resources with mtd_part_parser_cleanup() when finished with the returned
128 - * data.
129 + * o number of found partitions otherwise
130 */
131 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
132 - struct mtd_partitions *pparts,
133 struct mtd_part_parser_data *data)
134 {
135 + struct mtd_partitions pparts = { };
136 struct mtd_part_parser *parser;
137 int ret, err = 0;
138
139 @@ -1010,7 +994,7 @@ int parse_mtd_partitions(struct mtd_info
140 * handled in a separated function.
141 */
142 if (!strcmp(*types, "ofpart")) {
143 - ret = mtd_part_of_parse(master, pparts);
144 + ret = mtd_part_of_parse(master, &pparts);
145 } else {
146 pr_debug("%s: parsing partitions %s\n", master->name,
147 *types);
148 @@ -1021,13 +1005,17 @@ int parse_mtd_partitions(struct mtd_info
149 parser ? parser->name : NULL);
150 if (!parser)
151 continue;
152 - ret = mtd_part_do_parse(parser, master, pparts, data);
153 + ret = mtd_part_do_parse(parser, master, &pparts, data);
154 if (ret <= 0)
155 mtd_part_parser_put(parser);
156 }
157 /* Found partitions! */
158 - if (ret > 0)
159 - return 0;
160 + if (ret > 0) {
161 + err = add_mtd_partitions(master, pparts.parts,
162 + pparts.nr_parts);
163 + mtd_part_parser_cleanup(&pparts);
164 + return err ? err : pparts.nr_parts;
165 + }
166 /*
167 * Stash the first error we see; only report it if no parser
168 * succeeds