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