allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / partitions / check.c
blob770ca8055460847e5505bac6e43d8315c05ee89c
1 /*
2 * fs/partitions/check.c
4 * Code extracted from drivers/block/genhd.c
5 * Copyright (C) 1991-1998 Linus Torvalds
6 * Re-organised Feb 1998 Russell King
8 * We now have independent partition support from the
9 * block drivers, which allows all the partition code to
10 * be grouped in one location, and it to be mostly self
11 * contained.
13 * Added needed MAJORS for new pairs, {hdi,hdj}, {hdk,hdl}
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/fs.h>
19 #include <linux/kmod.h>
20 #include <linux/ctype.h>
22 #include "check.h"
24 #include "acorn.h"
25 #include "amiga.h"
26 #include "atari.h"
27 #include "ldm.h"
28 #include "mac.h"
29 #include "msdos.h"
30 #include "osf.h"
31 #include "sgi.h"
32 #include "sun.h"
33 #include "ibm.h"
34 #include "ultrix.h"
35 #include "efi.h"
36 #include "karma.h"
37 #include "sysv68.h"
39 #ifdef CONFIG_BLK_DEV_MD
40 extern void md_autodetect_dev(dev_t dev);
41 #endif
43 int warn_no_part = 1; /*This is ugly: should make genhd removable media aware*/
45 static int (*check_part[])(struct parsed_partitions *, struct block_device *) = {
47 * Probe partition formats with tables at disk address 0
48 * that also have an ADFS boot block at 0xdc0.
50 #ifdef CONFIG_ACORN_PARTITION_ICS
51 adfspart_check_ICS,
52 #endif
53 #ifdef CONFIG_ACORN_PARTITION_POWERTEC
54 adfspart_check_POWERTEC,
55 #endif
56 #ifdef CONFIG_ACORN_PARTITION_EESOX
57 adfspart_check_EESOX,
58 #endif
61 * Now move on to formats that only have partition info at
62 * disk address 0xdc0. Since these may also have stale
63 * PC/BIOS partition tables, they need to come before
64 * the msdos entry.
66 #ifdef CONFIG_ACORN_PARTITION_CUMANA
67 adfspart_check_CUMANA,
68 #endif
69 #ifdef CONFIG_ACORN_PARTITION_ADFS
70 adfspart_check_ADFS,
71 #endif
73 #ifdef CONFIG_EFI_PARTITION
74 efi_partition, /* this must come before msdos */
75 #endif
76 #ifdef CONFIG_SGI_PARTITION
77 sgi_partition,
78 #endif
79 #ifdef CONFIG_LDM_PARTITION
80 ldm_partition, /* this must come before msdos */
81 #endif
82 #ifdef CONFIG_MSDOS_PARTITION
83 msdos_partition,
84 #endif
85 #ifdef CONFIG_OSF_PARTITION
86 osf_partition,
87 #endif
88 #ifdef CONFIG_SUN_PARTITION
89 sun_partition,
90 #endif
91 #ifdef CONFIG_AMIGA_PARTITION
92 amiga_partition,
93 #endif
94 #ifdef CONFIG_ATARI_PARTITION
95 atari_partition,
96 #endif
97 #ifdef CONFIG_MAC_PARTITION
98 mac_partition,
99 #endif
100 #ifdef CONFIG_ULTRIX_PARTITION
101 ultrix_partition,
102 #endif
103 #ifdef CONFIG_IBM_PARTITION
104 ibm_partition,
105 #endif
106 #ifdef CONFIG_KARMA_PARTITION
107 karma_partition,
108 #endif
109 #ifdef CONFIG_SYSV68_PARTITION
110 sysv68_partition,
111 #endif
112 NULL
116 * disk_name() is used by partition check code and the genhd driver.
117 * It formats the devicename of the indicated disk into
118 * the supplied buffer (of size at least 32), and returns
119 * a pointer to that same buffer (for convenience).
122 char *disk_name(struct gendisk *hd, int part, char *buf)
124 if (!part)
125 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
126 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
127 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, part);
128 else
129 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, part);
131 return buf;
134 const char *bdevname(struct block_device *bdev, char *buf)
136 int part = MINOR(bdev->bd_dev) - bdev->bd_disk->first_minor;
137 return disk_name(bdev->bd_disk, part, buf);
140 EXPORT_SYMBOL(bdevname);
143 * There's very little reason to use this, you should really
144 * have a struct block_device just about everywhere and use
145 * bdevname() instead.
147 const char *__bdevname(dev_t dev, char *buffer)
149 scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
150 MAJOR(dev), MINOR(dev));
151 return buffer;
154 EXPORT_SYMBOL(__bdevname);
156 static struct parsed_partitions *
157 check_partition(struct gendisk *hd, struct block_device *bdev)
159 struct parsed_partitions *state;
160 int i, res, err;
162 state = kmalloc(sizeof(struct parsed_partitions), GFP_KERNEL);
163 if (!state)
164 return NULL;
166 disk_name(hd, 0, state->name);
167 printk(KERN_INFO " %s:", state->name);
168 if (isdigit(state->name[strlen(state->name)-1]))
169 sprintf(state->name, "p");
171 state->limit = hd->minors;
172 i = res = err = 0;
173 while (!res && check_part[i]) {
174 memset(&state->parts, 0, sizeof(state->parts));
175 res = check_part[i++](state, bdev);
176 if (res < 0) {
177 /* We have hit an I/O error which we don't report now.
178 * But record it, and let the others do their job.
180 err = res;
181 res = 0;
185 if (res > 0)
186 return state;
187 if (err)
188 /* The partition is unrecognized. So report I/O errors if there were any */
189 res = err;
190 if (!res)
191 printk(" unknown partition table\n");
192 else if (warn_no_part)
193 printk(" unable to read partition table\n");
194 kfree(state);
195 return ERR_PTR(res);
199 * sysfs bindings for partitions
202 struct part_attribute {
203 struct attribute attr;
204 ssize_t (*show)(struct hd_struct *,char *);
205 ssize_t (*store)(struct hd_struct *,const char *, size_t);
208 static ssize_t
209 part_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
211 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
212 struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr);
213 ssize_t ret = 0;
214 if (part_attr->show)
215 ret = part_attr->show(p, page);
216 return ret;
218 static ssize_t
219 part_attr_store(struct kobject * kobj, struct attribute * attr,
220 const char *page, size_t count)
222 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
223 struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr);
224 ssize_t ret = 0;
226 if (part_attr->store)
227 ret = part_attr->store(p, page, count);
228 return ret;
231 static struct sysfs_ops part_sysfs_ops = {
232 .show = part_attr_show,
233 .store = part_attr_store,
236 static ssize_t part_uevent_store(struct hd_struct * p,
237 const char *page, size_t count)
239 kobject_uevent(&p->kobj, KOBJ_ADD);
240 return count;
242 static ssize_t part_dev_read(struct hd_struct * p, char *page)
244 struct gendisk *disk = container_of(p->kobj.parent,struct gendisk,kobj);
245 dev_t dev = MKDEV(disk->major, disk->first_minor + p->partno);
246 return print_dev_t(page, dev);
248 static ssize_t part_start_read(struct hd_struct * p, char *page)
250 return sprintf(page, "%llu\n",(unsigned long long)p->start_sect);
252 static ssize_t part_size_read(struct hd_struct * p, char *page)
254 return sprintf(page, "%llu\n",(unsigned long long)p->nr_sects);
256 static ssize_t part_stat_read(struct hd_struct * p, char *page)
258 return sprintf(page, "%8u %8llu %8u %8llu\n",
259 p->ios[0], (unsigned long long)p->sectors[0],
260 p->ios[1], (unsigned long long)p->sectors[1]);
262 static struct part_attribute part_attr_uevent = {
263 .attr = {.name = "uevent", .mode = S_IWUSR },
264 .store = part_uevent_store
266 static struct part_attribute part_attr_dev = {
267 .attr = {.name = "dev", .mode = S_IRUGO },
268 .show = part_dev_read
270 static struct part_attribute part_attr_start = {
271 .attr = {.name = "start", .mode = S_IRUGO },
272 .show = part_start_read
274 static struct part_attribute part_attr_size = {
275 .attr = {.name = "size", .mode = S_IRUGO },
276 .show = part_size_read
278 static struct part_attribute part_attr_stat = {
279 .attr = {.name = "stat", .mode = S_IRUGO },
280 .show = part_stat_read
283 #ifdef CONFIG_FAIL_MAKE_REQUEST
285 static ssize_t part_fail_store(struct hd_struct * p,
286 const char *buf, size_t count)
288 int i;
290 if (count > 0 && sscanf(buf, "%d", &i) > 0)
291 p->make_it_fail = (i == 0) ? 0 : 1;
293 return count;
295 static ssize_t part_fail_read(struct hd_struct * p, char *page)
297 return sprintf(page, "%d\n", p->make_it_fail);
299 static struct part_attribute part_attr_fail = {
300 .attr = {.name = "make-it-fail", .mode = S_IRUGO | S_IWUSR },
301 .store = part_fail_store,
302 .show = part_fail_read
305 #endif
307 static struct attribute * default_attrs[] = {
308 &part_attr_uevent.attr,
309 &part_attr_dev.attr,
310 &part_attr_start.attr,
311 &part_attr_size.attr,
312 &part_attr_stat.attr,
313 #ifdef CONFIG_FAIL_MAKE_REQUEST
314 &part_attr_fail.attr,
315 #endif
316 NULL,
319 extern struct kset block_subsys;
321 static void part_release(struct kobject *kobj)
323 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
324 kfree(p);
327 struct kobj_type ktype_part = {
328 .release = part_release,
329 .default_attrs = default_attrs,
330 .sysfs_ops = &part_sysfs_ops,
333 static inline void partition_sysfs_add_subdir(struct hd_struct *p)
335 struct kobject *k;
337 k = kobject_get(&p->kobj);
338 p->holder_dir = kobject_add_dir(k, "holders");
339 kobject_put(k);
342 static inline void disk_sysfs_add_subdirs(struct gendisk *disk)
344 struct kobject *k;
346 k = kobject_get(&disk->kobj);
347 disk->holder_dir = kobject_add_dir(k, "holders");
348 disk->slave_dir = kobject_add_dir(k, "slaves");
349 kobject_put(k);
352 void delete_partition(struct gendisk *disk, int part)
354 struct hd_struct *p = disk->part[part-1];
355 if (!p)
356 return;
357 if (!p->nr_sects)
358 return;
359 disk->part[part-1] = NULL;
360 p->start_sect = 0;
361 p->nr_sects = 0;
362 p->ios[0] = p->ios[1] = 0;
363 p->sectors[0] = p->sectors[1] = 0;
364 sysfs_remove_link(&p->kobj, "subsystem");
365 kobject_unregister(p->holder_dir);
366 kobject_uevent(&p->kobj, KOBJ_REMOVE);
367 kobject_del(&p->kobj);
368 kobject_put(&p->kobj);
371 void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len, int flags)
373 struct hd_struct *p;
375 p = kzalloc(sizeof(*p), GFP_KERNEL);
376 if (!p)
377 return;
379 p->start_sect = start;
380 p->nr_sects = len;
381 p->partno = part;
382 p->policy = disk->policy;
384 if (isdigit(disk->kobj.name[strlen(disk->kobj.name)-1]))
385 snprintf(p->kobj.name,KOBJ_NAME_LEN,"%sp%d",disk->kobj.name,part);
386 else
387 snprintf(p->kobj.name,KOBJ_NAME_LEN,"%s%d",disk->kobj.name,part);
388 p->kobj.parent = &disk->kobj;
389 p->kobj.ktype = &ktype_part;
390 kobject_init(&p->kobj);
391 kobject_add(&p->kobj);
392 if (!disk->part_uevent_suppress)
393 kobject_uevent(&p->kobj, KOBJ_ADD);
394 sysfs_create_link(&p->kobj, &block_subsys.kobj, "subsystem");
395 if (flags & ADDPART_FLAG_WHOLEDISK) {
396 static struct attribute addpartattr = {
397 .name = "whole_disk",
398 .mode = S_IRUSR | S_IRGRP | S_IROTH,
399 .owner = THIS_MODULE,
402 sysfs_create_file(&p->kobj, &addpartattr);
404 partition_sysfs_add_subdir(p);
405 disk->part[part-1] = p;
408 static char *make_block_name(struct gendisk *disk)
410 char *name;
411 static char *block_str = "block:";
412 int size;
413 char *s;
415 size = strlen(block_str) + strlen(disk->disk_name) + 1;
416 name = kmalloc(size, GFP_KERNEL);
417 if (!name)
418 return NULL;
419 strcpy(name, block_str);
420 strcat(name, disk->disk_name);
421 /* ewww... some of these buggers have / in name... */
422 s = strchr(name, '/');
423 if (s)
424 *s = '!';
425 return name;
428 static int disk_sysfs_symlinks(struct gendisk *disk)
430 struct device *target = get_device(disk->driverfs_dev);
431 int err;
432 char *disk_name = NULL;
434 if (target) {
435 disk_name = make_block_name(disk);
436 if (!disk_name) {
437 err = -ENOMEM;
438 goto err_out;
441 err = sysfs_create_link(&disk->kobj, &target->kobj, "device");
442 if (err)
443 goto err_out_disk_name;
445 err = sysfs_create_link(&target->kobj, &disk->kobj, disk_name);
446 if (err)
447 goto err_out_dev_link;
450 err = sysfs_create_link(&disk->kobj, &block_subsys.kobj,
451 "subsystem");
452 if (err)
453 goto err_out_disk_name_lnk;
455 kfree(disk_name);
457 return 0;
459 err_out_disk_name_lnk:
460 if (target) {
461 sysfs_remove_link(&target->kobj, disk_name);
462 err_out_dev_link:
463 sysfs_remove_link(&disk->kobj, "device");
464 err_out_disk_name:
465 kfree(disk_name);
466 err_out:
467 put_device(target);
469 return err;
472 /* Not exported, helper to add_disk(). */
473 void register_disk(struct gendisk *disk)
475 struct block_device *bdev;
476 char *s;
477 int i;
478 struct hd_struct *p;
479 int err;
481 strlcpy(disk->kobj.name,disk->disk_name,KOBJ_NAME_LEN);
482 /* ewww... some of these buggers have / in name... */
483 s = strchr(disk->kobj.name, '/');
484 if (s)
485 *s = '!';
486 if ((err = kobject_add(&disk->kobj)))
487 return;
488 err = disk_sysfs_symlinks(disk);
489 if (err) {
490 kobject_del(&disk->kobj);
491 return;
493 disk_sysfs_add_subdirs(disk);
495 /* No minors to use for partitions */
496 if (disk->minors == 1)
497 goto exit;
499 /* No such device (e.g., media were just removed) */
500 if (!get_capacity(disk))
501 goto exit;
503 bdev = bdget_disk(disk, 0);
504 if (!bdev)
505 goto exit;
507 /* scan partition table, but suppress uevents */
508 bdev->bd_invalidated = 1;
509 disk->part_uevent_suppress = 1;
510 err = blkdev_get(bdev, FMODE_READ, 0);
511 disk->part_uevent_suppress = 0;
512 if (err < 0)
513 goto exit;
514 blkdev_put(bdev);
516 exit:
517 /* announce disk after possible partitions are already created */
518 kobject_uevent(&disk->kobj, KOBJ_ADD);
520 /* announce possible partitions */
521 for (i = 1; i < disk->minors; i++) {
522 p = disk->part[i-1];
523 if (!p || !p->nr_sects)
524 continue;
525 kobject_uevent(&p->kobj, KOBJ_ADD);
529 int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
531 struct parsed_partitions *state;
532 int p, res;
534 if (bdev->bd_part_count)
535 return -EBUSY;
536 res = invalidate_partition(disk, 0);
537 if (res)
538 return res;
539 bdev->bd_invalidated = 0;
540 for (p = 1; p < disk->minors; p++)
541 delete_partition(disk, p);
542 if (disk->fops->revalidate_disk)
543 disk->fops->revalidate_disk(disk);
544 if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
545 return 0;
546 if (IS_ERR(state)) /* I/O error reading the partition table */
547 return -EIO;
549 /* tell userspace that the media / partition table may have changed */
550 kobject_uevent(&disk->kobj, KOBJ_CHANGE);
552 for (p = 1; p < state->limit; p++) {
553 sector_t size = state->parts[p].size;
554 sector_t from = state->parts[p].from;
555 if (!size)
556 continue;
557 if (from >= get_capacity(disk)) {
558 printk(KERN_WARNING
559 "%s: p%d ignored, start %llu is behind the end of the disk\n",
560 disk->disk_name, p, (unsigned long long) from);
561 continue;
563 if (from + size > get_capacity(disk)) {
565 * we can not ignore partitions of broken tables
566 * created by for example camera firmware, but we
567 * limit them to the end of the disk to avoid
568 * creating invalid block devices
570 printk(KERN_WARNING
571 "%s: p%d size %llu exceeds device capacity, "
572 "limited to end of disk\n",
573 disk->disk_name, p, (unsigned long long) size);
574 size = get_capacity(disk) - from;
576 add_partition(disk, p, from, size, state->parts[p].flags);
577 #ifdef CONFIG_BLK_DEV_MD
578 if (state->parts[p].flags & ADDPART_FLAG_RAID)
579 md_autodetect_dev(bdev->bd_dev+p);
580 #endif
582 kfree(state);
583 return 0;
586 unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
588 struct address_space *mapping = bdev->bd_inode->i_mapping;
589 struct page *page;
591 page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
592 NULL);
593 if (!IS_ERR(page)) {
594 if (PageError(page))
595 goto fail;
596 p->v = page;
597 return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
598 fail:
599 page_cache_release(page);
601 p->v = NULL;
602 return NULL;
605 EXPORT_SYMBOL(read_dev_sector);
607 void del_gendisk(struct gendisk *disk)
609 int p;
611 /* invalidate stuff */
612 for (p = disk->minors - 1; p > 0; p--) {
613 invalidate_partition(disk, p);
614 delete_partition(disk, p);
616 invalidate_partition(disk, 0);
617 disk->capacity = 0;
618 disk->flags &= ~GENHD_FL_UP;
619 unlink_gendisk(disk);
620 disk_stat_set_all(disk, 0);
621 disk->stamp = 0;
623 kobject_uevent(&disk->kobj, KOBJ_REMOVE);
624 kobject_unregister(disk->holder_dir);
625 kobject_unregister(disk->slave_dir);
626 if (disk->driverfs_dev) {
627 char *disk_name = make_block_name(disk);
628 sysfs_remove_link(&disk->kobj, "device");
629 if (disk_name) {
630 sysfs_remove_link(&disk->driverfs_dev->kobj, disk_name);
631 kfree(disk_name);
633 put_device(disk->driverfs_dev);
634 disk->driverfs_dev = NULL;
636 sysfs_remove_link(&disk->kobj, "subsystem");
637 kobject_del(&disk->kobj);