blob: 722e12e5acc71e9b90051da29f5611b5e3182f9c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include "check.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
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"
Bob Copeland0e6e1db2006-01-16 22:14:20 -080036#include "karma.h"
Philippe De Muyter19d0e8c2007-05-08 00:29:15 -070037#include "sysv68.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
Philippe De Muyter19d0e8c2007-05-08 00:29:15 -0700109#ifdef CONFIG_SYSV68_PARTITION
110 sysv68_partition,
111#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 NULL
113};
114
115/*
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).
120 */
121
122char *disk_name(struct gendisk *hd, int part, char *buf)
123{
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);
130
131 return buf;
132}
133
134const char *bdevname(struct block_device *bdev, char *buf)
135{
136 int part = MINOR(bdev->bd_dev) - bdev->bd_disk->first_minor;
137 return disk_name(bdev->bd_disk, part, buf);
138}
139
140EXPORT_SYMBOL(bdevname);
141
142/*
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.
146 */
147const char *__bdevname(dev_t dev, char *buffer)
148{
149 scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
150 MAJOR(dev), MINOR(dev));
151 return buffer;
152}
153
154EXPORT_SYMBOL(__bdevname);
155
156static struct parsed_partitions *
157check_partition(struct gendisk *hd, struct block_device *bdev)
158{
159 struct parsed_partitions *state;
Suzuki K P57881dd2006-12-06 20:35:16 -0800160 int i, res, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 state = kmalloc(sizeof(struct parsed_partitions), GFP_KERNEL);
163 if (!state)
164 return NULL;
165
Greg Kroah-Hartmana2964182005-06-20 21:15:16 -0700166 disk_name(hd, 0, state->name);
167 printk(KERN_INFO " %s:", state->name);
168 if (isdigit(state->name[strlen(state->name)-1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 sprintf(state->name, "p");
Greg Kroah-Hartmana2964182005-06-20 21:15:16 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 state->limit = hd->minors;
Suzuki K P57881dd2006-12-06 20:35:16 -0800172 i = res = err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 while (!res && check_part[i]) {
174 memset(&state->parts, 0, sizeof(state->parts));
175 res = check_part[i++](state, bdev);
Suzuki K P57881dd2006-12-06 20:35:16 -0800176 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.
179 */
180 err = res;
181 res = 0;
182 }
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185 if (res > 0)
186 return state;
suzuki9bebff62007-03-07 20:41:24 -0800187 if (err)
Suzuki K P57881dd2006-12-06 20:35:16 -0800188 /* The partition is unrecognized. So report I/O errors if there were any */
189 res = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 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);
Suzuki Kp5127d002006-12-06 20:35:14 -0800195 return ERR_PTR(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198/*
199 * sysfs bindings for partitions
200 */
201
202struct part_attribute {
203 struct attribute attr;
204 ssize_t (*show)(struct hd_struct *,char *);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200205 ssize_t (*store)(struct hd_struct *,const char *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
208static ssize_t
209part_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
210{
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)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200215 ret = part_attr->show(p, page);
216 return ret;
217}
218static ssize_t
219part_attr_store(struct kobject * kobj, struct attribute * attr,
220 const char *page, size_t count)
221{
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;
225
226 if (part_attr->store)
227 ret = part_attr->store(p, page, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return ret;
229}
230
231static struct sysfs_ops part_sysfs_ops = {
232 .show = part_attr_show,
Kay Sieversa7fd6702005-10-01 14:49:43 +0200233 .store = part_attr_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
Kay Sieversa7fd6702005-10-01 14:49:43 +0200236static ssize_t part_uevent_store(struct hd_struct * p,
237 const char *page, size_t count)
238{
Kay Sievers312c0042005-11-16 09:00:00 +0100239 kobject_uevent(&p->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200240 return count;
241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static ssize_t part_dev_read(struct hd_struct * p, char *page)
243{
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);
247}
248static ssize_t part_start_read(struct hd_struct * p, char *page)
249{
250 return sprintf(page, "%llu\n",(unsigned long long)p->start_sect);
251}
252static ssize_t part_size_read(struct hd_struct * p, char *page)
253{
254 return sprintf(page, "%llu\n",(unsigned long long)p->nr_sects);
255}
256static ssize_t part_stat_read(struct hd_struct * p, char *page)
257{
258 return sprintf(page, "%8u %8llu %8u %8llu\n",
Jens Axboea3623572005-11-01 09:26:16 +0100259 p->ios[0], (unsigned long long)p->sectors[0],
260 p->ios[1], (unsigned long long)p->sectors[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
Kay Sieversa7fd6702005-10-01 14:49:43 +0200262static struct part_attribute part_attr_uevent = {
263 .attr = {.name = "uevent", .mode = S_IWUSR },
264 .store = part_uevent_store
265};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static struct part_attribute part_attr_dev = {
267 .attr = {.name = "dev", .mode = S_IRUGO },
268 .show = part_dev_read
269};
270static struct part_attribute part_attr_start = {
271 .attr = {.name = "start", .mode = S_IRUGO },
272 .show = part_start_read
273};
274static struct part_attribute part_attr_size = {
275 .attr = {.name = "size", .mode = S_IRUGO },
276 .show = part_size_read
277};
278static struct part_attribute part_attr_stat = {
279 .attr = {.name = "stat", .mode = S_IRUGO },
280 .show = part_stat_read
281};
282
Akinobu Mitac17bb492006-12-08 02:39:46 -0800283#ifdef CONFIG_FAIL_MAKE_REQUEST
284
285static ssize_t part_fail_store(struct hd_struct * p,
286 const char *buf, size_t count)
287{
288 int i;
289
290 if (count > 0 && sscanf(buf, "%d", &i) > 0)
291 p->make_it_fail = (i == 0) ? 0 : 1;
292
293 return count;
294}
295static ssize_t part_fail_read(struct hd_struct * p, char *page)
296{
297 return sprintf(page, "%d\n", p->make_it_fail);
298}
299static 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
303};
304
305#endif
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static struct attribute * default_attrs[] = {
Kay Sieversa7fd6702005-10-01 14:49:43 +0200308 &part_attr_uevent.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 &part_attr_dev.attr,
310 &part_attr_start.attr,
311 &part_attr_size.attr,
312 &part_attr_stat.attr,
Akinobu Mitac17bb492006-12-08 02:39:46 -0800313#ifdef CONFIG_FAIL_MAKE_REQUEST
314 &part_attr_fail.attr,
315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 NULL,
317};
318
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700319extern struct kset block_subsys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321static void part_release(struct kobject *kobj)
322{
323 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
324 kfree(p);
325}
326
327struct kobj_type ktype_part = {
328 .release = part_release,
329 .default_attrs = default_attrs,
330 .sysfs_ops = &part_sysfs_ops,
331};
332
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800333static inline void partition_sysfs_add_subdir(struct hd_struct *p)
334{
335 struct kobject *k;
336
337 k = kobject_get(&p->kobj);
338 p->holder_dir = kobject_add_dir(k, "holders");
339 kobject_put(k);
340}
341
342static inline void disk_sysfs_add_subdirs(struct gendisk *disk)
343{
344 struct kobject *k;
345
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);
350}
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352void delete_partition(struct gendisk *disk, int part)
353{
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;
Jens Axboea3623572005-11-01 09:26:16 +0100362 p->ios[0] = p->ios[1] = 0;
363 p->sectors[0] = p->sectors[1] = 0;
Kay Sieversb9d9c822006-06-15 15:31:56 +0200364 sysfs_remove_link(&p->kobj, "subsystem");
Mariusz Kozlowskib92be9f2007-02-14 21:03:39 +0100365 kobject_unregister(p->holder_dir);
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100366 kobject_uevent(&p->kobj, KOBJ_REMOVE);
367 kobject_del(&p->kobj);
368 kobject_put(&p->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -0800371void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 struct hd_struct *p;
374
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700375 p = kzalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (!p)
377 return;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 p->start_sect = start;
380 p->nr_sects = len;
381 p->partno = part;
Peter Oberparleiter25e206b2006-07-10 04:44:00 -0700382 p->policy = disk->policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Greg Kroah-Hartman19c38de2007-09-12 15:06:57 -0700384 if (isdigit(disk->kobj.k_name[strlen(disk->kobj.k_name)-1]))
385 kobject_set_name(&p->kobj, "%sp%d",
386 kobject_name(&disk->kobj), part);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 else
Greg Kroah-Hartman19c38de2007-09-12 15:06:57 -0700388 kobject_set_name(&p->kobj, "%s%d",
389 kobject_name(&disk->kobj),part);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 p->kobj.parent = &disk->kobj;
391 p->kobj.ktype = &ktype_part;
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100392 kobject_init(&p->kobj);
393 kobject_add(&p->kobj);
394 if (!disk->part_uevent_suppress)
395 kobject_uevent(&p->kobj, KOBJ_ADD);
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700396 sysfs_create_link(&p->kobj, &block_subsys.kobj, "subsystem");
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -0800397 if (flags & ADDPART_FLAG_WHOLEDISK) {
398 static struct attribute addpartattr = {
399 .name = "whole_disk",
400 .mode = S_IRUSR | S_IRGRP | S_IROTH,
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -0800401 };
402
403 sysfs_create_file(&p->kobj, &addpartattr);
404 }
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800405 partition_sysfs_add_subdir(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 disk->part[part-1] = p;
407}
408
Greg Kroah-Hartman8218ef82005-12-13 15:17:34 -0800409static char *make_block_name(struct gendisk *disk)
410{
411 char *name;
412 static char *block_str = "block:";
413 int size;
Stephen Rothwell2436f032006-04-10 00:17:20 -0700414 char *s;
Greg Kroah-Hartman8218ef82005-12-13 15:17:34 -0800415
416 size = strlen(block_str) + strlen(disk->disk_name) + 1;
417 name = kmalloc(size, GFP_KERNEL);
418 if (!name)
419 return NULL;
420 strcpy(name, block_str);
421 strcat(name, disk->disk_name);
Stephen Rothwell2436f032006-04-10 00:17:20 -0700422 /* ewww... some of these buggers have / in name... */
423 s = strchr(name, '/');
424 if (s)
425 *s = '!';
Greg Kroah-Hartman8218ef82005-12-13 15:17:34 -0800426 return name;
427}
428
Jeff Garzikeee44cc2006-10-17 00:10:23 -0700429static int disk_sysfs_symlinks(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct device *target = get_device(disk->driverfs_dev);
Jeff Garzikeee44cc2006-10-17 00:10:23 -0700432 int err;
433 char *disk_name = NULL;
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (target) {
Jeff Garzikeee44cc2006-10-17 00:10:23 -0700436 disk_name = make_block_name(disk);
437 if (!disk_name) {
438 err = -ENOMEM;
439 goto err_out;
Greg Kroah-Hartman8218ef82005-12-13 15:17:34 -0800440 }
Jeff Garzikeee44cc2006-10-17 00:10:23 -0700441
442 err = sysfs_create_link(&disk->kobj, &target->kobj, "device");
443 if (err)
444 goto err_out_disk_name;
445
446 err = sysfs_create_link(&target->kobj, &disk->kobj, disk_name);
447 if (err)
448 goto err_out_dev_link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Jeff Garzikeee44cc2006-10-17 00:10:23 -0700450
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700451 err = sysfs_create_link(&disk->kobj, &block_subsys.kobj,
Jeff Garzikeee44cc2006-10-17 00:10:23 -0700452 "subsystem");
453 if (err)
454 goto err_out_disk_name_lnk;
455
456 kfree(disk_name);
457
458 return 0;
459
460err_out_disk_name_lnk:
461 if (target) {
462 sysfs_remove_link(&target->kobj, disk_name);
463err_out_dev_link:
464 sysfs_remove_link(&disk->kobj, "device");
465err_out_disk_name:
466 kfree(disk_name);
467err_out:
468 put_device(target);
469 }
470 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473/* Not exported, helper to add_disk(). */
474void register_disk(struct gendisk *disk)
475{
476 struct block_device *bdev;
477 char *s;
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100478 int i;
479 struct hd_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 int err;
481
Greg Kroah-Hartman19c38de2007-09-12 15:06:57 -0700482 kobject_set_name(&disk->kobj, "%s", disk->disk_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* ewww... some of these buggers have / in name... */
Greg Kroah-Hartman19c38de2007-09-12 15:06:57 -0700484 s = strchr(disk->kobj.k_name, '/');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (s)
486 *s = '!';
487 if ((err = kobject_add(&disk->kobj)))
488 return;
Jeff Garzikeee44cc2006-10-17 00:10:23 -0700489 err = disk_sysfs_symlinks(disk);
490 if (err) {
491 kobject_del(&disk->kobj);
492 return;
493 }
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800494 disk_sysfs_add_subdirs(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 /* No minors to use for partitions */
Greg Kroah-Hartmana2964182005-06-20 21:15:16 -0700497 if (disk->minors == 1)
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100498 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* No such device (e.g., media were just removed) */
501 if (!get_capacity(disk))
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100502 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 bdev = bdget_disk(disk, 0);
505 if (!bdev)
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100506 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100508 /* scan partition table, but suppress uevents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 bdev->bd_invalidated = 1;
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100510 disk->part_uevent_suppress = 1;
511 err = blkdev_get(bdev, FMODE_READ, 0);
512 disk->part_uevent_suppress = 0;
513 if (err < 0)
514 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 blkdev_put(bdev);
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100516
517exit:
518 /* announce disk after possible partitions are already created */
519 kobject_uevent(&disk->kobj, KOBJ_ADD);
520
521 /* announce possible partitions */
522 for (i = 1; i < disk->minors; i++) {
523 p = disk->part[i-1];
524 if (!p || !p->nr_sects)
525 continue;
526 kobject_uevent(&p->kobj, KOBJ_ADD);
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
531{
532 struct parsed_partitions *state;
533 int p, res;
534
535 if (bdev->bd_part_count)
536 return -EBUSY;
537 res = invalidate_partition(disk, 0);
538 if (res)
539 return res;
540 bdev->bd_invalidated = 0;
541 for (p = 1; p < disk->minors; p++)
542 delete_partition(disk, p);
543 if (disk->fops->revalidate_disk)
544 disk->fops->revalidate_disk(disk);
545 if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
546 return 0;
Suzuki Kp5127d002006-12-06 20:35:14 -0800547 if (IS_ERR(state)) /* I/O error reading the partition table */
suzukib74a2f02007-03-16 13:38:25 -0800548 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 for (p = 1; p < state->limit; p++) {
550 sector_t size = state->parts[p].size;
551 sector_t from = state->parts[p].from;
552 if (!size)
553 continue;
Mike Miller98bd34e2006-06-23 02:06:07 -0700554 if (from + size > get_capacity(disk)) {
555 printk(" %s: p%d exceeds device capacity\n",
556 disk->disk_name, p);
557 }
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -0800558 add_partition(disk, p, from, size, state->parts[p].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559#ifdef CONFIG_BLK_DEV_MD
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -0800560 if (state->parts[p].flags & ADDPART_FLAG_RAID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 md_autodetect_dev(bdev->bd_dev+p);
562#endif
563 }
564 kfree(state);
565 return 0;
566}
567
568unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
569{
570 struct address_space *mapping = bdev->bd_inode->i_mapping;
571 struct page *page;
572
Pekka Enberg090d2b12006-06-23 02:05:08 -0700573 page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
574 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (!IS_ERR(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (PageError(page))
577 goto fail;
578 p->v = page;
579 return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
580fail:
581 page_cache_release(page);
582 }
583 p->v = NULL;
584 return NULL;
585}
586
587EXPORT_SYMBOL(read_dev_sector);
588
589void del_gendisk(struct gendisk *disk)
590{
591 int p;
592
593 /* invalidate stuff */
594 for (p = disk->minors - 1; p > 0; p--) {
595 invalidate_partition(disk, p);
596 delete_partition(disk, p);
597 }
598 invalidate_partition(disk, 0);
599 disk->capacity = 0;
600 disk->flags &= ~GENHD_FL_UP;
601 unlink_gendisk(disk);
602 disk_stat_set_all(disk, 0);
Chen, Kenneth W20e5c812005-10-13 21:48:42 +0200603 disk->stamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds032ebf22006-05-12 18:42:09 -0700605 kobject_uevent(&disk->kobj, KOBJ_REMOVE);
Mariusz Kozlowskib92be9f2007-02-14 21:03:39 +0100606 kobject_unregister(disk->holder_dir);
607 kobject_unregister(disk->slave_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (disk->driverfs_dev) {
Greg Kroah-Hartman8218ef82005-12-13 15:17:34 -0800609 char *disk_name = make_block_name(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 sysfs_remove_link(&disk->kobj, "device");
Greg Kroah-Hartman8218ef82005-12-13 15:17:34 -0800611 if (disk_name) {
612 sysfs_remove_link(&disk->driverfs_dev->kobj, disk_name);
613 kfree(disk_name);
614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 put_device(disk->driverfs_dev);
Linus Torvalds032ebf22006-05-12 18:42:09 -0700616 disk->driverfs_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200618 sysfs_remove_link(&disk->kobj, "subsystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 kobject_del(&disk->kobj);
620}