blob: 8dc1822a70226816396f3a6fe61a7808a0a3eb97 [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"
24#include "devfs.h"
25
26#include "acorn.h"
27#include "amiga.h"
28#include "atari.h"
29#include "ldm.h"
30#include "mac.h"
31#include "msdos.h"
32#include "osf.h"
33#include "sgi.h"
34#include "sun.h"
35#include "ibm.h"
36#include "ultrix.h"
37#include "efi.h"
38
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
106 NULL
107};
108
109/*
110 * disk_name() is used by partition check code and the genhd driver.
111 * It formats the devicename of the indicated disk into
112 * the supplied buffer (of size at least 32), and returns
113 * a pointer to that same buffer (for convenience).
114 */
115
116char *disk_name(struct gendisk *hd, int part, char *buf)
117{
118 if (!part)
119 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
120 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
121 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, part);
122 else
123 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, part);
124
125 return buf;
126}
127
128const char *bdevname(struct block_device *bdev, char *buf)
129{
130 int part = MINOR(bdev->bd_dev) - bdev->bd_disk->first_minor;
131 return disk_name(bdev->bd_disk, part, buf);
132}
133
134EXPORT_SYMBOL(bdevname);
135
136/*
137 * There's very little reason to use this, you should really
138 * have a struct block_device just about everywhere and use
139 * bdevname() instead.
140 */
141const char *__bdevname(dev_t dev, char *buffer)
142{
143 scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
144 MAJOR(dev), MINOR(dev));
145 return buffer;
146}
147
148EXPORT_SYMBOL(__bdevname);
149
150static struct parsed_partitions *
151check_partition(struct gendisk *hd, struct block_device *bdev)
152{
153 struct parsed_partitions *state;
154 int i, res;
155
156 state = kmalloc(sizeof(struct parsed_partitions), GFP_KERNEL);
157 if (!state)
158 return NULL;
159
160#ifdef CONFIG_DEVFS_FS
161 if (hd->devfs_name[0] != '\0') {
162 printk(KERN_INFO " /dev/%s:", hd->devfs_name);
163 sprintf(state->name, "p");
164 }
165#endif
166 else {
167 disk_name(hd, 0, state->name);
168 printk(KERN_INFO " %s:", state->name);
169 if (isdigit(state->name[strlen(state->name)-1]))
170 sprintf(state->name, "p");
171 }
172 state->limit = hd->minors;
173 i = res = 0;
174 while (!res && check_part[i]) {
175 memset(&state->parts, 0, sizeof(state->parts));
176 res = check_part[i++](state, bdev);
177 }
178 if (res > 0)
179 return state;
180 if (!res)
181 printk(" unknown partition table\n");
182 else if (warn_no_part)
183 printk(" unable to read partition table\n");
184 kfree(state);
185 return NULL;
186}
187
188/*
189 * sysfs bindings for partitions
190 */
191
192struct part_attribute {
193 struct attribute attr;
194 ssize_t (*show)(struct hd_struct *,char *);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200195 ssize_t (*store)(struct hd_struct *,const char *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
198static ssize_t
199part_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
200{
201 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
202 struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr);
203 ssize_t ret = 0;
204 if (part_attr->show)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200205 ret = part_attr->show(p, page);
206 return ret;
207}
208static ssize_t
209part_attr_store(struct kobject * kobj, struct attribute * attr,
210 const char *page, size_t count)
211{
212 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
213 struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr);
214 ssize_t ret = 0;
215
216 if (part_attr->store)
217 ret = part_attr->store(p, page, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return ret;
219}
220
221static struct sysfs_ops part_sysfs_ops = {
222 .show = part_attr_show,
Kay Sieversa7fd6702005-10-01 14:49:43 +0200223 .store = part_attr_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
Kay Sieversa7fd6702005-10-01 14:49:43 +0200226static ssize_t part_uevent_store(struct hd_struct * p,
227 const char *page, size_t count)
228{
229 kobject_hotplug(&p->kobj, KOBJ_ADD);
230 return count;
231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static ssize_t part_dev_read(struct hd_struct * p, char *page)
233{
234 struct gendisk *disk = container_of(p->kobj.parent,struct gendisk,kobj);
235 dev_t dev = MKDEV(disk->major, disk->first_minor + p->partno);
236 return print_dev_t(page, dev);
237}
238static ssize_t part_start_read(struct hd_struct * p, char *page)
239{
240 return sprintf(page, "%llu\n",(unsigned long long)p->start_sect);
241}
242static ssize_t part_size_read(struct hd_struct * p, char *page)
243{
244 return sprintf(page, "%llu\n",(unsigned long long)p->nr_sects);
245}
246static ssize_t part_stat_read(struct hd_struct * p, char *page)
247{
248 return sprintf(page, "%8u %8llu %8u %8llu\n",
Jens Axboea3623572005-11-01 09:26:16 +0100249 p->ios[0], (unsigned long long)p->sectors[0],
250 p->ios[1], (unsigned long long)p->sectors[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
Kay Sieversa7fd6702005-10-01 14:49:43 +0200252static struct part_attribute part_attr_uevent = {
253 .attr = {.name = "uevent", .mode = S_IWUSR },
254 .store = part_uevent_store
255};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static struct part_attribute part_attr_dev = {
257 .attr = {.name = "dev", .mode = S_IRUGO },
258 .show = part_dev_read
259};
260static struct part_attribute part_attr_start = {
261 .attr = {.name = "start", .mode = S_IRUGO },
262 .show = part_start_read
263};
264static struct part_attribute part_attr_size = {
265 .attr = {.name = "size", .mode = S_IRUGO },
266 .show = part_size_read
267};
268static struct part_attribute part_attr_stat = {
269 .attr = {.name = "stat", .mode = S_IRUGO },
270 .show = part_stat_read
271};
272
273static struct attribute * default_attrs[] = {
Kay Sieversa7fd6702005-10-01 14:49:43 +0200274 &part_attr_uevent.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 &part_attr_dev.attr,
276 &part_attr_start.attr,
277 &part_attr_size.attr,
278 &part_attr_stat.attr,
279 NULL,
280};
281
282extern struct subsystem block_subsys;
283
284static void part_release(struct kobject *kobj)
285{
286 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
287 kfree(p);
288}
289
290struct kobj_type ktype_part = {
291 .release = part_release,
292 .default_attrs = default_attrs,
293 .sysfs_ops = &part_sysfs_ops,
294};
295
296void delete_partition(struct gendisk *disk, int part)
297{
298 struct hd_struct *p = disk->part[part-1];
299 if (!p)
300 return;
301 if (!p->nr_sects)
302 return;
303 disk->part[part-1] = NULL;
304 p->start_sect = 0;
305 p->nr_sects = 0;
Jens Axboea3623572005-11-01 09:26:16 +0100306 p->ios[0] = p->ios[1] = 0;
307 p->sectors[0] = p->sectors[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 devfs_remove("%s/part%d", disk->devfs_name, part);
309 kobject_unregister(&p->kobj);
310}
311
312void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len)
313{
314 struct hd_struct *p;
315
316 p = kmalloc(sizeof(*p), GFP_KERNEL);
317 if (!p)
318 return;
319
320 memset(p, 0, sizeof(*p));
321 p->start_sect = start;
322 p->nr_sects = len;
323 p->partno = part;
324
325 devfs_mk_bdev(MKDEV(disk->major, disk->first_minor + part),
326 S_IFBLK|S_IRUSR|S_IWUSR,
327 "%s/part%d", disk->devfs_name, part);
328
329 if (isdigit(disk->kobj.name[strlen(disk->kobj.name)-1]))
330 snprintf(p->kobj.name,KOBJ_NAME_LEN,"%sp%d",disk->kobj.name,part);
331 else
332 snprintf(p->kobj.name,KOBJ_NAME_LEN,"%s%d",disk->kobj.name,part);
333 p->kobj.parent = &disk->kobj;
334 p->kobj.ktype = &ktype_part;
335 kobject_register(&p->kobj);
336 disk->part[part-1] = p;
337}
338
339static void disk_sysfs_symlinks(struct gendisk *disk)
340{
341 struct device *target = get_device(disk->driverfs_dev);
342 if (target) {
343 sysfs_create_link(&disk->kobj,&target->kobj,"device");
344 sysfs_create_link(&target->kobj,&disk->kobj,"block");
345 }
346}
347
348/* Not exported, helper to add_disk(). */
349void register_disk(struct gendisk *disk)
350{
351 struct block_device *bdev;
352 char *s;
353 int err;
354
355 strlcpy(disk->kobj.name,disk->disk_name,KOBJ_NAME_LEN);
356 /* ewww... some of these buggers have / in name... */
357 s = strchr(disk->kobj.name, '/');
358 if (s)
359 *s = '!';
360 if ((err = kobject_add(&disk->kobj)))
361 return;
362 disk_sysfs_symlinks(disk);
kay.sievers@vrfy.orgb41148c2005-04-18 21:57:36 -0700363 kobject_hotplug(&disk->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /* No minors to use for partitions */
366 if (disk->minors == 1) {
367 if (disk->devfs_name[0] != '\0')
368 devfs_add_disk(disk);
369 return;
370 }
371
372 /* always add handle for the whole disk */
373 devfs_add_partitioned(disk);
374
375 /* No such device (e.g., media were just removed) */
376 if (!get_capacity(disk))
377 return;
378
379 bdev = bdget_disk(disk, 0);
380 if (!bdev)
381 return;
382
383 bdev->bd_invalidated = 1;
384 if (blkdev_get(bdev, FMODE_READ, 0) < 0)
385 return;
386 blkdev_put(bdev);
387}
388
389int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
390{
391 struct parsed_partitions *state;
392 int p, res;
393
394 if (bdev->bd_part_count)
395 return -EBUSY;
396 res = invalidate_partition(disk, 0);
397 if (res)
398 return res;
399 bdev->bd_invalidated = 0;
400 for (p = 1; p < disk->minors; p++)
401 delete_partition(disk, p);
402 if (disk->fops->revalidate_disk)
403 disk->fops->revalidate_disk(disk);
404 if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
405 return 0;
406 for (p = 1; p < state->limit; p++) {
407 sector_t size = state->parts[p].size;
408 sector_t from = state->parts[p].from;
409 if (!size)
410 continue;
411 add_partition(disk, p, from, size);
412#ifdef CONFIG_BLK_DEV_MD
413 if (state->parts[p].flags)
414 md_autodetect_dev(bdev->bd_dev+p);
415#endif
416 }
417 kfree(state);
418 return 0;
419}
420
421unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
422{
423 struct address_space *mapping = bdev->bd_inode->i_mapping;
424 struct page *page;
425
426 page = read_cache_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
427 (filler_t *)mapping->a_ops->readpage, NULL);
428 if (!IS_ERR(page)) {
429 wait_on_page_locked(page);
430 if (!PageUptodate(page))
431 goto fail;
432 if (PageError(page))
433 goto fail;
434 p->v = page;
435 return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
436fail:
437 page_cache_release(page);
438 }
439 p->v = NULL;
440 return NULL;
441}
442
443EXPORT_SYMBOL(read_dev_sector);
444
445void del_gendisk(struct gendisk *disk)
446{
447 int p;
448
449 /* invalidate stuff */
450 for (p = disk->minors - 1; p > 0; p--) {
451 invalidate_partition(disk, p);
452 delete_partition(disk, p);
453 }
454 invalidate_partition(disk, 0);
455 disk->capacity = 0;
456 disk->flags &= ~GENHD_FL_UP;
457 unlink_gendisk(disk);
458 disk_stat_set_all(disk, 0);
Chen, Kenneth W20e5c812005-10-13 21:48:42 +0200459 disk->stamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 devfs_remove_disk(disk);
462
463 if (disk->driverfs_dev) {
464 sysfs_remove_link(&disk->kobj, "device");
465 sysfs_remove_link(&disk->driverfs_dev->kobj, "block");
466 put_device(disk->driverfs_dev);
467 }
kay.sievers@vrfy.orgb41148c2005-04-18 21:57:36 -0700468 kobject_hotplug(&disk->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 kobject_del(&disk->kobj);
470}