Skip to content

Commit a62c24d

Browse files
Dan Ehrenbergcomputersforpeace
authored andcommitted
mtd: part: Add sysfs variable for offset of partition
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 <[email protected]> Signed-off-by: Brian Norris <[email protected]>
1 parent 727dc61 commit a62c24d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

Documentation/ABI/testing/sysfs-class-mtd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,13 @@ Description:
222222
The number of blocks that are marked as reserved, if any, in
223223
this partition. These are typically used to store the in-flash
224224
bad block table (BBT).
225+
226+
What: /sys/class/mtd/mtdX/offset
227+
Date: March 2015
228+
KernelVersion: 4.1
229+
230+
Description:
231+
For a partition, the offset of that partition from the start
232+
of the master device in bytes. This attribute is absent on
233+
main devices, so it can be used to distinguish between
234+
partitions and devices that aren't partitions.

drivers/mtd/mtdpart.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,30 @@ static struct mtd_part *allocate_partition(struct mtd_info *master,
554554
return slave;
555555
}
556556

557+
static ssize_t mtd_partition_offset_show(struct device *dev,
558+
struct device_attribute *attr, char *buf)
559+
{
560+
struct mtd_info *mtd = dev_get_drvdata(dev);
561+
struct mtd_part *part = PART(mtd);
562+
return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
563+
}
564+
565+
static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
566+
567+
static const struct attribute *mtd_partition_attrs[] = {
568+
&dev_attr_offset.attr,
569+
NULL
570+
};
571+
572+
static int mtd_add_partition_attrs(struct mtd_part *new)
573+
{
574+
int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
575+
if (ret)
576+
printk(KERN_WARNING
577+
"mtd: failed to create partition attrs, err=%d\n", ret);
578+
return ret;
579+
}
580+
557581
int mtd_add_partition(struct mtd_info *master, const char *name,
558582
long long offset, long long length)
559583
{
@@ -603,6 +627,8 @@ int mtd_add_partition(struct mtd_info *master, const char *name,
603627

604628
add_mtd_device(&new->mtd);
605629

630+
mtd_add_partition_attrs(new);
631+
606632
return ret;
607633
err_inv:
608634
mutex_unlock(&mtd_partitions_mutex);
@@ -620,6 +646,8 @@ int mtd_del_partition(struct mtd_info *master, int partno)
620646
list_for_each_entry_safe(slave, next, &mtd_partitions, list)
621647
if ((slave->master == master) &&
622648
(slave->mtd.index == partno)) {
649+
sysfs_remove_files(&slave->mtd.dev.kobj,
650+
mtd_partition_attrs);
623651
ret = del_mtd_device(&slave->mtd);
624652
if (ret < 0)
625653
break;
@@ -663,6 +691,7 @@ int add_mtd_partitions(struct mtd_info *master,
663691
mutex_unlock(&mtd_partitions_mutex);
664692

665693
add_mtd_device(&slave->mtd);
694+
mtd_add_partition_attrs(slave);
666695

667696
cur_offset = slave->offset + slave->mtd.size;
668697
}

0 commit comments

Comments
 (0)