e1cb96fb883e5908e3c69587a371a590110884b8
[safe/jmp/linux-2.6] / block / genhd.c
1 /*
2  *  gendisk handling
3  */
4
5 #include <linux/module.h>
6 #include <linux/fs.h>
7 #include <linux/genhd.h>
8 #include <linux/kdev_t.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 #include <linux/buffer_head.h>
18 #include <linux/mutex.h>
19 #include <linux/idr.h>
20
21 #include "blk.h"
22
23 static DEFINE_MUTEX(block_class_lock);
24 #ifndef CONFIG_SYSFS_DEPRECATED
25 struct kobject *block_depr;
26 #endif
27
28 /* for extended dynamic devt allocation, currently only one major is used */
29 #define MAX_EXT_DEVT            (1 << MINORBITS)
30
31 /* For extended devt allocation.  ext_devt_mutex prevents look up
32  * results from going away underneath its user.
33  */
34 static DEFINE_MUTEX(ext_devt_mutex);
35 static DEFINE_IDR(ext_devt_idr);
36
37 static struct device_type disk_type;
38
39 /**
40  * disk_get_part - get partition
41  * @disk: disk to look partition from
42  * @partno: partition number
43  *
44  * Look for partition @partno from @disk.  If found, increment
45  * reference count and return it.
46  *
47  * CONTEXT:
48  * Don't care.
49  *
50  * RETURNS:
51  * Pointer to the found partition on success, NULL if not found.
52  */
53 struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
54 {
55         struct hd_struct *part;
56
57         if (unlikely(partno < 0 || partno >= disk_max_parts(disk)))
58                 return NULL;
59         rcu_read_lock();
60         part = rcu_dereference(disk->__part[partno]);
61         if (part)
62                 get_device(part_to_dev(part));
63         rcu_read_unlock();
64
65         return part;
66 }
67 EXPORT_SYMBOL_GPL(disk_get_part);
68
69 /**
70  * disk_part_iter_init - initialize partition iterator
71  * @piter: iterator to initialize
72  * @disk: disk to iterate over
73  * @flags: DISK_PITER_* flags
74  *
75  * Initialize @piter so that it iterates over partitions of @disk.
76  *
77  * CONTEXT:
78  * Don't care.
79  */
80 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
81                           unsigned int flags)
82 {
83         piter->disk = disk;
84         piter->part = NULL;
85
86         if (flags & DISK_PITER_REVERSE)
87                 piter->idx = disk_max_parts(piter->disk) - 1;
88         else if (flags & DISK_PITER_INCL_PART0)
89                 piter->idx = 0;
90         else
91                 piter->idx = 1;
92
93         piter->flags = flags;
94 }
95 EXPORT_SYMBOL_GPL(disk_part_iter_init);
96
97 /**
98  * disk_part_iter_next - proceed iterator to the next partition and return it
99  * @piter: iterator of interest
100  *
101  * Proceed @piter to the next partition and return it.
102  *
103  * CONTEXT:
104  * Don't care.
105  */
106 struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
107 {
108         int inc, end;
109
110         /* put the last partition */
111         disk_put_part(piter->part);
112         piter->part = NULL;
113
114         rcu_read_lock();
115
116         /* determine iteration parameters */
117         if (piter->flags & DISK_PITER_REVERSE) {
118                 inc = -1;
119                 if (piter->flags & DISK_PITER_INCL_PART0)
120                         end = -1;
121                 else
122                         end = 0;
123         } else {
124                 inc = 1;
125                 end = disk_max_parts(piter->disk);
126         }
127
128         /* iterate to the next partition */
129         for (; piter->idx != end; piter->idx += inc) {
130                 struct hd_struct *part;
131
132                 part = rcu_dereference(piter->disk->__part[piter->idx]);
133                 if (!part)
134                         continue;
135                 if (!(piter->flags & DISK_PITER_INCL_EMPTY) && !part->nr_sects)
136                         continue;
137
138                 get_device(part_to_dev(part));
139                 piter->part = part;
140                 piter->idx += inc;
141                 break;
142         }
143
144         rcu_read_unlock();
145
146         return piter->part;
147 }
148 EXPORT_SYMBOL_GPL(disk_part_iter_next);
149
150 /**
151  * disk_part_iter_exit - finish up partition iteration
152  * @piter: iter of interest
153  *
154  * Called when iteration is over.  Cleans up @piter.
155  *
156  * CONTEXT:
157  * Don't care.
158  */
159 void disk_part_iter_exit(struct disk_part_iter *piter)
160 {
161         disk_put_part(piter->part);
162         piter->part = NULL;
163 }
164 EXPORT_SYMBOL_GPL(disk_part_iter_exit);
165
166 /**
167  * disk_map_sector_rcu - map sector to partition
168  * @disk: gendisk of interest
169  * @sector: sector to map
170  *
171  * Find out which partition @sector maps to on @disk.  This is
172  * primarily used for stats accounting.
173  *
174  * CONTEXT:
175  * RCU read locked.  The returned partition pointer is valid only
176  * while preemption is disabled.
177  *
178  * RETURNS:
179  * Found partition on success, part0 is returned if no partition matches
180  */
181 struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
182 {
183         int i;
184
185         for (i = 1; i < disk_max_parts(disk); i++) {
186                 struct hd_struct *part = rcu_dereference(disk->__part[i]);
187
188                 if (part && part->start_sect <= sector &&
189                     sector < part->start_sect + part->nr_sects)
190                         return part;
191         }
192         return &disk->part0;
193 }
194 EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
195
196 /*
197  * Can be deleted altogether. Later.
198  *
199  */
200 static struct blk_major_name {
201         struct blk_major_name *next;
202         int major;
203         char name[16];
204 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
205
206 /* index in the above - for now: assume no multimajor ranges */
207 static inline int major_to_index(int major)
208 {
209         return major % BLKDEV_MAJOR_HASH_SIZE;
210 }
211
212 #ifdef CONFIG_PROC_FS
213 void blkdev_show(struct seq_file *seqf, off_t offset)
214 {
215         struct blk_major_name *dp;
216
217         if (offset < BLKDEV_MAJOR_HASH_SIZE) {
218                 mutex_lock(&block_class_lock);
219                 for (dp = major_names[offset]; dp; dp = dp->next)
220                         seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
221                 mutex_unlock(&block_class_lock);
222         }
223 }
224 #endif /* CONFIG_PROC_FS */
225
226 int register_blkdev(unsigned int major, const char *name)
227 {
228         struct blk_major_name **n, *p;
229         int index, ret = 0;
230
231         mutex_lock(&block_class_lock);
232
233         /* temporary */
234         if (major == 0) {
235                 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
236                         if (major_names[index] == NULL)
237                                 break;
238                 }
239
240                 if (index == 0) {
241                         printk("register_blkdev: failed to get major for %s\n",
242                                name);
243                         ret = -EBUSY;
244                         goto out;
245                 }
246                 major = index;
247                 ret = major;
248         }
249
250         p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
251         if (p == NULL) {
252                 ret = -ENOMEM;
253                 goto out;
254         }
255
256         p->major = major;
257         strlcpy(p->name, name, sizeof(p->name));
258         p->next = NULL;
259         index = major_to_index(major);
260
261         for (n = &major_names[index]; *n; n = &(*n)->next) {
262                 if ((*n)->major == major)
263                         break;
264         }
265         if (!*n)
266                 *n = p;
267         else
268                 ret = -EBUSY;
269
270         if (ret < 0) {
271                 printk("register_blkdev: cannot get major %d for %s\n",
272                        major, name);
273                 kfree(p);
274         }
275 out:
276         mutex_unlock(&block_class_lock);
277         return ret;
278 }
279
280 EXPORT_SYMBOL(register_blkdev);
281
282 void unregister_blkdev(unsigned int major, const char *name)
283 {
284         struct blk_major_name **n;
285         struct blk_major_name *p = NULL;
286         int index = major_to_index(major);
287
288         mutex_lock(&block_class_lock);
289         for (n = &major_names[index]; *n; n = &(*n)->next)
290                 if ((*n)->major == major)
291                         break;
292         if (!*n || strcmp((*n)->name, name)) {
293                 WARN_ON(1);
294         } else {
295                 p = *n;
296                 *n = p->next;
297         }
298         mutex_unlock(&block_class_lock);
299         kfree(p);
300 }
301
302 EXPORT_SYMBOL(unregister_blkdev);
303
304 static struct kobj_map *bdev_map;
305
306 /**
307  * blk_mangle_minor - scatter minor numbers apart
308  * @minor: minor number to mangle
309  *
310  * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
311  * is enabled.  Mangling twice gives the original value.
312  *
313  * RETURNS:
314  * Mangled value.
315  *
316  * CONTEXT:
317  * Don't care.
318  */
319 static int blk_mangle_minor(int minor)
320 {
321 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
322         int i;
323
324         for (i = 0; i < MINORBITS / 2; i++) {
325                 int low = minor & (1 << i);
326                 int high = minor & (1 << (MINORBITS - 1 - i));
327                 int distance = MINORBITS - 1 - 2 * i;
328
329                 minor ^= low | high;    /* clear both bits */
330                 low <<= distance;       /* swap the positions */
331                 high >>= distance;
332                 minor |= low | high;    /* and set */
333         }
334 #endif
335         return minor;
336 }
337
338 /**
339  * blk_alloc_devt - allocate a dev_t for a partition
340  * @part: partition to allocate dev_t for
341  * @gfp_mask: memory allocation flag
342  * @devt: out parameter for resulting dev_t
343  *
344  * Allocate a dev_t for block device.
345  *
346  * RETURNS:
347  * 0 on success, allocated dev_t is returned in *@devt.  -errno on
348  * failure.
349  *
350  * CONTEXT:
351  * Might sleep.
352  */
353 int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
354 {
355         struct gendisk *disk = part_to_disk(part);
356         int idx, rc;
357
358         /* in consecutive minor range? */
359         if (part->partno < disk->minors) {
360                 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
361                 return 0;
362         }
363
364         /* allocate ext devt */
365         do {
366                 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
367                         return -ENOMEM;
368                 rc = idr_get_new(&ext_devt_idr, part, &idx);
369         } while (rc == -EAGAIN);
370
371         if (rc)
372                 return rc;
373
374         if (idx > MAX_EXT_DEVT) {
375                 idr_remove(&ext_devt_idr, idx);
376                 return -EBUSY;
377         }
378
379         *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
380         return 0;
381 }
382
383 /**
384  * blk_free_devt - free a dev_t
385  * @devt: dev_t to free
386  *
387  * Free @devt which was allocated using blk_alloc_devt().
388  *
389  * CONTEXT:
390  * Might sleep.
391  */
392 void blk_free_devt(dev_t devt)
393 {
394         might_sleep();
395
396         if (devt == MKDEV(0, 0))
397                 return;
398
399         if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
400                 mutex_lock(&ext_devt_mutex);
401                 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
402                 mutex_unlock(&ext_devt_mutex);
403         }
404 }
405
406 static char *bdevt_str(dev_t devt, char *buf)
407 {
408         if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
409                 char tbuf[BDEVT_SIZE];
410                 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
411                 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
412         } else
413                 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
414
415         return buf;
416 }
417
418 /*
419  * Register device numbers dev..(dev+range-1)
420  * range must be nonzero
421  * The hash chain is sorted on range, so that subranges can override.
422  */
423 void blk_register_region(dev_t devt, unsigned long range, struct module *module,
424                          struct kobject *(*probe)(dev_t, int *, void *),
425                          int (*lock)(dev_t, void *), void *data)
426 {
427         kobj_map(bdev_map, devt, range, module, probe, lock, data);
428 }
429
430 EXPORT_SYMBOL(blk_register_region);
431
432 void blk_unregister_region(dev_t devt, unsigned long range)
433 {
434         kobj_unmap(bdev_map, devt, range);
435 }
436
437 EXPORT_SYMBOL(blk_unregister_region);
438
439 static struct kobject *exact_match(dev_t devt, int *partno, void *data)
440 {
441         struct gendisk *p = data;
442
443         return &disk_to_dev(p)->kobj;
444 }
445
446 static int exact_lock(dev_t devt, void *data)
447 {
448         struct gendisk *p = data;
449
450         if (!get_disk(p))
451                 return -1;
452         return 0;
453 }
454
455 /**
456  * add_disk - add partitioning information to kernel list
457  * @disk: per-device partitioning information
458  *
459  * This function registers the partitioning information in @disk
460  * with the kernel.
461  */
462 void add_disk(struct gendisk *disk)
463 {
464         struct backing_dev_info *bdi;
465         int retval;
466
467         disk->flags |= GENHD_FL_UP;
468         disk_to_dev(disk)->devt = MKDEV(disk->major, disk->first_minor);
469         blk_register_region(disk_devt(disk), disk->minors, NULL,
470                             exact_match, exact_lock, disk);
471         register_disk(disk);
472         blk_register_queue(disk);
473
474         bdi = &disk->queue->backing_dev_info;
475         bdi_register_dev(bdi, disk_devt(disk));
476         retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
477                                    "bdi");
478         WARN_ON(retval);
479 }
480
481 EXPORT_SYMBOL(add_disk);
482 EXPORT_SYMBOL(del_gendisk);     /* in partitions/check.c */
483
484 void unlink_gendisk(struct gendisk *disk)
485 {
486         sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
487         bdi_unregister(&disk->queue->backing_dev_info);
488         blk_unregister_queue(disk);
489         blk_unregister_region(disk_devt(disk), disk->minors);
490 }
491
492 /**
493  * get_gendisk - get partitioning information for a given device
494  * @devt: device to get partitioning information for
495  * @part: returned partition index
496  *
497  * This function gets the structure containing partitioning
498  * information for the given device @devt.
499  */
500 struct gendisk *get_gendisk(dev_t devt, int *partno)
501 {
502         struct gendisk *disk = NULL;
503
504         if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
505                 struct kobject *kobj;
506
507                 kobj = kobj_lookup(bdev_map, devt, partno);
508                 if (kobj)
509                         disk = dev_to_disk(kobj_to_dev(kobj));
510         } else {
511                 struct hd_struct *part;
512
513                 mutex_lock(&ext_devt_mutex);
514                 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
515                 if (part && get_disk(part_to_disk(part))) {
516                         *partno = part->partno;
517                         disk = part_to_disk(part);
518                 }
519                 mutex_unlock(&ext_devt_mutex);
520         }
521
522         return disk;
523 }
524
525 /**
526  * bdget_disk - do bdget() by gendisk and partition number
527  * @disk: gendisk of interest
528  * @partno: partition number
529  *
530  * Find partition @partno from @disk, do bdget() on it.
531  *
532  * CONTEXT:
533  * Don't care.
534  *
535  * RETURNS:
536  * Resulting block_device on success, NULL on failure.
537  */
538 extern struct block_device *bdget_disk(struct gendisk *disk, int partno)
539 {
540         struct hd_struct *part;
541         struct block_device *bdev = NULL;
542
543         part = disk_get_part(disk, partno);
544         if (part && (part->nr_sects || partno == 0))
545                 bdev = bdget(part_devt(part));
546         disk_put_part(part);
547
548         return bdev;
549 }
550 EXPORT_SYMBOL(bdget_disk);
551
552 /*
553  * print a full list of all partitions - intended for places where the root
554  * filesystem can't be mounted and thus to give the victim some idea of what
555  * went wrong
556  */
557 void __init printk_all_partitions(void)
558 {
559         struct class_dev_iter iter;
560         struct device *dev;
561
562         class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
563         while ((dev = class_dev_iter_next(&iter))) {
564                 struct gendisk *disk = dev_to_disk(dev);
565                 struct disk_part_iter piter;
566                 struct hd_struct *part;
567                 char name_buf[BDEVNAME_SIZE];
568                 char devt_buf[BDEVT_SIZE];
569
570                 /*
571                  * Don't show empty devices or things that have been
572                  * surpressed
573                  */
574                 if (get_capacity(disk) == 0 ||
575                     (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
576                         continue;
577
578                 /*
579                  * Note, unlike /proc/partitions, I am showing the
580                  * numbers in hex - the same format as the root=
581                  * option takes.
582                  */
583                 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
584                 while ((part = disk_part_iter_next(&piter))) {
585                         bool is_part0 = part == &disk->part0;
586
587                         printk("%s%s %10llu %s", is_part0 ? "" : "  ",
588                                bdevt_str(part_devt(part), devt_buf),
589                                (unsigned long long)part->nr_sects >> 1,
590                                disk_name(disk, part->partno, name_buf));
591                         if (is_part0) {
592                                 if (disk->driverfs_dev != NULL &&
593                                     disk->driverfs_dev->driver != NULL)
594                                         printk(" driver: %s\n",
595                                               disk->driverfs_dev->driver->name);
596                                 else
597                                         printk(" (driver?)\n");
598                         } else
599                                 printk("\n");
600                 }
601                 disk_part_iter_exit(&piter);
602         }
603         class_dev_iter_exit(&iter);
604 }
605
606 #ifdef CONFIG_PROC_FS
607 /* iterator */
608 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
609 {
610         loff_t skip = *pos;
611         struct class_dev_iter *iter;
612         struct device *dev;
613
614         iter = kmalloc(GFP_KERNEL, sizeof(*iter));
615         if (!iter)
616                 return ERR_PTR(-ENOMEM);
617
618         seqf->private = iter;
619         class_dev_iter_init(iter, &block_class, NULL, &disk_type);
620         do {
621                 dev = class_dev_iter_next(iter);
622                 if (!dev)
623                         return NULL;
624         } while (skip--);
625
626         return dev_to_disk(dev);
627 }
628
629 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
630 {
631         struct device *dev;
632
633         (*pos)++;
634         dev = class_dev_iter_next(seqf->private);
635         if (dev)
636                 return dev_to_disk(dev);
637
638         return NULL;
639 }
640
641 static void disk_seqf_stop(struct seq_file *seqf, void *v)
642 {
643         struct class_dev_iter *iter = seqf->private;
644
645         /* stop is called even after start failed :-( */
646         if (iter) {
647                 class_dev_iter_exit(iter);
648                 kfree(iter);
649         }
650 }
651
652 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
653 {
654         static void *p;
655
656         p = disk_seqf_start(seqf, pos);
657         if (!IS_ERR(p) && p)
658                 seq_puts(seqf, "major minor  #blocks  name\n\n");
659         return p;
660 }
661
662 static int show_partition(struct seq_file *seqf, void *v)
663 {
664         struct gendisk *sgp = v;
665         struct disk_part_iter piter;
666         struct hd_struct *part;
667         char buf[BDEVNAME_SIZE];
668
669         /* Don't show non-partitionable removeable devices or empty devices */
670         if (!get_capacity(sgp) || (!disk_partitionable(sgp) &&
671                                    (sgp->flags & GENHD_FL_REMOVABLE)))
672                 return 0;
673         if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
674                 return 0;
675
676         /* show the full disk and all non-0 size partitions of it */
677         disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
678         while ((part = disk_part_iter_next(&piter)))
679                 seq_printf(seqf, "%4d  %7d %10llu %s\n",
680                            MAJOR(part_devt(part)), MINOR(part_devt(part)),
681                            (unsigned long long)part->nr_sects >> 1,
682                            disk_name(sgp, part->partno, buf));
683         disk_part_iter_exit(&piter);
684
685         return 0;
686 }
687
688 const struct seq_operations partitions_op = {
689         .start  = show_partition_start,
690         .next   = disk_seqf_next,
691         .stop   = disk_seqf_stop,
692         .show   = show_partition
693 };
694 #endif
695
696
697 static struct kobject *base_probe(dev_t devt, int *partno, void *data)
698 {
699         if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
700                 /* Make old-style 2.4 aliases work */
701                 request_module("block-major-%d", MAJOR(devt));
702         return NULL;
703 }
704
705 static int __init genhd_device_init(void)
706 {
707         int error;
708
709         block_class.dev_kobj = sysfs_dev_block_kobj;
710         error = class_register(&block_class);
711         if (unlikely(error))
712                 return error;
713         bdev_map = kobj_map_init(base_probe, &block_class_lock);
714         blk_dev_init();
715
716 #ifndef CONFIG_SYSFS_DEPRECATED
717         /* create top-level block dir */
718         block_depr = kobject_create_and_add("block", NULL);
719 #endif
720         return 0;
721 }
722
723 subsys_initcall(genhd_device_init);
724
725 static ssize_t disk_range_show(struct device *dev,
726                                struct device_attribute *attr, char *buf)
727 {
728         struct gendisk *disk = dev_to_disk(dev);
729
730         return sprintf(buf, "%d\n", disk->minors);
731 }
732
733 static ssize_t disk_ext_range_show(struct device *dev,
734                                    struct device_attribute *attr, char *buf)
735 {
736         struct gendisk *disk = dev_to_disk(dev);
737
738         return sprintf(buf, "%d\n", disk_max_parts(disk));
739 }
740
741 static ssize_t disk_removable_show(struct device *dev,
742                                    struct device_attribute *attr, char *buf)
743 {
744         struct gendisk *disk = dev_to_disk(dev);
745
746         return sprintf(buf, "%d\n",
747                        (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
748 }
749
750 static ssize_t disk_ro_show(struct device *dev,
751                                    struct device_attribute *attr, char *buf)
752 {
753         struct gendisk *disk = dev_to_disk(dev);
754
755         return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
756 }
757
758 static ssize_t disk_capability_show(struct device *dev,
759                                     struct device_attribute *attr, char *buf)
760 {
761         struct gendisk *disk = dev_to_disk(dev);
762
763         return sprintf(buf, "%x\n", disk->flags);
764 }
765
766 static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
767 static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
768 static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
769 static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
770 static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
771 static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
772 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
773 #ifdef CONFIG_FAIL_MAKE_REQUEST
774 static struct device_attribute dev_attr_fail =
775         __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
776 #endif
777
778 static struct attribute *disk_attrs[] = {
779         &dev_attr_range.attr,
780         &dev_attr_ext_range.attr,
781         &dev_attr_removable.attr,
782         &dev_attr_ro.attr,
783         &dev_attr_size.attr,
784         &dev_attr_capability.attr,
785         &dev_attr_stat.attr,
786 #ifdef CONFIG_FAIL_MAKE_REQUEST
787         &dev_attr_fail.attr,
788 #endif
789         NULL
790 };
791
792 static struct attribute_group disk_attr_group = {
793         .attrs = disk_attrs,
794 };
795
796 static struct attribute_group *disk_attr_groups[] = {
797         &disk_attr_group,
798         NULL
799 };
800
801 static void disk_release(struct device *dev)
802 {
803         struct gendisk *disk = dev_to_disk(dev);
804
805         kfree(disk->random);
806         kfree(disk->__part);
807         free_part_stats(&disk->part0);
808         kfree(disk);
809 }
810 struct class block_class = {
811         .name           = "block",
812 };
813
814 static struct device_type disk_type = {
815         .name           = "disk",
816         .groups         = disk_attr_groups,
817         .release        = disk_release,
818 };
819
820 #ifdef CONFIG_PROC_FS
821 /*
822  * aggregate disk stat collector.  Uses the same stats that the sysfs
823  * entries do, above, but makes them available through one seq_file.
824  *
825  * The output looks suspiciously like /proc/partitions with a bunch of
826  * extra fields.
827  */
828 static int diskstats_show(struct seq_file *seqf, void *v)
829 {
830         struct gendisk *gp = v;
831         struct disk_part_iter piter;
832         struct hd_struct *hd;
833         char buf[BDEVNAME_SIZE];
834         int cpu;
835
836         /*
837         if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
838                 seq_puts(seqf,  "major minor name"
839                                 "     rio rmerge rsect ruse wio wmerge "
840                                 "wsect wuse running use aveq"
841                                 "\n\n");
842         */
843  
844         disk_part_iter_init(&piter, gp, DISK_PITER_INCL_PART0);
845         while ((hd = disk_part_iter_next(&piter))) {
846                 cpu = part_stat_lock();
847                 part_round_stats(cpu, hd);
848                 part_stat_unlock();
849                 seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
850                            "%u %lu %lu %llu %u %u %u %u\n",
851                            MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
852                            disk_name(gp, hd->partno, buf),
853                            part_stat_read(hd, ios[0]),
854                            part_stat_read(hd, merges[0]),
855                            (unsigned long long)part_stat_read(hd, sectors[0]),
856                            jiffies_to_msecs(part_stat_read(hd, ticks[0])),
857                            part_stat_read(hd, ios[1]),
858                            part_stat_read(hd, merges[1]),
859                            (unsigned long long)part_stat_read(hd, sectors[1]),
860                            jiffies_to_msecs(part_stat_read(hd, ticks[1])),
861                            hd->in_flight,
862                            jiffies_to_msecs(part_stat_read(hd, io_ticks)),
863                            jiffies_to_msecs(part_stat_read(hd, time_in_queue))
864                         );
865         }
866         disk_part_iter_exit(&piter);
867  
868         return 0;
869 }
870
871 const struct seq_operations diskstats_op = {
872         .start  = disk_seqf_start,
873         .next   = disk_seqf_next,
874         .stop   = disk_seqf_stop,
875         .show   = diskstats_show
876 };
877 #endif /* CONFIG_PROC_FS */
878
879 static void media_change_notify_thread(struct work_struct *work)
880 {
881         struct gendisk *gd = container_of(work, struct gendisk, async_notify);
882         char event[] = "MEDIA_CHANGE=1";
883         char *envp[] = { event, NULL };
884
885         /*
886          * set enviroment vars to indicate which event this is for
887          * so that user space will know to go check the media status.
888          */
889         kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
890         put_device(gd->driverfs_dev);
891 }
892
893 #if 0
894 void genhd_media_change_notify(struct gendisk *disk)
895 {
896         get_device(disk->driverfs_dev);
897         schedule_work(&disk->async_notify);
898 }
899 EXPORT_SYMBOL_GPL(genhd_media_change_notify);
900 #endif  /*  0  */
901
902 dev_t blk_lookup_devt(const char *name, int partno)
903 {
904         dev_t devt = MKDEV(0, 0);
905         struct class_dev_iter iter;
906         struct device *dev;
907
908         class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
909         while ((dev = class_dev_iter_next(&iter))) {
910                 struct gendisk *disk = dev_to_disk(dev);
911                 struct hd_struct *part;
912
913                 if (strcmp(dev->bus_id, name))
914                         continue;
915
916                 part = disk_get_part(disk, partno);
917                 if (part && (part->nr_sects || partno == 0)) {
918                         devt = part_devt(part);
919                         disk_put_part(part);
920                         break;
921                 }
922                 disk_put_part(part);
923         }
924         class_dev_iter_exit(&iter);
925         return devt;
926 }
927 EXPORT_SYMBOL(blk_lookup_devt);
928
929 struct gendisk *alloc_disk(int minors)
930 {
931         return alloc_disk_node(minors, -1);
932 }
933
934 struct gendisk *alloc_disk_node(int minors, int node_id)
935 {
936         return alloc_disk_ext_node(minors, 0, node_id);
937 }
938
939 struct gendisk *alloc_disk_ext(int minors, int ext_minors)
940 {
941         return alloc_disk_ext_node(minors, ext_minors, -1);
942 }
943
944 struct gendisk *alloc_disk_ext_node(int minors, int ext_minors, int node_id)
945 {
946         struct gendisk *disk;
947
948         disk = kmalloc_node(sizeof(struct gendisk),
949                                 GFP_KERNEL | __GFP_ZERO, node_id);
950         if (disk) {
951                 int tot_minors = minors + ext_minors;
952                 int size = tot_minors * sizeof(struct hd_struct *);
953
954                 if (!init_part_stats(&disk->part0)) {
955                         kfree(disk);
956                         return NULL;
957                 }
958
959                 disk->__part = kmalloc_node(size, GFP_KERNEL | __GFP_ZERO,
960                                             node_id);
961                 if (!disk->__part) {
962                                 free_part_stats(&disk->part0);
963                         kfree(disk);
964                         return NULL;
965                 }
966                 disk->__part[0] = &disk->part0;
967
968                 disk->minors = minors;
969                 disk->ext_minors = ext_minors;
970                 rand_initialize_disk(disk);
971                 disk_to_dev(disk)->class = &block_class;
972                 disk_to_dev(disk)->type = &disk_type;
973                 device_initialize(disk_to_dev(disk));
974                 INIT_WORK(&disk->async_notify,
975                         media_change_notify_thread);
976         }
977         return disk;
978 }
979
980 EXPORT_SYMBOL(alloc_disk);
981 EXPORT_SYMBOL(alloc_disk_node);
982 EXPORT_SYMBOL(alloc_disk_ext);
983 EXPORT_SYMBOL(alloc_disk_ext_node);
984
985 struct kobject *get_disk(struct gendisk *disk)
986 {
987         struct module *owner;
988         struct kobject *kobj;
989
990         if (!disk->fops)
991                 return NULL;
992         owner = disk->fops->owner;
993         if (owner && !try_module_get(owner))
994                 return NULL;
995         kobj = kobject_get(&disk_to_dev(disk)->kobj);
996         if (kobj == NULL) {
997                 module_put(owner);
998                 return NULL;
999         }
1000         return kobj;
1001
1002 }
1003
1004 EXPORT_SYMBOL(get_disk);
1005
1006 void put_disk(struct gendisk *disk)
1007 {
1008         if (disk)
1009                 kobject_put(&disk_to_dev(disk)->kobj);
1010 }
1011
1012 EXPORT_SYMBOL(put_disk);
1013
1014 void set_device_ro(struct block_device *bdev, int flag)
1015 {
1016         bdev->bd_part->policy = flag;
1017 }
1018
1019 EXPORT_SYMBOL(set_device_ro);
1020
1021 void set_disk_ro(struct gendisk *disk, int flag)
1022 {
1023         struct disk_part_iter piter;
1024         struct hd_struct *part;
1025
1026         disk_part_iter_init(&piter, disk,
1027                             DISK_PITER_INCL_EMPTY | DISK_PITER_INCL_PART0);
1028         while ((part = disk_part_iter_next(&piter)))
1029                 part->policy = flag;
1030         disk_part_iter_exit(&piter);
1031 }
1032
1033 EXPORT_SYMBOL(set_disk_ro);
1034
1035 int bdev_read_only(struct block_device *bdev)
1036 {
1037         if (!bdev)
1038                 return 0;
1039         return bdev->bd_part->policy;
1040 }
1041
1042 EXPORT_SYMBOL(bdev_read_only);
1043
1044 int invalidate_partition(struct gendisk *disk, int partno)
1045 {
1046         int res = 0;
1047         struct block_device *bdev = bdget_disk(disk, partno);
1048         if (bdev) {
1049                 fsync_bdev(bdev);
1050                 res = __invalidate_device(bdev);
1051                 bdput(bdev);
1052         }
1053         return res;
1054 }
1055
1056 EXPORT_SYMBOL(invalidate_partition);