blob: 8d7339511e5e962dfccca43298a06e24a48df846 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * gendisk handling
3 */
4
5#include <linux/config.h>
6#include <linux/module.h>
7#include <linux/fs.h>
8#include <linux/genhd.h>
9#include <linux/kernel.h>
10#include <linux/blkdev.h>
11#include <linux/init.h>
12#include <linux/spinlock.h>
13#include <linux/seq_file.h>
14#include <linux/slab.h>
15#include <linux/kmod.h>
16#include <linux/kobj_map.h>
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
Kay Sieversb9d9c822006-06-15 15:31:56 +020020struct subsystem block_subsys;
Jes Sorensen58383af2006-02-06 14:12:43 -080021static DEFINE_MUTEX(block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23/*
24 * Can be deleted altogether. Later.
25 *
26 */
27static struct blk_major_name {
28 struct blk_major_name *next;
29 int major;
30 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -080031} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/* index in the above - for now: assume no multimajor ranges */
34static inline int major_to_index(int major)
35{
Joe Korty68eef3b2006-03-31 02:30:32 -080036 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
Joe Korty68eef3b2006-03-31 02:30:32 -080039#ifdef CONFIG_PROC_FS
Neil Horman7170be52006-01-14 13:20:38 -080040
Joe Korty68eef3b2006-03-31 02:30:32 -080041void blkdev_show(struct seq_file *f, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -080042{
Joe Korty68eef3b2006-03-31 02:30:32 -080043 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -080044
Joe Korty68eef3b2006-03-31 02:30:32 -080045 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
46 mutex_lock(&block_subsys_lock);
47 for (dp = major_names[offset]; dp; dp = dp->next)
48 seq_printf(f, "%3d %s\n", dp->major, dp->name);
49 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
Neil Horman7170be52006-01-14 13:20:38 -080052
Joe Korty68eef3b2006-03-31 02:30:32 -080053#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55int register_blkdev(unsigned int major, const char *name)
56{
57 struct blk_major_name **n, *p;
58 int index, ret = 0;
59
Jes Sorensen58383af2006-02-06 14:12:43 -080060 mutex_lock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 /* temporary */
63 if (major == 0) {
64 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
65 if (major_names[index] == NULL)
66 break;
67 }
68
69 if (index == 0) {
70 printk("register_blkdev: failed to get major for %s\n",
71 name);
72 ret = -EBUSY;
73 goto out;
74 }
75 major = index;
76 ret = major;
77 }
78
79 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
80 if (p == NULL) {
81 ret = -ENOMEM;
82 goto out;
83 }
84
85 p->major = major;
86 strlcpy(p->name, name, sizeof(p->name));
87 p->next = NULL;
88 index = major_to_index(major);
89
90 for (n = &major_names[index]; *n; n = &(*n)->next) {
91 if ((*n)->major == major)
92 break;
93 }
94 if (!*n)
95 *n = p;
96 else
97 ret = -EBUSY;
98
99 if (ret < 0) {
100 printk("register_blkdev: cannot get major %d for %s\n",
101 major, name);
102 kfree(p);
103 }
104out:
Jes Sorensen58383af2006-02-06 14:12:43 -0800105 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return ret;
107}
108
109EXPORT_SYMBOL(register_blkdev);
110
111/* todo: make void - error printk here */
112int unregister_blkdev(unsigned int major, const char *name)
113{
114 struct blk_major_name **n;
115 struct blk_major_name *p = NULL;
116 int index = major_to_index(major);
117 int ret = 0;
118
Jes Sorensen58383af2006-02-06 14:12:43 -0800119 mutex_lock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 for (n = &major_names[index]; *n; n = &(*n)->next)
121 if ((*n)->major == major)
122 break;
123 if (!*n || strcmp((*n)->name, name))
124 ret = -EINVAL;
125 else {
126 p = *n;
127 *n = p->next;
128 }
Jes Sorensen58383af2006-02-06 14:12:43 -0800129 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 kfree(p);
131
132 return ret;
133}
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 */
144void blk_register_region(dev_t dev, unsigned long range, struct module *module,
145 struct kobject *(*probe)(dev_t, int *, void *),
146 int (*lock)(dev_t, void *), void *data)
147{
148 kobj_map(bdev_map, dev, range, module, probe, lock, data);
149}
150
151EXPORT_SYMBOL(blk_register_region);
152
153void blk_unregister_region(dev_t dev, unsigned long range)
154{
155 kobj_unmap(bdev_map, dev, range);
156}
157
158EXPORT_SYMBOL(blk_unregister_region);
159
160static struct kobject *exact_match(dev_t dev, int *part, void *data)
161{
162 struct gendisk *p = data;
163 return &p->kobj;
164}
165
166static int exact_lock(dev_t dev, void *data)
167{
168 struct gendisk *p = data;
169
170 if (!get_disk(p))
171 return -1;
172 return 0;
173}
174
175/**
176 * add_disk - add partitioning information to kernel list
177 * @disk: per-device partitioning information
178 *
179 * This function registers the partitioning information in @disk
180 * with the kernel.
181 */
182void add_disk(struct gendisk *disk)
183{
184 disk->flags |= GENHD_FL_UP;
185 blk_register_region(MKDEV(disk->major, disk->first_minor),
186 disk->minors, NULL, exact_match, exact_lock, disk);
187 register_disk(disk);
188 blk_register_queue(disk);
189}
190
191EXPORT_SYMBOL(add_disk);
192EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
193
194void unlink_gendisk(struct gendisk *disk)
195{
196 blk_unregister_queue(disk);
197 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
198 disk->minors);
199}
200
201#define to_disk(obj) container_of(obj,struct gendisk,kobj)
202
203/**
204 * get_gendisk - get partitioning information for a given device
205 * @dev: device to get partitioning information for
206 *
207 * This function gets the structure containing partitioning
208 * information for the given device @dev.
209 */
210struct gendisk *get_gendisk(dev_t dev, int *part)
211{
212 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
213 return kobj ? to_disk(kobj) : NULL;
214}
215
216#ifdef CONFIG_PROC_FS
217/* iterator */
218static void *part_start(struct seq_file *part, loff_t *pos)
219{
220 struct list_head *p;
221 loff_t l = *pos;
222
Jes Sorensen58383af2006-02-06 14:12:43 -0800223 mutex_lock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 list_for_each(p, &block_subsys.kset.list)
225 if (!l--)
226 return list_entry(p, struct gendisk, kobj.entry);
227 return NULL;
228}
229
230static void *part_next(struct seq_file *part, void *v, loff_t *pos)
231{
232 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
233 ++*pos;
234 return p==&block_subsys.kset.list ? NULL :
235 list_entry(p, struct gendisk, kobj.entry);
236}
237
238static void part_stop(struct seq_file *part, void *v)
239{
Jes Sorensen58383af2006-02-06 14:12:43 -0800240 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243static int show_partition(struct seq_file *part, void *v)
244{
245 struct gendisk *sgp = v;
246 int n;
247 char buf[BDEVNAME_SIZE];
248
249 if (&sgp->kobj.entry == block_subsys.kset.list.next)
250 seq_puts(part, "major minor #blocks name\n\n");
251
252 /* Don't show non-partitionable removeable devices or empty devices */
253 if (!get_capacity(sgp) ||
254 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
255 return 0;
256 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
257 return 0;
258
259 /* show the full disk and all non-0 size partitions of it */
260 seq_printf(part, "%4d %4d %10llu %s\n",
261 sgp->major, sgp->first_minor,
262 (unsigned long long)get_capacity(sgp) >> 1,
263 disk_name(sgp, 0, buf));
264 for (n = 0; n < sgp->minors - 1; n++) {
265 if (!sgp->part[n])
266 continue;
267 if (sgp->part[n]->nr_sects == 0)
268 continue;
269 seq_printf(part, "%4d %4d %10llu %s\n",
270 sgp->major, n + 1 + sgp->first_minor,
271 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
272 disk_name(sgp, n + 1, buf));
273 }
274
275 return 0;
276}
277
278struct seq_operations partitions_op = {
279 .start =part_start,
280 .next = part_next,
281 .stop = part_stop,
282 .show = show_partition
283};
284#endif
285
286
287extern int blk_dev_init(void);
288
289static struct kobject *base_probe(dev_t dev, int *part, void *data)
290{
291 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
292 /* Make old-style 2.4 aliases work */
293 request_module("block-major-%d", MAJOR(dev));
294 return NULL;
295}
296
297static int __init genhd_device_init(void)
298{
Jes Sorensen58383af2006-02-06 14:12:43 -0800299 bdev_map = kobj_map_init(base_probe, &block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 blk_dev_init();
301 subsystem_register(&block_subsys);
302 return 0;
303}
304
305subsys_initcall(genhd_device_init);
306
307
308
309/*
310 * kobject & sysfs bindings for block devices
311 */
312static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
313 char *page)
314{
315 struct gendisk *disk = to_disk(kobj);
316 struct disk_attribute *disk_attr =
317 container_of(attr,struct disk_attribute,attr);
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -0500318 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 if (disk_attr->show)
321 ret = disk_attr->show(disk,page);
322 return ret;
323}
324
Kay Sieversa7fd6702005-10-01 14:49:43 +0200325static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr,
326 const char *page, size_t count)
327{
328 struct gendisk *disk = to_disk(kobj);
329 struct disk_attribute *disk_attr =
330 container_of(attr,struct disk_attribute,attr);
331 ssize_t ret = 0;
332
333 if (disk_attr->store)
334 ret = disk_attr->store(disk, page, count);
335 return ret;
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static struct sysfs_ops disk_sysfs_ops = {
339 .show = &disk_attr_show,
Kay Sieversa7fd6702005-10-01 14:49:43 +0200340 .store = &disk_attr_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341};
342
Kay Sieversa7fd6702005-10-01 14:49:43 +0200343static ssize_t disk_uevent_store(struct gendisk * disk,
344 const char *buf, size_t count)
345{
Kay Sievers312c0042005-11-16 09:00:00 +0100346 kobject_uevent(&disk->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200347 return count;
348}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static ssize_t disk_dev_read(struct gendisk * disk, char *page)
350{
351 dev_t base = MKDEV(disk->major, disk->first_minor);
352 return print_dev_t(page, base);
353}
354static ssize_t disk_range_read(struct gendisk * disk, char *page)
355{
356 return sprintf(page, "%d\n", disk->minors);
357}
358static ssize_t disk_removable_read(struct gendisk * disk, char *page)
359{
360 return sprintf(page, "%d\n",
361 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
362
363}
364static ssize_t disk_size_read(struct gendisk * disk, char *page)
365{
366 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
367}
368
369static ssize_t disk_stats_read(struct gendisk * disk, char *page)
370{
371 preempt_disable();
372 disk_round_stats(disk);
373 preempt_enable();
374 return sprintf(page,
Ben Woodard837c7872006-03-22 08:09:31 +0100375 "%8lu %8lu %8llu %8u "
376 "%8lu %8lu %8llu %8u "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 "%8u %8u %8u"
378 "\n",
Jens Axboe47a00412005-11-09 13:38:47 +0100379 disk_stat_read(disk, ios[READ]),
380 disk_stat_read(disk, merges[READ]),
381 (unsigned long long)disk_stat_read(disk, sectors[READ]),
382 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
383 disk_stat_read(disk, ios[WRITE]),
384 disk_stat_read(disk, merges[WRITE]),
385 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
386 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 disk->in_flight,
388 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
389 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
390}
Kay Sieversa7fd6702005-10-01 14:49:43 +0200391static struct disk_attribute disk_attr_uevent = {
392 .attr = {.name = "uevent", .mode = S_IWUSR },
393 .store = disk_uevent_store
394};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395static struct disk_attribute disk_attr_dev = {
396 .attr = {.name = "dev", .mode = S_IRUGO },
397 .show = disk_dev_read
398};
399static struct disk_attribute disk_attr_range = {
400 .attr = {.name = "range", .mode = S_IRUGO },
401 .show = disk_range_read
402};
403static struct disk_attribute disk_attr_removable = {
404 .attr = {.name = "removable", .mode = S_IRUGO },
405 .show = disk_removable_read
406};
407static struct disk_attribute disk_attr_size = {
408 .attr = {.name = "size", .mode = S_IRUGO },
409 .show = disk_size_read
410};
411static struct disk_attribute disk_attr_stat = {
412 .attr = {.name = "stat", .mode = S_IRUGO },
413 .show = disk_stats_read
414};
415
416static struct attribute * default_attrs[] = {
Kay Sieversa7fd6702005-10-01 14:49:43 +0200417 &disk_attr_uevent.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 &disk_attr_dev.attr,
419 &disk_attr_range.attr,
420 &disk_attr_removable.attr,
421 &disk_attr_size.attr,
422 &disk_attr_stat.attr,
423 NULL,
424};
425
426static void disk_release(struct kobject * kobj)
427{
428 struct gendisk *disk = to_disk(kobj);
429 kfree(disk->random);
430 kfree(disk->part);
431 free_disk_stats(disk);
432 kfree(disk);
433}
434
435static struct kobj_type ktype_block = {
436 .release = disk_release,
437 .sysfs_ops = &disk_sysfs_ops,
438 .default_attrs = default_attrs,
439};
440
441extern struct kobj_type ktype_part;
442
Kay Sievers312c0042005-11-16 09:00:00 +0100443static int block_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 struct kobj_type *ktype = get_ktype(kobj);
446
447 return ((ktype == &ktype_block) || (ktype == &ktype_part));
448}
449
Kay Sievers312c0042005-11-16 09:00:00 +0100450static int block_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int num_envp, char *buffer, int buffer_size)
452{
453 struct kobj_type *ktype = get_ktype(kobj);
454 struct device *physdev;
455 struct gendisk *disk;
456 struct hd_struct *part;
457 int length = 0;
458 int i = 0;
459
460 if (ktype == &ktype_block) {
461 disk = container_of(kobj, struct gendisk, kobj);
Kay Sievers312c0042005-11-16 09:00:00 +0100462 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
463 &length, "MINOR=%u", disk->first_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 } else if (ktype == &ktype_part) {
465 disk = container_of(kobj->parent, struct gendisk, kobj);
466 part = container_of(kobj, struct hd_struct, kobj);
Kay Sievers312c0042005-11-16 09:00:00 +0100467 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
468 &length, "MINOR=%u",
469 disk->first_minor + part->partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 } else
471 return 0;
472
Kay Sievers312c0042005-11-16 09:00:00 +0100473 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
474 "MAJOR=%u", disk->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /* add physical device, backing this device */
477 physdev = disk->driverfs_dev;
478 if (physdev) {
479 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
480
Kay Sievers312c0042005-11-16 09:00:00 +0100481 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
482 &length, "PHYSDEVPATH=%s", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 kfree(path);
484
485 if (physdev->bus)
Kay Sievers312c0042005-11-16 09:00:00 +0100486 add_uevent_var(envp, num_envp, &i,
487 buffer, buffer_size, &length,
488 "PHYSDEVBUS=%s",
489 physdev->bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (physdev->driver)
Kay Sievers312c0042005-11-16 09:00:00 +0100492 add_uevent_var(envp, num_envp, &i,
493 buffer, buffer_size, &length,
494 "PHYSDEVDRIVER=%s",
495 physdev->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
498 /* terminate, set to next free slot, shrink available space */
499 envp[i] = NULL;
500 envp = &envp[i];
501 num_envp -= i;
502 buffer = &buffer[length];
503 buffer_size -= length;
504
505 return 0;
506}
507
Kay Sievers312c0042005-11-16 09:00:00 +0100508static struct kset_uevent_ops block_uevent_ops = {
509 .filter = block_uevent_filter,
510 .uevent = block_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511};
512
Kay Sieversb9d9c822006-06-15 15:31:56 +0200513decl_subsys(block, &ktype_block, &block_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515/*
516 * aggregate disk stat collector. Uses the same stats that the sysfs
517 * entries do, above, but makes them available through one seq_file.
518 * Watching a few disks may be efficient through sysfs, but watching
519 * all of them will be more efficient through this interface.
520 *
521 * The output looks suspiciously like /proc/partitions with a bunch of
522 * extra fields.
523 */
524
525/* iterator */
526static void *diskstats_start(struct seq_file *part, loff_t *pos)
527{
528 loff_t k = *pos;
529 struct list_head *p;
530
Jes Sorensen58383af2006-02-06 14:12:43 -0800531 mutex_lock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 list_for_each(p, &block_subsys.kset.list)
533 if (!k--)
534 return list_entry(p, struct gendisk, kobj.entry);
535 return NULL;
536}
537
538static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
539{
540 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
541 ++*pos;
542 return p==&block_subsys.kset.list ? NULL :
543 list_entry(p, struct gendisk, kobj.entry);
544}
545
546static void diskstats_stop(struct seq_file *part, void *v)
547{
Jes Sorensen58383af2006-02-06 14:12:43 -0800548 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551static int diskstats_show(struct seq_file *s, void *v)
552{
553 struct gendisk *gp = v;
554 char buf[BDEVNAME_SIZE];
555 int n = 0;
556
557 /*
558 if (&sgp->kobj.entry == block_subsys.kset.list.next)
559 seq_puts(s, "major minor name"
560 " rio rmerge rsect ruse wio wmerge "
561 "wsect wuse running use aveq"
562 "\n\n");
563 */
564
565 preempt_disable();
566 disk_round_stats(gp);
567 preempt_enable();
Ben Woodard837c7872006-03-22 08:09:31 +0100568 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 -0700569 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
Jens Axboea3623572005-11-01 09:26:16 +0100570 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
571 (unsigned long long)disk_stat_read(gp, sectors[0]),
572 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
573 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
574 (unsigned long long)disk_stat_read(gp, sectors[1]),
575 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 gp->in_flight,
577 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
578 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
579
580 /* now show all non-0 size partitions of it */
581 for (n = 0; n < gp->minors - 1; n++) {
582 struct hd_struct *hd = gp->part[n];
583
584 if (hd && hd->nr_sects)
585 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
586 gp->major, n + gp->first_minor + 1,
587 disk_name(gp, n + 1, buf),
Jens Axboea3623572005-11-01 09:26:16 +0100588 hd->ios[0], hd->sectors[0],
589 hd->ios[1], hd->sectors[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
592 return 0;
593}
594
595struct seq_operations diskstats_op = {
596 .start = diskstats_start,
597 .next = diskstats_next,
598 .stop = diskstats_stop,
599 .show = diskstats_show
600};
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602struct gendisk *alloc_disk(int minors)
603{
Christoph Lameter19460892005-06-23 00:08:19 -0700604 return alloc_disk_node(minors, -1);
605}
606
607struct gendisk *alloc_disk_node(int minors, int node_id)
608{
609 struct gendisk *disk;
610
611 disk = kmalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (disk) {
613 memset(disk, 0, sizeof(struct gendisk));
614 if (!init_disk_stats(disk)) {
615 kfree(disk);
616 return NULL;
617 }
618 if (minors > 1) {
619 int size = (minors - 1) * sizeof(struct hd_struct *);
Christoph Lameter19460892005-06-23 00:08:19 -0700620 disk->part = kmalloc_node(size, GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (!disk->part) {
622 kfree(disk);
623 return NULL;
624 }
625 memset(disk->part, 0, size);
626 }
627 disk->minors = minors;
628 kobj_set_kset_s(disk,block_subsys);
629 kobject_init(&disk->kobj);
630 rand_initialize_disk(disk);
631 }
632 return disk;
633}
634
635EXPORT_SYMBOL(alloc_disk);
Christoph Lameter19460892005-06-23 00:08:19 -0700636EXPORT_SYMBOL(alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638struct kobject *get_disk(struct gendisk *disk)
639{
640 struct module *owner;
641 struct kobject *kobj;
642
643 if (!disk->fops)
644 return NULL;
645 owner = disk->fops->owner;
646 if (owner && !try_module_get(owner))
647 return NULL;
648 kobj = kobject_get(&disk->kobj);
649 if (kobj == NULL) {
650 module_put(owner);
651 return NULL;
652 }
653 return kobj;
654
655}
656
657EXPORT_SYMBOL(get_disk);
658
659void put_disk(struct gendisk *disk)
660{
661 if (disk)
662 kobject_put(&disk->kobj);
663}
664
665EXPORT_SYMBOL(put_disk);
666
667void set_device_ro(struct block_device *bdev, int flag)
668{
669 if (bdev->bd_contains != bdev)
670 bdev->bd_part->policy = flag;
671 else
672 bdev->bd_disk->policy = flag;
673}
674
675EXPORT_SYMBOL(set_device_ro);
676
677void set_disk_ro(struct gendisk *disk, int flag)
678{
679 int i;
680 disk->policy = flag;
681 for (i = 0; i < disk->minors - 1; i++)
682 if (disk->part[i]) disk->part[i]->policy = flag;
683}
684
685EXPORT_SYMBOL(set_disk_ro);
686
687int bdev_read_only(struct block_device *bdev)
688{
689 if (!bdev)
690 return 0;
691 else if (bdev->bd_contains != bdev)
692 return bdev->bd_part->policy;
693 else
694 return bdev->bd_disk->policy;
695}
696
697EXPORT_SYMBOL(bdev_read_only);
698
699int invalidate_partition(struct gendisk *disk, int index)
700{
701 int res = 0;
702 struct block_device *bdev = bdget_disk(disk, index);
703 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -0700704 fsync_bdev(bdev);
705 res = __invalidate_device(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 bdput(bdev);
707 }
708 return res;
709}
710
711EXPORT_SYMBOL(invalidate_partition);