068-v4.18-0001-mtd-move-code-adding-registering-partitions-to-the-p.patch 5.8 KB

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