mtd: part: Add sysfs variable for offset of partition
authorDan Ehrenberg <dehrenberg@chromium.org>
Thu, 2 Apr 2015 22:15:11 +0000 (15:15 -0700)
committerBrian Norris <computersforpeace@gmail.com>
Mon, 6 Apr 2015 00:44:02 +0000 (17:44 -0700)
This patch makes a sysfs variable called 'offset' on each partition
which contains the offset in bytes from the beginning of the master
device that the partition starts.

Signed-off-by: Dan Ehrenberg <dehrenberg@chromium.org>
Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Documentation/ABI/testing/sysfs-class-mtd
drivers/mtd/mtdpart.c

index 76ee192f80a080b12f319d2e6624f38606f4efa7..3b5c3bca9186d13e8cf5f1911b2fc88424c6970f 100644 (file)
@@ -222,3 +222,13 @@ Description:
                The number of blocks that are marked as reserved, if any, in
                this partition. These are typically used to store the in-flash
                bad block table (BBT).
+
+What:          /sys/class/mtd/mtdX/offset
+Date:          March 2015
+KernelVersion: 4.1
+Contact:       linux-mtd@lists.infradead.org
+Description:
+               For a partition, the offset of that partition from the start
+               of the master device in bytes. This attribute is absent on
+               main devices, so it can be used to distinguish between
+               partitions and devices that aren't partitions.
index a19ec5a4e4095888ad8aaba63496d84328f18040..be803408670154955a7bcb8ae59a1d17ef93548e 100644 (file)
@@ -554,6 +554,30 @@ out_register:
        return slave;
 }
 
+static ssize_t mtd_partition_offset_show(struct device *dev,
+               struct device_attribute *attr, char *buf)
+{
+       struct mtd_info *mtd = dev_get_drvdata(dev);
+       struct mtd_part *part = PART(mtd);
+       return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
+}
+
+static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
+
+static const struct attribute *mtd_partition_attrs[] = {
+       &dev_attr_offset.attr,
+       NULL
+};
+
+static int mtd_add_partition_attrs(struct mtd_part *new)
+{
+       int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
+       if (ret)
+               printk(KERN_WARNING
+                      "mtd: failed to create partition attrs, err=%d\n", ret);
+       return ret;
+}
+
 int mtd_add_partition(struct mtd_info *master, const char *name,
                      long long offset, long long length)
 {
@@ -603,6 +627,8 @@ int mtd_add_partition(struct mtd_info *master, const char *name,
 
        add_mtd_device(&new->mtd);
 
+       mtd_add_partition_attrs(new);
+
        return ret;
 err_inv:
        mutex_unlock(&mtd_partitions_mutex);
@@ -620,6 +646,8 @@ int mtd_del_partition(struct mtd_info *master, int partno)
        list_for_each_entry_safe(slave, next, &mtd_partitions, list)
                if ((slave->master == master) &&
                    (slave->mtd.index == partno)) {
+                       sysfs_remove_files(&slave->mtd.dev.kobj,
+                                          mtd_partition_attrs);
                        ret = del_mtd_device(&slave->mtd);
                        if (ret < 0)
                                break;
@@ -663,6 +691,7 @@ int add_mtd_partitions(struct mtd_info *master,
                mutex_unlock(&mtd_partitions_mutex);
 
                add_mtd_device(&slave->mtd);
+               mtd_add_partition_attrs(slave);
 
                cur_offset = slave->offset + slave->mtd.size;
        }