const: make block_device_operations const
[linux-3.10.git] / fs / partitions / check.c
index 9a3a058..7b685e1 100644 (file)
@@ -18,6 +18,8 @@
 #include <linux/fs.h>
 #include <linux/kmod.h>
 #include <linux/ctype.h>
+#include <linux/genhd.h>
+#include <linux/blktrace_api.h>
 
 #include "check.h"
 
@@ -119,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);
@@ -168,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));
@@ -195,96 +196,85 @@ check_partition(struct gendisk *hd, struct block_device *bdev)
        return ERR_PTR(res);
 }
 
-/*
- * sysfs bindings for partitions
- */
-
-struct part_attribute {
-       struct attribute attr;
-       ssize_t (*show)(struct hd_struct *,char *);
-       ssize_t (*store)(struct hd_struct *,const char *, size_t);
-};
-
-static ssize_t 
-part_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
+static ssize_t part_partition_show(struct device *dev,
+                                  struct device_attribute *attr, char *buf)
 {
-       struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
-       struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr);
-       ssize_t ret = 0;
-       if (part_attr->show)
-               ret = part_attr->show(p, page);
-       return ret;
-}
-static ssize_t
-part_attr_store(struct kobject * kobj, struct attribute * attr,
-               const char *page, size_t count)
-{
-       struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
-       struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr);
-       ssize_t ret = 0;
+       struct hd_struct *p = dev_to_part(dev);
 
-       if (part_attr->store)
-               ret = part_attr->store(p, page, count);
-       return ret;
+       return sprintf(buf, "%d\n", p->partno);
 }
 
-static struct sysfs_ops part_sysfs_ops = {
-       .show   =       part_attr_show,
-       .store  =       part_attr_store,
-};
-
-static ssize_t part_uevent_store(struct hd_struct * p,
-                                const char *page, size_t count)
+static ssize_t part_start_show(struct device *dev,
+                              struct device_attribute *attr, char *buf)
 {
-       kobject_uevent(&p->kobj, KOBJ_ADD);
-       return count;
+       struct hd_struct *p = dev_to_part(dev);
+
+       return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect);
 }
-static ssize_t part_dev_read(struct hd_struct * p, char *page)
+
+ssize_t part_size_show(struct device *dev,
+                      struct device_attribute *attr, char *buf)
 {
-       struct gendisk *disk = container_of(p->kobj.parent,struct gendisk,kobj);
-       dev_t dev = MKDEV(disk->major, disk->first_minor + p->partno); 
-       return print_dev_t(page, dev);
+       struct hd_struct *p = dev_to_part(dev);
+       return sprintf(buf, "%llu\n",(unsigned long long)p->nr_sects);
 }
-static ssize_t part_start_read(struct hd_struct * p, char *page)
+
+ssize_t part_alignment_offset_show(struct device *dev,
+                                  struct device_attribute *attr, char *buf)
 {
-       return sprintf(page, "%llu\n",(unsigned long long)p->start_sect);
+       struct hd_struct *p = dev_to_part(dev);
+       return sprintf(buf, "%llu\n", (unsigned long long)p->alignment_offset);
 }
-static ssize_t part_size_read(struct hd_struct * p, char *page)
+
+ssize_t part_stat_show(struct device *dev,
+                      struct device_attribute *attr, char *buf)
 {
-       return sprintf(page, "%llu\n",(unsigned long long)p->nr_sects);
+       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 "
+               "%8u %8u %8u"
+               "\n",
+               part_stat_read(p, ios[READ]),
+               part_stat_read(p, merges[READ]),
+               (unsigned long long)part_stat_read(p, sectors[READ]),
+               jiffies_to_msecs(part_stat_read(p, ticks[READ])),
+               part_stat_read(p, ios[WRITE]),
+               part_stat_read(p, merges[WRITE]),
+               (unsigned long long)part_stat_read(p, sectors[WRITE]),
+               jiffies_to_msecs(part_stat_read(p, ticks[WRITE])),
+               part_in_flight(p),
+               jiffies_to_msecs(part_stat_read(p, io_ticks)),
+               jiffies_to_msecs(part_stat_read(p, time_in_queue)));
 }
-static ssize_t part_stat_read(struct hd_struct * p, char *page)
+
+ssize_t part_inflight_show(struct device *dev,
+                       struct device_attribute *attr, char *buf)
 {
-       return sprintf(page, "%8u %8llu %8u %8llu\n",
-                      p->ios[0], (unsigned long long)p->sectors[0],
-                      p->ios[1], (unsigned long long)p->sectors[1]);
+       struct hd_struct *p = dev_to_part(dev);
+
+       return sprintf(buf, "%8u %8u\n", p->in_flight[0], p->in_flight[1]);
 }
-static struct part_attribute part_attr_uevent = {
-       .attr = {.name = "uevent", .mode = S_IWUSR },
-       .store  = part_uevent_store
-};
-static struct part_attribute part_attr_dev = {
-       .attr = {.name = "dev", .mode = S_IRUGO },
-       .show   = part_dev_read
-};
-static struct part_attribute part_attr_start = {
-       .attr = {.name = "start", .mode = S_IRUGO },
-       .show   = part_start_read
-};
-static struct part_attribute part_attr_size = {
-       .attr = {.name = "size", .mode = S_IRUGO },
-       .show   = part_size_read
-};
-static struct part_attribute part_attr_stat = {
-       .attr = {.name = "stat", .mode = S_IRUGO },
-       .show   = part_stat_read
-};
 
 #ifdef CONFIG_FAIL_MAKE_REQUEST
+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 hd_struct * p,
-                              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;
 
        if (count > 0 && sscanf(buf, "%d", &i) > 0)
@@ -292,209 +282,220 @@ static ssize_t part_fail_store(struct hd_struct * p,
 
        return count;
 }
-static ssize_t part_fail_read(struct hd_struct * p, char *page)
-{
-       return sprintf(page, "%d\n", p->make_it_fail);
-}
-static struct part_attribute part_attr_fail = {
-       .attr = {.name = "make-it-fail", .mode = S_IRUGO | S_IWUSR },
-       .store  = part_fail_store,
-       .show   = part_fail_read
-};
+#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 * default_attrs[] = {
-       &part_attr_uevent.attr,
-       &part_attr_dev.attr,
-       &part_attr_start.attr,
-       &part_attr_size.attr,
-       &part_attr_stat.attr,
+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
-       &part_attr_fail.attr,
+       &dev_attr_fail.attr,
 #endif
-       NULL,
+       NULL
 };
 
-extern struct kset block_subsys;
+static struct attribute_group part_attr_group = {
+       .attrs = part_attrs,
+};
 
-static void part_release(struct kobject *kobj)
+static const struct attribute_group *part_attr_groups[] = {
+       &part_attr_group,
+#ifdef CONFIG_BLK_DEV_IO_TRACE
+       &blk_trace_attr_group,
+#endif
+       NULL
+};
+
+static void part_release(struct device *dev)
 {
-       struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
+       struct hd_struct *p = dev_to_part(dev);
+       free_part_stats(p);
        kfree(p);
 }
 
-struct kobj_type ktype_part = {
+struct device_type part_type = {
+       .name           = "partition",
+       .groups         = part_attr_groups,
        .release        = part_release,
-       .default_attrs  = default_attrs,
-       .sysfs_ops      = &part_sysfs_ops,
 };
 
-static inline void partition_sysfs_add_subdir(struct hd_struct *p)
+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(&p->kobj);
-       p->holder_dir = kobject_add_dir(k, "holders");
-       kobject_put(k);
+       part->start_sect = 0;
+       part->nr_sects = 0;
+       part_stat_set_all(part, 0);
+       put_device(part_to_dev(part));
 }
 
-static inline void disk_sysfs_add_subdirs(struct gendisk *disk)
+void delete_partition(struct gendisk *disk, int partno)
 {
-       struct kobject *k;
+       struct disk_part_tbl *ptbl = disk->part_tbl;
+       struct hd_struct *part;
+
+       if (partno >= ptbl->len)
+               return;
 
-       k = kobject_get(&disk->kobj);
-       disk->holder_dir = kobject_add_dir(k, "holders");
-       disk->slave_dir = kobject_add_dir(k, "slaves");
-       kobject_put(k);
+       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);
 }
 
-void delete_partition(struct gendisk *disk, int part)
+static ssize_t whole_disk_show(struct device *dev,
+                              struct device_attribute *attr, char *buf)
 {
-       struct hd_struct *p = disk->part[part-1];
-       if (!p)
-               return;
-       if (!p->nr_sects)
-               return;
-       disk->part[part-1] = NULL;
-       p->start_sect = 0;
-       p->nr_sects = 0;
-       p->ios[0] = p->ios[1] = 0;
-       p->sectors[0] = p->sectors[1] = 0;
-       sysfs_remove_link(&p->kobj, "subsystem");
-       kobject_unregister(p->holder_dir);
-       kobject_uevent(&p->kobj, KOBJ_REMOVE);
-       kobject_del(&p->kobj);
-       kobject_put(&p->kobj);
+       return 0;
 }
+static DEVICE_ATTR(whole_disk, S_IRUSR | S_IRGRP | S_IROTH,
+                  whole_disk_show, NULL);
 
-void 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;
+
+       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 = kmalloc(sizeof(*p), GFP_KERNEL);
+       p = kzalloc(sizeof(*p), GFP_KERNEL);
        if (!p)
-               return;
-       
-       memset(p, 0, sizeof(*p));
+               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->kobj.name[strlen(disk->kobj.name)-1]))
-               snprintf(p->kobj.name,KOBJ_NAME_LEN,"%sp%d",disk->kobj.name,part);
+       dname = dev_name(ddev);
+       if (isdigit(dname[strlen(dname) - 1]))
+               dev_set_name(pdev, "%sp%d", dname, partno);
        else
-               snprintf(p->kobj.name,KOBJ_NAME_LEN,"%s%d",disk->kobj.name,part);
-       p->kobj.parent = &disk->kobj;
-       p->kobj.ktype = &ktype_part;
-       kobject_init(&p->kobj);
-       kobject_add(&p->kobj);
-       if (!disk->part_uevent_suppress)
-               kobject_uevent(&p->kobj, KOBJ_ADD);
-       sysfs_create_link(&p->kobj, &block_subsys.kobj, "subsystem");
-       if (flags & ADDPART_FLAG_WHOLEDISK) {
-               static struct attribute addpartattr = {
-                       .name = "whole_disk",
-                       .mode = S_IRUSR | S_IRGRP | S_IROTH,
-                       .owner = THIS_MODULE,
-               };
+               dev_set_name(pdev, "%s%d", dname, partno);
 
-               sysfs_create_file(&p->kobj, &addpartattr);
-       }
-       partition_sysfs_add_subdir(p);
-       disk->part[part-1] = p;
-}
+       device_initialize(pdev);
+       pdev->class = &block_class;
+       pdev->type = &part_type;
+       pdev->parent = ddev;
 
-static char *make_block_name(struct gendisk *disk)
-{
-       char *name;
-       static char *block_str = "block:";
-       int size;
-       char *s;
-
-       size = strlen(block_str) + strlen(disk->disk_name) + 1;
-       name = kmalloc(size, GFP_KERNEL);
-       if (!name)
-               return NULL;
-       strcpy(name, block_str);
-       strcat(name, disk->disk_name);
-       /* ewww... some of these buggers have / in name... */
-       s = strchr(name, '/');
-       if (s)
-               *s = '!';
-       return name;
-}
-
-static int disk_sysfs_symlinks(struct gendisk *disk)
-{
-       struct device *target = get_device(disk->driverfs_dev);
-       int err;
-       char *disk_name = NULL;
+       err = blk_alloc_devt(p, &devt);
+       if (err)
+               goto out_free_stats;
+       pdev->devt = devt;
 
-       if (target) {
-               disk_name = make_block_name(disk);
-               if (!disk_name) {
-                       err = -ENOMEM;
-                       goto err_out;
-               }
+       /* delay uevent until 'holders' subdir is created */
+       dev_set_uevent_suppress(pdev, 1);
+       err = device_add(pdev);
+       if (err)
+               goto out_put;
 
-               err = sysfs_create_link(&disk->kobj, &target->kobj, "device");
-               if (err)
-                       goto err_out_disk_name;
+       err = -ENOMEM;
+       p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
+       if (!p->holder_dir)
+               goto out_del;
 
-               err = sysfs_create_link(&target->kobj, &disk->kobj, disk_name);
+       dev_set_uevent_suppress(pdev, 0);
+       if (flags & ADDPART_FLAG_WHOLEDISK) {
+               err = device_create_file(pdev, &dev_attr_whole_disk);
                if (err)
-                       goto err_out_dev_link;
+                       goto out_del;
        }
 
-       err = sysfs_create_link(&disk->kobj, &block_subsys.kobj,
-                               "subsystem");
-       if (err)
-               goto err_out_disk_name_lnk;
+       /* everything is up and running, commence */
+       INIT_RCU_HEAD(&p->rcu_head);
+       rcu_assign_pointer(ptbl->part[partno], p);
 
-       kfree(disk_name);
+       /* suppress uevent if the disk supresses it */
+       if (!dev_get_uevent_suppress(ddev))
+               kobject_uevent(&pdev->kobj, KOBJ_ADD);
 
-       return 0;
+       return p;
 
-err_out_disk_name_lnk:
-       if (target) {
-               sysfs_remove_link(&target->kobj, disk_name);
-err_out_dev_link:
-               sysfs_remove_link(&disk->kobj, "device");
-err_out_disk_name:
-               kfree(disk_name);
-err_out:
-               put_device(target);
-       }
-       return err;
+out_free_stats:
+       free_part_stats(p);
+out_free:
+       kfree(p);
+       return ERR_PTR(err);
+out_del:
+       kobject_put(p->holder_dir);
+       device_del(pdev);
+out_put:
+       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;
 
-       strlcpy(disk->kobj.name,disk->disk_name,KOBJ_NAME_LEN);
-       /* ewww... some of these buggers have / in name... */
-       s = strchr(disk->kobj.name, '/');
-       if (s)
-               *s = '!';
-       if ((err = kobject_add(&disk->kobj)))
+       ddev->parent = disk->driverfs_dev;
+
+       dev_set_name(ddev, disk->disk_name);
+
+       /* delay uevents, until we scanned partition table */
+       dev_set_uevent_suppress(ddev, 1);
+
+       if (device_add(ddev))
                return;
-       err = disk_sysfs_symlinks(disk);
+#ifndef CONFIG_SYSFS_DEPRECATED
+       err = sysfs_create_link(block_depr, &ddev->kobj,
+                               kobject_name(&ddev->kobj));
        if (err) {
-               kobject_del(&disk->kobj);
+               device_del(ddev);
                return;
        }
-       disk_sysfs_add_subdirs(disk);
+#endif
+       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) */
@@ -505,60 +506,120 @@ void register_disk(struct gendisk *disk)
        if (!bdev)
                goto exit;
 
-       /* scan partition table, but suppress uevents */
        bdev->bd_invalidated = 1;
-       disk->part_uevent_suppress = 1;
-       err = blkdev_get(bdev, FMODE_READ, 0);
-       disk->part_uevent_suppress = 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 already created */
-       kobject_uevent(&disk->kobj, KOBJ_ADD);
+       /* announce disk after possible partitions are created */
+       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->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_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)) {
-                       printk(" %s: p%d exceeds device capacity\n",
-                               disk->disk_name, p);
+                       const struct block_device_operations *bdops = disk->fops;
+                       unsigned long long capacity;
+
+                       printk(KERN_WARNING
+                              "%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;
+                       }
+               }
+               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;
                }
-               add_partition(disk, p, from, size, state->parts[p].flags);
 #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);
@@ -588,33 +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;
-
-       kobject_uevent(&disk->kobj, KOBJ_REMOVE);
-       kobject_unregister(disk->holder_dir);
-       kobject_unregister(disk->slave_dir);
-       if (disk->driverfs_dev) {
-               char *disk_name = make_block_name(disk);
-               sysfs_remove_link(&disk->kobj, "device");
-               if (disk_name) {
-                       sysfs_remove_link(&disk->driverfs_dev->kobj, disk_name);
-                       kfree(disk_name);
-               }
-               put_device(disk->driverfs_dev);
-               disk->driverfs_dev = NULL;
-       }
-       sysfs_remove_link(&disk->kobj, "subsystem");
-       kobject_del(&disk->kobj);
+       part_stat_set_all(&disk->part0, 0);
+       disk->part0.stamp = 0;
+
+       kobject_put(disk->part0.holder_dir);
+       kobject_put(disk->slave_dir);
+       disk->driverfs_dev = NULL;
+#ifndef CONFIG_SYSFS_DEPRECATED
+       sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
+#endif
+       device_del(disk_to_dev(disk));
 }