i915: Fix GR register array size off-by-one bug
[sfrench/cifs-2.6.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/kmod.h>
20 #include <linux/ctype.h>
21
22 #include "check.h"
23
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"
36 #include "karma.h"
37 #include "sysv68.h"
38
39 #ifdef CONFIG_BLK_DEV_MD
40 extern void md_autodetect_dev(dev_t dev);
41 #endif
42
43 int warn_no_part = 1; /*This is ugly: should make genhd removable media aware*/
44
45 static 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
82 #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 #ifdef CONFIG_KARMA_PARTITION
107         karma_partition,
108 #endif
109 #ifdef CONFIG_SYSV68_PARTITION
110         sysv68_partition,
111 #endif
112         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
122 char *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
134 const 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
140 EXPORT_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  */
147 const 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
154 EXPORT_SYMBOL(__bdevname);
155
156 static struct parsed_partitions *
157 check_partition(struct gendisk *hd, struct block_device *bdev)
158 {
159         struct parsed_partitions *state;
160         int i, res, err;
161
162         state = kmalloc(sizeof(struct parsed_partitions), GFP_KERNEL);
163         if (!state)
164                 return NULL;
165
166         disk_name(hd, 0, state->name);
167         printk(KERN_INFO " %s:", state->name);
168         if (isdigit(state->name[strlen(state->name)-1]))
169                 sprintf(state->name, "p");
170
171         state->limit = hd->minors;
172         i = res = err = 0;
173         while (!res && check_part[i]) {
174                 memset(&state->parts, 0, sizeof(state->parts));
175                 res = check_part[i++](state, bdev);
176                 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
184         }
185         if (res > 0)
186                 return state;
187         if (err)
188         /* The partition is unrecognized. So report I/O errors if there were any */
189                 res = err;
190         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);
195         return ERR_PTR(res);
196 }
197
198 static ssize_t part_start_show(struct device *dev,
199                                struct device_attribute *attr, char *buf)
200 {
201         struct hd_struct *p = dev_to_part(dev);
202
203         return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect);
204 }
205
206 static ssize_t part_size_show(struct device *dev,
207                               struct device_attribute *attr, char *buf)
208 {
209         struct hd_struct *p = dev_to_part(dev);
210         return sprintf(buf, "%llu\n",(unsigned long long)p->nr_sects);
211 }
212
213 static ssize_t part_stat_show(struct device *dev,
214                               struct device_attribute *attr, char *buf)
215 {
216         struct hd_struct *p = dev_to_part(dev);
217
218         return sprintf(buf, "%8u %8llu %8u %8llu\n",
219                        p->ios[0], (unsigned long long)p->sectors[0],
220                        p->ios[1], (unsigned long long)p->sectors[1]);
221 }
222
223 #ifdef CONFIG_FAIL_MAKE_REQUEST
224 static ssize_t part_fail_show(struct device *dev,
225                               struct device_attribute *attr, char *buf)
226 {
227         struct hd_struct *p = dev_to_part(dev);
228
229         return sprintf(buf, "%d\n", p->make_it_fail);
230 }
231
232 static ssize_t part_fail_store(struct device *dev,
233                                struct device_attribute *attr,
234                                const char *buf, size_t count)
235 {
236         struct hd_struct *p = dev_to_part(dev);
237         int i;
238
239         if (count > 0 && sscanf(buf, "%d", &i) > 0)
240                 p->make_it_fail = (i == 0) ? 0 : 1;
241
242         return count;
243 }
244 #endif
245
246 static DEVICE_ATTR(start, S_IRUGO, part_start_show, NULL);
247 static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
248 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
249 #ifdef CONFIG_FAIL_MAKE_REQUEST
250 static struct device_attribute dev_attr_fail =
251         __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
252 #endif
253
254 static struct attribute *part_attrs[] = {
255         &dev_attr_start.attr,
256         &dev_attr_size.attr,
257         &dev_attr_stat.attr,
258 #ifdef CONFIG_FAIL_MAKE_REQUEST
259         &dev_attr_fail.attr,
260 #endif
261         NULL
262 };
263
264 static struct attribute_group part_attr_group = {
265         .attrs = part_attrs,
266 };
267
268 static struct attribute_group *part_attr_groups[] = {
269         &part_attr_group,
270         NULL
271 };
272
273 static void part_release(struct device *dev)
274 {
275         struct hd_struct *p = dev_to_part(dev);
276         kfree(p);
277 }
278
279 struct device_type part_type = {
280         .name           = "partition",
281         .groups         = part_attr_groups,
282         .release        = part_release,
283 };
284
285 static inline void partition_sysfs_add_subdir(struct hd_struct *p)
286 {
287         struct kobject *k;
288
289         k = kobject_get(&p->dev.kobj);
290         p->holder_dir = kobject_create_and_add("holders", k);
291         kobject_put(k);
292 }
293
294 static inline void disk_sysfs_add_subdirs(struct gendisk *disk)
295 {
296         struct kobject *k;
297
298         k = kobject_get(&disk->dev.kobj);
299         disk->holder_dir = kobject_create_and_add("holders", k);
300         disk->slave_dir = kobject_create_and_add("slaves", k);
301         kobject_put(k);
302 }
303
304 void delete_partition(struct gendisk *disk, int part)
305 {
306         struct hd_struct *p = disk->part[part-1];
307
308         if (!p)
309                 return;
310         if (!p->nr_sects)
311                 return;
312         disk->part[part-1] = NULL;
313         p->start_sect = 0;
314         p->nr_sects = 0;
315         p->ios[0] = p->ios[1] = 0;
316         p->sectors[0] = p->sectors[1] = 0;
317         kobject_put(p->holder_dir);
318         device_del(&p->dev);
319         put_device(&p->dev);
320 }
321
322 void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len, int flags)
323 {
324         struct hd_struct *p;
325         int err;
326
327         p = kzalloc(sizeof(*p), GFP_KERNEL);
328         if (!p)
329                 return;
330
331         p->start_sect = start;
332         p->nr_sects = len;
333         p->partno = part;
334         p->policy = disk->policy;
335
336         if (isdigit(disk->dev.bus_id[strlen(disk->dev.bus_id)-1]))
337                 snprintf(p->dev.bus_id, BUS_ID_SIZE,
338                 "%sp%d", disk->dev.bus_id, part);
339         else
340                 snprintf(p->dev.bus_id, BUS_ID_SIZE,
341                          "%s%d", disk->dev.bus_id, part);
342
343         device_initialize(&p->dev);
344         p->dev.devt = MKDEV(disk->major, disk->first_minor + part);
345         p->dev.class = &block_class;
346         p->dev.type = &part_type;
347         p->dev.parent = &disk->dev;
348         disk->part[part-1] = p;
349
350         /* delay uevent until 'holders' subdir is created */
351         p->dev.uevent_suppress = 1;
352         device_add(&p->dev);
353         partition_sysfs_add_subdir(p);
354         p->dev.uevent_suppress = 0;
355         if (flags & ADDPART_FLAG_WHOLEDISK) {
356                 static struct attribute addpartattr = {
357                         .name = "whole_disk",
358                         .mode = S_IRUSR | S_IRGRP | S_IROTH,
359                 };
360                 err = sysfs_create_file(&p->dev.kobj, &addpartattr);
361         }
362
363         /* suppress uevent if the disk supresses it */
364         if (!disk->dev.uevent_suppress)
365                 kobject_uevent(&p->dev.kobj, KOBJ_ADD);
366 }
367
368 /* Not exported, helper to add_disk(). */
369 void register_disk(struct gendisk *disk)
370 {
371         struct block_device *bdev;
372         char *s;
373         int i;
374         struct hd_struct *p;
375         int err;
376
377         disk->dev.parent = disk->driverfs_dev;
378         disk->dev.devt = MKDEV(disk->major, disk->first_minor);
379
380         strlcpy(disk->dev.bus_id, disk->disk_name, KOBJ_NAME_LEN);
381         /* ewww... some of these buggers have / in the name... */
382         s = strchr(disk->dev.bus_id, '/');
383         if (s)
384                 *s = '!';
385
386         /* delay uevents, until we scanned partition table */
387         disk->dev.uevent_suppress = 1;
388
389         if (device_add(&disk->dev))
390                 return;
391 #ifndef CONFIG_SYSFS_DEPRECATED
392         err = sysfs_create_link(block_depr, &disk->dev.kobj,
393                                 kobject_name(&disk->dev.kobj));
394         if (err) {
395                 device_del(&disk->dev);
396                 return;
397         }
398 #endif
399         disk_sysfs_add_subdirs(disk);
400
401         /* No minors to use for partitions */
402         if (disk->minors == 1)
403                 goto exit;
404
405         /* No such device (e.g., media were just removed) */
406         if (!get_capacity(disk))
407                 goto exit;
408
409         bdev = bdget_disk(disk, 0);
410         if (!bdev)
411                 goto exit;
412
413         bdev->bd_invalidated = 1;
414         err = blkdev_get(bdev, FMODE_READ, 0);
415         if (err < 0)
416                 goto exit;
417         blkdev_put(bdev);
418
419 exit:
420         /* announce disk after possible partitions are created */
421         disk->dev.uevent_suppress = 0;
422         kobject_uevent(&disk->dev.kobj, KOBJ_ADD);
423
424         /* announce possible partitions */
425         for (i = 1; i < disk->minors; i++) {
426                 p = disk->part[i-1];
427                 if (!p || !p->nr_sects)
428                         continue;
429                 kobject_uevent(&p->dev.kobj, KOBJ_ADD);
430         }
431 }
432
433 int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
434 {
435         struct parsed_partitions *state;
436         int p, res;
437
438         if (bdev->bd_part_count)
439                 return -EBUSY;
440         res = invalidate_partition(disk, 0);
441         if (res)
442                 return res;
443         bdev->bd_invalidated = 0;
444         for (p = 1; p < disk->minors; p++)
445                 delete_partition(disk, p);
446         if (disk->fops->revalidate_disk)
447                 disk->fops->revalidate_disk(disk);
448         if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
449                 return 0;
450         if (IS_ERR(state))      /* I/O error reading the partition table */
451                 return -EIO;
452         for (p = 1; p < state->limit; p++) {
453                 sector_t size = state->parts[p].size;
454                 sector_t from = state->parts[p].from;
455                 if (!size)
456                         continue;
457                 if (from + size > get_capacity(disk)) {
458                         printk(" %s: p%d exceeds device capacity\n",
459                                 disk->disk_name, p);
460                 }
461                 add_partition(disk, p, from, size, state->parts[p].flags);
462 #ifdef CONFIG_BLK_DEV_MD
463                 if (state->parts[p].flags & ADDPART_FLAG_RAID)
464                         md_autodetect_dev(bdev->bd_dev+p);
465 #endif
466         }
467         kfree(state);
468         return 0;
469 }
470
471 unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
472 {
473         struct address_space *mapping = bdev->bd_inode->i_mapping;
474         struct page *page;
475
476         page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
477                                  NULL);
478         if (!IS_ERR(page)) {
479                 if (PageError(page))
480                         goto fail;
481                 p->v = page;
482                 return (unsigned char *)page_address(page) +  ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
483 fail:
484                 page_cache_release(page);
485         }
486         p->v = NULL;
487         return NULL;
488 }
489
490 EXPORT_SYMBOL(read_dev_sector);
491
492 void del_gendisk(struct gendisk *disk)
493 {
494         int p;
495
496         /* invalidate stuff */
497         for (p = disk->minors - 1; p > 0; p--) {
498                 invalidate_partition(disk, p);
499                 delete_partition(disk, p);
500         }
501         invalidate_partition(disk, 0);
502         disk->capacity = 0;
503         disk->flags &= ~GENHD_FL_UP;
504         unlink_gendisk(disk);
505         disk_stat_set_all(disk, 0);
506         disk->stamp = 0;
507
508         kobject_put(disk->holder_dir);
509         kobject_put(disk->slave_dir);
510         disk->driverfs_dev = NULL;
511 #ifndef CONFIG_SYSFS_DEPRECATED
512         sysfs_remove_link(block_depr, disk->dev.bus_id);
513 #endif
514         device_del(&disk->dev);
515 }