block: export the diskseq in uevents
[platform/kernel/linux-starfive.git] / block / genhd.c
index 79aa40b..e1b2f89 100644 (file)
 
 static struct kobject *block_depr;
 
+/*
+ * Unique, monotonically increasing sequential number associated with block
+ * devices instances (i.e. incremented each time a device is attached).
+ * Associating uevents with block devices in userspace is difficult and racy:
+ * the uevent netlink socket is lossy, and on slow and overloaded systems has
+ * a very high latency.
+ * Block devices do not have exclusive owners in userspace, any process can set
+ * one up (e.g. loop devices). Moreover, device names can be reused (e.g. loop0
+ * can be reused again and again).
+ * A userspace process setting up a block device and watching for its events
+ * cannot thus reliably tell whether an event relates to the device it just set
+ * up or another earlier instance with the same name.
+ * This sequential number allows userspace processes to solve this problem, and
+ * uniquely associate an uevent to the lifetime to a device.
+ */
+static atomic64_t diskseq;
+
 /* for extended dynamic devt allocation, currently only one major is used */
 #define NR_EXT_DEVT            (1 << MINORBITS)
 static DEFINE_IDA(ext_devt_ida);
@@ -78,11 +95,17 @@ bool set_capacity_and_notify(struct gendisk *disk, sector_t size)
 EXPORT_SYMBOL_GPL(set_capacity_and_notify);
 
 /*
- * Format the device name of the indicated disk into the supplied buffer and
- * return a pointer to that same buffer for convenience.
+ * Format the device name of the indicated block device into the supplied buffer
+ * and return a pointer to that same buffer for convenience.
+ *
+ * Note: do not use this in new code, use the %pg specifier to sprintf and
+ * printk insted.
  */
-char *disk_name(struct gendisk *hd, int partno, char *buf)
+const char *bdevname(struct block_device *bdev, char *buf)
 {
+       struct gendisk *hd = bdev->bd_disk;
+       int partno = bdev->bd_partno;
+
        if (!partno)
                snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
        else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
@@ -92,11 +115,6 @@ char *disk_name(struct gendisk *hd, int partno, char *buf)
 
        return buf;
 }
-
-const char *bdevname(struct block_device *bdev, char *buf)
-{
-       return disk_name(bdev->bd_disk, bdev->bd_partno, buf);
-}
 EXPORT_SYMBOL(bdevname);
 
 static void part_stat_read_all(struct block_device *part,
@@ -365,12 +383,12 @@ void disk_uevent(struct gendisk *disk, enum kobject_action action)
        xa_for_each(&disk->part_tbl, idx, part) {
                if (bdev_is_partition(part) && !bdev_nr_sectors(part))
                        continue;
-               if (!bdgrab(part))
+               if (!kobject_get_unless_zero(&part->bd_device.kobj))
                        continue;
 
                rcu_read_unlock();
                kobject_uevent(bdev_kobj(part), action);
-               bdput(part);
+               put_device(&part->bd_device);
                rcu_read_lock();
        }
        rcu_read_unlock();
@@ -585,6 +603,7 @@ void del_gendisk(struct gendisk *disk)
        disk_del_events(disk);
 
        mutex_lock(&disk->open_mutex);
+       remove_inode_hash(disk->part0->bd_inode);
        disk->flags &= ~GENHD_FL_UP;
        blk_drop_partitions(disk);
        mutex_unlock(&disk->open_mutex);
@@ -592,12 +611,6 @@ void del_gendisk(struct gendisk *disk)
        fsync_bdev(disk->part0);
        __invalidate_device(disk->part0, true);
 
-       /*
-        * Unhash the bdev inode for this device so that it can't be looked
-        * up any more even if openers still hold references to it.
-        */
-       remove_inode_hash(disk->part0->bd_inode);
-
        set_capacity(disk, 0);
 
        if (!(disk->flags & GENHD_FL_HIDDEN)) {
@@ -683,7 +696,6 @@ void __init printk_all_partitions(void)
        while ((dev = class_dev_iter_next(&iter))) {
                struct gendisk *disk = dev_to_disk(dev);
                struct block_device *part;
-               char name_buf[BDEVNAME_SIZE];
                char devt_buf[BDEVT_SIZE];
                unsigned long idx;
 
@@ -703,11 +715,10 @@ void __init printk_all_partitions(void)
                xa_for_each(&disk->part_tbl, idx, part) {
                        if (!bdev_nr_sectors(part))
                                continue;
-                       printk("%s%s %10llu %s %s",
+                       printk("%s%s %10llu %pg %s",
                               bdev_is_partition(part) ? "  " : "",
                               bdevt_str(part->bd_dev, devt_buf),
-                              bdev_nr_sectors(part) >> 1,
-                              disk_name(disk, part->bd_partno, name_buf),
+                              bdev_nr_sectors(part) >> 1, part,
                               part->bd_meta_info ?
                                        part->bd_meta_info->uuid : "");
                        if (bdev_is_partition(part))
@@ -785,7 +796,6 @@ static int show_partition(struct seq_file *seqf, void *v)
        struct gendisk *sgp = v;
        struct block_device *part;
        unsigned long idx;
-       char buf[BDEVNAME_SIZE];
 
        /* Don't show non-partitionable removeable devices or empty devices */
        if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
@@ -798,10 +808,9 @@ static int show_partition(struct seq_file *seqf, void *v)
        xa_for_each(&sgp->part_tbl, idx, part) {
                if (!bdev_nr_sectors(part))
                        continue;
-               seq_printf(seqf, "%4d  %7d %10llu %s\n",
+               seq_printf(seqf, "%4d  %7d %10llu %pg\n",
                           MAJOR(part->bd_dev), MINOR(part->bd_dev),
-                          bdev_nr_sectors(part) >> 1,
-                          disk_name(sgp, part->bd_partno, buf));
+                          bdev_nr_sectors(part) >> 1, part);
        }
        rcu_read_unlock();
        return 0;
@@ -1079,13 +1088,21 @@ static void disk_release(struct device *dev)
        disk_release_events(disk);
        kfree(disk->random);
        xa_destroy(&disk->part_tbl);
-       bdput(disk->part0);
        if (test_bit(GD_QUEUE_REF, &disk->state) && disk->queue)
                blk_put_queue(disk->queue);
-       kfree(disk);
+       iput(disk->part0->bd_inode);    /* frees the disk */
 }
+
+static int block_uevent(struct device *dev, struct kobj_uevent_env *env)
+{
+       struct gendisk *disk = dev_to_disk(dev);
+
+       return add_uevent_var(env, "DISKSEQ=%llu", disk->diskseq);
+}
+
 struct class block_class = {
        .name           = "block",
+       .dev_uevent     = block_uevent,
 };
 
 static char *block_devnode(struct device *dev, umode_t *mode,
@@ -1117,7 +1134,6 @@ static int diskstats_show(struct seq_file *seqf, void *v)
 {
        struct gendisk *gp = v;
        struct block_device *hd;
-       char buf[BDEVNAME_SIZE];
        unsigned int inflight;
        struct disk_stats stat;
        unsigned long idx;
@@ -1140,15 +1156,14 @@ static int diskstats_show(struct seq_file *seqf, void *v)
                else
                        inflight = part_in_flight(hd);
 
-               seq_printf(seqf, "%4d %7d %s "
+               seq_printf(seqf, "%4d %7d %pg "
                           "%lu %lu %lu %u "
                           "%lu %lu %lu %u "
                           "%u %u %u "
                           "%lu %lu %lu %u "
                           "%lu %u"
                           "\n",
-                          MAJOR(hd->bd_dev), MINOR(hd->bd_dev),
-                          disk_name(gp, hd->bd_partno, buf),
+                          MAJOR(hd->bd_dev), MINOR(hd->bd_dev), hd,
                           stat.ios[STAT_READ],
                           stat.merges[STAT_READ],
                           stat.sectors[STAT_READ],
@@ -1263,11 +1278,13 @@ struct gendisk *__alloc_disk_node(int minors, int node_id)
        disk_to_dev(disk)->class = &block_class;
        disk_to_dev(disk)->type = &disk_type;
        device_initialize(disk_to_dev(disk));
+       inc_diskseq(disk);
+
        return disk;
 
 out_destroy_part_tbl:
        xa_destroy(&disk->part_tbl);
-       bdput(disk->part0);
+       iput(disk->part0->bd_inode);
 out_free_disk:
        kfree(disk);
        return NULL;
@@ -1363,3 +1380,8 @@ int bdev_read_only(struct block_device *bdev)
        return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
 }
 EXPORT_SYMBOL(bdev_read_only);
+
+void inc_diskseq(struct gendisk *disk)
+{
+       disk->diskseq = atomic64_inc_return(&diskseq);
+}