blob: 36bd3e12a6d4c58bdda0dde62237a50d84b93d09 [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>
8#include <linux/kernel.h>
9#include <linux/blkdev.h>
10#include <linux/init.h>
11#include <linux/spinlock.h>
12#include <linux/seq_file.h>
13#include <linux/slab.h>
14#include <linux/kmod.h>
15#include <linux/kobj_map.h>
Christoph Hellwig2ef41632005-05-05 16:15:59 -070016#include <linux/buffer_head.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080017#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Kay Sieversb9d9c822006-06-15 15:31:56 +020019struct subsystem block_subsys;
Jes Sorensen58383af2006-02-06 14:12:43 -080020static DEFINE_MUTEX(block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/*
23 * Can be deleted altogether. Later.
24 *
25 */
26static struct blk_major_name {
27 struct blk_major_name *next;
28 int major;
29 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -080030} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/* index in the above - for now: assume no multimajor ranges */
33static inline int major_to_index(int major)
34{
Joe Korty68eef3b2006-03-31 02:30:32 -080035 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Joe Korty68eef3b2006-03-31 02:30:32 -080038#ifdef CONFIG_PROC_FS
Neil Horman7170be52006-01-14 13:20:38 -080039
Joe Korty68eef3b2006-03-31 02:30:32 -080040void blkdev_show(struct seq_file *f, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -080041{
Joe Korty68eef3b2006-03-31 02:30:32 -080042 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -080043
Joe Korty68eef3b2006-03-31 02:30:32 -080044 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
45 mutex_lock(&block_subsys_lock);
46 for (dp = major_names[offset]; dp; dp = dp->next)
47 seq_printf(f, "%3d %s\n", dp->major, dp->name);
48 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
Neil Horman7170be52006-01-14 13:20:38 -080051
Joe Korty68eef3b2006-03-31 02:30:32 -080052#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54int register_blkdev(unsigned int major, const char *name)
55{
56 struct blk_major_name **n, *p;
57 int index, ret = 0;
58
Jes Sorensen58383af2006-02-06 14:12:43 -080059 mutex_lock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 /* temporary */
62 if (major == 0) {
63 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
Andrew Mortonfdf892b2007-02-12 00:51:44 -080064 /*
65 * Disallow the LANANA-assigned LOCAL/EXPERIMENTAL
66 * majors
67 */
68 if ((60 <= index && index <= 63) ||
69 (120 <= index && index <= 127) ||
70 (240 <= index && index <= 254))
71 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (major_names[index] == NULL)
73 break;
74 }
75
76 if (index == 0) {
77 printk("register_blkdev: failed to get major for %s\n",
78 name);
79 ret = -EBUSY;
80 goto out;
81 }
82 major = index;
83 ret = major;
84 }
85
86 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
87 if (p == NULL) {
88 ret = -ENOMEM;
89 goto out;
90 }
91
92 p->major = major;
93 strlcpy(p->name, name, sizeof(p->name));
94 p->next = NULL;
95 index = major_to_index(major);
96
97 for (n = &major_names[index]; *n; n = &(*n)->next) {
98 if ((*n)->major == major)
99 break;
100 }
101 if (!*n)
102 *n = p;
103 else
104 ret = -EBUSY;
105
106 if (ret < 0) {
107 printk("register_blkdev: cannot get major %d for %s\n",
108 major, name);
109 kfree(p);
110 }
111out:
Jes Sorensen58383af2006-02-06 14:12:43 -0800112 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return ret;
114}
115
116EXPORT_SYMBOL(register_blkdev);
117
118/* todo: make void - error printk here */
119int unregister_blkdev(unsigned int major, const char *name)
120{
121 struct blk_major_name **n;
122 struct blk_major_name *p = NULL;
123 int index = major_to_index(major);
124 int ret = 0;
125
Jes Sorensen58383af2006-02-06 14:12:43 -0800126 mutex_lock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 for (n = &major_names[index]; *n; n = &(*n)->next)
128 if ((*n)->major == major)
129 break;
130 if (!*n || strcmp((*n)->name, name))
131 ret = -EINVAL;
132 else {
133 p = *n;
134 *n = p->next;
135 }
Jes Sorensen58383af2006-02-06 14:12:43 -0800136 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 kfree(p);
138
139 return ret;
140}
141
142EXPORT_SYMBOL(unregister_blkdev);
143
144static struct kobj_map *bdev_map;
145
146/*
147 * Register device numbers dev..(dev+range-1)
148 * range must be nonzero
149 * The hash chain is sorted on range, so that subranges can override.
150 */
151void blk_register_region(dev_t dev, unsigned long range, struct module *module,
152 struct kobject *(*probe)(dev_t, int *, void *),
153 int (*lock)(dev_t, void *), void *data)
154{
155 kobj_map(bdev_map, dev, range, module, probe, lock, data);
156}
157
158EXPORT_SYMBOL(blk_register_region);
159
160void blk_unregister_region(dev_t dev, unsigned long range)
161{
162 kobj_unmap(bdev_map, dev, range);
163}
164
165EXPORT_SYMBOL(blk_unregister_region);
166
167static struct kobject *exact_match(dev_t dev, int *part, void *data)
168{
169 struct gendisk *p = data;
170 return &p->kobj;
171}
172
173static int exact_lock(dev_t dev, void *data)
174{
175 struct gendisk *p = data;
176
177 if (!get_disk(p))
178 return -1;
179 return 0;
180}
181
182/**
183 * add_disk - add partitioning information to kernel list
184 * @disk: per-device partitioning information
185 *
186 * This function registers the partitioning information in @disk
187 * with the kernel.
188 */
189void add_disk(struct gendisk *disk)
190{
191 disk->flags |= GENHD_FL_UP;
192 blk_register_region(MKDEV(disk->major, disk->first_minor),
193 disk->minors, NULL, exact_match, exact_lock, disk);
194 register_disk(disk);
195 blk_register_queue(disk);
196}
197
198EXPORT_SYMBOL(add_disk);
199EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
200
201void unlink_gendisk(struct gendisk *disk)
202{
203 blk_unregister_queue(disk);
204 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
205 disk->minors);
206}
207
208#define to_disk(obj) container_of(obj,struct gendisk,kobj)
209
210/**
211 * get_gendisk - get partitioning information for a given device
212 * @dev: device to get partitioning information for
213 *
214 * This function gets the structure containing partitioning
215 * information for the given device @dev.
216 */
217struct gendisk *get_gendisk(dev_t dev, int *part)
218{
219 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
220 return kobj ? to_disk(kobj) : NULL;
221}
222
223#ifdef CONFIG_PROC_FS
224/* iterator */
225static void *part_start(struct seq_file *part, loff_t *pos)
226{
227 struct list_head *p;
228 loff_t l = *pos;
229
Jes Sorensen58383af2006-02-06 14:12:43 -0800230 mutex_lock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 list_for_each(p, &block_subsys.kset.list)
232 if (!l--)
233 return list_entry(p, struct gendisk, kobj.entry);
234 return NULL;
235}
236
237static void *part_next(struct seq_file *part, void *v, loff_t *pos)
238{
239 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
240 ++*pos;
241 return p==&block_subsys.kset.list ? NULL :
242 list_entry(p, struct gendisk, kobj.entry);
243}
244
245static void part_stop(struct seq_file *part, void *v)
246{
Jes Sorensen58383af2006-02-06 14:12:43 -0800247 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250static int show_partition(struct seq_file *part, void *v)
251{
252 struct gendisk *sgp = v;
253 int n;
254 char buf[BDEVNAME_SIZE];
255
256 if (&sgp->kobj.entry == block_subsys.kset.list.next)
257 seq_puts(part, "major minor #blocks name\n\n");
258
259 /* Don't show non-partitionable removeable devices or empty devices */
260 if (!get_capacity(sgp) ||
261 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
262 return 0;
263 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
264 return 0;
265
266 /* show the full disk and all non-0 size partitions of it */
267 seq_printf(part, "%4d %4d %10llu %s\n",
268 sgp->major, sgp->first_minor,
269 (unsigned long long)get_capacity(sgp) >> 1,
270 disk_name(sgp, 0, buf));
271 for (n = 0; n < sgp->minors - 1; n++) {
272 if (!sgp->part[n])
273 continue;
274 if (sgp->part[n]->nr_sects == 0)
275 continue;
276 seq_printf(part, "%4d %4d %10llu %s\n",
277 sgp->major, n + 1 + sgp->first_minor,
278 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
279 disk_name(sgp, n + 1, buf));
280 }
281
282 return 0;
283}
284
285struct seq_operations partitions_op = {
286 .start =part_start,
287 .next = part_next,
288 .stop = part_stop,
289 .show = show_partition
290};
291#endif
292
293
294extern int blk_dev_init(void);
295
296static struct kobject *base_probe(dev_t dev, int *part, void *data)
297{
298 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
299 /* Make old-style 2.4 aliases work */
300 request_module("block-major-%d", MAJOR(dev));
301 return NULL;
302}
303
304static int __init genhd_device_init(void)
305{
Randy Dunlap87a57262006-09-29 01:58:56 -0700306 int err;
307
Jes Sorensen58383af2006-02-06 14:12:43 -0800308 bdev_map = kobj_map_init(base_probe, &block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 blk_dev_init();
Randy Dunlap87a57262006-09-29 01:58:56 -0700310 err = subsystem_register(&block_subsys);
311 if (err < 0)
312 printk(KERN_WARNING "%s: subsystem_register error: %d\n",
313 __FUNCTION__, err);
314 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
317subsys_initcall(genhd_device_init);
318
319
320
321/*
322 * kobject & sysfs bindings for block devices
323 */
324static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
325 char *page)
326{
327 struct gendisk *disk = to_disk(kobj);
328 struct disk_attribute *disk_attr =
329 container_of(attr,struct disk_attribute,attr);
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -0500330 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (disk_attr->show)
333 ret = disk_attr->show(disk,page);
334 return ret;
335}
336
Kay Sieversa7fd6702005-10-01 14:49:43 +0200337static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr,
338 const char *page, size_t count)
339{
340 struct gendisk *disk = to_disk(kobj);
341 struct disk_attribute *disk_attr =
342 container_of(attr,struct disk_attribute,attr);
343 ssize_t ret = 0;
344
345 if (disk_attr->store)
346 ret = disk_attr->store(disk, page, count);
347 return ret;
348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static struct sysfs_ops disk_sysfs_ops = {
351 .show = &disk_attr_show,
Kay Sieversa7fd6702005-10-01 14:49:43 +0200352 .store = &disk_attr_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353};
354
Kay Sieversa7fd6702005-10-01 14:49:43 +0200355static ssize_t disk_uevent_store(struct gendisk * disk,
356 const char *buf, size_t count)
357{
Kay Sievers312c0042005-11-16 09:00:00 +0100358 kobject_uevent(&disk->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200359 return count;
360}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static ssize_t disk_dev_read(struct gendisk * disk, char *page)
362{
363 dev_t base = MKDEV(disk->major, disk->first_minor);
364 return print_dev_t(page, base);
365}
366static ssize_t disk_range_read(struct gendisk * disk, char *page)
367{
368 return sprintf(page, "%d\n", disk->minors);
369}
370static ssize_t disk_removable_read(struct gendisk * disk, char *page)
371{
372 return sprintf(page, "%d\n",
373 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
374
375}
376static ssize_t disk_size_read(struct gendisk * disk, char *page)
377{
378 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
379}
380
381static ssize_t disk_stats_read(struct gendisk * disk, char *page)
382{
383 preempt_disable();
384 disk_round_stats(disk);
385 preempt_enable();
386 return sprintf(page,
Ben Woodard837c7872006-03-22 08:09:31 +0100387 "%8lu %8lu %8llu %8u "
388 "%8lu %8lu %8llu %8u "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 "%8u %8u %8u"
390 "\n",
Jens Axboe47a00412005-11-09 13:38:47 +0100391 disk_stat_read(disk, ios[READ]),
392 disk_stat_read(disk, merges[READ]),
393 (unsigned long long)disk_stat_read(disk, sectors[READ]),
394 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
395 disk_stat_read(disk, ios[WRITE]),
396 disk_stat_read(disk, merges[WRITE]),
397 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
398 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 disk->in_flight,
400 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
401 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
402}
Kay Sieversa7fd6702005-10-01 14:49:43 +0200403static struct disk_attribute disk_attr_uevent = {
404 .attr = {.name = "uevent", .mode = S_IWUSR },
405 .store = disk_uevent_store
406};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static struct disk_attribute disk_attr_dev = {
408 .attr = {.name = "dev", .mode = S_IRUGO },
409 .show = disk_dev_read
410};
411static struct disk_attribute disk_attr_range = {
412 .attr = {.name = "range", .mode = S_IRUGO },
413 .show = disk_range_read
414};
415static struct disk_attribute disk_attr_removable = {
416 .attr = {.name = "removable", .mode = S_IRUGO },
417 .show = disk_removable_read
418};
419static struct disk_attribute disk_attr_size = {
420 .attr = {.name = "size", .mode = S_IRUGO },
421 .show = disk_size_read
422};
423static struct disk_attribute disk_attr_stat = {
424 .attr = {.name = "stat", .mode = S_IRUGO },
425 .show = disk_stats_read
426};
427
Akinobu Mitac17bb492006-12-08 02:39:46 -0800428#ifdef CONFIG_FAIL_MAKE_REQUEST
429
430static ssize_t disk_fail_store(struct gendisk * disk,
431 const char *buf, size_t count)
432{
433 int i;
434
435 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
436 if (i == 0)
437 disk->flags &= ~GENHD_FL_FAIL;
438 else
439 disk->flags |= GENHD_FL_FAIL;
440 }
441
442 return count;
443}
444static ssize_t disk_fail_read(struct gendisk * disk, char *page)
445{
446 return sprintf(page, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
447}
448static struct disk_attribute disk_attr_fail = {
449 .attr = {.name = "make-it-fail", .mode = S_IRUGO | S_IWUSR },
450 .store = disk_fail_store,
451 .show = disk_fail_read
452};
453
454#endif
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456static struct attribute * default_attrs[] = {
Kay Sieversa7fd6702005-10-01 14:49:43 +0200457 &disk_attr_uevent.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 &disk_attr_dev.attr,
459 &disk_attr_range.attr,
460 &disk_attr_removable.attr,
461 &disk_attr_size.attr,
462 &disk_attr_stat.attr,
Akinobu Mitac17bb492006-12-08 02:39:46 -0800463#ifdef CONFIG_FAIL_MAKE_REQUEST
464 &disk_attr_fail.attr,
465#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 NULL,
467};
468
469static void disk_release(struct kobject * kobj)
470{
471 struct gendisk *disk = to_disk(kobj);
472 kfree(disk->random);
473 kfree(disk->part);
474 free_disk_stats(disk);
475 kfree(disk);
476}
477
478static struct kobj_type ktype_block = {
479 .release = disk_release,
480 .sysfs_ops = &disk_sysfs_ops,
481 .default_attrs = default_attrs,
482};
483
484extern struct kobj_type ktype_part;
485
Kay Sievers312c0042005-11-16 09:00:00 +0100486static int block_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct kobj_type *ktype = get_ktype(kobj);
489
490 return ((ktype == &ktype_block) || (ktype == &ktype_part));
491}
492
Kay Sievers312c0042005-11-16 09:00:00 +0100493static int block_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 int num_envp, char *buffer, int buffer_size)
495{
496 struct kobj_type *ktype = get_ktype(kobj);
497 struct device *physdev;
498 struct gendisk *disk;
499 struct hd_struct *part;
500 int length = 0;
501 int i = 0;
502
503 if (ktype == &ktype_block) {
504 disk = container_of(kobj, struct gendisk, kobj);
Kay Sievers312c0042005-11-16 09:00:00 +0100505 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
506 &length, "MINOR=%u", disk->first_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 } else if (ktype == &ktype_part) {
508 disk = container_of(kobj->parent, struct gendisk, kobj);
509 part = container_of(kobj, struct hd_struct, kobj);
Kay Sievers312c0042005-11-16 09:00:00 +0100510 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
511 &length, "MINOR=%u",
512 disk->first_minor + part->partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 } else
514 return 0;
515
Kay Sievers312c0042005-11-16 09:00:00 +0100516 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
517 "MAJOR=%u", disk->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 /* add physical device, backing this device */
520 physdev = disk->driverfs_dev;
521 if (physdev) {
522 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
523
Kay Sievers312c0042005-11-16 09:00:00 +0100524 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
525 &length, "PHYSDEVPATH=%s", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 kfree(path);
527
528 if (physdev->bus)
Kay Sievers312c0042005-11-16 09:00:00 +0100529 add_uevent_var(envp, num_envp, &i,
530 buffer, buffer_size, &length,
531 "PHYSDEVBUS=%s",
532 physdev->bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 if (physdev->driver)
Kay Sievers312c0042005-11-16 09:00:00 +0100535 add_uevent_var(envp, num_envp, &i,
536 buffer, buffer_size, &length,
537 "PHYSDEVDRIVER=%s",
538 physdev->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
541 /* terminate, set to next free slot, shrink available space */
542 envp[i] = NULL;
543 envp = &envp[i];
544 num_envp -= i;
545 buffer = &buffer[length];
546 buffer_size -= length;
547
548 return 0;
549}
550
Kay Sievers312c0042005-11-16 09:00:00 +0100551static struct kset_uevent_ops block_uevent_ops = {
552 .filter = block_uevent_filter,
553 .uevent = block_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554};
555
Kay Sieversb9d9c822006-06-15 15:31:56 +0200556decl_subsys(block, &ktype_block, &block_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558/*
559 * aggregate disk stat collector. Uses the same stats that the sysfs
560 * entries do, above, but makes them available through one seq_file.
561 * Watching a few disks may be efficient through sysfs, but watching
562 * all of them will be more efficient through this interface.
563 *
564 * The output looks suspiciously like /proc/partitions with a bunch of
565 * extra fields.
566 */
567
568/* iterator */
569static void *diskstats_start(struct seq_file *part, loff_t *pos)
570{
571 loff_t k = *pos;
572 struct list_head *p;
573
Jes Sorensen58383af2006-02-06 14:12:43 -0800574 mutex_lock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 list_for_each(p, &block_subsys.kset.list)
576 if (!k--)
577 return list_entry(p, struct gendisk, kobj.entry);
578 return NULL;
579}
580
581static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
582{
583 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
584 ++*pos;
585 return p==&block_subsys.kset.list ? NULL :
586 list_entry(p, struct gendisk, kobj.entry);
587}
588
589static void diskstats_stop(struct seq_file *part, void *v)
590{
Jes Sorensen58383af2006-02-06 14:12:43 -0800591 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594static int diskstats_show(struct seq_file *s, void *v)
595{
596 struct gendisk *gp = v;
597 char buf[BDEVNAME_SIZE];
598 int n = 0;
599
600 /*
601 if (&sgp->kobj.entry == block_subsys.kset.list.next)
602 seq_puts(s, "major minor name"
603 " rio rmerge rsect ruse wio wmerge "
604 "wsect wuse running use aveq"
605 "\n\n");
606 */
607
608 preempt_disable();
609 disk_round_stats(gp);
610 preempt_enable();
Ben Woodard837c7872006-03-22 08:09:31 +0100611 seq_printf(s, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
Jens Axboea3623572005-11-01 09:26:16 +0100613 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
614 (unsigned long long)disk_stat_read(gp, sectors[0]),
615 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
616 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
617 (unsigned long long)disk_stat_read(gp, sectors[1]),
618 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 gp->in_flight,
620 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
621 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
622
623 /* now show all non-0 size partitions of it */
624 for (n = 0; n < gp->minors - 1; n++) {
625 struct hd_struct *hd = gp->part[n];
626
627 if (hd && hd->nr_sects)
628 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
629 gp->major, n + gp->first_minor + 1,
630 disk_name(gp, n + 1, buf),
Jens Axboea3623572005-11-01 09:26:16 +0100631 hd->ios[0], hd->sectors[0],
632 hd->ios[1], hd->sectors[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634
635 return 0;
636}
637
638struct seq_operations diskstats_op = {
639 .start = diskstats_start,
640 .next = diskstats_next,
641 .stop = diskstats_stop,
642 .show = diskstats_show
643};
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645struct gendisk *alloc_disk(int minors)
646{
Christoph Lameter19460892005-06-23 00:08:19 -0700647 return alloc_disk_node(minors, -1);
648}
649
650struct gendisk *alloc_disk_node(int minors, int node_id)
651{
652 struct gendisk *disk;
653
654 disk = kmalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (disk) {
656 memset(disk, 0, sizeof(struct gendisk));
657 if (!init_disk_stats(disk)) {
658 kfree(disk);
659 return NULL;
660 }
661 if (minors > 1) {
662 int size = (minors - 1) * sizeof(struct hd_struct *);
Christoph Lameter19460892005-06-23 00:08:19 -0700663 disk->part = kmalloc_node(size, GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (!disk->part) {
665 kfree(disk);
666 return NULL;
667 }
668 memset(disk->part, 0, size);
669 }
670 disk->minors = minors;
671 kobj_set_kset_s(disk,block_subsys);
672 kobject_init(&disk->kobj);
673 rand_initialize_disk(disk);
674 }
675 return disk;
676}
677
678EXPORT_SYMBOL(alloc_disk);
Christoph Lameter19460892005-06-23 00:08:19 -0700679EXPORT_SYMBOL(alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681struct kobject *get_disk(struct gendisk *disk)
682{
683 struct module *owner;
684 struct kobject *kobj;
685
686 if (!disk->fops)
687 return NULL;
688 owner = disk->fops->owner;
689 if (owner && !try_module_get(owner))
690 return NULL;
691 kobj = kobject_get(&disk->kobj);
692 if (kobj == NULL) {
693 module_put(owner);
694 return NULL;
695 }
696 return kobj;
697
698}
699
700EXPORT_SYMBOL(get_disk);
701
702void put_disk(struct gendisk *disk)
703{
704 if (disk)
705 kobject_put(&disk->kobj);
706}
707
708EXPORT_SYMBOL(put_disk);
709
710void set_device_ro(struct block_device *bdev, int flag)
711{
712 if (bdev->bd_contains != bdev)
713 bdev->bd_part->policy = flag;
714 else
715 bdev->bd_disk->policy = flag;
716}
717
718EXPORT_SYMBOL(set_device_ro);
719
720void set_disk_ro(struct gendisk *disk, int flag)
721{
722 int i;
723 disk->policy = flag;
724 for (i = 0; i < disk->minors - 1; i++)
725 if (disk->part[i]) disk->part[i]->policy = flag;
726}
727
728EXPORT_SYMBOL(set_disk_ro);
729
730int bdev_read_only(struct block_device *bdev)
731{
732 if (!bdev)
733 return 0;
734 else if (bdev->bd_contains != bdev)
735 return bdev->bd_part->policy;
736 else
737 return bdev->bd_disk->policy;
738}
739
740EXPORT_SYMBOL(bdev_read_only);
741
742int invalidate_partition(struct gendisk *disk, int index)
743{
744 int res = 0;
745 struct block_device *bdev = bdget_disk(disk, index);
746 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -0700747 fsync_bdev(bdev);
748 res = __invalidate_device(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 bdput(bdev);
750 }
751 return res;
752}
753
754EXPORT_SYMBOL(invalidate_partition);