]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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" | |
0e6e1db4 | 38 | #include "karma.h" |
1da177e4 LT |
39 | |
40 | #ifdef CONFIG_BLK_DEV_MD | |
41 | extern void md_autodetect_dev(dev_t dev); | |
42 | #endif | |
43 | ||
44 | int warn_no_part = 1; /*This is ugly: should make genhd removable media aware*/ | |
45 | ||
46 | static int (*check_part[])(struct parsed_partitions *, struct block_device *) = { | |
47 | /* | |
48 | * Probe partition formats with tables at disk address 0 | |
49 | * that also have an ADFS boot block at 0xdc0. | |
50 | */ | |
51 | #ifdef CONFIG_ACORN_PARTITION_ICS | |
52 | adfspart_check_ICS, | |
53 | #endif | |
54 | #ifdef CONFIG_ACORN_PARTITION_POWERTEC | |
55 | adfspart_check_POWERTEC, | |
56 | #endif | |
57 | #ifdef CONFIG_ACORN_PARTITION_EESOX | |
58 | adfspart_check_EESOX, | |
59 | #endif | |
60 | ||
61 | /* | |
62 | * Now move on to formats that only have partition info at | |
63 | * disk address 0xdc0. Since these may also have stale | |
64 | * PC/BIOS partition tables, they need to come before | |
65 | * the msdos entry. | |
66 | */ | |
67 | #ifdef CONFIG_ACORN_PARTITION_CUMANA | |
68 | adfspart_check_CUMANA, | |
69 | #endif | |
70 | #ifdef CONFIG_ACORN_PARTITION_ADFS | |
71 | adfspart_check_ADFS, | |
72 | #endif | |
73 | ||
74 | #ifdef CONFIG_EFI_PARTITION | |
75 | efi_partition, /* this must come before msdos */ | |
76 | #endif | |
77 | #ifdef CONFIG_SGI_PARTITION | |
78 | sgi_partition, | |
79 | #endif | |
80 | #ifdef CONFIG_LDM_PARTITION | |
81 | ldm_partition, /* this must come before msdos */ | |
82 | #endif | |
1da177e4 LT |
83 | #ifdef CONFIG_MSDOS_PARTITION |
84 | msdos_partition, | |
85 | #endif | |
86 | #ifdef CONFIG_OSF_PARTITION | |
87 | osf_partition, | |
88 | #endif | |
89 | #ifdef CONFIG_SUN_PARTITION | |
90 | sun_partition, | |
91 | #endif | |
92 | #ifdef CONFIG_AMIGA_PARTITION | |
93 | amiga_partition, | |
94 | #endif | |
95 | #ifdef CONFIG_ATARI_PARTITION | |
96 | atari_partition, | |
97 | #endif | |
98 | #ifdef CONFIG_MAC_PARTITION | |
99 | mac_partition, | |
100 | #endif | |
101 | #ifdef CONFIG_ULTRIX_PARTITION | |
102 | ultrix_partition, | |
103 | #endif | |
104 | #ifdef CONFIG_IBM_PARTITION | |
105 | ibm_partition, | |
0e6e1db4 BC |
106 | #endif |
107 | #ifdef CONFIG_KARMA_PARTITION | |
108 | karma_partition, | |
1da177e4 LT |
109 | #endif |
110 | NULL | |
111 | }; | |
112 | ||
113 | /* | |
114 | * disk_name() is used by partition check code and the genhd driver. | |
115 | * It formats the devicename of the indicated disk into | |
116 | * the supplied buffer (of size at least 32), and returns | |
117 | * a pointer to that same buffer (for convenience). | |
118 | */ | |
119 | ||
120 | char *disk_name(struct gendisk *hd, int part, char *buf) | |
121 | { | |
122 | if (!part) | |
123 | snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name); | |
124 | else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) | |
125 | snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, part); | |
126 | else | |
127 | snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, part); | |
128 | ||
129 | return buf; | |
130 | } | |
131 | ||
132 | const char *bdevname(struct block_device *bdev, char *buf) | |
133 | { | |
134 | int part = MINOR(bdev->bd_dev) - bdev->bd_disk->first_minor; | |
135 | return disk_name(bdev->bd_disk, part, buf); | |
136 | } | |
137 | ||
138 | EXPORT_SYMBOL(bdevname); | |
139 | ||
140 | /* | |
141 | * There's very little reason to use this, you should really | |
142 | * have a struct block_device just about everywhere and use | |
143 | * bdevname() instead. | |
144 | */ | |
145 | const char *__bdevname(dev_t dev, char *buffer) | |
146 | { | |
147 | scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)", | |
148 | MAJOR(dev), MINOR(dev)); | |
149 | return buffer; | |
150 | } | |
151 | ||
152 | EXPORT_SYMBOL(__bdevname); | |
153 | ||
154 | static struct parsed_partitions * | |
155 | check_partition(struct gendisk *hd, struct block_device *bdev) | |
156 | { | |
157 | struct parsed_partitions *state; | |
158 | int i, res; | |
159 | ||
160 | state = kmalloc(sizeof(struct parsed_partitions), GFP_KERNEL); | |
161 | if (!state) | |
162 | return NULL; | |
163 | ||
164 | #ifdef CONFIG_DEVFS_FS | |
165 | if (hd->devfs_name[0] != '\0') { | |
166 | printk(KERN_INFO " /dev/%s:", hd->devfs_name); | |
167 | sprintf(state->name, "p"); | |
168 | } | |
169 | #endif | |
170 | else { | |
171 | disk_name(hd, 0, state->name); | |
172 | printk(KERN_INFO " %s:", state->name); | |
173 | if (isdigit(state->name[strlen(state->name)-1])) | |
174 | sprintf(state->name, "p"); | |
175 | } | |
176 | state->limit = hd->minors; | |
177 | i = res = 0; | |
178 | while (!res && check_part[i]) { | |
179 | memset(&state->parts, 0, sizeof(state->parts)); | |
180 | res = check_part[i++](state, bdev); | |
181 | } | |
182 | if (res > 0) | |
183 | return state; | |
184 | if (!res) | |
185 | printk(" unknown partition table\n"); | |
186 | else if (warn_no_part) | |
187 | printk(" unable to read partition table\n"); | |
188 | kfree(state); | |
189 | return NULL; | |
190 | } | |
191 | ||
192 | /* | |
193 | * sysfs bindings for partitions | |
194 | */ | |
195 | ||
196 | struct part_attribute { | |
197 | struct attribute attr; | |
198 | ssize_t (*show)(struct hd_struct *,char *); | |
a7fd6706 | 199 | ssize_t (*store)(struct hd_struct *,const char *, size_t); |
1da177e4 LT |
200 | }; |
201 | ||
202 | static ssize_t | |
203 | part_attr_show(struct kobject * kobj, struct attribute * attr, char * page) | |
204 | { | |
205 | struct hd_struct * p = container_of(kobj,struct hd_struct,kobj); | |
206 | struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr); | |
207 | ssize_t ret = 0; | |
208 | if (part_attr->show) | |
a7fd6706 KS |
209 | ret = part_attr->show(p, page); |
210 | return ret; | |
211 | } | |
212 | static ssize_t | |
213 | part_attr_store(struct kobject * kobj, struct attribute * attr, | |
214 | const char *page, size_t count) | |
215 | { | |
216 | struct hd_struct * p = container_of(kobj,struct hd_struct,kobj); | |
217 | struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr); | |
218 | ssize_t ret = 0; | |
219 | ||
220 | if (part_attr->store) | |
221 | ret = part_attr->store(p, page, count); | |
1da177e4 LT |
222 | return ret; |
223 | } | |
224 | ||
225 | static struct sysfs_ops part_sysfs_ops = { | |
226 | .show = part_attr_show, | |
a7fd6706 | 227 | .store = part_attr_store, |
1da177e4 LT |
228 | }; |
229 | ||
a7fd6706 KS |
230 | static ssize_t part_uevent_store(struct hd_struct * p, |
231 | const char *page, size_t count) | |
232 | { | |
312c004d | 233 | kobject_uevent(&p->kobj, KOBJ_ADD); |
a7fd6706 KS |
234 | return count; |
235 | } | |
1da177e4 LT |
236 | static ssize_t part_dev_read(struct hd_struct * p, char *page) |
237 | { | |
238 | struct gendisk *disk = container_of(p->kobj.parent,struct gendisk,kobj); | |
239 | dev_t dev = MKDEV(disk->major, disk->first_minor + p->partno); | |
240 | return print_dev_t(page, dev); | |
241 | } | |
242 | static ssize_t part_start_read(struct hd_struct * p, char *page) | |
243 | { | |
244 | return sprintf(page, "%llu\n",(unsigned long long)p->start_sect); | |
245 | } | |
246 | static ssize_t part_size_read(struct hd_struct * p, char *page) | |
247 | { | |
248 | return sprintf(page, "%llu\n",(unsigned long long)p->nr_sects); | |
249 | } | |
250 | static ssize_t part_stat_read(struct hd_struct * p, char *page) | |
251 | { | |
252 | return sprintf(page, "%8u %8llu %8u %8llu\n", | |
a362357b JA |
253 | p->ios[0], (unsigned long long)p->sectors[0], |
254 | p->ios[1], (unsigned long long)p->sectors[1]); | |
1da177e4 | 255 | } |
a7fd6706 KS |
256 | static struct part_attribute part_attr_uevent = { |
257 | .attr = {.name = "uevent", .mode = S_IWUSR }, | |
258 | .store = part_uevent_store | |
259 | }; | |
1da177e4 LT |
260 | static struct part_attribute part_attr_dev = { |
261 | .attr = {.name = "dev", .mode = S_IRUGO }, | |
262 | .show = part_dev_read | |
263 | }; | |
264 | static struct part_attribute part_attr_start = { | |
265 | .attr = {.name = "start", .mode = S_IRUGO }, | |
266 | .show = part_start_read | |
267 | }; | |
268 | static struct part_attribute part_attr_size = { | |
269 | .attr = {.name = "size", .mode = S_IRUGO }, | |
270 | .show = part_size_read | |
271 | }; | |
272 | static struct part_attribute part_attr_stat = { | |
273 | .attr = {.name = "stat", .mode = S_IRUGO }, | |
274 | .show = part_stat_read | |
275 | }; | |
276 | ||
277 | static struct attribute * default_attrs[] = { | |
a7fd6706 | 278 | &part_attr_uevent.attr, |
1da177e4 LT |
279 | &part_attr_dev.attr, |
280 | &part_attr_start.attr, | |
281 | &part_attr_size.attr, | |
282 | &part_attr_stat.attr, | |
283 | NULL, | |
284 | }; | |
285 | ||
286 | extern struct subsystem block_subsys; | |
287 | ||
288 | static void part_release(struct kobject *kobj) | |
289 | { | |
290 | struct hd_struct * p = container_of(kobj,struct hd_struct,kobj); | |
291 | kfree(p); | |
292 | } | |
293 | ||
294 | struct kobj_type ktype_part = { | |
295 | .release = part_release, | |
296 | .default_attrs = default_attrs, | |
297 | .sysfs_ops = &part_sysfs_ops, | |
298 | }; | |
299 | ||
6a4d44c1 JN |
300 | static inline void partition_sysfs_add_subdir(struct hd_struct *p) |
301 | { | |
302 | struct kobject *k; | |
303 | ||
304 | k = kobject_get(&p->kobj); | |
305 | p->holder_dir = kobject_add_dir(k, "holders"); | |
306 | kobject_put(k); | |
307 | } | |
308 | ||
309 | static inline void disk_sysfs_add_subdirs(struct gendisk *disk) | |
310 | { | |
311 | struct kobject *k; | |
312 | ||
313 | k = kobject_get(&disk->kobj); | |
314 | disk->holder_dir = kobject_add_dir(k, "holders"); | |
315 | disk->slave_dir = kobject_add_dir(k, "slaves"); | |
316 | kobject_put(k); | |
317 | } | |
6a4d44c1 | 318 | |
1da177e4 LT |
319 | void delete_partition(struct gendisk *disk, int part) |
320 | { | |
321 | struct hd_struct *p = disk->part[part-1]; | |
322 | if (!p) | |
323 | return; | |
324 | if (!p->nr_sects) | |
325 | return; | |
326 | disk->part[part-1] = NULL; | |
327 | p->start_sect = 0; | |
328 | p->nr_sects = 0; | |
a362357b JA |
329 | p->ios[0] = p->ios[1] = 0; |
330 | p->sectors[0] = p->sectors[1] = 0; | |
1da177e4 | 331 | devfs_remove("%s/part%d", disk->devfs_name, part); |
6a4d44c1 JN |
332 | if (p->holder_dir) |
333 | kobject_unregister(p->holder_dir); | |
d4d7e5df KS |
334 | kobject_uevent(&p->kobj, KOBJ_REMOVE); |
335 | kobject_del(&p->kobj); | |
336 | kobject_put(&p->kobj); | |
1da177e4 LT |
337 | } |
338 | ||
339 | void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len) | |
340 | { | |
341 | struct hd_struct *p; | |
342 | ||
343 | p = kmalloc(sizeof(*p), GFP_KERNEL); | |
344 | if (!p) | |
345 | return; | |
346 | ||
347 | memset(p, 0, sizeof(*p)); | |
348 | p->start_sect = start; | |
349 | p->nr_sects = len; | |
350 | p->partno = part; | |
351 | ||
352 | devfs_mk_bdev(MKDEV(disk->major, disk->first_minor + part), | |
353 | S_IFBLK|S_IRUSR|S_IWUSR, | |
354 | "%s/part%d", disk->devfs_name, part); | |
355 | ||
356 | if (isdigit(disk->kobj.name[strlen(disk->kobj.name)-1])) | |
357 | snprintf(p->kobj.name,KOBJ_NAME_LEN,"%sp%d",disk->kobj.name,part); | |
358 | else | |
359 | snprintf(p->kobj.name,KOBJ_NAME_LEN,"%s%d",disk->kobj.name,part); | |
360 | p->kobj.parent = &disk->kobj; | |
361 | p->kobj.ktype = &ktype_part; | |
d4d7e5df KS |
362 | kobject_init(&p->kobj); |
363 | kobject_add(&p->kobj); | |
364 | if (!disk->part_uevent_suppress) | |
365 | kobject_uevent(&p->kobj, KOBJ_ADD); | |
6a4d44c1 | 366 | partition_sysfs_add_subdir(p); |
1da177e4 LT |
367 | disk->part[part-1] = p; |
368 | } | |
369 | ||
8218ef80 GKH |
370 | static char *make_block_name(struct gendisk *disk) |
371 | { | |
372 | char *name; | |
373 | static char *block_str = "block:"; | |
374 | int size; | |
2436f039 | 375 | char *s; |
8218ef80 GKH |
376 | |
377 | size = strlen(block_str) + strlen(disk->disk_name) + 1; | |
378 | name = kmalloc(size, GFP_KERNEL); | |
379 | if (!name) | |
380 | return NULL; | |
381 | strcpy(name, block_str); | |
382 | strcat(name, disk->disk_name); | |
2436f039 SR |
383 | /* ewww... some of these buggers have / in name... */ |
384 | s = strchr(name, '/'); | |
385 | if (s) | |
386 | *s = '!'; | |
8218ef80 GKH |
387 | return name; |
388 | } | |
389 | ||
1da177e4 LT |
390 | static void disk_sysfs_symlinks(struct gendisk *disk) |
391 | { | |
392 | struct device *target = get_device(disk->driverfs_dev); | |
393 | if (target) { | |
8218ef80 | 394 | char *disk_name = make_block_name(disk); |
1da177e4 | 395 | sysfs_create_link(&disk->kobj,&target->kobj,"device"); |
8218ef80 GKH |
396 | if (disk_name) { |
397 | sysfs_create_link(&target->kobj,&disk->kobj,disk_name); | |
398 | kfree(disk_name); | |
399 | } | |
1da177e4 LT |
400 | } |
401 | } | |
402 | ||
403 | /* Not exported, helper to add_disk(). */ | |
404 | void register_disk(struct gendisk *disk) | |
405 | { | |
406 | struct block_device *bdev; | |
407 | char *s; | |
d4d7e5df KS |
408 | int i; |
409 | struct hd_struct *p; | |
1da177e4 LT |
410 | int err; |
411 | ||
412 | strlcpy(disk->kobj.name,disk->disk_name,KOBJ_NAME_LEN); | |
413 | /* ewww... some of these buggers have / in name... */ | |
414 | s = strchr(disk->kobj.name, '/'); | |
415 | if (s) | |
416 | *s = '!'; | |
417 | if ((err = kobject_add(&disk->kobj))) | |
418 | return; | |
419 | disk_sysfs_symlinks(disk); | |
6a4d44c1 | 420 | disk_sysfs_add_subdirs(disk); |
1da177e4 LT |
421 | |
422 | /* No minors to use for partitions */ | |
423 | if (disk->minors == 1) { | |
424 | if (disk->devfs_name[0] != '\0') | |
425 | devfs_add_disk(disk); | |
d4d7e5df | 426 | goto exit; |
1da177e4 LT |
427 | } |
428 | ||
429 | /* always add handle for the whole disk */ | |
430 | devfs_add_partitioned(disk); | |
431 | ||
432 | /* No such device (e.g., media were just removed) */ | |
433 | if (!get_capacity(disk)) | |
d4d7e5df | 434 | goto exit; |
1da177e4 LT |
435 | |
436 | bdev = bdget_disk(disk, 0); | |
437 | if (!bdev) | |
d4d7e5df | 438 | goto exit; |
1da177e4 | 439 | |
d4d7e5df | 440 | /* scan partition table, but suppress uevents */ |
1da177e4 | 441 | bdev->bd_invalidated = 1; |
d4d7e5df KS |
442 | disk->part_uevent_suppress = 1; |
443 | err = blkdev_get(bdev, FMODE_READ, 0); | |
444 | disk->part_uevent_suppress = 0; | |
445 | if (err < 0) | |
446 | goto exit; | |
1da177e4 | 447 | blkdev_put(bdev); |
d4d7e5df KS |
448 | |
449 | exit: | |
450 | /* announce disk after possible partitions are already created */ | |
451 | kobject_uevent(&disk->kobj, KOBJ_ADD); | |
452 | ||
453 | /* announce possible partitions */ | |
454 | for (i = 1; i < disk->minors; i++) { | |
455 | p = disk->part[i-1]; | |
456 | if (!p || !p->nr_sects) | |
457 | continue; | |
458 | kobject_uevent(&p->kobj, KOBJ_ADD); | |
459 | } | |
1da177e4 LT |
460 | } |
461 | ||
462 | int rescan_partitions(struct gendisk *disk, struct block_device *bdev) | |
463 | { | |
464 | struct parsed_partitions *state; | |
465 | int p, res; | |
466 | ||
467 | if (bdev->bd_part_count) | |
468 | return -EBUSY; | |
469 | res = invalidate_partition(disk, 0); | |
470 | if (res) | |
471 | return res; | |
472 | bdev->bd_invalidated = 0; | |
473 | for (p = 1; p < disk->minors; p++) | |
474 | delete_partition(disk, p); | |
475 | if (disk->fops->revalidate_disk) | |
476 | disk->fops->revalidate_disk(disk); | |
477 | if (!get_capacity(disk) || !(state = check_partition(disk, bdev))) | |
478 | return 0; | |
479 | for (p = 1; p < state->limit; p++) { | |
480 | sector_t size = state->parts[p].size; | |
481 | sector_t from = state->parts[p].from; | |
482 | if (!size) | |
483 | continue; | |
484 | add_partition(disk, p, from, size); | |
485 | #ifdef CONFIG_BLK_DEV_MD | |
486 | if (state->parts[p].flags) | |
487 | md_autodetect_dev(bdev->bd_dev+p); | |
488 | #endif | |
489 | } | |
490 | kfree(state); | |
491 | return 0; | |
492 | } | |
493 | ||
494 | unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p) | |
495 | { | |
496 | struct address_space *mapping = bdev->bd_inode->i_mapping; | |
497 | struct page *page; | |
498 | ||
499 | page = read_cache_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)), | |
500 | (filler_t *)mapping->a_ops->readpage, NULL); | |
501 | if (!IS_ERR(page)) { | |
502 | wait_on_page_locked(page); | |
503 | if (!PageUptodate(page)) | |
504 | goto fail; | |
505 | if (PageError(page)) | |
506 | goto fail; | |
507 | p->v = page; | |
508 | return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9); | |
509 | fail: | |
510 | page_cache_release(page); | |
511 | } | |
512 | p->v = NULL; | |
513 | return NULL; | |
514 | } | |
515 | ||
516 | EXPORT_SYMBOL(read_dev_sector); | |
517 | ||
518 | void del_gendisk(struct gendisk *disk) | |
519 | { | |
520 | int p; | |
521 | ||
522 | /* invalidate stuff */ | |
523 | for (p = disk->minors - 1; p > 0; p--) { | |
524 | invalidate_partition(disk, p); | |
525 | delete_partition(disk, p); | |
526 | } | |
527 | invalidate_partition(disk, 0); | |
528 | disk->capacity = 0; | |
529 | disk->flags &= ~GENHD_FL_UP; | |
530 | unlink_gendisk(disk); | |
531 | disk_stat_set_all(disk, 0); | |
20e5c81f | 532 | disk->stamp = 0; |
1da177e4 LT |
533 | |
534 | devfs_remove_disk(disk); | |
535 | ||
6a4d44c1 JN |
536 | if (disk->holder_dir) |
537 | kobject_unregister(disk->holder_dir); | |
538 | if (disk->slave_dir) | |
539 | kobject_unregister(disk->slave_dir); | |
1da177e4 | 540 | if (disk->driverfs_dev) { |
8218ef80 | 541 | char *disk_name = make_block_name(disk); |
1da177e4 | 542 | sysfs_remove_link(&disk->kobj, "device"); |
8218ef80 GKH |
543 | if (disk_name) { |
544 | sysfs_remove_link(&disk->driverfs_dev->kobj, disk_name); | |
545 | kfree(disk_name); | |
546 | } | |
1da177e4 LT |
547 | put_device(disk->driverfs_dev); |
548 | } | |
312c004d | 549 | kobject_uevent(&disk->kobj, KOBJ_REMOVE); |
1da177e4 LT |
550 | kobject_del(&disk->kobj); |
551 | } |