2 * Partition table and disk geometry handling
4 * This obsoletes the partition-handling code in genhd.c:
5 * Userspace can look at a disk in arbitrary format and tell
6 * the kernel what partitions there are on the disk, and how
7 * these should be numbered.
8 * It also allows one to repartition a disk that is being used.
10 * A single ioctl with lots of subfunctions:
12 * Device number stuff:
13 * get_whole_disk() (given the device number of a partition, find
14 * the device number of the encompassing disk)
15 * get_all_partitions() (given the device number of a disk, return the
16 * device numbers of all its known partitions)
21 * test_partition_in_use() (also for test_disk_in_use)
26 * get_bios_drivedata()
28 * For today, only the partition stuff - aeb, 990515
31 #include <linux/errno.h>
32 #include <linux/fs.h> /* for BLKRASET, ... */
33 #include <linux/sched.h> /* for capable() */
34 #include <linux/blk.h> /* for set_device_ro() */
35 #include <linux/blkpg.h>
36 #include <linux/genhd.h>
37 #include <linux/swap.h> /* for is_swap_partition() */
38 #include <linux/module.h> /* for EXPORT_SYMBOL */
40 #include <asm/uaccess.h>
43 * What is the data describing a partition?
45 * 1. a device number (kdev_t)
46 * 2. a starting sector and number of sectors (hd_struct)
47 * given in the part[] array of the gendisk structure for the drive.
49 * The number of sectors is replicated in the sizes[] array of
50 * the gendisk structure for the major, which again is copied to
51 * the blk_size[][] array.
52 * (However, hd_struct has the number of 512-byte sectors,
53 * g->sizes[] and blk_size[][] have the number of 1024-byte blocks.)
54 * Note that several drives may have the same major.
60 * returns: EINVAL: bad parameters
61 * ENXIO: cannot find drive
62 * EBUSY: proposed partition overlaps an existing one
63 * or has the same number as an existing one
66 int add_partition(kdev_t dev, struct blkpg_partition *p) {
68 long long ppstart, pplength;
70 int i, drive, first_minor, end_minor, minor;
72 /* convert bytes to sectors, check for fit in a hd_struct */
73 ppstart = (p->start >> 9);
74 pplength = (p->length >> 9);
77 if (pstart != ppstart || plength != pplength
78 || pstart < 0 || plength < 0)
81 /* find the drive major */
87 drive = (MINOR(dev) >> g->minor_shift);
88 first_minor = (drive << g->minor_shift);
89 end_minor = first_minor + g->max_p;
90 if (drive >= g->nr_real)
93 /* drive and partition number OK? */
94 if (first_minor != MINOR(dev) || p->pno <= 0 || p->pno >= g->max_p)
97 /* partition number in use? */
98 minor = first_minor + p->pno;
99 if (g->part[minor].nr_sects != 0)
103 for (i=first_minor+1; i<end_minor; i++)
104 if (!(pstart+plength <= g->part[i].start_sect ||
105 pstart >= g->part[i].start_sect + g->part[i].nr_sects))
109 g->part[minor].start_sect = pstart;
110 g->part[minor].nr_sects = plength;
112 g->sizes[minor] = (plength >> (BLOCK_SIZE_BITS - 9));
113 devfs_register_partitions (g, first_minor, 0);
118 * Delete a partition given by partition number
120 * returns: EINVAL: bad parameters
121 * ENXIO: cannot find partition
122 * EBUSY: partition is busy
125 * Note that the dev argument refers to the entire disk, not the partition.
127 int del_partition(kdev_t dev, struct blkpg_partition *p) {
130 int drive, first_minor, minor;
132 /* find the drive major */
133 g = get_gendisk(dev);
137 /* drive and partition number OK? */
138 drive = (MINOR(dev) >> g->minor_shift);
139 first_minor = (drive << g->minor_shift);
140 if (first_minor != MINOR(dev) || p->pno <= 0 || p->pno >= g->max_p)
143 /* existing drive and partition? */
144 minor = first_minor + p->pno;
145 if (drive >= g->nr_real || g->part[minor].nr_sects == 0)
148 /* partition in use? Incomplete check for now. */
149 devp = MKDEV(MAJOR(dev), minor);
150 if (is_mounted(devp) || is_swap_partition(devp))
155 invalidate_buffers(devp);
157 g->part[minor].start_sect = 0;
158 g->part[minor].nr_sects = 0;
161 devfs_register_partitions (g, first_minor, 0);
166 int blkpg_ioctl(kdev_t dev, struct blkpg_ioctl_arg *arg)
168 struct blkpg_ioctl_arg a;
169 struct blkpg_partition p;
172 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
176 case BLKPG_ADD_PARTITION:
177 case BLKPG_DEL_PARTITION:
179 if (len < sizeof(struct blkpg_partition))
181 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
183 if (!capable(CAP_SYS_ADMIN))
185 if (a.op == BLKPG_ADD_PARTITION)
186 return add_partition(dev, &p);
188 return del_partition(dev, &p);
195 * Common ioctl's for block devices
198 int blk_ioctl(kdev_t dev, unsigned int cmd, unsigned long arg)
209 if (!capable(CAP_SYS_ADMIN))
211 if (get_user(intval, (int *)(arg)))
213 set_device_ro(dev, intval);
216 intval = (is_read_only(dev) != 0);
217 return put_user(intval, (int *)(arg));
220 if(!capable(CAP_SYS_ADMIN))
224 read_ahead[MAJOR(dev)] = arg;
229 return put_user(read_ahead[MAJOR(dev)], (long *) arg);
232 if(!capable(CAP_SYS_ADMIN))
235 invalidate_buffers(dev);
239 /* get block device sector size as needed e.g. by fdisk */
240 intval = get_hardsect_size(dev);
241 return put_user(intval, (int *) arg);
245 g = get_gendisk(dev);
247 ullval = g->part[MINOR(dev)].nr_sects;
249 if (cmd == BLKGETSIZE)
250 return put_user((unsigned long)ullval, (unsigned long *)arg);
252 return put_user(ullval << 9, (u64 *)arg);
254 case BLKRRPART: /* Re-read partition tables */
255 if (!capable(CAP_SYS_ADMIN))
257 return reread_partitions(dev, 1);
261 return blkpg_ioctl(dev, (struct blkpg_ioctl_arg *) arg);
264 return blkelvget_ioctl(&blk_get_queue(dev)->elevator,
265 (blkelv_ioctl_arg_t *) arg);
267 return blkelvset_ioctl(&blk_get_queue(dev)->elevator,
268 (blkelv_ioctl_arg_t *) arg);
271 /* get the logical block size (cf. BLKSSZGET) */
273 if (blksize_size[MAJOR(dev)])
274 intval = blksize_size[MAJOR(dev)][MINOR(dev)];
275 return put_user (intval, (int *) arg);
278 /* set the logical block size */
279 if (!capable (CAP_SYS_ADMIN))
283 if (get_user (intval, (int *) arg))
285 if (intval > PAGE_SIZE || intval < 512 ||
286 (intval & (intval - 1)))
288 if (is_mounted (dev) || is_swap_partition (dev))
290 set_blocksize (dev, intval);
298 EXPORT_SYMBOL(blk_ioctl);