1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 1991-1998 Linus Torvalds
4 * Re-organised Feb 1998 Russell King
5 * Copyright (C) 2020 Christoph Hellwig
6 */
7 #include <linux/fs.h>
8 #include <linux/major.h>
9 #include <linux/slab.h>
10 #include <linux/ctype.h>
11 #include <linux/genhd.h>
12 #include <linux/vmalloc.h>
13 #include <linux/blktrace_api.h>
14 #include <linux/raid/detect.h>
15 #include "check.h"
16
17 static int (*check_part[])(struct parsed_partitions *) = {
18 /*
19 * Probe partition formats with tables at disk address 0
20 * that also have an ADFS boot block at 0xdc0.
21 */
22 #ifdef CONFIG_ACORN_PARTITION_ICS
23 adfspart_check_ICS,
24 #endif
25 #ifdef CONFIG_ACORN_PARTITION_POWERTEC
26 adfspart_check_POWERTEC,
27 #endif
28 #ifdef CONFIG_ACORN_PARTITION_EESOX
29 adfspart_check_EESOX,
30 #endif
31
32 /*
33 * Now move on to formats that only have partition info at
34 * disk address 0xdc0. Since these may also have stale
35 * PC/BIOS partition tables, they need to come before
36 * the msdos entry.
37 */
38 #ifdef CONFIG_ACORN_PARTITION_CUMANA
39 adfspart_check_CUMANA,
40 #endif
41 #ifdef CONFIG_ACORN_PARTITION_ADFS
42 adfspart_check_ADFS,
43 #endif
44
45 #ifdef CONFIG_CMDLINE_PARTITION
46 cmdline_partition,
47 #endif
48 #ifdef CONFIG_EFI_PARTITION
49 efi_partition, /* this must come before msdos */
50 #endif
51 #ifdef CONFIG_SGI_PARTITION
52 sgi_partition,
53 #endif
54 #ifdef CONFIG_LDM_PARTITION
55 ldm_partition, /* this must come before msdos */
56 #endif
57 #ifdef CONFIG_MSDOS_PARTITION
58 msdos_partition,
59 #endif
60 #ifdef CONFIG_OSF_PARTITION
61 osf_partition,
62 #endif
63 #ifdef CONFIG_SUN_PARTITION
64 sun_partition,
65 #endif
66 #ifdef CONFIG_AMIGA_PARTITION
67 amiga_partition,
68 #endif
69 #ifdef CONFIG_ATARI_PARTITION
70 atari_partition,
71 #endif
72 #ifdef CONFIG_MAC_PARTITION
73 mac_partition,
74 #endif
75 #ifdef CONFIG_ULTRIX_PARTITION
76 ultrix_partition,
77 #endif
78 #ifdef CONFIG_IBM_PARTITION
79 ibm_partition,
80 #endif
81 #ifdef CONFIG_KARMA_PARTITION
82 karma_partition,
83 #endif
84 #ifdef CONFIG_SYSV68_PARTITION
85 sysv68_partition,
86 #endif
87 NULL
88 };
89
bdev_set_nr_sectors(struct block_device * bdev,sector_t sectors)90 static void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors)
91 {
92 spin_lock(&bdev->bd_size_lock);
93 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
94 bdev->bd_nr_sectors = sectors;
95 spin_unlock(&bdev->bd_size_lock);
96 }
97
allocate_partitions(struct gendisk * hd)98 static struct parsed_partitions *allocate_partitions(struct gendisk *hd)
99 {
100 struct parsed_partitions *state;
101 int nr;
102
103 state = kzalloc(sizeof(*state), GFP_KERNEL);
104 if (!state)
105 return NULL;
106
107 nr = disk_max_parts(hd);
108 state->parts = vzalloc(array_size(nr, sizeof(state->parts[0])));
109 if (!state->parts) {
110 kfree(state);
111 return NULL;
112 }
113
114 state->limit = nr;
115
116 return state;
117 }
118
free_partitions(struct parsed_partitions * state)119 static void free_partitions(struct parsed_partitions *state)
120 {
121 vfree(state->parts);
122 kfree(state);
123 }
124
check_partition(struct gendisk * hd)125 static struct parsed_partitions *check_partition(struct gendisk *hd)
126 {
127 struct parsed_partitions *state;
128 int i, res, err;
129
130 state = allocate_partitions(hd);
131 if (!state)
132 return NULL;
133 state->pp_buf = (char *)__get_free_page(GFP_KERNEL);
134 if (!state->pp_buf) {
135 free_partitions(state);
136 return NULL;
137 }
138 state->pp_buf[0] = '\0';
139
140 state->disk = hd;
141 snprintf(state->name, BDEVNAME_SIZE, "%s", hd->disk_name);
142 snprintf(state->pp_buf, PAGE_SIZE, " %s:", state->name);
143 if (isdigit(state->name[strlen(state->name)-1]))
144 sprintf(state->name, "p");
145
146 i = res = err = 0;
147 while (!res && check_part[i]) {
148 memset(state->parts, 0, state->limit * sizeof(state->parts[0]));
149 res = check_part[i++](state);
150 if (res < 0) {
151 /*
152 * We have hit an I/O error which we don't report now.
153 * But record it, and let the others do their job.
154 */
155 err = res;
156 res = 0;
157 }
158
159 }
160 if (res > 0) {
161 printk(KERN_INFO "%s", state->pp_buf);
162
163 free_page((unsigned long)state->pp_buf);
164 return state;
165 }
166 if (state->access_beyond_eod)
167 err = -ENOSPC;
168 /*
169 * The partition is unrecognized. So report I/O errors if there were any
170 */
171 if (err)
172 res = err;
173 if (res) {
174 strlcat(state->pp_buf,
175 " unable to read partition table\n", PAGE_SIZE);
176 printk(KERN_INFO "%s", state->pp_buf);
177 }
178
179 free_page((unsigned long)state->pp_buf);
180 free_partitions(state);
181 return ERR_PTR(res);
182 }
183
part_partition_show(struct device * dev,struct device_attribute * attr,char * buf)184 static ssize_t part_partition_show(struct device *dev,
185 struct device_attribute *attr, char *buf)
186 {
187 return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_partno);
188 }
189
part_start_show(struct device * dev,struct device_attribute * attr,char * buf)190 static ssize_t part_start_show(struct device *dev,
191 struct device_attribute *attr, char *buf)
192 {
193 return sprintf(buf, "%llu\n", dev_to_bdev(dev)->bd_start_sect);
194 }
195
part_ro_show(struct device * dev,struct device_attribute * attr,char * buf)196 static ssize_t part_ro_show(struct device *dev,
197 struct device_attribute *attr, char *buf)
198 {
199 return sprintf(buf, "%d\n", bdev_read_only(dev_to_bdev(dev)));
200 }
201
part_alignment_offset_show(struct device * dev,struct device_attribute * attr,char * buf)202 static ssize_t part_alignment_offset_show(struct device *dev,
203 struct device_attribute *attr, char *buf)
204 {
205 struct block_device *bdev = dev_to_bdev(dev);
206
207 return sprintf(buf, "%u\n",
208 queue_limit_alignment_offset(&bdev_get_queue(bdev)->limits,
209 bdev->bd_start_sect));
210 }
211
part_discard_alignment_show(struct device * dev,struct device_attribute * attr,char * buf)212 static ssize_t part_discard_alignment_show(struct device *dev,
213 struct device_attribute *attr, char *buf)
214 {
215 struct block_device *bdev = dev_to_bdev(dev);
216
217 return sprintf(buf, "%u\n",
218 queue_limit_discard_alignment(&bdev_get_queue(bdev)->limits,
219 bdev->bd_start_sect));
220 }
221
222 static DEVICE_ATTR(partition, 0444, part_partition_show, NULL);
223 static DEVICE_ATTR(start, 0444, part_start_show, NULL);
224 static DEVICE_ATTR(size, 0444, part_size_show, NULL);
225 static DEVICE_ATTR(ro, 0444, part_ro_show, NULL);
226 static DEVICE_ATTR(alignment_offset, 0444, part_alignment_offset_show, NULL);
227 static DEVICE_ATTR(discard_alignment, 0444, part_discard_alignment_show, NULL);
228 static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
229 static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
230 #ifdef CONFIG_FAIL_MAKE_REQUEST
231 static struct device_attribute dev_attr_fail =
232 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
233 #endif
234
235 static struct attribute *part_attrs[] = {
236 &dev_attr_partition.attr,
237 &dev_attr_start.attr,
238 &dev_attr_size.attr,
239 &dev_attr_ro.attr,
240 &dev_attr_alignment_offset.attr,
241 &dev_attr_discard_alignment.attr,
242 &dev_attr_stat.attr,
243 &dev_attr_inflight.attr,
244 #ifdef CONFIG_FAIL_MAKE_REQUEST
245 &dev_attr_fail.attr,
246 #endif
247 NULL
248 };
249
250 static struct attribute_group part_attr_group = {
251 .attrs = part_attrs,
252 };
253
254 static const struct attribute_group *part_attr_groups[] = {
255 &part_attr_group,
256 #ifdef CONFIG_BLK_DEV_IO_TRACE
257 &blk_trace_attr_group,
258 #endif
259 NULL
260 };
261
part_release(struct device * dev)262 static void part_release(struct device *dev)
263 {
264 put_disk(dev_to_bdev(dev)->bd_disk);
265 iput(dev_to_bdev(dev)->bd_inode);
266 }
267
part_uevent(struct device * dev,struct kobj_uevent_env * env)268 static int part_uevent(struct device *dev, struct kobj_uevent_env *env)
269 {
270 struct block_device *part = dev_to_bdev(dev);
271
272 add_uevent_var(env, "PARTN=%u", part->bd_partno);
273 if (part->bd_meta_info && part->bd_meta_info->volname[0])
274 add_uevent_var(env, "PARTNAME=%s", part->bd_meta_info->volname);
275 return 0;
276 }
277
278 struct device_type part_type = {
279 .name = "partition",
280 .groups = part_attr_groups,
281 .release = part_release,
282 .uevent = part_uevent,
283 };
284
delete_partition(struct block_device * part)285 static void delete_partition(struct block_device *part)
286 {
287 lockdep_assert_held(&part->bd_disk->open_mutex);
288
289 fsync_bdev(part);
290 __invalidate_device(part, true);
291
292 xa_erase(&part->bd_disk->part_tbl, part->bd_partno);
293 kobject_put(part->bd_holder_dir);
294 device_del(&part->bd_device);
295
296 /*
297 * Remove the block device from the inode hash, so that it cannot be
298 * looked up any more even when openers still hold references.
299 */
300 remove_inode_hash(part->bd_inode);
301
302 put_device(&part->bd_device);
303 }
304
whole_disk_show(struct device * dev,struct device_attribute * attr,char * buf)305 static ssize_t whole_disk_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
307 {
308 return 0;
309 }
310 static DEVICE_ATTR(whole_disk, 0444, whole_disk_show, NULL);
311
312 /*
313 * Must be called either with open_mutex held, before a disk can be opened or
314 * after all disk users are gone.
315 */
add_partition(struct gendisk * disk,int partno,sector_t start,sector_t len,int flags,struct partition_meta_info * info)316 static struct block_device *add_partition(struct gendisk *disk, int partno,
317 sector_t start, sector_t len, int flags,
318 struct partition_meta_info *info)
319 {
320 dev_t devt = MKDEV(0, 0);
321 struct device *ddev = disk_to_dev(disk);
322 struct device *pdev;
323 struct block_device *bdev;
324 const char *dname;
325 int err;
326
327 lockdep_assert_held(&disk->open_mutex);
328
329 if (partno >= disk_max_parts(disk))
330 return ERR_PTR(-EINVAL);
331
332 /*
333 * Partitions are not supported on zoned block devices that are used as
334 * such.
335 */
336 switch (disk->queue->limits.zoned) {
337 case BLK_ZONED_HM:
338 pr_warn("%s: partitions not supported on host managed zoned block device\n",
339 disk->disk_name);
340 return ERR_PTR(-ENXIO);
341 case BLK_ZONED_HA:
342 pr_info("%s: disabling host aware zoned block device support due to partitions\n",
343 disk->disk_name);
344 blk_queue_set_zoned(disk, BLK_ZONED_NONE);
345 break;
346 case BLK_ZONED_NONE:
347 break;
348 }
349
350 if (xa_load(&disk->part_tbl, partno))
351 return ERR_PTR(-EBUSY);
352
353 /* ensure we always have a reference to the whole disk */
354 get_device(disk_to_dev(disk));
355
356 err = -ENOMEM;
357 bdev = bdev_alloc(disk, partno);
358 if (!bdev)
359 goto out_put_disk;
360
361 bdev->bd_start_sect = start;
362 bdev_set_nr_sectors(bdev, len);
363
364 pdev = &bdev->bd_device;
365 dname = dev_name(ddev);
366 if (isdigit(dname[strlen(dname) - 1]))
367 dev_set_name(pdev, "%sp%d", dname, partno);
368 else
369 dev_set_name(pdev, "%s%d", dname, partno);
370
371 device_initialize(pdev);
372 pdev->class = &block_class;
373 pdev->type = &part_type;
374 pdev->parent = ddev;
375
376 /* in consecutive minor range? */
377 if (bdev->bd_partno < disk->minors) {
378 devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);
379 } else {
380 err = blk_alloc_ext_minor();
381 if (err < 0)
382 goto out_put;
383 devt = MKDEV(BLOCK_EXT_MAJOR, err);
384 }
385 pdev->devt = devt;
386
387 if (info) {
388 err = -ENOMEM;
389 bdev->bd_meta_info = kmemdup(info, sizeof(*info), GFP_KERNEL);
390 if (!bdev->bd_meta_info)
391 goto out_put;
392 }
393
394 /* delay uevent until 'holders' subdir is created */
395 dev_set_uevent_suppress(pdev, 1);
396 err = device_add(pdev);
397 if (err)
398 goto out_put;
399
400 err = -ENOMEM;
401 bdev->bd_holder_dir = kobject_create_and_add("holders", &pdev->kobj);
402 if (!bdev->bd_holder_dir)
403 goto out_del;
404
405 dev_set_uevent_suppress(pdev, 0);
406 if (flags & ADDPART_FLAG_WHOLEDISK) {
407 err = device_create_file(pdev, &dev_attr_whole_disk);
408 if (err)
409 goto out_del;
410 }
411
412 /* everything is up and running, commence */
413 err = xa_insert(&disk->part_tbl, partno, bdev, GFP_KERNEL);
414 if (err)
415 goto out_del;
416 bdev_add(bdev, devt);
417
418 /* suppress uevent if the disk suppresses it */
419 if (!dev_get_uevent_suppress(ddev))
420 kobject_uevent(&pdev->kobj, KOBJ_ADD);
421 return bdev;
422
423 out_del:
424 kobject_put(bdev->bd_holder_dir);
425 device_del(pdev);
426 out_put:
427 put_device(pdev);
428 return ERR_PTR(err);
429 out_put_disk:
430 put_disk(disk);
431 return ERR_PTR(err);
432 }
433
partition_overlaps(struct gendisk * disk,sector_t start,sector_t length,int skip_partno)434 static bool partition_overlaps(struct gendisk *disk, sector_t start,
435 sector_t length, int skip_partno)
436 {
437 struct block_device *part;
438 bool overlap = false;
439 unsigned long idx;
440
441 rcu_read_lock();
442 xa_for_each_start(&disk->part_tbl, idx, part, 1) {
443 if (part->bd_partno != skip_partno &&
444 start < part->bd_start_sect + bdev_nr_sectors(part) &&
445 start + length > part->bd_start_sect) {
446 overlap = true;
447 break;
448 }
449 }
450 rcu_read_unlock();
451
452 return overlap;
453 }
454
bdev_add_partition(struct gendisk * disk,int partno,sector_t start,sector_t length)455 int bdev_add_partition(struct gendisk *disk, int partno, sector_t start,
456 sector_t length)
457 {
458 struct block_device *part;
459 int ret;
460
461 mutex_lock(&disk->open_mutex);
462 if (!disk_live(disk)) {
463 ret = -ENXIO;
464 goto out;
465 }
466
467 if (partition_overlaps(disk, start, length, -1)) {
468 ret = -EBUSY;
469 goto out;
470 }
471
472 part = add_partition(disk, partno, start, length,
473 ADDPART_FLAG_NONE, NULL);
474 ret = PTR_ERR_OR_ZERO(part);
475 out:
476 mutex_unlock(&disk->open_mutex);
477 return ret;
478 }
479
bdev_del_partition(struct gendisk * disk,int partno)480 int bdev_del_partition(struct gendisk *disk, int partno)
481 {
482 struct block_device *part = NULL;
483 int ret = -ENXIO;
484
485 mutex_lock(&disk->open_mutex);
486 part = xa_load(&disk->part_tbl, partno);
487 if (!part)
488 goto out_unlock;
489
490 ret = -EBUSY;
491 if (part->bd_openers)
492 goto out_unlock;
493
494 delete_partition(part);
495 ret = 0;
496 out_unlock:
497 mutex_unlock(&disk->open_mutex);
498 return ret;
499 }
500
bdev_resize_partition(struct gendisk * disk,int partno,sector_t start,sector_t length)501 int bdev_resize_partition(struct gendisk *disk, int partno, sector_t start,
502 sector_t length)
503 {
504 struct block_device *part = NULL;
505 int ret = -ENXIO;
506
507 mutex_lock(&disk->open_mutex);
508 part = xa_load(&disk->part_tbl, partno);
509 if (!part)
510 goto out_unlock;
511
512 ret = -EINVAL;
513 if (start != part->bd_start_sect)
514 goto out_unlock;
515
516 ret = -EBUSY;
517 if (partition_overlaps(disk, start, length, partno))
518 goto out_unlock;
519
520 bdev_set_nr_sectors(part, length);
521
522 ret = 0;
523 out_unlock:
524 mutex_unlock(&disk->open_mutex);
525 return ret;
526 }
527
disk_unlock_native_capacity(struct gendisk * disk)528 static bool disk_unlock_native_capacity(struct gendisk *disk)
529 {
530 const struct block_device_operations *bdops = disk->fops;
531
532 if (bdops->unlock_native_capacity &&
533 !(disk->flags & GENHD_FL_NATIVE_CAPACITY)) {
534 printk(KERN_CONT "enabling native capacity\n");
535 bdops->unlock_native_capacity(disk);
536 disk->flags |= GENHD_FL_NATIVE_CAPACITY;
537 return true;
538 } else {
539 printk(KERN_CONT "truncated\n");
540 return false;
541 }
542 }
543
blk_drop_partitions(struct gendisk * disk)544 void blk_drop_partitions(struct gendisk *disk)
545 {
546 struct block_device *part;
547 unsigned long idx;
548
549 lockdep_assert_held(&disk->open_mutex);
550
551 xa_for_each_start(&disk->part_tbl, idx, part, 1)
552 delete_partition(part);
553 }
554
blk_add_partition(struct gendisk * disk,struct parsed_partitions * state,int p)555 static bool blk_add_partition(struct gendisk *disk,
556 struct parsed_partitions *state, int p)
557 {
558 sector_t size = state->parts[p].size;
559 sector_t from = state->parts[p].from;
560 struct block_device *part;
561
562 if (!size)
563 return true;
564
565 if (from >= get_capacity(disk)) {
566 printk(KERN_WARNING
567 "%s: p%d start %llu is beyond EOD, ",
568 disk->disk_name, p, (unsigned long long) from);
569 if (disk_unlock_native_capacity(disk))
570 return false;
571 return true;
572 }
573
574 if (from + size > get_capacity(disk)) {
575 printk(KERN_WARNING
576 "%s: p%d size %llu extends beyond EOD, ",
577 disk->disk_name, p, (unsigned long long) size);
578
579 if (disk_unlock_native_capacity(disk))
580 return false;
581
582 /*
583 * We can not ignore partitions of broken tables created by for
584 * example camera firmware, but we limit them to the end of the
585 * disk to avoid creating invalid block devices.
586 */
587 size = get_capacity(disk) - from;
588 }
589
590 part = add_partition(disk, p, from, size, state->parts[p].flags,
591 &state->parts[p].info);
592 if (IS_ERR(part) && PTR_ERR(part) != -ENXIO) {
593 printk(KERN_ERR " %s: p%d could not be added: %ld\n",
594 disk->disk_name, p, -PTR_ERR(part));
595 return true;
596 }
597
598 if (IS_BUILTIN(CONFIG_BLK_DEV_MD) &&
599 (state->parts[p].flags & ADDPART_FLAG_RAID))
600 md_autodetect_dev(part->bd_dev);
601
602 return true;
603 }
604
blk_add_partitions(struct gendisk * disk)605 static int blk_add_partitions(struct gendisk *disk)
606 {
607 struct parsed_partitions *state;
608 int ret = -EAGAIN, p;
609
610 if (!disk_part_scan_enabled(disk))
611 return 0;
612
613 state = check_partition(disk);
614 if (!state)
615 return 0;
616 if (IS_ERR(state)) {
617 /*
618 * I/O error reading the partition table. If we tried to read
619 * beyond EOD, retry after unlocking the native capacity.
620 */
621 if (PTR_ERR(state) == -ENOSPC) {
622 printk(KERN_WARNING "%s: partition table beyond EOD, ",
623 disk->disk_name);
624 if (disk_unlock_native_capacity(disk))
625 return -EAGAIN;
626 }
627 return -EIO;
628 }
629
630 /*
631 * Partitions are not supported on host managed zoned block devices.
632 */
633 if (disk->queue->limits.zoned == BLK_ZONED_HM) {
634 pr_warn("%s: ignoring partition table on host managed zoned block device\n",
635 disk->disk_name);
636 ret = 0;
637 goto out_free_state;
638 }
639
640 /*
641 * If we read beyond EOD, try unlocking native capacity even if the
642 * partition table was successfully read as we could be missing some
643 * partitions.
644 */
645 if (state->access_beyond_eod) {
646 printk(KERN_WARNING
647 "%s: partition table partially beyond EOD, ",
648 disk->disk_name);
649 if (disk_unlock_native_capacity(disk))
650 goto out_free_state;
651 }
652
653 /* tell userspace that the media / partition table may have changed */
654 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
655
656 for (p = 1; p < state->limit; p++)
657 if (!blk_add_partition(disk, state, p))
658 goto out_free_state;
659
660 ret = 0;
661 out_free_state:
662 free_partitions(state);
663 return ret;
664 }
665
bdev_disk_changed(struct gendisk * disk,bool invalidate)666 int bdev_disk_changed(struct gendisk *disk, bool invalidate)
667 {
668 int ret = 0;
669
670 lockdep_assert_held(&disk->open_mutex);
671
672 if (!disk_live(disk))
673 return -ENXIO;
674
675 rescan:
676 if (disk->open_partitions)
677 return -EBUSY;
678 sync_blockdev(disk->part0);
679 invalidate_bdev(disk->part0);
680 blk_drop_partitions(disk);
681
682 clear_bit(GD_NEED_PART_SCAN, &disk->state);
683
684 /*
685 * Historically we only set the capacity to zero for devices that
686 * support partitions (independ of actually having partitions created).
687 * Doing that is rather inconsistent, but changing it broke legacy
688 * udisks polling for legacy ide-cdrom devices. Use the crude check
689 * below to get the sane behavior for most device while not breaking
690 * userspace for this particular setup.
691 */
692 if (invalidate) {
693 if (disk_part_scan_enabled(disk) ||
694 !(disk->flags & GENHD_FL_REMOVABLE))
695 set_capacity(disk, 0);
696 }
697
698 if (get_capacity(disk)) {
699 ret = blk_add_partitions(disk);
700 if (ret == -EAGAIN)
701 goto rescan;
702 } else if (invalidate) {
703 /*
704 * Tell userspace that the media / partition table may have
705 * changed.
706 */
707 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
708 }
709
710 return ret;
711 }
712 /*
713 * Only exported for loop and dasd for historic reasons. Don't use in new
714 * code!
715 */
716 EXPORT_SYMBOL_GPL(bdev_disk_changed);
717
read_part_sector(struct parsed_partitions * state,sector_t n,Sector * p)718 void *read_part_sector(struct parsed_partitions *state, sector_t n, Sector *p)
719 {
720 struct address_space *mapping = state->disk->part0->bd_inode->i_mapping;
721 struct page *page;
722
723 if (n >= get_capacity(state->disk)) {
724 state->access_beyond_eod = true;
725 return NULL;
726 }
727
728 page = read_mapping_page(mapping,
729 (pgoff_t)(n >> (PAGE_SHIFT - 9)), NULL);
730 if (IS_ERR(page))
731 goto out;
732 if (PageError(page))
733 goto out_put_page;
734
735 p->v = page;
736 return (unsigned char *)page_address(page) +
737 ((n & ((1 << (PAGE_SHIFT - 9)) - 1)) << SECTOR_SHIFT);
738 out_put_page:
739 put_page(page);
740 out:
741 p->v = NULL;
742 return NULL;
743 }
744