upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / block / genhd.c
1 /*
2  *  gendisk handling
3  */
4
5 #include <linux/config.h>
6 #include <linux/module.h>
7 #include <linux/fs.h>
8 #include <linux/genhd.h>
9 #include <linux/kernel.h>
10 #include <linux/blkdev.h>
11 #include <linux/init.h>
12 #include <linux/spinlock.h>
13 #include <linux/seq_file.h>
14 #include <linux/slab.h>
15 #include <linux/kmod.h>
16 #include <linux/kobj_map.h>
17
18 #define MAX_PROBE_HASH 255      /* random */
19
20 static struct subsystem block_subsys;
21
22 /*
23  * Can be deleted altogether. Later.
24  *
25  * Modified under both block_subsys.rwsem and major_names_lock.
26  */
27 static struct blk_major_name {
28         struct blk_major_name *next;
29         int major;
30         char name[16];
31 } *major_names[MAX_PROBE_HASH];
32
33 static spinlock_t major_names_lock = SPIN_LOCK_UNLOCKED;
34
35 /* index in the above - for now: assume no multimajor ranges */
36 static inline int major_to_index(int major)
37 {
38         return major % MAX_PROBE_HASH;
39 }
40
41 /* get block device names in somewhat random order */
42 int get_blkdev_list(char *p)
43 {
44         struct blk_major_name *n;
45         int i, len;
46
47         len = sprintf(p, "\nBlock devices:\n");
48
49         down_read(&block_subsys.rwsem);
50         for (i = 0; i < ARRAY_SIZE(major_names); i++) {
51                 for (n = major_names[i]; n; n = n->next)
52                         len += sprintf(p+len, "%3d %s\n",
53                                        n->major, n->name);
54         }
55         up_read(&block_subsys.rwsem);
56
57         return len;
58 }
59
60 int register_blkdev(unsigned int major, const char *name)
61 {
62         struct blk_major_name **n, *p;
63         int index, ret = 0;
64         unsigned long flags;
65
66         down_write(&block_subsys.rwsem);
67
68         /* temporary */
69         if (major == 0) {
70                 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
71                         if (major_names[index] == NULL)
72                                 break;
73                 }
74
75                 if (index == 0) {
76                         printk("register_blkdev: failed to get major for %s\n",
77                                name);
78                         ret = -EBUSY;
79                         goto out;
80                 }
81                 major = index;
82                 ret = major;
83         }
84
85         p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
86         if (p == NULL) {
87                 ret = -ENOMEM;
88                 goto out;
89         }
90
91         p->major = major;
92         strlcpy(p->name, name, sizeof(p->name));
93         p->next = NULL;
94         index = major_to_index(major);
95
96         spin_lock_irqsave(&major_names_lock, flags);
97         for (n = &major_names[index]; *n; n = &(*n)->next) {
98                 if ((*n)->major == major)
99                         break;
100         }
101         if (!*n)
102                 *n = p;
103         else
104                 ret = -EBUSY;
105         spin_unlock_irqrestore(&major_names_lock, flags);
106
107         if (ret < 0) {
108                 printk("register_blkdev: cannot get major %d for %s\n",
109                        major, name);
110                 kfree(p);
111         }
112 out:
113         up_write(&block_subsys.rwsem);
114         return ret;
115 }
116
117 EXPORT_SYMBOL(register_blkdev);
118
119 /* todo: make void - error printk here */
120 int unregister_blkdev(unsigned int major, const char *name)
121 {
122         struct blk_major_name **n;
123         struct blk_major_name *p = NULL;
124         int index = major_to_index(major);
125         unsigned long flags;
126         int ret = 0;
127
128         down_write(&block_subsys.rwsem);
129         spin_lock_irqsave(&major_names_lock, flags);
130         for (n = &major_names[index]; *n; n = &(*n)->next)
131                 if ((*n)->major == major)
132                         break;
133         if (!*n || strcmp((*n)->name, name))
134                 ret = -EINVAL;
135         else {
136                 p = *n;
137                 *n = p->next;
138         }
139         spin_unlock_irqrestore(&major_names_lock, flags);
140         up_write(&block_subsys.rwsem);
141         kfree(p);
142
143         return ret;
144 }
145
146 EXPORT_SYMBOL(unregister_blkdev);
147
148 static struct kobj_map *bdev_map;
149
150 /*
151  * Register device numbers dev..(dev+range-1)
152  * range must be nonzero
153  * The hash chain is sorted on range, so that subranges can override.
154  */
155 void blk_register_region(dev_t dev, unsigned long range, struct module *module,
156                          struct kobject *(*probe)(dev_t, int *, void *),
157                          int (*lock)(dev_t, void *), void *data)
158 {
159         kobj_map(bdev_map, dev, range, module, probe, lock, data);
160 }
161
162 EXPORT_SYMBOL(blk_register_region);
163
164 void blk_unregister_region(dev_t dev, unsigned long range)
165 {
166         kobj_unmap(bdev_map, dev, range);
167 }
168
169 EXPORT_SYMBOL(blk_unregister_region);
170
171 static struct kobject *exact_match(dev_t dev, int *part, void *data)
172 {
173         struct gendisk *p = data;
174         return &p->kobj;
175 }
176
177 static int exact_lock(dev_t dev, void *data)
178 {
179         struct gendisk *p = data;
180
181         if (!get_disk(p))
182                 return -1;
183         return 0;
184 }
185
186 /**
187  * add_disk - add partitioning information to kernel list
188  * @disk: per-device partitioning information
189  *
190  * This function registers the partitioning information in @disk
191  * with the kernel.
192  */
193 void add_disk(struct gendisk *disk)
194 {
195         disk->flags |= GENHD_FL_UP;
196         blk_register_region(MKDEV(disk->major, disk->first_minor),
197                             disk->minors, NULL, exact_match, exact_lock, disk);
198         register_disk(disk);
199         blk_register_queue(disk);
200 }
201
202 EXPORT_SYMBOL(add_disk);
203 EXPORT_SYMBOL(del_gendisk);     /* in partitions/check.c */
204
205 void unlink_gendisk(struct gendisk *disk)
206 {
207         blk_unregister_queue(disk);
208         blk_unregister_region(MKDEV(disk->major, disk->first_minor),
209                               disk->minors);
210 }
211
212 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
213
214 /**
215  * get_gendisk - get partitioning information for a given device
216  * @dev: device to get partitioning information for
217  *
218  * This function gets the structure containing partitioning
219  * information for the given device @dev.
220  */
221 struct gendisk *get_gendisk(dev_t dev, int *part)
222 {
223         struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
224         return  kobj ? to_disk(kobj) : NULL;
225 }
226
227 #ifdef CONFIG_PROC_FS
228 /* iterator */
229 static void *part_start(struct seq_file *part, loff_t *pos)
230 {
231         struct list_head *p;
232         loff_t l = *pos;
233
234         down_read(&block_subsys.rwsem);
235         list_for_each(p, &block_subsys.kset.list)
236                 if (!l--)
237                         return list_entry(p, struct gendisk, kobj.entry);
238         return NULL;
239 }
240
241 static void *part_next(struct seq_file *part, void *v, loff_t *pos)
242 {
243         struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
244         ++*pos;
245         return p==&block_subsys.kset.list ? NULL : 
246                 list_entry(p, struct gendisk, kobj.entry);
247 }
248
249 static void part_stop(struct seq_file *part, void *v)
250 {
251         up_read(&block_subsys.rwsem);
252 }
253
254 static int show_partition(struct seq_file *part, void *v)
255 {
256         struct gendisk *sgp = v;
257         int n;
258         char buf[BDEVNAME_SIZE];
259
260         if (&sgp->kobj.entry == block_subsys.kset.list.next)
261                 seq_puts(part, "major minor  #blocks  name\n\n");
262
263         /* Don't show non-partitionable removeable devices or empty devices */
264         if (!get_capacity(sgp) ||
265                         (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
266                 return 0;
267         if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
268                 return 0;
269
270         /* show the full disk and all non-0 size partitions of it */
271         seq_printf(part, "%4d  %4d %10llu %s\n",
272                 sgp->major, sgp->first_minor,
273                 (unsigned long long)get_capacity(sgp) >> 1,
274                 disk_name(sgp, 0, buf));
275         for (n = 0; n < sgp->minors - 1; n++) {
276                 if (!sgp->part[n])
277                         continue;
278                 if (sgp->part[n]->nr_sects == 0)
279                         continue;
280                 seq_printf(part, "%4d  %4d %10llu %s\n",
281                         sgp->major, n + 1 + sgp->first_minor,
282                         (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
283                         disk_name(sgp, n + 1, buf));
284         }
285
286         return 0;
287 }
288
289 struct seq_operations partitions_op = {
290         .start =part_start,
291         .next = part_next,
292         .stop = part_stop,
293         .show = show_partition
294 };
295 #endif
296
297
298 extern int blk_dev_init(void);
299
300 static struct kobject *base_probe(dev_t dev, int *part, void *data)
301 {
302         if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
303                 /* Make old-style 2.4 aliases work */
304                 request_module("block-major-%d", MAJOR(dev));
305         return NULL;
306 }
307
308 int __init device_init(void)
309 {
310         bdev_map = kobj_map_init(base_probe, &block_subsys);
311         blk_dev_init();
312         subsystem_register(&block_subsys);
313         return 0;
314 }
315
316 subsys_initcall(device_init);
317
318
319
320 /*
321  * kobject & sysfs bindings for block devices
322  */
323
324 struct disk_attribute {
325         struct attribute attr;
326         ssize_t (*show)(struct gendisk *, char *);
327 };
328
329 static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
330                               char *page)
331 {
332         struct gendisk *disk = to_disk(kobj);
333         struct disk_attribute *disk_attr =
334                 container_of(attr,struct disk_attribute,attr);
335         ssize_t ret = 0;
336
337         if (disk_attr->show)
338                 ret = disk_attr->show(disk,page);
339         return ret;
340 }
341
342 static struct sysfs_ops disk_sysfs_ops = {
343         .show   = &disk_attr_show,
344 };
345
346 static ssize_t disk_dev_read(struct gendisk * disk, char *page)
347 {
348         dev_t base = MKDEV(disk->major, disk->first_minor); 
349         return print_dev_t(page, base);
350 }
351 static ssize_t disk_range_read(struct gendisk * disk, char *page)
352 {
353         return sprintf(page, "%d\n", disk->minors);
354 }
355 static ssize_t disk_removable_read(struct gendisk * disk, char *page)
356 {
357         return sprintf(page, "%d\n",
358                        (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
359
360 }
361 static ssize_t disk_size_read(struct gendisk * disk, char *page)
362 {
363         return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
364 }
365
366 static ssize_t disk_stats_read(struct gendisk * disk, char *page)
367 {
368         preempt_disable();
369         disk_round_stats(disk);
370         preempt_enable();
371         return sprintf(page,
372                 "%8u %8u %8llu %8u "
373                 "%8u %8u %8llu %8u "
374                 "%8u %8u %8u"
375                 "\n",
376                 disk_stat_read(disk, reads), disk_stat_read(disk, read_merges),
377                 (unsigned long long)disk_stat_read(disk, read_sectors),
378                 jiffies_to_msecs(disk_stat_read(disk, read_ticks)),
379                 disk_stat_read(disk, writes), 
380                 disk_stat_read(disk, write_merges),
381                 (unsigned long long)disk_stat_read(disk, write_sectors),
382                 jiffies_to_msecs(disk_stat_read(disk, write_ticks)),
383                 disk->in_flight,
384                 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
385                 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
386 }
387 static struct disk_attribute disk_attr_dev = {
388         .attr = {.name = "dev", .mode = S_IRUGO },
389         .show   = disk_dev_read
390 };
391 static struct disk_attribute disk_attr_range = {
392         .attr = {.name = "range", .mode = S_IRUGO },
393         .show   = disk_range_read
394 };
395 static struct disk_attribute disk_attr_removable = {
396         .attr = {.name = "removable", .mode = S_IRUGO },
397         .show   = disk_removable_read
398 };
399 static struct disk_attribute disk_attr_size = {
400         .attr = {.name = "size", .mode = S_IRUGO },
401         .show   = disk_size_read
402 };
403 static struct disk_attribute disk_attr_stat = {
404         .attr = {.name = "stat", .mode = S_IRUGO },
405         .show   = disk_stats_read
406 };
407
408 static struct attribute * default_attrs[] = {
409         &disk_attr_dev.attr,
410         &disk_attr_range.attr,
411         &disk_attr_removable.attr,
412         &disk_attr_size.attr,
413         &disk_attr_stat.attr,
414         NULL,
415 };
416
417 static void disk_release(struct kobject * kobj)
418 {
419         struct gendisk *disk = to_disk(kobj);
420         kfree(disk->random);
421         kfree(disk->part);
422         free_disk_stats(disk);
423         kfree(disk);
424 }
425
426 static struct kobj_type ktype_block = {
427         .release        = disk_release,
428         .sysfs_ops      = &disk_sysfs_ops,
429         .default_attrs  = default_attrs,
430 };
431
432 extern struct kobj_type ktype_part;
433
434 static int block_hotplug_filter(struct kset *kset, struct kobject *kobj)
435 {
436         struct kobj_type *ktype = get_ktype(kobj);
437
438         return ((ktype == &ktype_block) || (ktype == &ktype_part));
439 }
440
441 static int block_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
442                          int num_envp, char *buffer, int buffer_size)
443 {
444         struct device *dev = NULL;
445         struct kobj_type *ktype = get_ktype(kobj);
446         int length = 0;
447         int i = 0;
448
449         /* get physical device backing disk or partition */
450         if (ktype == &ktype_block) {
451                 struct gendisk *disk = container_of(kobj, struct gendisk, kobj);
452                 dev = disk->driverfs_dev;
453         } else if (ktype == &ktype_part) {
454                 struct gendisk *disk = container_of(kobj->parent, struct gendisk, kobj);
455                 dev = disk->driverfs_dev;
456         }
457
458         if (dev) {
459                 /* add physical device, backing this device  */
460                 char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
461
462                 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
463                                     &length, "PHYSDEVPATH=%s", path);
464                 kfree(path);
465
466                 /* add bus name of physical device */
467                 if (dev->bus)
468                         add_hotplug_env_var(envp, num_envp, &i,
469                                             buffer, buffer_size, &length,
470                                             "PHYSDEVBUS=%s", dev->bus->name);
471
472                 /* add driver name of physical device */
473                 if (dev->driver)
474                         add_hotplug_env_var(envp, num_envp, &i,
475                                             buffer, buffer_size, &length,
476                                             "PHYSDEVDRIVER=%s", dev->driver->name);
477
478                 envp[i] = NULL;
479         }
480
481         return 0;
482 }
483
484 static struct kset_hotplug_ops block_hotplug_ops = {
485         .filter         = block_hotplug_filter,
486         .hotplug        = block_hotplug,
487 };
488
489 /* declare block_subsys. */
490 static decl_subsys(block, &ktype_block, &block_hotplug_ops);
491
492
493 /*
494  * aggregate disk stat collector.  Uses the same stats that the sysfs
495  * entries do, above, but makes them available through one seq_file.
496  * Watching a few disks may be efficient through sysfs, but watching
497  * all of them will be more efficient through this interface.
498  *
499  * The output looks suspiciously like /proc/partitions with a bunch of
500  * extra fields.
501  */
502
503 /* iterator */
504 static void *diskstats_start(struct seq_file *part, loff_t *pos)
505 {
506         loff_t k = *pos;
507         struct list_head *p;
508
509         down_read(&block_subsys.rwsem);
510         list_for_each(p, &block_subsys.kset.list)
511                 if (!k--)
512                         return list_entry(p, struct gendisk, kobj.entry);
513         return NULL;
514 }
515
516 static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
517 {
518         struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
519         ++*pos;
520         return p==&block_subsys.kset.list ? NULL :
521                 list_entry(p, struct gendisk, kobj.entry);
522 }
523
524 static void diskstats_stop(struct seq_file *part, void *v)
525 {
526         up_read(&block_subsys.rwsem);
527 }
528
529 static int diskstats_show(struct seq_file *s, void *v)
530 {
531         struct gendisk *gp = v;
532         char buf[BDEVNAME_SIZE];
533         int n = 0;
534
535         /*
536         if (&sgp->kobj.entry == block_subsys.kset.list.next)
537                 seq_puts(s,     "major minor name"
538                                 "     rio rmerge rsect ruse wio wmerge "
539                                 "wsect wuse running use aveq"
540                                 "\n\n");
541         */
542  
543         preempt_disable();
544         disk_round_stats(gp);
545         preempt_enable();
546         seq_printf(s, "%4d %4d %s %u %u %llu %u %u %u %llu %u %u %u %u\n",
547                 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
548                 disk_stat_read(gp, reads), disk_stat_read(gp, read_merges),
549                 (unsigned long long)disk_stat_read(gp, read_sectors),
550                 jiffies_to_msecs(disk_stat_read(gp, read_ticks)),
551                 disk_stat_read(gp, writes), disk_stat_read(gp, write_merges),
552                 (unsigned long long)disk_stat_read(gp, write_sectors),
553                 jiffies_to_msecs(disk_stat_read(gp, write_ticks)),
554                 gp->in_flight,
555                 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
556                 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
557
558         /* now show all non-0 size partitions of it */
559         for (n = 0; n < gp->minors - 1; n++) {
560                 struct hd_struct *hd = gp->part[n];
561
562                 if (hd && hd->nr_sects)
563                         seq_printf(s, "%4d %4d %s %u %u %u %u\n",
564                                 gp->major, n + gp->first_minor + 1,
565                                 disk_name(gp, n + 1, buf),
566                                 hd->reads, hd->read_sectors,
567                                 hd->writes, hd->write_sectors);
568         }
569  
570         return 0;
571 }
572
573 struct seq_operations diskstats_op = {
574         .start  = diskstats_start,
575         .next   = diskstats_next,
576         .stop   = diskstats_stop,
577         .show   = diskstats_show
578 };
579
580
581 struct gendisk *alloc_disk(int minors)
582 {
583         struct gendisk *disk = kmalloc(sizeof(struct gendisk), GFP_KERNEL);
584         if (disk) {
585                 memset(disk, 0, sizeof(struct gendisk));
586                 if (!init_disk_stats(disk)) {
587                         kfree(disk);
588                         return NULL;
589                 }
590                 if (minors > 1) {
591                         int size = (minors - 1) * sizeof(struct hd_struct *);
592                         disk->part = kmalloc(size, GFP_KERNEL);
593                         if (!disk->part) {
594                                 kfree(disk);
595                                 return NULL;
596                         }
597                         memset(disk->part, 0, size);
598                 }
599                 disk->minors = minors;
600                 kobj_set_kset_s(disk,block_subsys);
601                 kobject_init(&disk->kobj);
602                 rand_initialize_disk(disk);
603         }
604         return disk;
605 }
606
607 EXPORT_SYMBOL(alloc_disk);
608
609 struct kobject *get_disk(struct gendisk *disk)
610 {
611         struct module *owner;
612         struct kobject *kobj;
613
614         if (!disk->fops)
615                 return NULL;
616         owner = disk->fops->owner;
617         if (owner && !try_module_get(owner))
618                 return NULL;
619         kobj = kobject_get(&disk->kobj);
620         if (kobj == NULL) {
621                 module_put(owner);
622                 return NULL;
623         }
624         return kobj;
625
626 }
627
628 EXPORT_SYMBOL(get_disk);
629
630 void put_disk(struct gendisk *disk)
631 {
632         if (disk)
633                 kobject_put(&disk->kobj);
634 }
635
636 EXPORT_SYMBOL(put_disk);
637
638 void set_device_ro(struct block_device *bdev, int flag)
639 {
640         if (bdev->bd_contains != bdev)
641                 bdev->bd_part->policy = flag;
642         else
643                 bdev->bd_disk->policy = flag;
644 }
645
646 EXPORT_SYMBOL(set_device_ro);
647
648 void set_disk_ro(struct gendisk *disk, int flag)
649 {
650         int i;
651         disk->policy = flag;
652         for (i = 0; i < disk->minors - 1; i++)
653                 if (disk->part[i]) disk->part[i]->policy = flag;
654 }
655
656 EXPORT_SYMBOL(set_disk_ro);
657
658 int bdev_read_only(struct block_device *bdev)
659 {
660         if (!bdev)
661                 return 0;
662         else if (bdev->bd_contains != bdev)
663                 return bdev->bd_part->policy;
664         else
665                 return bdev->bd_disk->policy;
666 }
667
668 EXPORT_SYMBOL(bdev_read_only);
669
670 int invalidate_partition(struct gendisk *disk, int index)
671 {
672         int res = 0;
673         struct block_device *bdev = bdget_disk(disk, index);
674         if (bdev)
675                 res = __invalidate_device(bdev, 1);
676         bdput(bdev);
677         return res;
678 }
679
680 EXPORT_SYMBOL(invalidate_partition);