blob: 9a64045ff845976b5610609d2c37c86f5f9d1778 [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
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200198static ssize_t part_start_show(struct device *dev,
199 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200201 struct hd_struct *p = dev_to_part(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200202
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200203 return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200206static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200213static ssize_t part_stat_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200215{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200216 struct hd_struct *p = dev_to_part(dev);
217
218 return sprintf(buf, "%8u %8llu %8u %8llu\n",
Jens Axboea3623572005-11-01 09:26:16 +0100219 p->ios[0], (unsigned long long)p->sectors[0],
220 p->ios[1], (unsigned long long)p->sectors[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Akinobu Mitac17bb492006-12-08 02:39:46 -0800223#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200224static 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);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800228
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200229 return sprintf(buf, "%d\n", p->make_it_fail);
230}
231
232static ssize_t part_fail_store(struct device *dev,
233 struct device_attribute *attr,
Akinobu Mitac17bb492006-12-08 02:39:46 -0800234 const char *buf, size_t count)
235{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200236 struct hd_struct *p = dev_to_part(dev);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800237 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}
Akinobu Mitac17bb492006-12-08 02:39:46 -0800244#endif
245
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200246static DEVICE_ATTR(start, S_IRUGO, part_start_show, NULL);
247static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
248static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800249#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200250static struct device_attribute dev_attr_fail =
251 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800252#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200253
254static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262};
263
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200264static struct attribute_group part_attr_group = {
265 .attrs = part_attrs,
266};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200268static struct attribute_group *part_attr_groups[] = {
269 &part_attr_group,
270 NULL
271};
272
273static void part_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200275 struct hd_struct *p = dev_to_part(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 kfree(p);
277}
278
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200279struct device_type part_type = {
280 .name = "partition",
281 .groups = part_attr_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 .release = part_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283};
284
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800285static inline void partition_sysfs_add_subdir(struct hd_struct *p)
286{
287 struct kobject *k;
288
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200289 k = kobject_get(&p->dev.kobj);
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800290 p->holder_dir = kobject_create_and_add("holders", k);
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800291 kobject_put(k);
292}
293
294static inline void disk_sysfs_add_subdirs(struct gendisk *disk)
295{
296 struct kobject *k;
297
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200298 k = kobject_get(&disk->dev.kobj);
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800299 disk->holder_dir = kobject_create_and_add("holders", k);
300 disk->slave_dir = kobject_create_and_add("slaves", k);
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800301 kobject_put(k);
302}
Jun'ichi Nomura6a4d44c2006-03-27 01:17:55 -0800303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304void delete_partition(struct gendisk *disk, int part)
305{
306 struct hd_struct *p = disk->part[part-1];
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 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;
Jens Axboea3623572005-11-01 09:26:16 +0100315 p->ios[0] = p->ios[1] = 0;
316 p->sectors[0] = p->sectors[1] = 0;
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800317 kobject_put(p->holder_dir);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200318 device_del(&p->dev);
319 put_device(&p->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Greg Kroah-Hartmanf4a00a22008-02-06 23:33:39 -0800322static ssize_t whole_disk_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
324{
325 return 0;
326}
327static DEVICE_ATTR(whole_disk, S_IRUSR | S_IRGRP | S_IROTH,
328 whole_disk_show, NULL);
329
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -0800330void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct hd_struct *p;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200333 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700335 p = kzalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (!p)
337 return;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 p->start_sect = start;
340 p->nr_sects = len;
341 p->partno = part;
Peter Oberparleiter25e206b2006-07-10 04:44:00 -0700342 p->policy = disk->policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200344 if (isdigit(disk->dev.bus_id[strlen(disk->dev.bus_id)-1]))
345 snprintf(p->dev.bus_id, BUS_ID_SIZE,
346 "%sp%d", disk->dev.bus_id, part);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200348 snprintf(p->dev.bus_id, BUS_ID_SIZE,
349 "%s%d", disk->dev.bus_id, part);
350
351 device_initialize(&p->dev);
352 p->dev.devt = MKDEV(disk->major, disk->first_minor + part);
353 p->dev.class = &block_class;
354 p->dev.type = &part_type;
355 p->dev.parent = &disk->dev;
356 disk->part[part-1] = p;
357
358 /* delay uevent until 'holders' subdir is created */
359 p->dev.uevent_suppress = 1;
360 device_add(&p->dev);
361 partition_sysfs_add_subdir(p);
362 p->dev.uevent_suppress = 0;
Greg Kroah-Hartmanf4a00a22008-02-06 23:33:39 -0800363 if (flags & ADDPART_FLAG_WHOLEDISK)
364 err = device_create_file(&p->dev, &dev_attr_whole_disk);
Jeff Garzikeee44cc2006-10-17 00:10:23 -0700365
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200366 /* suppress uevent if the disk supresses it */
367 if (!disk->dev.uevent_suppress)
368 kobject_uevent(&p->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371/* Not exported, helper to add_disk(). */
372void register_disk(struct gendisk *disk)
373{
374 struct block_device *bdev;
375 char *s;
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100376 int i;
377 struct hd_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 int err;
379
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200380 disk->dev.parent = disk->driverfs_dev;
381 disk->dev.devt = MKDEV(disk->major, disk->first_minor);
382
383 strlcpy(disk->dev.bus_id, disk->disk_name, KOBJ_NAME_LEN);
384 /* ewww... some of these buggers have / in the name... */
385 s = strchr(disk->dev.bus_id, '/');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (s)
387 *s = '!';
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200388
389 /* delay uevents, until we scanned partition table */
390 disk->dev.uevent_suppress = 1;
391
392 if (device_add(&disk->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200394#ifndef CONFIG_SYSFS_DEPRECATED
395 err = sysfs_create_link(block_depr, &disk->dev.kobj,
396 kobject_name(&disk->dev.kobj));
Jeff Garzikeee44cc2006-10-17 00:10:23 -0700397 if (err) {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200398 device_del(&disk->dev);
Jeff Garzikeee44cc2006-10-17 00:10:23 -0700399 return;
400 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200401#endif
402 disk_sysfs_add_subdirs(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /* No minors to use for partitions */
Greg Kroah-Hartmana2964182005-06-20 21:15:16 -0700405 if (disk->minors == 1)
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100406 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* No such device (e.g., media were just removed) */
409 if (!get_capacity(disk))
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100410 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 bdev = bdget_disk(disk, 0);
413 if (!bdev)
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100414 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 bdev->bd_invalidated = 1;
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100417 err = blkdev_get(bdev, FMODE_READ, 0);
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100418 if (err < 0)
419 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 blkdev_put(bdev);
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100421
422exit:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200423 /* announce disk after possible partitions are created */
424 disk->dev.uevent_suppress = 0;
425 kobject_uevent(&disk->dev.kobj, KOBJ_ADD);
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100426
427 /* announce possible partitions */
428 for (i = 1; i < disk->minors; i++) {
429 p = disk->part[i-1];
430 if (!p || !p->nr_sects)
431 continue;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200432 kobject_uevent(&p->dev.kobj, KOBJ_ADD);
Kay Sieversd4d7e5d2006-03-24 20:45:35 +0100433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
437{
438 struct parsed_partitions *state;
439 int p, res;
440
441 if (bdev->bd_part_count)
442 return -EBUSY;
443 res = invalidate_partition(disk, 0);
444 if (res)
445 return res;
446 bdev->bd_invalidated = 0;
447 for (p = 1; p < disk->minors; p++)
448 delete_partition(disk, p);
449 if (disk->fops->revalidate_disk)
450 disk->fops->revalidate_disk(disk);
451 if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
452 return 0;
Suzuki Kp5127d002006-12-06 20:35:14 -0800453 if (IS_ERR(state)) /* I/O error reading the partition table */
suzukib74a2f02007-03-16 13:38:25 -0800454 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 for (p = 1; p < state->limit; p++) {
456 sector_t size = state->parts[p].size;
457 sector_t from = state->parts[p].from;
458 if (!size)
459 continue;
Mike Miller98bd34e2006-06-23 02:06:07 -0700460 if (from + size > get_capacity(disk)) {
461 printk(" %s: p%d exceeds device capacity\n",
462 disk->disk_name, p);
463 }
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -0800464 add_partition(disk, p, from, size, state->parts[p].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#ifdef CONFIG_BLK_DEV_MD
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -0800466 if (state->parts[p].flags & ADDPART_FLAG_RAID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 md_autodetect_dev(bdev->bd_dev+p);
468#endif
469 }
470 kfree(state);
471 return 0;
472}
473
474unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
475{
476 struct address_space *mapping = bdev->bd_inode->i_mapping;
477 struct page *page;
478
Pekka Enberg090d2b12006-06-23 02:05:08 -0700479 page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
480 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (!IS_ERR(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (PageError(page))
483 goto fail;
484 p->v = page;
485 return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
486fail:
487 page_cache_release(page);
488 }
489 p->v = NULL;
490 return NULL;
491}
492
493EXPORT_SYMBOL(read_dev_sector);
494
495void del_gendisk(struct gendisk *disk)
496{
497 int p;
498
499 /* invalidate stuff */
500 for (p = disk->minors - 1; p > 0; p--) {
501 invalidate_partition(disk, p);
502 delete_partition(disk, p);
503 }
504 invalidate_partition(disk, 0);
505 disk->capacity = 0;
506 disk->flags &= ~GENHD_FL_UP;
507 unlink_gendisk(disk);
508 disk_stat_set_all(disk, 0);
Chen, Kenneth W20e5c812005-10-13 21:48:42 +0200509 disk->stamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800511 kobject_put(disk->holder_dir);
512 kobject_put(disk->slave_dir);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200513 disk->driverfs_dev = NULL;
514#ifndef CONFIG_SYSFS_DEPRECATED
515 sysfs_remove_link(block_depr, disk->dev.bus_id);
516#endif
517 device_del(&disk->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}