Skip to content

Commit fa9156a

Browse files
Christoph Hellwigaxboe
authored andcommitted
block: refactor blkpg_ioctl
Split each sub-command out into a separate helper, and move those helpers to block/partitions/core.c instead of having a lot of partition manipulation logic open coded in block/ioctl.c. Signed-off-by: Christoph Hellwig <hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent b4fd63f commit fa9156a

3 files changed

Lines changed: 145 additions & 128 deletions

File tree

‎block/blk.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,13 @@ char *disk_name(struct gendisk *hd, int partno, char *buf);
389389
#define ADDPART_FLAG_NONE 0
390390
#define ADDPART_FLAG_RAID 1
391391
#define ADDPART_FLAG_WHOLEDISK 2
392-
struct hd_struct *__must_check add_partition(struct gendisk *disk, int partno,
393-
sector_t start, sector_t len, int flags,
394-
struct partition_meta_info *info);
395392
void __delete_partition(struct percpu_ref *ref);
396393
void delete_partition(struct gendisk *disk, int partno);
394+
int bdev_add_partition(struct block_device *bdev, int partno,
395+
sector_t start, sector_t length);
396+
int bdev_del_partition(struct block_device *bdev, int partno);
397+
int bdev_resize_partition(struct block_device *bdev, int partno,
398+
sector_t start, sector_t length);
397399
int disk_expand_part_tbl(struct gendisk *disk, int target);
398400

399401
static inline int hd_ref_init(struct hd_struct *part)

‎block/ioctl.c‎

Lines changed: 26 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -16,142 +16,44 @@
1616
static int blkpg_do_ioctl(struct block_device *bdev,
1717
struct blkpg_partition __user *upart, int op)
1818
{
19-
struct block_device *bdevp;
20-
struct gendisk *disk;
21-
struct hd_struct *part, *lpart;
2219
struct blkpg_partition p;
23-
struct disk_part_iter piter;
2420
long long start, length;
25-
int partno;
2621

2722
if (!capable(CAP_SYS_ADMIN))
2823
return -EACCES;
2924
if (copy_from_user(&p, upart, sizeof(struct blkpg_partition)))
3025
return -EFAULT;
31-
disk = bdev->bd_disk;
3226
if (bdev != bdev->bd_contains)
3327
return -EINVAL;
34-
partno = p.pno;
35-
if (partno <= 0)
28+
29+
if (p.pno <= 0)
3630
return -EINVAL;
31+
32+
if (op == BLKPG_DEL_PARTITION)
33+
return bdev_del_partition(bdev, p.pno);
34+
35+
start = p.start >> SECTOR_SHIFT;
36+
length = p.length >> SECTOR_SHIFT;
37+
38+
/* check for fit in a hd_struct */
39+
if (sizeof(sector_t) < sizeof(long long)) {
40+
long pstart = start, plength = length;
41+
42+
if (pstart != start || plength != length || pstart < 0 ||
43+
plength < 0 || p.pno > 65535)
44+
return -EINVAL;
45+
}
46+
3747
switch (op) {
38-
case BLKPG_ADD_PARTITION:
39-
start = p.start >> 9;
40-
length = p.length >> 9;
41-
/* check for fit in a hd_struct */
42-
if (sizeof(sector_t) == sizeof(long) &&
43-
sizeof(long long) > sizeof(long)) {
44-
long pstart = start, plength = length;
45-
if (pstart != start || plength != length
46-
|| pstart < 0 || plength < 0 || partno > 65535)
47-
return -EINVAL;
48-
}
49-
/* check if partition is aligned to blocksize */
50-
if (p.start & (bdev_logical_block_size(bdev) - 1))
51-
return -EINVAL;
52-
53-
mutex_lock(&bdev->bd_mutex);
54-
55-
/* overlap? */
56-
disk_part_iter_init(&piter, disk,
57-
DISK_PITER_INCL_EMPTY);
58-
while ((part = disk_part_iter_next(&piter))) {
59-
if (!(start + length <= part->start_sect ||
60-
start >= part->start_sect + part->nr_sects)) {
61-
disk_part_iter_exit(&piter);
62-
mutex_unlock(&bdev->bd_mutex);
63-
return -EBUSY;
64-
}
65-
}
66-
disk_part_iter_exit(&piter);
67-
68-
/* all seems OK */
69-
part = add_partition(disk, partno, start, length,
70-
ADDPART_FLAG_NONE, NULL);
71-
mutex_unlock(&bdev->bd_mutex);
72-
return PTR_ERR_OR_ZERO(part);
73-
case BLKPG_DEL_PARTITION:
74-
part = disk_get_part(disk, partno);
75-
if (!part)
76-
return -ENXIO;
77-
78-
bdevp = bdget(part_devt(part));
79-
disk_put_part(part);
80-
if (!bdevp)
81-
return -ENOMEM;
82-
83-
mutex_lock(&bdevp->bd_mutex);
84-
if (bdevp->bd_openers) {
85-
mutex_unlock(&bdevp->bd_mutex);
86-
bdput(bdevp);
87-
return -EBUSY;
88-
}
89-
/* all seems OK */
90-
fsync_bdev(bdevp);
91-
invalidate_bdev(bdevp);
92-
93-
mutex_lock_nested(&bdev->bd_mutex, 1);
94-
delete_partition(disk, partno);
95-
mutex_unlock(&bdev->bd_mutex);
96-
mutex_unlock(&bdevp->bd_mutex);
97-
bdput(bdevp);
98-
99-
return 0;
100-
case BLKPG_RESIZE_PARTITION:
101-
start = p.start >> 9;
102-
/* new length of partition in bytes */
103-
length = p.length >> 9;
104-
/* check for fit in a hd_struct */
105-
if (sizeof(sector_t) == sizeof(long) &&
106-
sizeof(long long) > sizeof(long)) {
107-
long pstart = start, plength = length;
108-
if (pstart != start || plength != length
109-
|| pstart < 0 || plength < 0)
110-
return -EINVAL;
111-
}
112-
part = disk_get_part(disk, partno);
113-
if (!part)
114-
return -ENXIO;
115-
bdevp = bdget(part_devt(part));
116-
if (!bdevp) {
117-
disk_put_part(part);
118-
return -ENOMEM;
119-
}
120-
mutex_lock(&bdevp->bd_mutex);
121-
mutex_lock_nested(&bdev->bd_mutex, 1);
122-
if (start != part->start_sect) {
123-
mutex_unlock(&bdevp->bd_mutex);
124-
mutex_unlock(&bdev->bd_mutex);
125-
bdput(bdevp);
126-
disk_put_part(part);
127-
return -EINVAL;
128-
}
129-
/* overlap? */
130-
disk_part_iter_init(&piter, disk,
131-
DISK_PITER_INCL_EMPTY);
132-
while ((lpart = disk_part_iter_next(&piter))) {
133-
if (lpart->partno != partno &&
134-
!(start + length <= lpart->start_sect ||
135-
start >= lpart->start_sect + lpart->nr_sects)
136-
) {
137-
disk_part_iter_exit(&piter);
138-
mutex_unlock(&bdevp->bd_mutex);
139-
mutex_unlock(&bdev->bd_mutex);
140-
bdput(bdevp);
141-
disk_put_part(part);
142-
return -EBUSY;
143-
}
144-
}
145-
disk_part_iter_exit(&piter);
146-
part_nr_sects_write(part, (sector_t)length);
147-
i_size_write(bdevp->bd_inode, p.length);
148-
mutex_unlock(&bdevp->bd_mutex);
149-
mutex_unlock(&bdev->bd_mutex);
150-
bdput(bdevp);
151-
disk_put_part(part);
152-
return 0;
153-
default:
48+
case BLKPG_ADD_PARTITION:
49+
/* check if partition is aligned to blocksize */
50+
if (p.start & (bdev_logical_block_size(bdev) - 1))
15451
return -EINVAL;
52+
return bdev_add_partition(bdev, p.pno, start, length);
53+
case BLKPG_RESIZE_PARTITION:
54+
return bdev_resize_partition(bdev, p.pno, start, length);
55+
default:
56+
return -EINVAL;
15557
}
15658
}
15759

‎block/partitions/core.c‎

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ static DEVICE_ATTR(whole_disk, 0444, whole_disk_show, NULL);
335335
* Must be called either with bd_mutex held, before a disk can be opened or
336336
* after all disk users are gone.
337337
*/
338-
struct hd_struct *add_partition(struct gendisk *disk, int partno,
338+
static struct hd_struct *add_partition(struct gendisk *disk, int partno,
339339
sector_t start, sector_t len, int flags,
340340
struct partition_meta_info *info)
341341
{
@@ -472,6 +472,119 @@ struct hd_struct *add_partition(struct gendisk *disk, int partno,
472472
return ERR_PTR(err);
473473
}
474474

475+
static bool partition_overlaps(struct gendisk *disk, sector_t start,
476+
sector_t length, int skip_partno)
477+
{
478+
struct disk_part_iter piter;
479+
struct hd_struct *part;
480+
bool overlap = false;
481+
482+
disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
483+
while ((part = disk_part_iter_next(&piter))) {
484+
if (part->partno == skip_partno ||
485+
start >= part->start_sect + part->nr_sects ||
486+
start + length <= part->start_sect)
487+
continue;
488+
overlap = true;
489+
break;
490+
}
491+
492+
disk_part_iter_exit(&piter);
493+
return overlap;
494+
}
495+
496+
int bdev_add_partition(struct block_device *bdev, int partno,
497+
sector_t start, sector_t length)
498+
{
499+
struct hd_struct *part;
500+
501+
mutex_lock(&bdev->bd_mutex);
502+
if (partition_overlaps(bdev->bd_disk, start, length, -1)) {
503+
mutex_unlock(&bdev->bd_mutex);
504+
return -EBUSY;
505+
}
506+
507+
part = add_partition(bdev->bd_disk, partno, start, length,
508+
ADDPART_FLAG_NONE, NULL);
509+
mutex_unlock(&bdev->bd_mutex);
510+
return PTR_ERR_OR_ZERO(part);
511+
}
512+
513+
int bdev_del_partition(struct block_device *bdev, int partno)
514+
{
515+
struct block_device *bdevp;
516+
struct hd_struct *part;
517+
int ret = 0;
518+
519+
part = disk_get_part(bdev->bd_disk, partno);
520+
if (!part)
521+
return -ENXIO;
522+
523+
bdevp = bdget(part_devt(part));
524+
disk_put_part(part);
525+
if (!bdevp)
526+
return -ENOMEM;
527+
528+
mutex_lock(&bdevp->bd_mutex);
529+
530+
ret = -EBUSY;
531+
if (bdevp->bd_openers)
532+
goto out_unlock;
533+
534+
fsync_bdev(bdevp);
535+
invalidate_bdev(bdevp);
536+
537+
mutex_lock_nested(&bdev->bd_mutex, 1);
538+
delete_partition(bdev->bd_disk, partno);
539+
mutex_unlock(&bdev->bd_mutex);
540+
541+
ret = 0;
542+
out_unlock:
543+
mutex_unlock(&bdevp->bd_mutex);
544+
bdput(bdevp);
545+
return ret;
546+
}
547+
548+
int bdev_resize_partition(struct block_device *bdev, int partno,
549+
sector_t start, sector_t length)
550+
{
551+
struct block_device *bdevp;
552+
struct hd_struct *part;
553+
int ret = 0;
554+
555+
part = disk_get_part(bdev->bd_disk, partno);
556+
if (!part)
557+
return -ENXIO;
558+
559+
ret = -ENOMEM;
560+
bdevp = bdget(part_devt(part));
561+
if (!bdevp)
562+
goto out_put_part;
563+
564+
mutex_lock(&bdevp->bd_mutex);
565+
mutex_lock_nested(&bdev->bd_mutex, 1);
566+
567+
ret = -EINVAL;
568+
if (start != part->start_sect)
569+
goto out_unlock;
570+
571+
ret = -EBUSY;
572+
if (partition_overlaps(bdev->bd_disk, start, length, partno))
573+
goto out_unlock;
574+
575+
part_nr_sects_write(part, (sector_t)length);
576+
i_size_write(bdevp->bd_inode, length << SECTOR_SHIFT);
577+
578+
ret = 0;
579+
out_unlock:
580+
mutex_unlock(&bdevp->bd_mutex);
581+
mutex_unlock(&bdev->bd_mutex);
582+
bdput(bdevp);
583+
out_put_part:
584+
disk_put_part(part);
585+
return ret;
586+
}
587+
475588
static bool disk_unlock_native_capacity(struct gendisk *disk)
476589
{
477590
const struct block_device_operations *bdops = disk->fops;

0 commit comments

Comments
 (0)