block: use struct parsed_partitions *state universally in partition check code
[pandora-kernel.git] / fs / partitions / check.c
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/slab.h>
20 #include <linux/kmod.h>
21 #include <linux/ctype.h>
22 #include <linux/genhd.h>
23 #include <linux/blktrace_api.h>
24
25 #include "check.h"
26
27 #include "acorn.h"
28 #include "amiga.h"
29 #include "atari.h"
30 #include "ldm.h"
31 #include "mac.h"
32 #include "msdos.h"
33 #include "osf.h"
34 #include "sgi.h"
35 #include "sun.h"
36 #include "ibm.h"
37 #include "ultrix.h"
38 #include "efi.h"
39 #include "karma.h"
40 #include "sysv68.h"
41
42 #ifdef CONFIG_BLK_DEV_MD
43 extern void md_autodetect_dev(dev_t dev);
44 #endif
45
46 int warn_no_part = 1; /*This is ugly: should make genhd removable media aware*/
47
48 static int (*check_part[])(struct parsed_partitions *) = {
49         /*
50          * Probe partition formats with tables at disk address 0
51          * that also have an ADFS boot block at 0xdc0.
52          */
53 #ifdef CONFIG_ACORN_PARTITION_ICS
54         adfspart_check_ICS,
55 #endif
56 #ifdef CONFIG_ACORN_PARTITION_POWERTEC
57         adfspart_check_POWERTEC,
58 #endif
59 #ifdef CONFIG_ACORN_PARTITION_EESOX
60         adfspart_check_EESOX,
61 #endif
62
63         /*
64          * Now move on to formats that only have partition info at
65          * disk address 0xdc0.  Since these may also have stale
66          * PC/BIOS partition tables, they need to come before
67          * the msdos entry.
68          */
69 #ifdef CONFIG_ACORN_PARTITION_CUMANA
70         adfspart_check_CUMANA,
71 #endif
72 #ifdef CONFIG_ACORN_PARTITION_ADFS
73         adfspart_check_ADFS,
74 #endif
75
76 #ifdef CONFIG_EFI_PARTITION
77         efi_partition,          /* this must come before msdos */
78 #endif
79 #ifdef CONFIG_SGI_PARTITION
80         sgi_partition,
81 #endif
82 #ifdef CONFIG_LDM_PARTITION
83         ldm_partition,          /* this must come before msdos */
84 #endif
85 #ifdef CONFIG_MSDOS_PARTITION
86         msdos_partition,
87 #endif
88 #ifdef CONFIG_OSF_PARTITION
89         osf_partition,
90 #endif
91 #ifdef CONFIG_SUN_PARTITION
92         sun_partition,
93 #endif
94 #ifdef CONFIG_AMIGA_PARTITION
95         amiga_partition,
96 #endif
97 #ifdef CONFIG_ATARI_PARTITION
98         atari_partition,
99 #endif
100 #ifdef CONFIG_MAC_PARTITION
101         mac_partition,
102 #endif
103 #ifdef CONFIG_ULTRIX_PARTITION
104         ultrix_partition,
105 #endif
106 #ifdef CONFIG_IBM_PARTITION
107         ibm_partition,
108 #endif
109 #ifdef CONFIG_KARMA_PARTITION
110         karma_partition,
111 #endif
112 #ifdef CONFIG_SYSV68_PARTITION
113         sysv68_partition,
114 #endif
115         NULL
116 };
117  
118 /*
119  * disk_name() is used by partition check code and the genhd driver.
120  * It formats the devicename of the indicated disk into
121  * the supplied buffer (of size at least 32), and returns
122  * a pointer to that same buffer (for convenience).
123  */
124
125 char *disk_name(struct gendisk *hd, int partno, char *buf)
126 {
127         if (!partno)
128                 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
129         else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
130                 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
131         else
132                 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
133
134         return buf;
135 }
136
137 const char *bdevname(struct block_device *bdev, char *buf)
138 {
139         return disk_name(bdev->bd_disk, bdev->bd_part->partno, buf);
140 }
141
142 EXPORT_SYMBOL(bdevname);
143
144 /*
145  * There's very little reason to use this, you should really
146  * have a struct block_device just about everywhere and use
147  * bdevname() instead.
148  */
149 const char *__bdevname(dev_t dev, char *buffer)
150 {
151         scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
152                                 MAJOR(dev), MINOR(dev));
153         return buffer;
154 }
155
156 EXPORT_SYMBOL(__bdevname);
157
158 static struct parsed_partitions *
159 check_partition(struct gendisk *hd, struct block_device *bdev)
160 {
161         struct parsed_partitions *state;
162         int i, res, err;
163
164         state = kmalloc(sizeof(struct parsed_partitions), GFP_KERNEL);
165         if (!state)
166                 return NULL;
167
168         state->bdev = bdev;
169         disk_name(hd, 0, state->name);
170         printk(KERN_INFO " %s:", state->name);
171         if (isdigit(state->name[strlen(state->name)-1]))
172                 sprintf(state->name, "p");
173
174         state->limit = disk_max_parts(hd);
175         i = res = err = 0;
176         while (!res && check_part[i]) {
177                 memset(&state->parts, 0, sizeof(state->parts));
178                 res = check_part[i++](state);
179                 if (res < 0) {
180                         /* We have hit an I/O error which we don't report now.
181                         * But record it, and let the others do their job.
182                         */
183                         err = res;
184                         res = 0;
185                 }
186
187         }
188         if (res > 0)
189                 return state;
190         if (err)
191         /* The partition is unrecognized. So report I/O errors if there were any */
192                 res = err;
193         if (!res)
194                 printk(" unknown partition table\n");
195         else if (warn_no_part)
196                 printk(" unable to read partition table\n");
197         kfree(state);
198         return ERR_PTR(res);
199 }
200
201 static ssize_t part_partition_show(struct device *dev,
202                                    struct device_attribute *attr, char *buf)
203 {
204         struct hd_struct *p = dev_to_part(dev);
205
206         return sprintf(buf, "%d\n", p->partno);
207 }
208
209 static ssize_t part_start_show(struct device *dev,
210                                struct device_attribute *attr, char *buf)
211 {
212         struct hd_struct *p = dev_to_part(dev);
213
214         return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect);
215 }
216
217 ssize_t part_size_show(struct device *dev,
218                        struct device_attribute *attr, char *buf)
219 {
220         struct hd_struct *p = dev_to_part(dev);
221         return sprintf(buf, "%llu\n",(unsigned long long)p->nr_sects);
222 }
223
224 ssize_t part_alignment_offset_show(struct device *dev,
225                                    struct device_attribute *attr, char *buf)
226 {
227         struct hd_struct *p = dev_to_part(dev);
228         return sprintf(buf, "%llu\n", (unsigned long long)p->alignment_offset);
229 }
230
231 ssize_t part_discard_alignment_show(struct device *dev,
232                                    struct device_attribute *attr, char *buf)
233 {
234         struct hd_struct *p = dev_to_part(dev);
235         return sprintf(buf, "%u\n", p->discard_alignment);
236 }
237
238 ssize_t part_stat_show(struct device *dev,
239                        struct device_attribute *attr, char *buf)
240 {
241         struct hd_struct *p = dev_to_part(dev);
242         int cpu;
243
244         cpu = part_stat_lock();
245         part_round_stats(cpu, p);
246         part_stat_unlock();
247         return sprintf(buf,
248                 "%8lu %8lu %8llu %8u "
249                 "%8lu %8lu %8llu %8u "
250                 "%8u %8u %8u"
251                 "\n",
252                 part_stat_read(p, ios[READ]),
253                 part_stat_read(p, merges[READ]),
254                 (unsigned long long)part_stat_read(p, sectors[READ]),
255                 jiffies_to_msecs(part_stat_read(p, ticks[READ])),
256                 part_stat_read(p, ios[WRITE]),
257                 part_stat_read(p, merges[WRITE]),
258                 (unsigned long long)part_stat_read(p, sectors[WRITE]),
259                 jiffies_to_msecs(part_stat_read(p, ticks[WRITE])),
260                 part_in_flight(p),
261                 jiffies_to_msecs(part_stat_read(p, io_ticks)),
262                 jiffies_to_msecs(part_stat_read(p, time_in_queue)));
263 }
264
265 ssize_t part_inflight_show(struct device *dev,
266                         struct device_attribute *attr, char *buf)
267 {
268         struct hd_struct *p = dev_to_part(dev);
269
270         return sprintf(buf, "%8u %8u\n", p->in_flight[0], p->in_flight[1]);
271 }
272
273 #ifdef CONFIG_FAIL_MAKE_REQUEST
274 ssize_t part_fail_show(struct device *dev,
275                        struct device_attribute *attr, char *buf)
276 {
277         struct hd_struct *p = dev_to_part(dev);
278
279         return sprintf(buf, "%d\n", p->make_it_fail);
280 }
281
282 ssize_t part_fail_store(struct device *dev,
283                         struct device_attribute *attr,
284                         const char *buf, size_t count)
285 {
286         struct hd_struct *p = dev_to_part(dev);
287         int i;
288
289         if (count > 0 && sscanf(buf, "%d", &i) > 0)
290                 p->make_it_fail = (i == 0) ? 0 : 1;
291
292         return count;
293 }
294 #endif
295
296 static DEVICE_ATTR(partition, S_IRUGO, part_partition_show, NULL);
297 static DEVICE_ATTR(start, S_IRUGO, part_start_show, NULL);
298 static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
299 static DEVICE_ATTR(alignment_offset, S_IRUGO, part_alignment_offset_show, NULL);
300 static DEVICE_ATTR(discard_alignment, S_IRUGO, part_discard_alignment_show,
301                    NULL);
302 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
303 static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
304 #ifdef CONFIG_FAIL_MAKE_REQUEST
305 static struct device_attribute dev_attr_fail =
306         __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
307 #endif
308
309 static struct attribute *part_attrs[] = {
310         &dev_attr_partition.attr,
311         &dev_attr_start.attr,
312         &dev_attr_size.attr,
313         &dev_attr_alignment_offset.attr,
314         &dev_attr_discard_alignment.attr,
315         &dev_attr_stat.attr,
316         &dev_attr_inflight.attr,
317 #ifdef CONFIG_FAIL_MAKE_REQUEST
318         &dev_attr_fail.attr,
319 #endif
320         NULL
321 };
322
323 static struct attribute_group part_attr_group = {
324         .attrs = part_attrs,
325 };
326
327 static const struct attribute_group *part_attr_groups[] = {
328         &part_attr_group,
329 #ifdef CONFIG_BLK_DEV_IO_TRACE
330         &blk_trace_attr_group,
331 #endif
332         NULL
333 };
334
335 static void part_release(struct device *dev)
336 {
337         struct hd_struct *p = dev_to_part(dev);
338         free_part_stats(p);
339         kfree(p);
340 }
341
342 struct device_type part_type = {
343         .name           = "partition",
344         .groups         = part_attr_groups,
345         .release        = part_release,
346 };
347
348 static void delete_partition_rcu_cb(struct rcu_head *head)
349 {
350         struct hd_struct *part = container_of(head, struct hd_struct, rcu_head);
351
352         part->start_sect = 0;
353         part->nr_sects = 0;
354         part_stat_set_all(part, 0);
355         put_device(part_to_dev(part));
356 }
357
358 void delete_partition(struct gendisk *disk, int partno)
359 {
360         struct disk_part_tbl *ptbl = disk->part_tbl;
361         struct hd_struct *part;
362
363         if (partno >= ptbl->len)
364                 return;
365
366         part = ptbl->part[partno];
367         if (!part)
368                 return;
369
370         blk_free_devt(part_devt(part));
371         rcu_assign_pointer(ptbl->part[partno], NULL);
372         rcu_assign_pointer(ptbl->last_lookup, NULL);
373         kobject_put(part->holder_dir);
374         device_del(part_to_dev(part));
375
376         call_rcu(&part->rcu_head, delete_partition_rcu_cb);
377 }
378
379 static ssize_t whole_disk_show(struct device *dev,
380                                struct device_attribute *attr, char *buf)
381 {
382         return 0;
383 }
384 static DEVICE_ATTR(whole_disk, S_IRUSR | S_IRGRP | S_IROTH,
385                    whole_disk_show, NULL);
386
387 struct hd_struct *add_partition(struct gendisk *disk, int partno,
388                                 sector_t start, sector_t len, int flags)
389 {
390         struct hd_struct *p;
391         dev_t devt = MKDEV(0, 0);
392         struct device *ddev = disk_to_dev(disk);
393         struct device *pdev;
394         struct disk_part_tbl *ptbl;
395         const char *dname;
396         int err;
397
398         err = disk_expand_part_tbl(disk, partno);
399         if (err)
400                 return ERR_PTR(err);
401         ptbl = disk->part_tbl;
402
403         if (ptbl->part[partno])
404                 return ERR_PTR(-EBUSY);
405
406         p = kzalloc(sizeof(*p), GFP_KERNEL);
407         if (!p)
408                 return ERR_PTR(-EBUSY);
409
410         if (!init_part_stats(p)) {
411                 err = -ENOMEM;
412                 goto out_free;
413         }
414         pdev = part_to_dev(p);
415
416         p->start_sect = start;
417         p->alignment_offset =
418                 queue_limit_alignment_offset(&disk->queue->limits, start);
419         p->discard_alignment =
420                 queue_limit_discard_alignment(&disk->queue->limits, start);
421         p->nr_sects = len;
422         p->partno = partno;
423         p->policy = get_disk_ro(disk);
424
425         dname = dev_name(ddev);
426         if (isdigit(dname[strlen(dname) - 1]))
427                 dev_set_name(pdev, "%sp%d", dname, partno);
428         else
429                 dev_set_name(pdev, "%s%d", dname, partno);
430
431         device_initialize(pdev);
432         pdev->class = &block_class;
433         pdev->type = &part_type;
434         pdev->parent = ddev;
435
436         err = blk_alloc_devt(p, &devt);
437         if (err)
438                 goto out_free_stats;
439         pdev->devt = devt;
440
441         /* delay uevent until 'holders' subdir is created */
442         dev_set_uevent_suppress(pdev, 1);
443         err = device_add(pdev);
444         if (err)
445                 goto out_put;
446
447         err = -ENOMEM;
448         p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
449         if (!p->holder_dir)
450                 goto out_del;
451
452         dev_set_uevent_suppress(pdev, 0);
453         if (flags & ADDPART_FLAG_WHOLEDISK) {
454                 err = device_create_file(pdev, &dev_attr_whole_disk);
455                 if (err)
456                         goto out_del;
457         }
458
459         /* everything is up and running, commence */
460         INIT_RCU_HEAD(&p->rcu_head);
461         rcu_assign_pointer(ptbl->part[partno], p);
462
463         /* suppress uevent if the disk supresses it */
464         if (!dev_get_uevent_suppress(ddev))
465                 kobject_uevent(&pdev->kobj, KOBJ_ADD);
466
467         return p;
468
469 out_free_stats:
470         free_part_stats(p);
471 out_free:
472         kfree(p);
473         return ERR_PTR(err);
474 out_del:
475         kobject_put(p->holder_dir);
476         device_del(pdev);
477 out_put:
478         put_device(pdev);
479         blk_free_devt(devt);
480         return ERR_PTR(err);
481 }
482
483 /* Not exported, helper to add_disk(). */
484 void register_disk(struct gendisk *disk)
485 {
486         struct device *ddev = disk_to_dev(disk);
487         struct block_device *bdev;
488         struct disk_part_iter piter;
489         struct hd_struct *part;
490         int err;
491
492         ddev->parent = disk->driverfs_dev;
493
494         dev_set_name(ddev, disk->disk_name);
495
496         /* delay uevents, until we scanned partition table */
497         dev_set_uevent_suppress(ddev, 1);
498
499         if (device_add(ddev))
500                 return;
501 #ifndef CONFIG_SYSFS_DEPRECATED
502         err = sysfs_create_link(block_depr, &ddev->kobj,
503                                 kobject_name(&ddev->kobj));
504         if (err) {
505                 device_del(ddev);
506                 return;
507         }
508 #endif
509         disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
510         disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
511
512         /* No minors to use for partitions */
513         if (!disk_partitionable(disk))
514                 goto exit;
515
516         /* No such device (e.g., media were just removed) */
517         if (!get_capacity(disk))
518                 goto exit;
519
520         bdev = bdget_disk(disk, 0);
521         if (!bdev)
522                 goto exit;
523
524         bdev->bd_invalidated = 1;
525         err = blkdev_get(bdev, FMODE_READ);
526         if (err < 0)
527                 goto exit;
528         blkdev_put(bdev, FMODE_READ);
529
530 exit:
531         /* announce disk after possible partitions are created */
532         dev_set_uevent_suppress(ddev, 0);
533         kobject_uevent(&ddev->kobj, KOBJ_ADD);
534
535         /* announce possible partitions */
536         disk_part_iter_init(&piter, disk, 0);
537         while ((part = disk_part_iter_next(&piter)))
538                 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
539         disk_part_iter_exit(&piter);
540 }
541
542 int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
543 {
544         struct disk_part_iter piter;
545         struct hd_struct *part;
546         struct parsed_partitions *state;
547         int p, highest, res;
548 rescan:
549         if (bdev->bd_part_count)
550                 return -EBUSY;
551         res = invalidate_partition(disk, 0);
552         if (res)
553                 return res;
554
555         disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
556         while ((part = disk_part_iter_next(&piter)))
557                 delete_partition(disk, part->partno);
558         disk_part_iter_exit(&piter);
559
560         if (disk->fops->revalidate_disk)
561                 disk->fops->revalidate_disk(disk);
562         check_disk_size_change(disk, bdev);
563         bdev->bd_invalidated = 0;
564         if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
565                 return 0;
566         if (IS_ERR(state))      /* I/O error reading the partition table */
567                 return -EIO;
568
569         /* tell userspace that the media / partition table may have changed */
570         kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
571
572         /* Detect the highest partition number and preallocate
573          * disk->part_tbl.  This is an optimization and not strictly
574          * necessary.
575          */
576         for (p = 1, highest = 0; p < state->limit; p++)
577                 if (state->parts[p].size)
578                         highest = p;
579
580         disk_expand_part_tbl(disk, highest);
581
582         /* add partitions */
583         for (p = 1; p < state->limit; p++) {
584                 sector_t size, from;
585
586                 size = state->parts[p].size;
587                 if (!size)
588                         continue;
589
590                 from = state->parts[p].from;
591                 if (from >= get_capacity(disk)) {
592                         printk(KERN_WARNING
593                                "%s: p%d ignored, start %llu is behind the end of the disk\n",
594                                disk->disk_name, p, (unsigned long long) from);
595                         continue;
596                 }
597
598                 if (from + size > get_capacity(disk)) {
599                         const struct block_device_operations *bdops = disk->fops;
600
601                         printk(KERN_WARNING
602                                "%s: p%d size %llu exceeds device capacity, ",
603                                disk->disk_name, p, (unsigned long long) size);
604
605                         if (bdops->unlock_native_capacity &&
606                             (disk->flags & GENHD_FL_NATIVE_CAPACITY) == 0) {
607                                 printk(KERN_CONT "enabling native capacity\n");
608                                 bdops->unlock_native_capacity(disk);
609                                 disk->flags |= GENHD_FL_NATIVE_CAPACITY;
610                                 /* free state and restart */
611                                 kfree(state);
612                                 goto rescan;
613                         } else {
614                                 /*
615                                  * we can not ignore partitions of broken tables
616                                  * created by for example camera firmware, but
617                                  * we limit them to the end of the disk to avoid
618                                  * creating invalid block devices
619                                  */
620                                 printk(KERN_CONT "limited to end of disk\n");
621                                 size = get_capacity(disk) - from;
622                         }
623                 }
624                 part = add_partition(disk, p, from, size,
625                                      state->parts[p].flags);
626                 if (IS_ERR(part)) {
627                         printk(KERN_ERR " %s: p%d could not be added: %ld\n",
628                                disk->disk_name, p, -PTR_ERR(part));
629                         continue;
630                 }
631 #ifdef CONFIG_BLK_DEV_MD
632                 if (state->parts[p].flags & ADDPART_FLAG_RAID)
633                         md_autodetect_dev(part_to_dev(part)->devt);
634 #endif
635         }
636         kfree(state);
637         return 0;
638 }
639
640 unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
641 {
642         struct address_space *mapping = bdev->bd_inode->i_mapping;
643         struct page *page;
644
645         page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
646                                  NULL);
647         if (!IS_ERR(page)) {
648                 if (PageError(page))
649                         goto fail;
650                 p->v = page;
651                 return (unsigned char *)page_address(page) +  ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
652 fail:
653                 page_cache_release(page);
654         }
655         p->v = NULL;
656         return NULL;
657 }
658
659 EXPORT_SYMBOL(read_dev_sector);
660
661 void del_gendisk(struct gendisk *disk)
662 {
663         struct disk_part_iter piter;
664         struct hd_struct *part;
665
666         /* invalidate stuff */
667         disk_part_iter_init(&piter, disk,
668                              DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
669         while ((part = disk_part_iter_next(&piter))) {
670                 invalidate_partition(disk, part->partno);
671                 delete_partition(disk, part->partno);
672         }
673         disk_part_iter_exit(&piter);
674
675         invalidate_partition(disk, 0);
676         blk_free_devt(disk_to_dev(disk)->devt);
677         set_capacity(disk, 0);
678         disk->flags &= ~GENHD_FL_UP;
679         unlink_gendisk(disk);
680         part_stat_set_all(&disk->part0, 0);
681         disk->part0.stamp = 0;
682
683         kobject_put(disk->part0.holder_dir);
684         kobject_put(disk->slave_dir);
685         disk->driverfs_dev = NULL;
686 #ifndef CONFIG_SYSFS_DEPRECATED
687         sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
688 #endif
689         device_del(disk_to_dev(disk));
690 }