const: make block_device_operations const
[linux-3.10.git] / fs / partitions / check.c
index 16f98d8..7b685e1 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/kmod.h>
 #include <linux/ctype.h>
 #include <linux/genhd.h>
+#include <linux/blktrace_api.h>
 
 #include "check.h"
 
@@ -120,22 +121,21 @@ static int (*check_part[])(struct parsed_partitions *, struct block_device *) =
  * a pointer to that same buffer (for convenience).
  */
 
-char *disk_name(struct gendisk *hd, int part, char *buf)
+char *disk_name(struct gendisk *hd, int partno, char *buf)
 {
-       if (!part)
+       if (!partno)
                snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
        else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
-               snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, part);
+               snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
        else
-               snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, part);
+               snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
 
        return buf;
 }
 
 const char *bdevname(struct block_device *bdev, char *buf)
 {
-       int part = MINOR(bdev->bd_dev) - bdev->bd_disk->first_minor;
-       return disk_name(bdev->bd_disk, part, buf);
+       return disk_name(bdev->bd_disk, bdev->bd_part->partno, buf);
 }
 
 EXPORT_SYMBOL(bdevname);
@@ -169,7 +169,7 @@ check_partition(struct gendisk *hd, struct block_device *bdev)
        if (isdigit(state->name[strlen(state->name)-1]))
                sprintf(state->name, "p");
 
-       state->limit = hd->minors;
+       state->limit = disk_max_parts(hd);
        i = res = err = 0;
        while (!res && check_part[i]) {
                memset(&state->parts, 0, sizeof(state->parts));
@@ -196,6 +196,14 @@ check_partition(struct gendisk *hd, struct block_device *bdev)
        return ERR_PTR(res);
 }
 
+static ssize_t part_partition_show(struct device *dev,
+                                  struct device_attribute *attr, char *buf)
+{
+       struct hd_struct *p = dev_to_part(dev);
+
+       return sprintf(buf, "%d\n", p->partno);
+}
+
 static ssize_t part_start_show(struct device *dev,
                               struct device_attribute *attr, char *buf)
 {
@@ -204,21 +212,29 @@ static ssize_t part_start_show(struct device *dev,
        return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect);
 }
 
-static ssize_t part_size_show(struct device *dev,
-                             struct device_attribute *attr, char *buf)
+ssize_t part_size_show(struct device *dev,
+                      struct device_attribute *attr, char *buf)
 {
        struct hd_struct *p = dev_to_part(dev);
        return sprintf(buf, "%llu\n",(unsigned long long)p->nr_sects);
 }
 
-static ssize_t part_stat_show(struct device *dev,
-                             struct device_attribute *attr, char *buf)
+ssize_t part_alignment_offset_show(struct device *dev,
+                                  struct device_attribute *attr, char *buf)
 {
        struct hd_struct *p = dev_to_part(dev);
+       return sprintf(buf, "%llu\n", (unsigned long long)p->alignment_offset);
+}
 
-       preempt_disable();
-       part_round_stats(p);
-       preempt_enable();
+ssize_t part_stat_show(struct device *dev,
+                      struct device_attribute *attr, char *buf)
+{
+       struct hd_struct *p = dev_to_part(dev);
+       int cpu;
+
+       cpu = part_stat_lock();
+       part_round_stats(cpu, p);
+       part_stat_unlock();
        return sprintf(buf,
                "%8lu %8lu %8llu %8u "
                "%8lu %8lu %8llu %8u "
@@ -232,23 +248,31 @@ static ssize_t part_stat_show(struct device *dev,
                part_stat_read(p, merges[WRITE]),
                (unsigned long long)part_stat_read(p, sectors[WRITE]),
                jiffies_to_msecs(part_stat_read(p, ticks[WRITE])),
-               p->in_flight,
+               part_in_flight(p),
                jiffies_to_msecs(part_stat_read(p, io_ticks)),
                jiffies_to_msecs(part_stat_read(p, time_in_queue)));
 }
 
+ssize_t part_inflight_show(struct device *dev,
+                       struct device_attribute *attr, char *buf)
+{
+       struct hd_struct *p = dev_to_part(dev);
+
+       return sprintf(buf, "%8u %8u\n", p->in_flight[0], p->in_flight[1]);
+}
+
 #ifdef CONFIG_FAIL_MAKE_REQUEST
-static ssize_t part_fail_show(struct device *dev,
-                             struct device_attribute *attr, char *buf)
+ssize_t part_fail_show(struct device *dev,
+                      struct device_attribute *attr, char *buf)
 {
        struct hd_struct *p = dev_to_part(dev);
 
        return sprintf(buf, "%d\n", p->make_it_fail);
 }
 
-static ssize_t part_fail_store(struct device *dev,
-                              struct device_attribute *attr,
-                              const char *buf, size_t count)
+ssize_t part_fail_store(struct device *dev,
+                       struct device_attribute *attr,
+                       const char *buf, size_t count)
 {
        struct hd_struct *p = dev_to_part(dev);
        int i;
@@ -260,18 +284,24 @@ static ssize_t part_fail_store(struct device *dev,
 }
 #endif
 
+static DEVICE_ATTR(partition, S_IRUGO, part_partition_show, NULL);
 static DEVICE_ATTR(start, S_IRUGO, part_start_show, NULL);
 static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
+static DEVICE_ATTR(alignment_offset, S_IRUGO, part_alignment_offset_show, NULL);
 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
+static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_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);
 #endif
 
 static struct attribute *part_attrs[] = {
+       &dev_attr_partition.attr,
        &dev_attr_start.attr,
        &dev_attr_size.attr,
+       &dev_attr_alignment_offset.attr,
        &dev_attr_stat.attr,
+       &dev_attr_inflight.attr,
 #ifdef CONFIG_FAIL_MAKE_REQUEST
        &dev_attr_fail.attr,
 #endif
@@ -282,8 +312,11 @@ static struct attribute_group part_attr_group = {
        .attrs = part_attrs,
 };
 
-static struct attribute_group *part_attr_groups[] = {
+static const struct attribute_group *part_attr_groups[] = {
        &part_attr_group,
+#ifdef CONFIG_BLK_DEV_IO_TRACE
+       &blk_trace_attr_group,
+#endif
        NULL
 };
 
@@ -300,29 +333,35 @@ struct device_type part_type = {
        .release        = part_release,
 };
 
-static inline void disk_sysfs_add_subdirs(struct gendisk *disk)
+static void delete_partition_rcu_cb(struct rcu_head *head)
 {
-       struct kobject *k;
+       struct hd_struct *part = container_of(head, struct hd_struct, rcu_head);
 
-       k = kobject_get(&disk->dev.kobj);
-       disk->holder_dir = kobject_create_and_add("holders", k);
-       disk->slave_dir = kobject_create_and_add("slaves", k);
-       kobject_put(k);
+       part->start_sect = 0;
+       part->nr_sects = 0;
+       part_stat_set_all(part, 0);
+       put_device(part_to_dev(part));
 }
 
-void delete_partition(struct gendisk *disk, int part)
+void delete_partition(struct gendisk *disk, int partno)
 {
-       struct hd_struct *p = disk->part[part-1];
+       struct disk_part_tbl *ptbl = disk->part_tbl;
+       struct hd_struct *part;
 
-       if (!p)
+       if (partno >= ptbl->len)
                return;
-       disk->part[part-1] = NULL;
-       p->start_sect = 0;
-       p->nr_sects = 0;
-       part_stat_set_all(p, 0);
-       kobject_put(p->holder_dir);
-       device_del(&p->dev);
-       put_device(&p->dev);
+
+       part = ptbl->part[partno];
+       if (!part)
+               return;
+
+       blk_free_devt(part_devt(part));
+       rcu_assign_pointer(ptbl->part[partno], NULL);
+       rcu_assign_pointer(ptbl->last_lookup, NULL);
+       kobject_put(part->holder_dir);
+       device_del(part_to_dev(part));
+
+       call_rcu(&part->rcu_head, delete_partition_rcu_cb);
 }
 
 static ssize_t whole_disk_show(struct device *dev,
@@ -333,113 +372,130 @@ static ssize_t whole_disk_show(struct device *dev,
 static DEVICE_ATTR(whole_disk, S_IRUSR | S_IRGRP | S_IROTH,
                   whole_disk_show, NULL);
 
-int add_partition(struct gendisk *disk, int part, sector_t start, sector_t len, int flags)
+struct hd_struct *add_partition(struct gendisk *disk, int partno,
+                               sector_t start, sector_t len, int flags)
 {
        struct hd_struct *p;
+       dev_t devt = MKDEV(0, 0);
+       struct device *ddev = disk_to_dev(disk);
+       struct device *pdev;
+       struct disk_part_tbl *ptbl;
+       const char *dname;
        int err;
 
-       if (disk->part[part - 1])
-               return -EBUSY;
+       err = disk_expand_part_tbl(disk, partno);
+       if (err)
+               return ERR_PTR(err);
+       ptbl = disk->part_tbl;
+
+       if (ptbl->part[partno])
+               return ERR_PTR(-EBUSY);
 
        p = kzalloc(sizeof(*p), GFP_KERNEL);
        if (!p)
-               return -ENOMEM;
+               return ERR_PTR(-EBUSY);
 
        if (!init_part_stats(p)) {
                err = -ENOMEM;
                goto out_free;
        }
+       pdev = part_to_dev(p);
+
        p->start_sect = start;
+       p->alignment_offset = queue_sector_alignment_offset(disk->queue, start);
        p->nr_sects = len;
-       p->partno = part;
-       p->policy = disk->policy;
+       p->partno = partno;
+       p->policy = get_disk_ro(disk);
 
-       if (isdigit(disk->dev.bus_id[strlen(disk->dev.bus_id)-1]))
-               snprintf(p->dev.bus_id, BUS_ID_SIZE,
-               "%sp%d", disk->dev.bus_id, part);
+       dname = dev_name(ddev);
+       if (isdigit(dname[strlen(dname) - 1]))
+               dev_set_name(pdev, "%sp%d", dname, partno);
        else
-               snprintf(p->dev.bus_id, BUS_ID_SIZE,
-                        "%s%d", disk->dev.bus_id, part);
+               dev_set_name(pdev, "%s%d", dname, partno);
+
+       device_initialize(pdev);
+       pdev->class = &block_class;
+       pdev->type = &part_type;
+       pdev->parent = ddev;
 
-       device_initialize(&p->dev);
-       p->dev.devt = MKDEV(disk->major, disk->first_minor + part);
-       p->dev.class = &block_class;
-       p->dev.type = &part_type;
-       p->dev.parent = &disk->dev;
+       err = blk_alloc_devt(p, &devt);
+       if (err)
+               goto out_free_stats;
+       pdev->devt = devt;
 
        /* delay uevent until 'holders' subdir is created */
-       p->dev.uevent_suppress = 1;
-       err = device_add(&p->dev);
+       dev_set_uevent_suppress(pdev, 1);
+       err = device_add(pdev);
        if (err)
                goto out_put;
 
        err = -ENOMEM;
-       p->holder_dir = kobject_create_and_add("holders", &p->dev.kobj);
+       p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
        if (!p->holder_dir)
                goto out_del;
 
-       p->dev.uevent_suppress = 0;
+       dev_set_uevent_suppress(pdev, 0);
        if (flags & ADDPART_FLAG_WHOLEDISK) {
-               err = device_create_file(&p->dev, &dev_attr_whole_disk);
+               err = device_create_file(pdev, &dev_attr_whole_disk);
                if (err)
                        goto out_del;
        }
 
        /* everything is up and running, commence */
-       disk->part[part - 1] = p;
+       INIT_RCU_HEAD(&p->rcu_head);
+       rcu_assign_pointer(ptbl->part[partno], p);
 
        /* suppress uevent if the disk supresses it */
-       if (!disk->dev.uevent_suppress)
-               kobject_uevent(&p->dev.kobj, KOBJ_ADD);
+       if (!dev_get_uevent_suppress(ddev))
+               kobject_uevent(&pdev->kobj, KOBJ_ADD);
 
-       return 0;
+       return p;
 
+out_free_stats:
+       free_part_stats(p);
 out_free:
        kfree(p);
-       return err;
+       return ERR_PTR(err);
 out_del:
        kobject_put(p->holder_dir);
-       device_del(&p->dev);
+       device_del(pdev);
 out_put:
-       put_device(&p->dev);
-       return err;
+       put_device(pdev);
+       blk_free_devt(devt);
+       return ERR_PTR(err);
 }
 
 /* Not exported, helper to add_disk(). */
 void register_disk(struct gendisk *disk)
 {
+       struct device *ddev = disk_to_dev(disk);
        struct block_device *bdev;
-       char *s;
-       int i;
-       struct hd_struct *p;
+       struct disk_part_iter piter;
+       struct hd_struct *part;
        int err;
 
-       disk->dev.parent = disk->driverfs_dev;
-       disk->dev.devt = MKDEV(disk->major, disk->first_minor);
+       ddev->parent = disk->driverfs_dev;
 
-       strlcpy(disk->dev.bus_id, disk->disk_name, BUS_ID_SIZE);
-       /* ewww... some of these buggers have / in the name... */
-       s = strchr(disk->dev.bus_id, '/');
-       if (s)
-               *s = '!';
+       dev_set_name(ddev, disk->disk_name);
 
        /* delay uevents, until we scanned partition table */
-       disk->dev.uevent_suppress = 1;
+       dev_set_uevent_suppress(ddev, 1);
 
-       if (device_add(&disk->dev))
+       if (device_add(ddev))
                return;
 #ifndef CONFIG_SYSFS_DEPRECATED
-       err = sysfs_create_link(block_depr, &disk->dev.kobj,
-                               kobject_name(&disk->dev.kobj));
+       err = sysfs_create_link(block_depr, &ddev->kobj,
+                               kobject_name(&ddev->kobj));
        if (err) {
-               device_del(&disk->dev);
+               device_del(ddev);
                return;
        }
 #endif
-       disk_sysfs_add_subdirs(disk);
+       disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
+       disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
 
        /* No minors to use for partitions */
-       if (disk->minors == 1)
+       if (!disk_partitionable(disk))
                goto exit;
 
        /* No such device (e.g., media were just removed) */
@@ -451,67 +507,119 @@ void register_disk(struct gendisk *disk)
                goto exit;
 
        bdev->bd_invalidated = 1;
-       err = blkdev_get(bdev, FMODE_READ, 0);
+       err = blkdev_get(bdev, FMODE_READ);
        if (err < 0)
                goto exit;
-       blkdev_put(bdev);
+       blkdev_put(bdev, FMODE_READ);
 
 exit:
        /* announce disk after possible partitions are created */
-       disk->dev.uevent_suppress = 0;
-       kobject_uevent(&disk->dev.kobj, KOBJ_ADD);
+       dev_set_uevent_suppress(ddev, 0);
+       kobject_uevent(&ddev->kobj, KOBJ_ADD);
 
        /* announce possible partitions */
-       for (i = 1; i < disk->minors; i++) {
-               p = disk->part[i-1];
-               if (!p || !p->nr_sects)
-                       continue;
-               kobject_uevent(&p->dev.kobj, KOBJ_ADD);
-       }
+       disk_part_iter_init(&piter, disk, 0);
+       while ((part = disk_part_iter_next(&piter)))
+               kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
+       disk_part_iter_exit(&piter);
 }
 
 int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
 {
+       struct disk_part_iter piter;
+       struct hd_struct *part;
        struct parsed_partitions *state;
-       int p, res;
+       int p, highest, res;
 
        if (bdev->bd_part_count)
                return -EBUSY;
        res = invalidate_partition(disk, 0);
        if (res)
                return res;
-       bdev->bd_invalidated = 0;
-       for (p = 1; p < disk->minors; p++)
-               delete_partition(disk, p);
+
+       disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
+       while ((part = disk_part_iter_next(&piter)))
+               delete_partition(disk, part->partno);
+       disk_part_iter_exit(&piter);
+
        if (disk->fops->revalidate_disk)
                disk->fops->revalidate_disk(disk);
+       check_disk_size_change(disk, bdev);
+       bdev->bd_invalidated = 0;
        if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
                return 0;
        if (IS_ERR(state))      /* I/O error reading the partition table */
                return -EIO;
 
        /* tell userspace that the media / partition table may have changed */
-       kobject_uevent(&disk->dev.kobj, KOBJ_CHANGE);
+       kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
+
+       /* Detect the highest partition number and preallocate
+        * disk->part_tbl.  This is an optimization and not strictly
+        * necessary.
+        */
+       for (p = 1, highest = 0; p < state->limit; p++)
+               if (state->parts[p].size)
+                       highest = p;
+
+       disk_expand_part_tbl(disk, highest);
 
+       /* add partitions */
        for (p = 1; p < state->limit; p++) {
-               sector_t size = state->parts[p].size;
-               sector_t from = state->parts[p].from;
+               sector_t size, from;
+try_scan:
+               size = state->parts[p].size;
                if (!size)
                        continue;
+
+               from = state->parts[p].from;
+               if (from >= get_capacity(disk)) {
+                       printk(KERN_WARNING
+                              "%s: p%d ignored, start %llu is behind the end of the disk\n",
+                              disk->disk_name, p, (unsigned long long) from);
+                       continue;
+               }
+
                if (from + size > get_capacity(disk)) {
+                       const struct block_device_operations *bdops = disk->fops;
+                       unsigned long long capacity;
+
                        printk(KERN_WARNING
-                               "%s: p%d exceeds device capacity\n",
-                               disk->disk_name, p);
+                              "%s: p%d size %llu exceeds device capacity, ",
+                              disk->disk_name, p, (unsigned long long) size);
+
+                       if (bdops->set_capacity &&
+                           (disk->flags & GENHD_FL_NATIVE_CAPACITY) == 0) {
+                               printk(KERN_CONT "enabling native capacity\n");
+                               capacity = bdops->set_capacity(disk, ~0ULL);
+                               disk->flags |= GENHD_FL_NATIVE_CAPACITY;
+                               if (capacity > get_capacity(disk)) {
+                                       set_capacity(disk, capacity);
+                                       check_disk_size_change(disk, bdev);
+                                       bdev->bd_invalidated = 0;
+                               }
+                               goto try_scan;
+                       } else {
+                               /*
+                                * we can not ignore partitions of broken tables
+                                * created by for example camera firmware, but
+                                * we limit them to the end of the disk to avoid
+                                * creating invalid block devices
+                                */
+                               printk(KERN_CONT "limited to end of disk\n");
+                               size = get_capacity(disk) - from;
+                       }
                }
-               res = add_partition(disk, p, from, size, state->parts[p].flags);
-               if (res) {
-                       printk(KERN_ERR " %s: p%d could not be added: %d\n",
-                               disk->disk_name, p, -res);
+               part = add_partition(disk, p, from, size,
+                                    state->parts[p].flags);
+               if (IS_ERR(part)) {
+                       printk(KERN_ERR " %s: p%d could not be added: %ld\n",
+                              disk->disk_name, p, -PTR_ERR(part));
                        continue;
                }
 #ifdef CONFIG_BLK_DEV_MD
                if (state->parts[p].flags & ADDPART_FLAG_RAID)
-                       md_autodetect_dev(bdev->bd_dev+p);
+                       md_autodetect_dev(part_to_dev(part)->devt);
 #endif
        }
        kfree(state);
@@ -541,25 +649,31 @@ EXPORT_SYMBOL(read_dev_sector);
 
 void del_gendisk(struct gendisk *disk)
 {
-       int p;
+       struct disk_part_iter piter;
+       struct hd_struct *part;
 
        /* invalidate stuff */
-       for (p = disk->minors - 1; p > 0; p--) {
-               invalidate_partition(disk, p);
-               delete_partition(disk, p);
+       disk_part_iter_init(&piter, disk,
+                            DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
+       while ((part = disk_part_iter_next(&piter))) {
+               invalidate_partition(disk, part->partno);
+               delete_partition(disk, part->partno);
        }
+       disk_part_iter_exit(&piter);
+
        invalidate_partition(disk, 0);
-       disk->capacity = 0;
+       blk_free_devt(disk_to_dev(disk)->devt);
+       set_capacity(disk, 0);
        disk->flags &= ~GENHD_FL_UP;
        unlink_gendisk(disk);
-       disk_stat_set_all(disk, 0);
-       disk->stamp = 0;
+       part_stat_set_all(&disk->part0, 0);
+       disk->part0.stamp = 0;
 
-       kobject_put(disk->holder_dir);
+       kobject_put(disk->part0.holder_dir);
        kobject_put(disk->slave_dir);
        disk->driverfs_dev = NULL;
 #ifndef CONFIG_SYSFS_DEPRECATED
-       sysfs_remove_link(block_depr, disk->dev.bus_id);
+       sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
 #endif
-       device_del(&disk->dev);
+       device_del(disk_to_dev(disk));
 }