blob: 2ab7701eb2fb52937b7a9a1cd4bccb0747a9b125 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/partitions/check.c
3 *
4 * Code extracted from drivers/block/genhd.c
5 * Copyright (C) 1991-1998 Linus Torvalds
6 * Re-organised Feb 1998 Russell King
7 *
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.
12 *
13 * Added needed MAJORS for new pairs, {hdi,hdj}, {hdk,hdl}
14 */
15
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>
21#include <linux/devfs_fs_kernel.h>
22
23#include "check.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "acorn.h"
26#include "amiga.h"
27#include "atari.h"
28#include "ldm.h"
29#include "mac.h"
30#include "msdos.h"
31#include "osf.h"
32#include "sgi.h"
33#include "sun.h"
34#include "ibm.h"
35#include "ultrix.h"
36#include "efi.h"
Bob Copeland0e6e1db2006-01-16 22:14:20 -080037#include "karma.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#ifdef CONFIG_BLK_DEV_MD
40extern void md_autodetect_dev(dev_t dev);
41#endif
42
43int warn_no_part = 1; /*This is ugly: should make genhd removable media aware*/
44
45static int (*check_part[])(struct parsed_partitions *, struct block_device *) = {
46 /*
47 * Probe partition formats with tables at disk address 0
48 * that also have an ADFS boot block at 0xdc0.
49 */
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
59
60 /*
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.
65 */
66#ifdef CONFIG_ACORN_PARTITION_CUMANA
67 adfspart_check_CUMANA,
68#endif
69#ifdef CONFIG_ACORN_PARTITION_ADFS
70 adfspart_check_ADFS,
71#endif
72
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#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
Bob Copeland0e6e1db2006-01-16 22:14:20 -0800106#ifdef CONFIG_KARMA_PARTITION
107 karma_partition,
108#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 NULL
110};
111
112/*
113 * disk_name() is used by partition check code and the genhd driver.
114 * It formats the devicename of the indicated disk into
115 * the supplied buffer (of size at least 32), and returns
116 * a pointer to that same buffer (for convenience).
117 */
118
119char *disk_name(struct gendisk *hd, int part, char *buf)
120{
121 if (!part)
122 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
123 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
124 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, part);
125 else
126 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, part);
127
128 return buf;
129}
130
131const char *bdevname(struct block_device *bdev, char *buf)
132{
133 int part = MINOR(bdev->bd_dev) - bdev->bd_disk->first_minor;
134 return disk_name(bdev->bd_disk, part, buf);
135}
136
137EXPORT_SYMBOL(bdevname);
138
139/*
140 * There's very little reason to use this, you should really
141 * have a struct block_device just about everywhere and use
142 * bdevname() instead.
143 */
144const char *__bdevname(dev_t dev, char *buffer)
145{
146 scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
147 MAJOR(dev), MINOR(dev));
148 return buffer;
149}
150
151EXPORT_SYMBOL(__bdevname);
152
153static struct parsed_partitions *
154check_partition(struct gendisk *hd, struct block_device *bdev)
155{
156 struct parsed_partitions *state;
157 int i, res;
158
159 state = kmalloc(sizeof(struct parsed_partitions), GFP_KERNEL);
160 if (!state)
161 return NULL;
162
Greg Kroah-Hartmana2964182005-06-20 21:15:16 -0700163 disk_name(hd, 0, state->name);
164 printk(KERN_INFO " %s:", state->name);
165 if (isdigit(state->name[strlen(state->name)-1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 sprintf(state->name, "p");
Greg Kroah-Hartmana2964182005-06-20 21:15:16 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 state->limit = hd->minors;
169 i = res = 0;
170 while (!res && check_part[i]) {
171 memset(&state->parts, 0, sizeof(state->parts));
172 res = check_part[i++](state, bdev);
173 }
174 if (res > 0)
175 return state;
176 if (!res)
177 printk(" unknown partition table\n");
178 else if (warn_no_part)
179 printk(" unable to read partition table\n");
180 kfree(state);
181 return NULL;
182}
183
184/*
185 * sysfs bindings for partitions
186 */
187
188struct part_attribute {
189 struct attribute attr;
190 ssize_t (*show)(struct hd_struct *,char *);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200191 ssize_t (*store)(struct hd_struct *,const char *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
194static ssize_t
195part_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
196{
197 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
198 struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr);
199 ssize_t ret = 0;
200 if (part_attr->show)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200201 ret = part_attr->show(p, page);
202 return ret;
203}
204static ssize_t
205part_attr_store(struct kobject * kobj, struct attribute * attr,
206 const char *page, size_t count)
207{
208 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
209 struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr);
210 ssize_t ret = 0;
211
212 if (part_attr->store)
213 ret = part_attr->store(p, page, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return ret;
215}
216
217static struct sysfs_ops part_sysfs_ops = {
218 .show = part_attr_show,
Kay Sieversa7fd6702005-10-01 14:49:43 +0200219 .store = part_attr_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221
Kay Sieversa7fd6702005-10-01 14:49:43 +0200222static ssize_t part_uevent_store(struct hd_struct * p,
223 const char *page, size_t count)
224{
Kay Sievers312c0042005-11-16 09:00:00 +0100225 kobject_uevent(&p->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200226 return count;
227}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static ssize_t part_dev_read(struct hd_struct * p, char *page)
229{
230 struct gendisk *disk = container_of(p->kobj.parent,struct gendisk,kobj);
231 dev_t dev = MKDEV(disk->major, disk->first_minor + p->partno);
232 return print_dev_t(page, dev);
233}
234static ssize_t part_start_read(struct hd_struct * p, char *page)
235{
236 return sprintf(page, "%llu\n",(unsigned long long)p->start_sect);
237}
238static ssize_t part_size_read(struct hd_struct * p, char *page)
239{
240 return sprintf(page, "%llu\n",(unsigned long long)p->nr_sects);
241}
242static ssize_t part_stat_read(struct hd_struct * p, char *page)
243{
244 return sprintf(page, "%8u %8llu %8u %8llu\n",
Jens Axboea3623572005-11-01 09:26:16 +0100245 p->ios[0], (unsigned long long)p->sectors[0],
246 p->ios[1], (unsigned long long)p->sectors[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
Kay Sieversa7fd6702005-10-01 14:49:43 +0200248static struct part_attribute part_attr_uevent = {
249 .attr = {.name = "uevent", .mode = S_IWUSR },
250 .store = part_uevent_store
251};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static struct part_attribute part_attr_dev = {
253 .attr = {.name = "dev", .mode = S_IRUGO },
254 .show = part_dev_read
255};
256static struct part_attribute part_attr_start = {
257 .attr = {.name = "start", .mode = S_IRUGO },
258 .show = part_start_read
259};
260static struct part_attribute part_attr_size = {
261 .attr = {.name = "size", .mode = S_IRUGO },
262 .show = part_size_read
263};
264static struct part_attribute part_attr_stat = {
265 .attr = {.name = "stat", .mode = S_IRUGO },
266 .show = part_stat_read
267};
268
269static struct attribute * default_attrs[] = {
Kay Sieversa7fd6702005-10-01 14:49:43 +0200270 &part_attr_uevent.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 &part_attr_dev.attr,
272 &part_attr_start.attr,
273 &part_attr_size.attr,
274 &part_attr_stat.attr,
275 NULL,
276};
277
278extern struct subsystem block_subsys;
279
280static void part_release(struct kobject *kobj)
281{
282 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
283 kfree(p);
284}
285
286struct kobj_type ktype_part = {
287 .release = part_release,
288 .default_attrs = default_attrs,
289 .sysfs_ops = &part_sysfs_ops,
290};
291
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800292static inline void partition_sysfs_add_subdir(struct hd_struct *p)
293{
294 struct kobject *k;
295
296 k = kobject_get(&p->kobj);
297 p->holder_dir = kobject_add_dir(k, "holders");
298 kobject_put(k);
299}
300
301static inline void disk_sysfs_add_subdirs(struct gendisk *disk)
302{
303 struct kobject *k;
304
305 k = kobject_get(&disk->kobj);
306 disk->holder_dir = kobject_add_dir(k, "holders");
307 disk->slave_dir = kobject_add_dir(k, "slaves");
308 kobject_put(k);
309}
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311void delete_partition(struct gendisk *disk, int part)
312{
313 struct hd_struct *p = disk->part[part-1];
314 if (!p)
315 return;
316 if (!p->nr_sects)
317 return;
318 disk->part[part-1] = NULL;
319 p->start_sect = 0;
320 p->nr_sects = 0;
Jens Axboea3623572005-11-01 09:26:16 +0100321 p->ios[0] = p->ios[1] = 0;
322 p->sectors[0] = p->sectors[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 devfs_remove("%s/part%d", disk->devfs_name, part);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200324 sysfs_remove_link(&p->kobj, "subsystem");
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800325 if (p->holder_dir)
326 kobject_unregister(p->holder_dir);
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100327 kobject_uevent(&p->kobj, KOBJ_REMOVE);
328 kobject_del(&p->kobj);
329 kobject_put(&p->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len)
333{
334 struct hd_struct *p;
335
336 p = kmalloc(sizeof(*p), GFP_KERNEL);
337 if (!p)
338 return;
339
340 memset(p, 0, sizeof(*p));
341 p->start_sect = start;
342 p->nr_sects = len;
343 p->partno = part;
344
345 devfs_mk_bdev(MKDEV(disk->major, disk->first_minor + part),
346 S_IFBLK|S_IRUSR|S_IWUSR,
347 "%s/part%d", disk->devfs_name, part);
348
349 if (isdigit(disk->kobj.name[strlen(disk->kobj.name)-1]))
350 snprintf(p->kobj.name,KOBJ_NAME_LEN,"%sp%d",disk->kobj.name,part);
351 else
352 snprintf(p->kobj.name,KOBJ_NAME_LEN,"%s%d",disk->kobj.name,part);
353 p->kobj.parent = &disk->kobj;
354 p->kobj.ktype = &ktype_part;
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100355 kobject_init(&p->kobj);
356 kobject_add(&p->kobj);
357 if (!disk->part_uevent_suppress)
358 kobject_uevent(&p->kobj, KOBJ_ADD);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200359 sysfs_create_link(&p->kobj, &block_subsys.kset.kobj, "subsystem");
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800360 partition_sysfs_add_subdir(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 disk->part[part-1] = p;
362}
363
Greg Kroah-Hartman8218ef82005-12-13 15:17:34 -0800364static char *make_block_name(struct gendisk *disk)
365{
366 char *name;
367 static char *block_str = "block:";
368 int size;
Stephen Rothwell2436f032006-04-10 00:17:20 -0700369 char *s;
Greg Kroah-Hartman8218ef82005-12-13 15:17:34 -0800370
371 size = strlen(block_str) + strlen(disk->disk_name) + 1;
372 name = kmalloc(size, GFP_KERNEL);
373 if (!name)
374 return NULL;
375 strcpy(name, block_str);
376 strcat(name, disk->disk_name);
Stephen Rothwell2436f032006-04-10 00:17:20 -0700377 /* ewww... some of these buggers have / in name... */
378 s = strchr(name, '/');
379 if (s)
380 *s = '!';
Greg Kroah-Hartman8218ef82005-12-13 15:17:34 -0800381 return name;
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static void disk_sysfs_symlinks(struct gendisk *disk)
385{
386 struct device *target = get_device(disk->driverfs_dev);
387 if (target) {
Greg Kroah-Hartman8218ef82005-12-13 15:17:34 -0800388 char *disk_name = make_block_name(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 sysfs_create_link(&disk->kobj,&target->kobj,"device");
Greg Kroah-Hartman8218ef82005-12-13 15:17:34 -0800390 if (disk_name) {
391 sysfs_create_link(&target->kobj,&disk->kobj,disk_name);
392 kfree(disk_name);
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200395 sysfs_create_link(&disk->kobj, &block_subsys.kset.kobj, "subsystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398/* Not exported, helper to add_disk(). */
399void register_disk(struct gendisk *disk)
400{
401 struct block_device *bdev;
402 char *s;
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100403 int i;
404 struct hd_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int err;
406
407 strlcpy(disk->kobj.name,disk->disk_name,KOBJ_NAME_LEN);
408 /* ewww... some of these buggers have / in name... */
409 s = strchr(disk->kobj.name, '/');
410 if (s)
411 *s = '!';
412 if ((err = kobject_add(&disk->kobj)))
413 return;
414 disk_sysfs_symlinks(disk);
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800415 disk_sysfs_add_subdirs(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /* No minors to use for partitions */
Greg Kroah-Hartmana2964182005-06-20 21:15:16 -0700418 if (disk->minors == 1)
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100419 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /* No such device (e.g., media were just removed) */
422 if (!get_capacity(disk))
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100423 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 bdev = bdget_disk(disk, 0);
426 if (!bdev)
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100427 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100429 /* scan partition table, but suppress uevents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 bdev->bd_invalidated = 1;
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100431 disk->part_uevent_suppress = 1;
432 err = blkdev_get(bdev, FMODE_READ, 0);
433 disk->part_uevent_suppress = 0;
434 if (err < 0)
435 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 blkdev_put(bdev);
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100437
438exit:
439 /* announce disk after possible partitions are already created */
440 kobject_uevent(&disk->kobj, KOBJ_ADD);
441
442 /* announce possible partitions */
443 for (i = 1; i < disk->minors; i++) {
444 p = disk->part[i-1];
445 if (!p || !p->nr_sects)
446 continue;
447 kobject_uevent(&p->kobj, KOBJ_ADD);
448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
452{
453 struct parsed_partitions *state;
454 int p, res;
455
456 if (bdev->bd_part_count)
457 return -EBUSY;
458 res = invalidate_partition(disk, 0);
459 if (res)
460 return res;
461 bdev->bd_invalidated = 0;
462 for (p = 1; p < disk->minors; p++)
463 delete_partition(disk, p);
464 if (disk->fops->revalidate_disk)
465 disk->fops->revalidate_disk(disk);
466 if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
467 return 0;
468 for (p = 1; p < state->limit; p++) {
469 sector_t size = state->parts[p].size;
470 sector_t from = state->parts[p].from;
471 if (!size)
472 continue;
Mike Miller98bd34e2006-06-23 02:06:07 -0700473 if (from + size > get_capacity(disk)) {
474 printk(" %s: p%d exceeds device capacity\n",
475 disk->disk_name, p);
476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 add_partition(disk, p, from, size);
478#ifdef CONFIG_BLK_DEV_MD
479 if (state->parts[p].flags)
480 md_autodetect_dev(bdev->bd_dev+p);
481#endif
482 }
483 kfree(state);
484 return 0;
485}
486
487unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
488{
489 struct address_space *mapping = bdev->bd_inode->i_mapping;
490 struct page *page;
491
Pekka Enberg090d2b12006-06-23 02:05:08 -0700492 page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
493 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (!IS_ERR(page)) {
495 wait_on_page_locked(page);
496 if (!PageUptodate(page))
497 goto fail;
498 if (PageError(page))
499 goto fail;
500 p->v = page;
501 return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
502fail:
503 page_cache_release(page);
504 }
505 p->v = NULL;
506 return NULL;
507}
508
509EXPORT_SYMBOL(read_dev_sector);
510
511void del_gendisk(struct gendisk *disk)
512{
513 int p;
514
515 /* invalidate stuff */
516 for (p = disk->minors - 1; p > 0; p--) {
517 invalidate_partition(disk, p);
518 delete_partition(disk, p);
519 }
520 invalidate_partition(disk, 0);
521 disk->capacity = 0;
522 disk->flags &= ~GENHD_FL_UP;
523 unlink_gendisk(disk);
524 disk_stat_set_all(disk, 0);
Chen, Kenneth W20e5c812005-10-13 21:48:42 +0200525 disk->stamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Linus Torvalds032ebf262006-05-12 18:42:09 -0700527 kobject_uevent(&disk->kobj, KOBJ_REMOVE);
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800528 if (disk->holder_dir)
529 kobject_unregister(disk->holder_dir);
530 if (disk->slave_dir)
531 kobject_unregister(disk->slave_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (disk->driverfs_dev) {
Greg Kroah-Hartman8218ef82005-12-13 15:17:34 -0800533 char *disk_name = make_block_name(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 sysfs_remove_link(&disk->kobj, "device");
Greg Kroah-Hartman8218ef82005-12-13 15:17:34 -0800535 if (disk_name) {
536 sysfs_remove_link(&disk->driverfs_dev->kobj, disk_name);
537 kfree(disk_name);
538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 put_device(disk->driverfs_dev);
Linus Torvalds032ebf262006-05-12 18:42:09 -0700540 disk->driverfs_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200542 sysfs_remove_link(&disk->kobj, "subsystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 kobject_del(&disk->kobj);
544}