macvlan: make operstate and carrier more accurate
[deliverable/linux.git] / drivers / mtd / mtdpart.c
index c32b127b197679e7ef003dc59d52c9742b24a113..10bf304027dd90af54a7b82aff21ce15ed83fe6d 100644 (file)
@@ -687,7 +687,7 @@ int add_mtd_partitions(struct mtd_info *master,
 static DEFINE_SPINLOCK(part_parser_lock);
 static LIST_HEAD(part_parsers);
 
-static struct mtd_part_parser *get_partition_parser(const char *name)
+static struct mtd_part_parser *mtd_part_parser_get(const char *name)
 {
        struct mtd_part_parser *p, *ret = NULL;
 
@@ -704,12 +704,28 @@ static struct mtd_part_parser *get_partition_parser(const char *name)
        return ret;
 }
 
-#define put_partition_parser(p) do { module_put((p)->owner); } while (0)
+static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
+{
+       module_put(p->owner);
+}
+
+/*
+ * Many partition parsers just expected the core to kfree() all their data in
+ * one chunk. Do that by default.
+ */
+static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
+                                           int nr_parts)
+{
+       kfree(pparts);
+}
 
 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
 {
        p->owner = owner;
 
+       if (!p->cleanup)
+               p->cleanup = &mtd_part_parser_cleanup_default;
+
        spin_lock(&part_parser_lock);
        list_add(&p->list, &part_parsers);
        spin_unlock(&part_parser_lock);
@@ -740,7 +756,7 @@ static const char * const default_mtd_part_types[] = {
  * parse_mtd_partitions - parse MTD partitions
  * @master: the master partition (describes whole MTD device)
  * @types: names of partition parsers to try or %NULL
- * @pparts: array of partitions found is returned here
+ * @pparts: info about partitions found is returned here
  * @data: MTD partition parser-specific data
  *
  * This function tries to find partition on MTD device @master. It uses MTD
@@ -752,12 +768,13 @@ static const char * const default_mtd_part_types[] = {
  *
  * This function may return:
  * o a negative error code in case of failure
- * o zero if no partitions were found
- * o a positive number of found partitions, in which case on exit @pparts will
- *   point to an array containing this number of &struct mtd_info objects.
+ * o zero otherwise, and @pparts will describe the partitions, number of
+ *   partitions, and the parser which parsed them. Caller must release
+ *   resources with mtd_part_parser_cleanup() when finished with the returned
+ *   data.
  */
 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
-                        struct mtd_partition **pparts,
+                        struct mtd_partition*pparts,
                         struct mtd_part_parser_data *data)
 {
        struct mtd_part_parser *parser;
@@ -768,22 +785,24 @@ int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
 
        for ( ; *types; types++) {
                pr_debug("%s: parsing partitions %s\n", master->name, *types);
-               parser = get_partition_parser(*types);
+               parser = mtd_part_parser_get(*types);
                if (!parser && !request_module("%s", *types))
-                       parser = get_partition_parser(*types);
+                       parser = mtd_part_parser_get(*types);
                pr_debug("%s: got parser %s\n", master->name,
                         parser ? parser->name : NULL);
                if (!parser)
                        continue;
-               ret = (*parser->parse_fn)(master, pparts, data);
+               ret = (*parser->parse_fn)(master, &pparts->parts, data);
                pr_debug("%s: parser %s: %i\n",
                         master->name, parser->name, ret);
-               put_partition_parser(parser);
                if (ret > 0) {
                        printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
                               ret, parser->name, master->name);
-                       return ret;
+                       pparts->nr_parts = ret;
+                       pparts->parser = parser;
+                       return 0;
                }
+               mtd_part_parser_put(parser);
                /*
                 * Stash the first error we see; only report it if no parser
                 * succeeds
@@ -794,6 +813,22 @@ int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
        return err;
 }
 
+void mtd_part_parser_cleanup(struct mtd_partitions *parts)
+{
+       const struct mtd_part_parser *parser;
+
+       if (!parts)
+               return;
+
+       parser = parts->parser;
+       if (parser) {
+               if (parser->cleanup)
+                       parser->cleanup(parts->parts, parts->nr_parts);
+
+               mtd_part_parser_put(parser);
+       }
+}
+
 int mtd_is_partition(const struct mtd_info *mtd)
 {
        struct mtd_part *part;
This page took 0.02839 seconds and 5 git commands to generate.