1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
* Partition table and disk geometry handling
*
* This obsoletes the partition-handling code in genhd.c:
* Userspace can look at a disk in arbitrary format and tell
* the kernel what partitions there are on the disk, and how
* these should be numbered.
* It also allows one to repartition a disk that is being used.
*
* A single ioctl with lots of subfunctions:
*
* Device number stuff:
* get_whole_disk() (given the device number of a partition, find
* the device number of the encompassing disk)
* get_all_partitions() (given the device number of a disk, return the
* device numbers of all its known partitions)
*
* Partition stuff:
* add_partition()
* delete_partition()
* test_partition_in_use() (also for test_disk_in_use)
*
* Geometry stuff:
* get_geometry()
* set_geometry()
* get_bios_drivedata()
*
* For today, only the partition stuff - aeb, 990515
*/
#include <linux/errno.h>
#include <linux/fs.h> /* for BLKRASET, ... */
#include <linux/sched.h> /* for capable() */
#include <linux/blk.h> /* for set_device_ro() */
#include <linux/blkpg.h>
#include <linux/genhd.h>
#include <linux/swap.h> /* for is_swap_partition() */
#include <linux/module.h> /* for EXPORT_SYMBOL */
#include <asm/uaccess.h>
/*
* What is the data describing a partition?
*
* 1. a device number (kdev_t)
* 2. a starting sector and number of sectors (hd_struct)
* given in the part[] array of the gendisk structure for the drive.
*
* The number of sectors is replicated in the sizes[] array of
* the gendisk structure for the major, which again is copied to
* the blk_size[][] array.
* (However, hd_struct has the number of 512-byte sectors,
* g->sizes[] and blk_size[][] have the number of 1024-byte blocks.)
* Note that several drives may have the same major.
*/
/* a linear search, superfluous when dev is a pointer */
static struct gendisk *get_gendisk(kdev_t dev) {
struct gendisk *g;
int m = MAJOR(dev);
for (g = gendisk_head; g; g = g->next)
if (g->major == m)
break;
return g;
}
/*
* Add a partition.
*
* returns: EINVAL: bad parameters
* ENXIO: cannot find drive
* EBUSY: proposed partition overlaps an existing one
* or has the same number as an existing one
* 0: all OK.
*/
int add_partition(kdev_t dev, struct blkpg_partition *p) {
struct gendisk *g;
long long ppstart, pplength;
long pstart, plength;
int i, drive, first_minor, end_minor, minor;
/* convert bytes to sectors, check for fit in a hd_struct */
ppstart = (p->start >> 9);
pplength = (p->length >> 9);
pstart = ppstart;
plength = pplength;
if (pstart != ppstart || plength != pplength
|| pstart < 0 || plength < 0)
return -EINVAL;
/* find the drive major */
g = get_gendisk(dev);
if (!g)
return -ENXIO;
/* existing drive? */
drive = (MINOR(dev) >> g->minor_shift);
first_minor = (drive << g->minor_shift);
end_minor = first_minor + g->max_p;
if (drive >= g->nr_real)
return -ENXIO;
/* drive and partition number OK? */
if (first_minor != MINOR(dev) || p->pno <= 0 || p->pno >= g->max_p)
return -EINVAL;
/* partition number in use? */
minor = first_minor + p->pno;
if (g->part[minor].nr_sects != 0)
return -EBUSY;
/* overlap? */
for (i=first_minor+1; i<end_minor; i++)
if (!(pstart+plength <= g->part[i].start_sect ||
pstart >= g->part[i].start_sect + g->part[i].nr_sects))
return -EBUSY;
/* all seems OK */
g->part[minor].start_sect = pstart;
g->part[minor].nr_sects = plength;
if (g->sizes)
g->sizes[minor] = (plength >> (BLOCK_SIZE_BITS - 9));
return 0;
}
/*
* Delete a partition given by partition number
*
* returns: EINVAL: bad parameters
* ENXIO: cannot find partition
* EBUSY: partition is busy
* 0: all OK.
*
* Note that the dev argument refers to the entire disk, not the partition.
*/
int del_partition(kdev_t dev, struct blkpg_partition *p) {
struct gendisk *g;
kdev_t devp;
int drive, first_minor, minor;
/* find the drive major */
g = get_gendisk(dev);
if (!g)
return -ENXIO;
/* drive and partition number OK? */
drive = (MINOR(dev) >> g->minor_shift);
first_minor = (drive << g->minor_shift);
if (first_minor != MINOR(dev) || p->pno <= 0 || p->pno >= g->max_p)
return -EINVAL;
/* existing drive and partition? */
minor = first_minor + p->pno;
if (drive >= g->nr_real || g->part[minor].nr_sects == 0)
return -ENXIO;
/* partition in use? Incomplete check for now. */
devp = MKDEV(MAJOR(dev), minor);
if (get_super(devp) || /* mounted? */
is_swap_partition(devp))
return -EBUSY;
/* all seems OK */
fsync_dev(devp);
invalidate_buffers(devp);
g->part[minor].start_sect = 0;
g->part[minor].nr_sects = 0;
if (g->sizes)
g->sizes[minor] = 0;
return 0;
}
int blkpg_ioctl(kdev_t dev, struct blkpg_ioctl_arg *arg)
{
struct blkpg_ioctl_arg a;
struct blkpg_partition p;
int len;
if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
return -EFAULT;
switch (a.op) {
case BLKPG_ADD_PARTITION:
case BLKPG_DEL_PARTITION:
len = a.datalen;
if (len < sizeof(struct blkpg_partition))
return -EINVAL;
if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
return -EFAULT;
if (!capable(CAP_SYS_ADMIN))
return -EACCES;
if (a.op == BLKPG_ADD_PARTITION)
return add_partition(dev, &p);
else
return del_partition(dev, &p);
default:
return -EINVAL;
}
}
/*
* Common ioctl's for block devices
*/
int blk_ioctl(kdev_t dev, unsigned int cmd, unsigned long arg)
{
int intval;
switch (cmd) {
case BLKROSET:
if (!capable(CAP_SYS_ADMIN))
return -EACCES;
if (get_user(intval, (int *)(arg)))
return -EFAULT;
set_device_ro(dev, intval);
return 0;
case BLKROGET:
intval = (is_read_only(dev) != 0);
return put_user(intval, (int *)(arg));
case BLKRASET:
if(!capable(CAP_SYS_ADMIN))
return -EACCES;
if(!dev || arg > 0xff)
return -EINVAL;
read_ahead[MAJOR(dev)] = arg;
return 0;
case BLKRAGET:
if (!arg)
return -EINVAL;
return put_user(read_ahead[MAJOR(dev)], (long *) arg);
case BLKFLSBUF:
if(!capable(CAP_SYS_ADMIN))
return -EACCES;
if (!dev)
return -EINVAL;
fsync_dev(dev);
invalidate_buffers(dev);
return 0;
case BLKSSZGET:
/* get block device sector size as needed e.g. by fdisk */
intval = get_hardsect_size(dev);
return put_user(intval, (int *) arg);
#if 0
case BLKGETSIZE:
/* Today get_gendisk() requires a linear scan;
add this when dev has pointer type. */
g = get_gendisk(dev);
if (!g)
longval = 0;
else
longval = g->part[MINOR(dev)].nr_sects;
return put_user(longval, (long *) arg);
#endif
#if 0
case BLKRRPART: /* Re-read partition tables */
if (!capable(CAP_SYS_ADMIN))
return -EACCES;
return reread_partitions(dev, 1);
#endif
case BLKPG:
return blkpg_ioctl(dev, (struct blkpg_ioctl_arg *) arg);
default:
return -EINVAL;
}
}
EXPORT_SYMBOL(blk_ioctl);
|