fs: partitions: Add support for named partitions
authorColin Cross <ccross@android.com>
Wed, 7 Apr 2010 19:05:32 +0000 (12:05 -0700)
committerColin Cross <ccross@android.com>
Thu, 30 Sep 2010 00:49:47 +0000 (17:49 -0700)
Adds a new file in /sys/block/<block>/<partition> called partition_name
that contains the name of the partition, if specified by the partition
handler.

Change-Id: I6648ed95eabefd1d00edbfdfd99eeb971d15f4b3
Signed-off-by: Colin Cross <ccross@android.com>
fs/partitions/check.c
fs/partitions/check.h
include/linux/genhd.h

index 77e420c0d2c0ba0923de0e71bd775eff25e0c42a..dba7af33ea3a0c71efac9caf49640ddf196170cb 100644 (file)
@@ -286,6 +286,13 @@ ssize_t part_inflight_show(struct device *dev,
        return sprintf(buf, "%8u %8u\n", p->in_flight[0], p->in_flight[1]);
 }
 
+ssize_t part_partition_name_show(struct device *dev,
+                       struct device_attribute *attr, char *buf)
+{
+       struct hd_struct *p = dev_to_part(dev);
+       return sprintf(buf, "%s\n", p->partition_name);
+}
+
 #ifdef CONFIG_FAIL_MAKE_REQUEST
 ssize_t part_fail_show(struct device *dev,
                       struct device_attribute *attr, char *buf)
@@ -317,6 +324,8 @@ static DEVICE_ATTR(discard_alignment, S_IRUGO, part_discard_alignment_show,
                   NULL);
 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
 static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
+static DEVICE_ATTR(partition_name, S_IRUGO, part_partition_name_show, NULL);
+
 #ifdef CONFIG_FAIL_MAKE_REQUEST
 static struct device_attribute dev_attr_fail =
        __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
@@ -330,6 +339,7 @@ static struct attribute *part_attrs[] = {
        &dev_attr_discard_alignment.attr,
        &dev_attr_stat.attr,
        &dev_attr_inflight.attr,
+       &dev_attr_partition_name.attr,
 #ifdef CONFIG_FAIL_MAKE_REQUEST
        &dev_attr_fail.attr,
 #endif
@@ -409,6 +419,11 @@ static ssize_t whole_disk_show(struct device *dev,
 static DEVICE_ATTR(whole_disk, S_IRUSR | S_IRGRP | S_IROTH,
                   whole_disk_show, NULL);
 
+static void name_partition(struct hd_struct *p, const char *name)
+{
+       strlcpy(p->partition_name, name, GENHD_PART_NAME_SIZE);
+}
+
 struct hd_struct *add_partition(struct gendisk *disk, int partno,
                                sector_t start, sector_t len, int flags)
 {
@@ -691,6 +706,7 @@ rescan:
                               disk->disk_name, p, -PTR_ERR(part));
                        continue;
                }
+               name_partition(part, state->parts[p].name);
 #ifdef CONFIG_BLK_DEV_MD
                if (state->parts[p].flags & ADDPART_FLAG_RAID)
                        md_autodetect_dev(part_to_dev(part)->devt);
index 8e4e103ba216d39c0d55d0002a75b18ad23d447f..5ef9da15197bb7d047703725da36b4d3eb8284f2 100644 (file)
@@ -1,6 +1,8 @@
 #include <linux/pagemap.h>
 #include <linux/blkdev.h>
 
+#define PART_NAME_SIZE 128
+
 /*
  * add_gd_partition adds a partitions details to the devices partition
  * description.
@@ -12,6 +14,7 @@ struct parsed_partitions {
                sector_t from;
                sector_t size;
                int flags;
+               char name[PART_NAME_SIZE];
        } parts[DISK_MAX_PARTS];
        int next;
        int limit;
@@ -30,7 +33,8 @@ static inline void *read_part_sector(struct parsed_partitions *state,
 }
 
 static inline void
-put_partition(struct parsed_partitions *p, int n, sector_t from, sector_t size)
+put_named_partition(struct parsed_partitions *p, int n, sector_t from,
+       sector_t size, const char *name, size_t name_size)
 {
        if (n < p->limit) {
                char tmp[1 + BDEVNAME_SIZE + 10 + 1];
@@ -39,8 +43,22 @@ put_partition(struct parsed_partitions *p, int n, sector_t from, sector_t size)
                p->parts[n].size = size;
                snprintf(tmp, sizeof(tmp), " %s%d", p->name, n);
                strlcat(p->pp_buf, tmp, PAGE_SIZE);
+               if (name) {
+                       if (name_size > PART_NAME_SIZE - 1)
+                               name_size = PART_NAME_SIZE - 1;
+                       memcpy(p->parts[n].name, name, name_size);
+                       p->parts[n].name[name_size] = 0;
+                       snprintf(tmp, sizeof(tmp), " (%s)", p->parts[n].name);
+                       strlcat(p->pp_buf, tmp, PAGE_SIZE);
+               }
        }
 }
 
+static inline void
+put_partition(struct parsed_partitions *p, int n, sector_t from, sector_t size)
+{
+       put_named_partition(p, n, from, size, NULL, 0);
+}
+
 extern int warn_no_part;
 
index 5f2f4c4d8fb0594720bfc1643cec75fbf12aaa36..27bc65f3cef0260b21f6f6408a4c52186ab14606 100644 (file)
@@ -21,6 +21,8 @@
 #define disk_to_dev(disk)      (&(disk)->part0.__dev)
 #define part_to_dev(part)      (&((part)->__dev))
 
+#define GENHD_PART_NAME_SIZE   128
+
 extern struct device_type part_type;
 extern struct kobject *block_depr;
 extern struct class block_class;
@@ -106,6 +108,7 @@ struct hd_struct {
        struct disk_stats dkstats;
 #endif
        struct rcu_head rcu_head;
+       char partition_name[GENHD_PART_NAME_SIZE];
 };
 
 #define GENHD_FL_REMOVABLE                     1