sh: convert /proc/cpu/aligmnent, /proc/cpu/kernel_alignment to seq_file
[safe/jmp/linux-2.6] / block / genhd.c
index 32ee73c..517e433 100644 (file)
@@ -10,6 +10,7 @@
 #include <linux/blkdev.h>
 #include <linux/init.h>
 #include <linux/spinlock.h>
+#include <linux/proc_fs.h>
 #include <linux/seq_file.h>
 #include <linux/slab.h>
 #include <linux/kmod.h>
@@ -97,7 +98,7 @@ void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
 
        if (flags & DISK_PITER_REVERSE)
                piter->idx = ptbl->len - 1;
-       else if (flags & DISK_PITER_INCL_PART0)
+       else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
                piter->idx = 0;
        else
                piter->idx = 1;
@@ -133,7 +134,8 @@ struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
        /* determine iteration parameters */
        if (piter->flags & DISK_PITER_REVERSE) {
                inc = -1;
-               if (piter->flags & DISK_PITER_INCL_PART0)
+               if (piter->flags & (DISK_PITER_INCL_PART0 |
+                                   DISK_PITER_INCL_EMPTY_PART0))
                        end = -1;
                else
                        end = 0;
@@ -149,7 +151,10 @@ struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
                part = rcu_dereference(ptbl->part[piter->idx]);
                if (!part)
                        continue;
-               if (!(piter->flags & DISK_PITER_INCL_EMPTY) && !part->nr_sects)
+               if (!part->nr_sects &&
+                   !(piter->flags & DISK_PITER_INCL_EMPTY) &&
+                   !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
+                     piter->idx == 0))
                        continue;
 
                get_device(part_to_dev(part));
@@ -180,6 +185,12 @@ void disk_part_iter_exit(struct disk_part_iter *piter)
 }
 EXPORT_SYMBOL_GPL(disk_part_iter_exit);
 
+static inline int sector_in_part(struct hd_struct *part, sector_t sector)
+{
+       return part->start_sect <= sector &&
+               sector < part->start_sect + part->nr_sects;
+}
+
 /**
  * disk_map_sector_rcu - map sector to partition
  * @disk: gendisk of interest
@@ -198,16 +209,22 @@ EXPORT_SYMBOL_GPL(disk_part_iter_exit);
 struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
 {
        struct disk_part_tbl *ptbl;
+       struct hd_struct *part;
        int i;
 
        ptbl = rcu_dereference(disk->part_tbl);
 
+       part = rcu_dereference(ptbl->last_lookup);
+       if (part && sector_in_part(part, sector))
+               return part;
+
        for (i = 1; i < ptbl->len; i++) {
-               struct hd_struct *part = rcu_dereference(ptbl->part[i]);
+               part = rcu_dereference(ptbl->part[i]);
 
-               if (part && part->start_sect <= sector &&
-                   sector < part->start_sect + part->nr_sects)
+               if (part && sector_in_part(part, sector)) {
+                       rcu_assign_pointer(ptbl->last_lookup, part);
                        return part;
+               }
        }
        return &disk->part0;
 }
@@ -243,6 +260,22 @@ void blkdev_show(struct seq_file *seqf, off_t offset)
 }
 #endif /* CONFIG_PROC_FS */
 
+/**
+ * register_blkdev - register a new block device
+ *
+ * @major: the requested major device number [1..255]. If @major=0, try to
+ *         allocate any unused major number.
+ * @name: the name of the new block device as a zero terminated string
+ *
+ * The @name must be unique within the system.
+ *
+ * The return value depends on the @major input parameter.
+ *  - if a major device number was requested in range [1..255] then the
+ *    function returns zero on success, or a negative error code
+ *  - if any unused major number was requested with @major=0 parameter
+ *    then the return value is the allocated major number in range
+ *    [1..255] or a negative error code otherwise
+ */
 int register_blkdev(unsigned int major, const char *name)
 {
        struct blk_major_name **n, *p;
@@ -358,7 +391,6 @@ static int blk_mangle_minor(int minor)
 /**
  * blk_alloc_devt - allocate a dev_t for a partition
  * @part: partition to allocate dev_t for
- * @gfp_mask: memory allocation flag
  * @devt: out parameter for resulting dev_t
  *
  * Allocate a dev_t for block device.
@@ -535,7 +567,7 @@ void unlink_gendisk(struct gendisk *disk)
 /**
  * get_gendisk - get partitioning information for a given device
  * @devt: device to get partitioning information for
- * @part: returned partition index
+ * @partno: returned partition index
  *
  * This function gets the structure containing partitioning
  * information for the given device @devt.
@@ -584,7 +616,7 @@ struct block_device *bdget_disk(struct gendisk *disk, int partno)
        struct block_device *bdev = NULL;
 
        part = disk_get_part(disk, partno);
-       if (part && (part->nr_sects || partno == 0))
+       if (part)
                bdev = bdget(part_devt(part));
        disk_put_part(part);
 
@@ -697,7 +729,7 @@ static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
        static void *p;
 
        p = disk_seqf_start(seqf, pos);
-       if (!IS_ERR(p) && p)
+       if (!IS_ERR(p) && p && !*pos)
                seq_puts(seqf, "major minor  #blocks  name\n\n");
        return p;
 }
@@ -728,12 +760,24 @@ static int show_partition(struct seq_file *seqf, void *v)
        return 0;
 }
 
-const struct seq_operations partitions_op = {
+static const struct seq_operations partitions_op = {
        .start  = show_partition_start,
        .next   = disk_seqf_next,
        .stop   = disk_seqf_stop,
        .show   = show_partition
 };
+
+static int partitions_open(struct inode *inode, struct file *file)
+{
+       return seq_open(file, &partitions_op);
+}
+
+static const struct file_operations proc_partitions_operations = {
+       .open           = partitions_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = seq_release,
+};
 #endif
 
 
@@ -756,6 +800,8 @@ static int __init genhd_device_init(void)
        bdev_map = kobj_map_init(base_probe, &block_class_lock);
        blk_dev_init();
 
+       register_blkdev(BLOCK_EXT_MAJOR, "blkext");
+
 #ifndef CONFIG_SYSFS_DEPRECATED
        /* create top-level block dir */
        block_depr = kobject_create_and_add("block", NULL);
@@ -806,17 +852,33 @@ static ssize_t disk_capability_show(struct device *dev,
        return sprintf(buf, "%x\n", disk->flags);
 }
 
+static ssize_t disk_alignment_offset_show(struct device *dev,
+                                         struct device_attribute *attr,
+                                         char *buf)
+{
+       struct gendisk *disk = dev_to_disk(dev);
+
+       return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
+}
+
 static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
 static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
 static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
 static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
 static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
+static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL);
 static DEVICE_ATTR(capability, S_IRUGO, disk_capability_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
+#ifdef CONFIG_FAIL_IO_TIMEOUT
+static struct device_attribute dev_attr_fail_timeout =
+       __ATTR(io-timeout-fail,  S_IRUGO|S_IWUSR, part_timeout_show,
+               part_timeout_store);
+#endif
 
 static struct attribute *disk_attrs[] = {
        &dev_attr_range.attr,
@@ -824,11 +886,16 @@ static struct attribute *disk_attrs[] = {
        &dev_attr_removable.attr,
        &dev_attr_ro.attr,
        &dev_attr_size.attr,
+       &dev_attr_alignment_offset.attr,
        &dev_attr_capability.attr,
        &dev_attr_stat.attr,
+       &dev_attr_inflight.attr,
 #ifdef CONFIG_FAIL_MAKE_REQUEST
        &dev_attr_fail.attr,
 #endif
+#ifdef CONFIG_FAIL_IO_TIMEOUT
+       &dev_attr_fail_timeout.attr,
+#endif
        NULL
 };
 
@@ -836,7 +903,7 @@ static struct attribute_group disk_attr_group = {
        .attrs = disk_attrs,
 };
 
-static struct attribute_group *disk_attr_groups[] = {
+static const struct attribute_group *disk_attr_groups[] = {
        &disk_attr_group,
        NULL
 };
@@ -866,8 +933,11 @@ static void disk_replace_part_tbl(struct gendisk *disk,
        struct disk_part_tbl *old_ptbl = disk->part_tbl;
 
        rcu_assign_pointer(disk->part_tbl, new_ptbl);
-       if (old_ptbl)
+
+       if (old_ptbl) {
+               rcu_assign_pointer(old_ptbl->last_lookup, NULL);
                call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
+       }
 }
 
 /**
@@ -928,10 +998,20 @@ struct class block_class = {
        .name           = "block",
 };
 
+static char *block_devnode(struct device *dev, mode_t *mode)
+{
+       struct gendisk *disk = dev_to_disk(dev);
+
+       if (disk->devnode)
+               return disk->devnode(disk, mode);
+       return NULL;
+}
+
 static struct device_type disk_type = {
        .name           = "disk",
        .groups         = disk_attr_groups,
        .release        = disk_release,
+       .devnode        = block_devnode,
 };
 
 #ifdef CONFIG_PROC_FS
@@ -958,7 +1038,7 @@ static int diskstats_show(struct seq_file *seqf, void *v)
                                "\n\n");
        */
  
-       disk_part_iter_init(&piter, gp, DISK_PITER_INCL_PART0);
+       disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
        while ((hd = disk_part_iter_next(&piter))) {
                cpu = part_stat_lock();
                part_round_stats(cpu, hd);
@@ -975,7 +1055,7 @@ static int diskstats_show(struct seq_file *seqf, void *v)
                           part_stat_read(hd, merges[1]),
                           (unsigned long long)part_stat_read(hd, sectors[1]),
                           jiffies_to_msecs(part_stat_read(hd, ticks[1])),
-                          hd->in_flight,
+                          part_in_flight(hd),
                           jiffies_to_msecs(part_stat_read(hd, io_ticks)),
                           jiffies_to_msecs(part_stat_read(hd, time_in_queue))
                        );
@@ -985,12 +1065,32 @@ static int diskstats_show(struct seq_file *seqf, void *v)
        return 0;
 }
 
-const struct seq_operations diskstats_op = {
+static const struct seq_operations diskstats_op = {
        .start  = disk_seqf_start,
        .next   = disk_seqf_next,
        .stop   = disk_seqf_stop,
        .show   = diskstats_show
 };
+
+static int diskstats_open(struct inode *inode, struct file *file)
+{
+       return seq_open(file, &diskstats_op);
+}
+
+static const struct file_operations proc_diskstats_operations = {
+       .open           = diskstats_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = seq_release,
+};
+
+static int __init proc_genhd_init(void)
+{
+       proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
+       proc_create("partitions", 0, NULL, &proc_partitions_operations);
+       return 0;
+}
+module_init(proc_genhd_init);
 #endif /* CONFIG_PROC_FS */
 
 static void media_change_notify_thread(struct work_struct *work)
@@ -1027,11 +1127,19 @@ dev_t blk_lookup_devt(const char *name, int partno)
                struct gendisk *disk = dev_to_disk(dev);
                struct hd_struct *part;
 
-               if (strcmp(dev->bus_id, name))
+               if (strcmp(dev_name(dev), name))
                        continue;
 
+               if (partno < disk->minors) {
+                       /* We need to return the right devno, even
+                        * if the partition doesn't exist yet.
+                        */
+                       devt = MKDEV(MAJOR(dev->devt),
+                                    MINOR(dev->devt) + partno);
+                       break;
+               }
                part = disk_get_part(disk, partno);
-               if (part && (part->nr_sects || partno == 0)) {
+               if (part) {
                        devt = part_devt(part);
                        disk_put_part(part);
                        break;
@@ -1060,6 +1168,7 @@ struct gendisk *alloc_disk_node(int minors, int node_id)
                        kfree(disk);
                        return NULL;
                }
+               disk->node_id = node_id;
                if (disk_expand_part_tbl(disk, 0)) {
                        free_part_stats(&disk->part0);
                        kfree(disk);
@@ -1074,7 +1183,6 @@ struct gendisk *alloc_disk_node(int minors, int node_id)
                device_initialize(disk_to_dev(disk));
                INIT_WORK(&disk->async_notify,
                        media_change_notify_thread);
-               disk->node_id = node_id;
        }
        return disk;
 }
@@ -1109,6 +1217,16 @@ void put_disk(struct gendisk *disk)
 
 EXPORT_SYMBOL(put_disk);
 
+static void set_disk_ro_uevent(struct gendisk *gd, int ro)
+{
+       char event[] = "DISK_RO=1";
+       char *envp[] = { event, NULL };
+
+       if (!ro)
+               event[8] = '0';
+       kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
+}
+
 void set_device_ro(struct block_device *bdev, int flag)
 {
        bdev->bd_part->policy = flag;
@@ -1121,8 +1239,12 @@ void set_disk_ro(struct gendisk *disk, int flag)
        struct disk_part_iter piter;
        struct hd_struct *part;
 
-       disk_part_iter_init(&piter, disk,
-                           DISK_PITER_INCL_EMPTY | DISK_PITER_INCL_PART0);
+       if (disk->part0.policy != flag) {
+               set_disk_ro_uevent(disk, flag);
+               disk->part0.policy = flag;
+       }
+
+       disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
        while ((part = disk_part_iter_next(&piter)))
                part->policy = flag;
        disk_part_iter_exit(&piter);