blob: fa32d09fda24c1f840454f3671dd8ace2cd491de [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * gendisk handling
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/module.h>
6#include <linux/fs.h>
7#include <linux/genhd.h>
Andrew Mortonb446b602007-02-20 13:57:48 -08008#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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>
Christoph Hellwig2ef41632005-05-05 16:15:59 -070017#include <linux/buffer_head.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080018#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Adrian Bunkff88972c2008-03-04 11:23:45 +010020#include "blk.h"
21
Kay Sieversedfaa7c2007-05-21 22:08:01 +020022static DEFINE_MUTEX(block_class_lock);
23#ifndef CONFIG_SYSFS_DEPRECATED
24struct kobject *block_depr;
25#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Adrian Bunk1826ead2008-03-04 11:23:46 +010027static struct device_type disk_type;
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
30 * Can be deleted altogether. Later.
31 *
32 */
33static struct blk_major_name {
34 struct blk_major_name *next;
35 int major;
36 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -080037} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/* index in the above - for now: assume no multimajor ranges */
40static inline int major_to_index(int major)
41{
Joe Korty68eef3b2006-03-31 02:30:32 -080042 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
Joe Korty68eef3b2006-03-31 02:30:32 -080045#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +020046void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -080047{
Joe Korty68eef3b2006-03-31 02:30:32 -080048 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -080049
Joe Korty68eef3b2006-03-31 02:30:32 -080050 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
Kay Sieversedfaa7c2007-05-21 22:08:01 +020051 mutex_lock(&block_class_lock);
Joe Korty68eef3b2006-03-31 02:30:32 -080052 for (dp = major_names[offset]; dp; dp = dp->next)
Tejun Heocf771cb2008-09-03 09:01:09 +020053 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Kay Sieversedfaa7c2007-05-21 22:08:01 +020054 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
Joe Korty68eef3b2006-03-31 02:30:32 -080057#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59int register_blkdev(unsigned int major, const char *name)
60{
61 struct blk_major_name **n, *p;
62 int index, ret = 0;
63
Kay Sieversedfaa7c2007-05-21 22:08:01 +020064 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 /* temporary */
67 if (major == 0) {
68 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
69 if (major_names[index] == NULL)
70 break;
71 }
72
73 if (index == 0) {
74 printk("register_blkdev: failed to get major for %s\n",
75 name);
76 ret = -EBUSY;
77 goto out;
78 }
79 major = index;
80 ret = major;
81 }
82
83 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
84 if (p == NULL) {
85 ret = -ENOMEM;
86 goto out;
87 }
88
89 p->major = major;
90 strlcpy(p->name, name, sizeof(p->name));
91 p->next = NULL;
92 index = major_to_index(major);
93
94 for (n = &major_names[index]; *n; n = &(*n)->next) {
95 if ((*n)->major == major)
96 break;
97 }
98 if (!*n)
99 *n = p;
100 else
101 ret = -EBUSY;
102
103 if (ret < 0) {
104 printk("register_blkdev: cannot get major %d for %s\n",
105 major, name);
106 kfree(p);
107 }
108out:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200109 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return ret;
111}
112
113EXPORT_SYMBOL(register_blkdev);
114
Akinobu Mitaf4480242007-07-17 04:03:47 -0700115void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct blk_major_name **n;
118 struct blk_major_name *p = NULL;
119 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200121 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 for (n = &major_names[index]; *n; n = &(*n)->next)
123 if ((*n)->major == major)
124 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700125 if (!*n || strcmp((*n)->name, name)) {
126 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700127 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 p = *n;
129 *n = p->next;
130 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200131 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135EXPORT_SYMBOL(unregister_blkdev);
136
137static struct kobj_map *bdev_map;
138
139/*
140 * Register device numbers dev..(dev+range-1)
141 * range must be nonzero
142 * The hash chain is sorted on range, so that subranges can override.
143 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200144void blk_register_region(dev_t devt, unsigned long range, struct module *module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct kobject *(*probe)(dev_t, int *, void *),
146 int (*lock)(dev_t, void *), void *data)
147{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200148 kobj_map(bdev_map, devt, range, module, probe, lock, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151EXPORT_SYMBOL(blk_register_region);
152
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200153void blk_unregister_region(dev_t devt, unsigned long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200155 kobj_unmap(bdev_map, devt, range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158EXPORT_SYMBOL(blk_unregister_region);
159
Tejun Heocf771cb2008-09-03 09:01:09 +0200160static struct kobject *exact_match(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct gendisk *p = data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200163
164 return &p->dev.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200167static int exact_lock(dev_t devt, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct gendisk *p = data;
170
171 if (!get_disk(p))
172 return -1;
173 return 0;
174}
175
176/**
177 * add_disk - add partitioning information to kernel list
178 * @disk: per-device partitioning information
179 *
180 * This function registers the partitioning information in @disk
181 * with the kernel.
182 */
183void add_disk(struct gendisk *disk)
184{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700185 struct backing_dev_info *bdi;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400186 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 disk->flags |= GENHD_FL_UP;
Tejun Heof331c022008-09-03 09:01:48 +0200189 disk->dev.devt = MKDEV(disk->major, disk->first_minor);
190 blk_register_region(disk_devt(disk), disk->minors, NULL,
191 exact_match, exact_lock, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 register_disk(disk);
193 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700194
195 bdi = &disk->queue->backing_dev_info;
Tejun Heof331c022008-09-03 09:01:48 +0200196 bdi_register_dev(bdi, disk_devt(disk));
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400197 retval = sysfs_create_link(&disk->dev.kobj, &bdi->dev->kobj, "bdi");
198 WARN_ON(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201EXPORT_SYMBOL(add_disk);
202EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
203
204void unlink_gendisk(struct gendisk *disk)
205{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700206 sysfs_remove_link(&disk->dev.kobj, "bdi");
207 bdi_unregister(&disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 blk_unregister_queue(disk);
Tejun Heof331c022008-09-03 09:01:48 +0200209 blk_unregister_region(disk_devt(disk), disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/**
213 * get_gendisk - get partitioning information for a given device
Randy Dunlap710027a2008-08-19 20:13:11 +0200214 * @devt: device to get partitioning information for
215 * @part: returned partition index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 *
217 * This function gets the structure containing partitioning
Randy Dunlap710027a2008-08-19 20:13:11 +0200218 * information for the given device @devt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
Tejun Heocf771cb2008-09-03 09:01:09 +0200220struct gendisk *get_gendisk(dev_t devt, int *partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Tejun Heocf771cb2008-09-03 09:01:09 +0200222 struct kobject *kobj = kobj_lookup(bdev_map, devt, partno);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200223 struct device *dev = kobj_to_dev(kobj);
224
225 return kobj ? dev_to_disk(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Tejun Heof331c022008-09-03 09:01:48 +0200228/**
229 * bdget_disk - do bdget() by gendisk and partition number
230 * @disk: gendisk of interest
231 * @partno: partition number
232 *
233 * Find partition @partno from @disk, do bdget() on it.
234 *
235 * CONTEXT:
236 * Don't care.
237 *
238 * RETURNS:
239 * Resulting block_device on success, NULL on failure.
240 */
241extern struct block_device *bdget_disk(struct gendisk *disk, int partno)
242{
243 dev_t devt = MKDEV(0, 0);
244
245 if (partno == 0)
246 devt = disk_devt(disk);
247 else {
248 struct hd_struct *part = disk->part[partno - 1];
249
250 if (part && part->nr_sects)
251 devt = part_devt(part);
252 }
253
254 if (likely(devt != MKDEV(0, 0)))
255 return bdget(devt);
256 return NULL;
257}
258EXPORT_SYMBOL(bdget_disk);
259
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700260/*
261 * print a full list of all partitions - intended for places where the root
262 * filesystem can't be mounted and thus to give the victim some idea of what
263 * went wrong
264 */
265void __init printk_all_partitions(void)
266{
Tejun Heodef4e382008-09-03 08:57:12 +0200267 struct class_dev_iter iter;
268 struct device *dev;
269
270 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
271 while ((dev = class_dev_iter_next(&iter))) {
272 struct gendisk *disk = dev_to_disk(dev);
273 char buf[BDEVNAME_SIZE];
274 int n;
275
276 /*
277 * Don't show empty devices or things that have been
278 * surpressed
279 */
280 if (get_capacity(disk) == 0 ||
281 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
282 continue;
283
284 /*
285 * Note, unlike /proc/partitions, I am showing the
286 * numbers in hex - the same format as the root=
287 * option takes.
288 */
289 printk("%02x%02x %10llu %s",
Tejun Heof331c022008-09-03 09:01:48 +0200290 MAJOR(disk_devt(disk)), MINOR(disk_devt(disk)),
Tejun Heodef4e382008-09-03 08:57:12 +0200291 (unsigned long long)get_capacity(disk) >> 1,
292 disk_name(disk, 0, buf));
293 if (disk->driverfs_dev != NULL &&
294 disk->driverfs_dev->driver != NULL)
295 printk(" driver: %s\n",
296 disk->driverfs_dev->driver->name);
297 else
298 printk(" (driver?)\n");
299
300 /* now show the partitions */
Tejun Heof331c022008-09-03 09:01:48 +0200301 for (n = 0; n < disk_max_parts(disk); ++n) {
302 struct hd_struct *part = disk->part[n];
303
304 if (!part || !part->nr_sects)
Tejun Heodef4e382008-09-03 08:57:12 +0200305 continue;
306 printk(" %02x%02x %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200307 MAJOR(part_devt(part)), MINOR(part_devt(part)),
308 (unsigned long long)part->nr_sects >> 1,
309 disk_name(disk, part->partno, buf));
Tejun Heodef4e382008-09-03 08:57:12 +0200310 }
311 }
312 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#ifdef CONFIG_PROC_FS
316/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200317static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400318{
Tejun Heodef4e382008-09-03 08:57:12 +0200319 loff_t skip = *pos;
320 struct class_dev_iter *iter;
321 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400322
Tejun Heodef4e382008-09-03 08:57:12 +0200323 iter = kmalloc(GFP_KERNEL, sizeof(*iter));
324 if (!iter)
325 return ERR_PTR(-ENOMEM);
326
327 seqf->private = iter;
328 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
329 do {
330 dev = class_dev_iter_next(iter);
331 if (!dev)
332 return NULL;
333 } while (skip--);
334
335 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400336}
337
Tejun Heodef4e382008-09-03 08:57:12 +0200338static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200340 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400341
Tejun Heodef4e382008-09-03 08:57:12 +0200342 (*pos)++;
343 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200344 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400345 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return NULL;
348}
349
Tejun Heodef4e382008-09-03 08:57:12 +0200350static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400351{
Tejun Heodef4e382008-09-03 08:57:12 +0200352 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400353
Tejun Heodef4e382008-09-03 08:57:12 +0200354 /* stop is called even after start failed :-( */
355 if (iter) {
356 class_dev_iter_exit(iter);
357 kfree(iter);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Tejun Heodef4e382008-09-03 08:57:12 +0200361static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Tejun Heodef4e382008-09-03 08:57:12 +0200363 static void *p;
364
365 p = disk_seqf_start(seqf, pos);
366 if (!IS_ERR(p) && p)
367 seq_puts(seqf, "major minor #blocks name\n\n");
368 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Tejun Heocf771cb2008-09-03 09:01:09 +0200371static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 struct gendisk *sgp = v;
374 int n;
375 char buf[BDEVNAME_SIZE];
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heof331c022008-09-03 09:01:48 +0200378 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
379 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return 0;
381 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
382 return 0;
383
384 /* show the full disk and all non-0 size partitions of it */
Tejun Heocf771cb2008-09-03 09:01:09 +0200385 seq_printf(seqf, "%4d %4d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200386 MAJOR(disk_devt(sgp)), MINOR(disk_devt(sgp)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 (unsigned long long)get_capacity(sgp) >> 1,
388 disk_name(sgp, 0, buf));
Tejun Heof331c022008-09-03 09:01:48 +0200389 for (n = 0; n < disk_max_parts(sgp); n++) {
390 struct hd_struct *part = sgp->part[n];
391 if (!part)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 continue;
Tejun Heof331c022008-09-03 09:01:48 +0200393 if (part->nr_sects == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 continue;
Tejun Heocf771cb2008-09-03 09:01:09 +0200395 seq_printf(seqf, "%4d %4d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200396 MAJOR(part_devt(part)), MINOR(part_devt(part)),
397 (unsigned long long)part->nr_sects >> 1,
398 disk_name(sgp, part->partno, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 return 0;
402}
403
Jan Engelhardt12f32bb2008-01-29 20:57:51 +0100404const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200405 .start = show_partition_start,
406 .next = disk_seqf_next,
407 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200408 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409};
410#endif
411
412
Tejun Heocf771cb2008-09-03 09:01:09 +0200413static struct kobject *base_probe(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200415 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /* Make old-style 2.4 aliases work */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200417 request_module("block-major-%d", MAJOR(devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return NULL;
419}
420
421static int __init genhd_device_init(void)
422{
Dan Williamse105b8b2008-04-21 10:51:07 -0700423 int error;
424
425 block_class.dev_kobj = sysfs_dev_block_kobj;
426 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -0700427 if (unlikely(error))
428 return error;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200429 bdev_map = kobj_map_init(base_probe, &block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200431
432#ifndef CONFIG_SYSFS_DEPRECATED
433 /* create top-level block dir */
434 block_depr = kobject_create_and_add("block", NULL);
435#endif
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439subsys_initcall(genhd_device_init);
440
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200441static ssize_t disk_range_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200444 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200446 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200449static ssize_t disk_removable_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200451{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200452 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200453
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200454 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200456}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Kay Sievers1c9ce522008-06-13 09:41:00 +0200458static ssize_t disk_ro_show(struct device *dev,
459 struct device_attribute *attr, char *buf)
460{
461 struct gendisk *disk = dev_to_disk(dev);
462
463 return sprintf(buf, "%d\n", disk->policy ? 1 : 0);
464}
465
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200466static ssize_t disk_size_show(struct device *dev,
467 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200469 struct gendisk *disk = dev_to_disk(dev);
470
471 return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200473
474static ssize_t disk_capability_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700476{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200477 struct gendisk *disk = dev_to_disk(dev);
478
479 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700480}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200481
482static ssize_t disk_stat_show(struct device *dev,
483 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200485 struct gendisk *disk = dev_to_disk(dev);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 preempt_disable();
488 disk_round_stats(disk);
489 preempt_enable();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200490 return sprintf(buf,
Ben Woodard837c7872006-03-22 08:09:31 +0100491 "%8lu %8lu %8llu %8u "
492 "%8lu %8lu %8llu %8u "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 "%8u %8u %8u"
494 "\n",
Jens Axboe47a00412005-11-09 13:38:47 +0100495 disk_stat_read(disk, ios[READ]),
496 disk_stat_read(disk, merges[READ]),
497 (unsigned long long)disk_stat_read(disk, sectors[READ]),
498 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
499 disk_stat_read(disk, ios[WRITE]),
500 disk_stat_read(disk, merges[WRITE]),
501 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
502 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 disk->in_flight,
504 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
505 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
506}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Akinobu Mitac17bb492006-12-08 02:39:46 -0800508#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200509static ssize_t disk_fail_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
511{
512 struct gendisk *disk = dev_to_disk(dev);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800513
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200514 return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
515}
516
517static ssize_t disk_fail_store(struct device *dev,
518 struct device_attribute *attr,
Akinobu Mitac17bb492006-12-08 02:39:46 -0800519 const char *buf, size_t count)
520{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200521 struct gendisk *disk = dev_to_disk(dev);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800522 int i;
523
524 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
525 if (i == 0)
526 disk->flags &= ~GENHD_FL_FAIL;
527 else
528 disk->flags |= GENHD_FL_FAIL;
529 }
530
531 return count;
532}
Akinobu Mitac17bb492006-12-08 02:39:46 -0800533
534#endif
535
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200536static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
537static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200538static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200539static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL);
540static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
541static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800542#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200543static struct device_attribute dev_attr_fail =
544 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800545#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200546
547static struct attribute *disk_attrs[] = {
548 &dev_attr_range.attr,
549 &dev_attr_removable.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +0200550 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200551 &dev_attr_size.attr,
552 &dev_attr_capability.attr,
553 &dev_attr_stat.attr,
554#ifdef CONFIG_FAIL_MAKE_REQUEST
555 &dev_attr_fail.attr,
556#endif
557 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558};
559
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200560static struct attribute_group disk_attr_group = {
561 .attrs = disk_attrs,
562};
563
564static struct attribute_group *disk_attr_groups[] = {
565 &disk_attr_group,
566 NULL
567};
568
569static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200571 struct gendisk *disk = dev_to_disk(dev);
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 kfree(disk->random);
574 kfree(disk->part);
575 free_disk_stats(disk);
576 kfree(disk);
577}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200578struct class block_class = {
579 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580};
581
Adrian Bunk1826ead2008-03-04 11:23:46 +0100582static struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200583 .name = "disk",
584 .groups = disk_attr_groups,
585 .release = disk_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586};
587
Randy Dunlapa6e2ba82008-05-23 09:44:11 -0700588#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200589/*
590 * aggregate disk stat collector. Uses the same stats that the sysfs
591 * entries do, above, but makes them available through one seq_file.
592 *
593 * The output looks suspiciously like /proc/partitions with a bunch of
594 * extra fields.
595 */
596static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 struct gendisk *gp = v;
599 char buf[BDEVNAME_SIZE];
Tejun Heo310a2c12008-08-25 19:47:17 +0900600 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /*
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200603 if (&gp->dev.kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200604 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 " rio rmerge rsect ruse wio wmerge "
606 "wsect wuse running use aveq"
607 "\n\n");
608 */
609
610 preempt_disable();
611 disk_round_stats(gp);
612 preempt_enable();
Tejun Heocf771cb2008-09-03 09:01:09 +0200613 seq_printf(seqf, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
Tejun Heof331c022008-09-03 09:01:48 +0200614 MAJOR(disk_devt(gp)), MINOR(disk_devt(gp)),
615 disk_name(gp, 0, buf),
Jens Axboea3623572005-11-01 09:26:16 +0100616 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
617 (unsigned long long)disk_stat_read(gp, sectors[0]),
618 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
619 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
620 (unsigned long long)disk_stat_read(gp, sectors[1]),
621 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 gp->in_flight,
623 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
624 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
625
626 /* now show all non-0 size partitions of it */
Tejun Heof331c022008-09-03 09:01:48 +0200627 for (n = 0; n < disk_max_parts(gp); n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct hd_struct *hd = gp->part[n];
629
Jerome Marchand28f39d52008-02-08 11:04:56 +0100630 if (!hd || !hd->nr_sects)
631 continue;
632
633 preempt_disable();
634 part_round_stats(hd);
635 preempt_enable();
Tejun Heocf771cb2008-09-03 09:01:09 +0200636 seq_printf(seqf, "%4d %4d %s %lu %lu %llu "
Jerome Marchand28f39d52008-02-08 11:04:56 +0100637 "%u %lu %lu %llu %u %u %u %u\n",
Tejun Heof331c022008-09-03 09:01:48 +0200638 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
639 disk_name(gp, hd->partno, buf),
Jerome Marchand28f39d52008-02-08 11:04:56 +0100640 part_stat_read(hd, ios[0]),
641 part_stat_read(hd, merges[0]),
642 (unsigned long long)part_stat_read(hd, sectors[0]),
643 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
644 part_stat_read(hd, ios[1]),
645 part_stat_read(hd, merges[1]),
646 (unsigned long long)part_stat_read(hd, sectors[1]),
647 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
648 hd->in_flight,
649 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
650 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
651 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
654 return 0;
655}
656
Jan Engelhardt12f32bb2008-01-29 20:57:51 +0100657const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200658 .start = disk_seqf_start,
659 .next = disk_seqf_next,
660 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 .show = diskstats_show
662};
Randy Dunlapa6e2ba82008-05-23 09:44:11 -0700663#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700665static void media_change_notify_thread(struct work_struct *work)
666{
667 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
668 char event[] = "MEDIA_CHANGE=1";
669 char *envp[] = { event, NULL };
670
671 /*
672 * set enviroment vars to indicate which event this is for
673 * so that user space will know to go check the media status.
674 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200675 kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp);
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700676 put_device(gd->driverfs_dev);
677}
678
Adrian Bunk1826ead2008-03-04 11:23:46 +0100679#if 0
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700680void genhd_media_change_notify(struct gendisk *disk)
681{
682 get_device(disk->driverfs_dev);
683 schedule_work(&disk->async_notify);
684}
685EXPORT_SYMBOL_GPL(genhd_media_change_notify);
Adrian Bunk1826ead2008-03-04 11:23:46 +0100686#endif /* 0 */
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700687
Tejun Heocf771cb2008-09-03 09:01:09 +0200688dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200689{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200690 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +0200691 struct class_dev_iter iter;
692 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200693
Tejun Heodef4e382008-09-03 08:57:12 +0200694 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
695 while ((dev = class_dev_iter_next(&iter))) {
696 struct gendisk *disk = dev_to_disk(dev);
697
Tejun Heof331c022008-09-03 09:01:48 +0200698 if (strcmp(dev->bus_id, name))
699 continue;
700 if (partno < 0 || partno > disk_max_parts(disk))
701 continue;
702
703 if (partno == 0)
704 devt = disk_devt(disk);
705 else {
706 struct hd_struct *part = disk->part[partno - 1];
707
708 if (!part || !part->nr_sects)
709 continue;
710
711 devt = part_devt(part);
Tejun Heodef4e382008-09-03 08:57:12 +0200712 }
Tejun Heof331c022008-09-03 09:01:48 +0200713 break;
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200714 }
Tejun Heodef4e382008-09-03 08:57:12 +0200715 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200716 return devt;
717}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200718EXPORT_SYMBOL(blk_lookup_devt);
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720struct gendisk *alloc_disk(int minors)
721{
Christoph Lameter19460892005-06-23 00:08:19 -0700722 return alloc_disk_node(minors, -1);
723}
724
725struct gendisk *alloc_disk_node(int minors, int node_id)
726{
727 struct gendisk *disk;
728
Christoph Lameter94f60302007-07-17 04:03:29 -0700729 disk = kmalloc_node(sizeof(struct gendisk),
730 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (disk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (!init_disk_stats(disk)) {
733 kfree(disk);
734 return NULL;
735 }
736 if (minors > 1) {
737 int size = (minors - 1) * sizeof(struct hd_struct *);
Christoph Lameter94f60302007-07-17 04:03:29 -0700738 disk->part = kmalloc_node(size,
739 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (!disk->part) {
Jerome Marchandc7674032007-11-23 09:17:53 +0100741 free_disk_stats(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 kfree(disk);
743 return NULL;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 disk->minors = minors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 rand_initialize_disk(disk);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200748 disk->dev.class = &block_class;
749 disk->dev.type = &disk_type;
750 device_initialize(&disk->dev);
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700751 INIT_WORK(&disk->async_notify,
752 media_change_notify_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754 return disk;
755}
756
757EXPORT_SYMBOL(alloc_disk);
Christoph Lameter19460892005-06-23 00:08:19 -0700758EXPORT_SYMBOL(alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760struct kobject *get_disk(struct gendisk *disk)
761{
762 struct module *owner;
763 struct kobject *kobj;
764
765 if (!disk->fops)
766 return NULL;
767 owner = disk->fops->owner;
768 if (owner && !try_module_get(owner))
769 return NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200770 kobj = kobject_get(&disk->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (kobj == NULL) {
772 module_put(owner);
773 return NULL;
774 }
775 return kobj;
776
777}
778
779EXPORT_SYMBOL(get_disk);
780
781void put_disk(struct gendisk *disk)
782{
783 if (disk)
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200784 kobject_put(&disk->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787EXPORT_SYMBOL(put_disk);
788
789void set_device_ro(struct block_device *bdev, int flag)
790{
791 if (bdev->bd_contains != bdev)
792 bdev->bd_part->policy = flag;
793 else
794 bdev->bd_disk->policy = flag;
795}
796
797EXPORT_SYMBOL(set_device_ro);
798
799void set_disk_ro(struct gendisk *disk, int flag)
800{
801 int i;
802 disk->policy = flag;
Tejun Heof331c022008-09-03 09:01:48 +0200803 for (i = 0; i < disk_max_parts(disk); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (disk->part[i]) disk->part[i]->policy = flag;
805}
806
807EXPORT_SYMBOL(set_disk_ro);
808
809int bdev_read_only(struct block_device *bdev)
810{
811 if (!bdev)
812 return 0;
813 else if (bdev->bd_contains != bdev)
814 return bdev->bd_part->policy;
815 else
816 return bdev->bd_disk->policy;
817}
818
819EXPORT_SYMBOL(bdev_read_only);
820
Tejun Heocf771cb2008-09-03 09:01:09 +0200821int invalidate_partition(struct gendisk *disk, int partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 int res = 0;
Tejun Heocf771cb2008-09-03 09:01:09 +0200824 struct block_device *bdev = bdget_disk(disk, partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -0700826 fsync_bdev(bdev);
827 res = __invalidate_device(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 bdput(bdev);
829 }
830 return res;
831}
832
833EXPORT_SYMBOL(invalidate_partition);