Skip to content

Commit 5ac67ce

Browse files
Rafał MiłeckiBoris Brezillon
authored andcommitted
mtd: move code adding (registering) partitions to the parse_mtd_partitions()
This commit slightly simplifies the code. Every parse_mtd_partitions() caller (out of two existing ones) had to add partitions & cleanup parser on its own. This moves that responsibility into the function. That change also allows dropping struct mtd_partitions argument. There is one minor behavior change caused by this cleanup. If parse_mtd_partitions() fails to add partitions (add_mtd_partitions() return an error) then mtd_device_parse_register() will still try to add (register) fallback partitions. It's a real corner case affecting one of uncommon error paths and shouldn't cause any harm. Signed-off-by: Rafał Miłecki <rafal@milecki.pl> Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
1 parent 0fe3ede commit 5ac67ce

File tree

3 files changed

+20
-39
lines changed

3 files changed

+20
-39
lines changed

drivers/mtd/mtdcore.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,6 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
700700
const struct mtd_partition *parts,
701701
int nr_parts)
702702
{
703-
struct mtd_partitions parsed = { };
704703
int ret;
705704

706705
mtd_set_dev_defaults(mtd);
@@ -712,13 +711,10 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
712711
}
713712

714713
/* Prefer parsed partitions over driver-provided fallback */
715-
ret = parse_mtd_partitions(mtd, types, &parsed, parser_data);
716-
if (!ret && parsed.nr_parts) {
717-
parts = parsed.parts;
718-
nr_parts = parsed.nr_parts;
719-
}
720-
721-
if (nr_parts)
714+
ret = parse_mtd_partitions(mtd, types, parser_data);
715+
if (ret > 0)
716+
ret = 0;
717+
else if (nr_parts)
722718
ret = add_mtd_partitions(mtd, parts, nr_parts);
723719
else if (!device_is_registered(&mtd->dev))
724720
ret = add_mtd_device(mtd);
@@ -744,8 +740,6 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
744740
}
745741

746742
out:
747-
/* Cleanup any parsed partitions */
748-
mtd_part_parser_cleanup(&parsed);
749743
if (ret && device_is_registered(&mtd->dev))
750744
del_mtd_device(mtd);
751745

drivers/mtd/mtdcore.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ int del_mtd_partitions(struct mtd_info *);
1515
struct mtd_partitions;
1616

1717
int parse_mtd_partitions(struct mtd_info *master, const char * const *types,
18-
struct mtd_partitions *pparts,
1918
struct mtd_part_parser_data *data);
2019

2120
void mtd_part_parser_cleanup(struct mtd_partitions *parts);

drivers/mtd/mtdpart.c

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -335,20 +335,7 @@ static inline void free_partition(struct mtd_part *p)
335335
*/
336336
static int mtd_parse_part(struct mtd_part *slave, const char *const *types)
337337
{
338-
struct mtd_partitions parsed;
339-
int err;
340-
341-
err = parse_mtd_partitions(&slave->mtd, types, &parsed, NULL);
342-
if (err)
343-
return err;
344-
else if (!parsed.nr_parts)
345-
return -ENOENT;
346-
347-
err = add_mtd_partitions(&slave->mtd, parsed.parts, parsed.nr_parts);
348-
349-
mtd_part_parser_cleanup(&parsed);
350-
351-
return err;
338+
return parse_mtd_partitions(&slave->mtd, types, NULL);
352339
}
353340

354341
static struct mtd_part *allocate_partition(struct mtd_info *parent,
@@ -933,30 +920,27 @@ static int mtd_part_of_parse(struct mtd_info *master,
933920
}
934921

935922
/**
936-
* parse_mtd_partitions - parse MTD partitions
923+
* parse_mtd_partitions - parse and register MTD partitions
924+
*
937925
* @master: the master partition (describes whole MTD device)
938926
* @types: names of partition parsers to try or %NULL
939-
* @pparts: info about partitions found is returned here
940927
* @data: MTD partition parser-specific data
941928
*
942-
* This function tries to find partition on MTD device @master. It uses MTD
943-
* partition parsers, specified in @types. However, if @types is %NULL, then
944-
* the default list of parsers is used. The default list contains only the
929+
* This function tries to find & register partitions on MTD device @master. It
930+
* uses MTD partition parsers, specified in @types. However, if @types is %NULL,
931+
* then the default list of parsers is used. The default list contains only the
945932
* "cmdlinepart" and "ofpart" parsers ATM.
946933
* Note: If there are more then one parser in @types, the kernel only takes the
947934
* partitions parsed out by the first parser.
948935
*
949936
* This function may return:
950937
* o a negative error code in case of failure
951-
* o zero otherwise, and @pparts will describe the partitions, number of
952-
* partitions, and the parser which parsed them. Caller must release
953-
* resources with mtd_part_parser_cleanup() when finished with the returned
954-
* data.
938+
* o number of found partitions otherwise
955939
*/
956940
int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
957-
struct mtd_partitions *pparts,
958941
struct mtd_part_parser_data *data)
959942
{
943+
struct mtd_partitions pparts = { };
960944
struct mtd_part_parser *parser;
961945
int ret, err = 0;
962946

@@ -970,7 +954,7 @@ int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
970954
* handled in a separated function.
971955
*/
972956
if (!strcmp(*types, "ofpart")) {
973-
ret = mtd_part_of_parse(master, pparts);
957+
ret = mtd_part_of_parse(master, &pparts);
974958
} else {
975959
pr_debug("%s: parsing partitions %s\n", master->name,
976960
*types);
@@ -981,13 +965,17 @@ int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
981965
parser ? parser->name : NULL);
982966
if (!parser)
983967
continue;
984-
ret = mtd_part_do_parse(parser, master, pparts, data);
968+
ret = mtd_part_do_parse(parser, master, &pparts, data);
985969
if (ret <= 0)
986970
mtd_part_parser_put(parser);
987971
}
988972
/* Found partitions! */
989-
if (ret > 0)
990-
return 0;
973+
if (ret > 0) {
974+
err = add_mtd_partitions(master, pparts.parts,
975+
pparts.nr_parts);
976+
mtd_part_parser_cleanup(&pparts);
977+
return err ? err : pparts.nr_parts;
978+
}
991979
/*
992980
* Stash the first error we see; only report it if no parser
993981
* succeeds

0 commit comments

Comments
 (0)