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